Skip to content

Commit

Permalink
fix: bug in models associations, make factory synchronous, fix readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Korzunin committed Sep 11, 2021
1 parent caf335e commit b8f4b2d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ prisma generate
```typescript
import { createSequelizeInstance } from './prisma/sequelize';

const { sequelize, models } = new createSequelizeInstance({
const { sequelize, models } = createSequelizeInstance({
ssl: true,
dialectOptions: {
connectTimeout: 1000,
Expand Down
9 changes: 6 additions & 3 deletions plop/index.ts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const databaseUrl = config.datasource.url.fromEnvVar
? env[config.datasource.url.fromEnvVar]
: config.datasource.url.value;

export const createSequelizeInstance = async (options?: Options) => {
export const createSequelizeInstance = (options?: Options) => {
const withDefaults = mergeDeepRight({
define: {
freezeTableName: true,
Expand All @@ -27,14 +27,17 @@ export const createSequelizeInstance = async (options?: Options) => {

const sequelize = new Sequelize(databaseUrl, withDefaults(options ?? {}));

// First initialize all models
Object.keys(models).forEach((model) => {
models[model].initialize?.(sequelize);
});

// Then apply associations
Object.keys(models).forEach((model) => {
models[model].associate?.(models);
models[model].hooks?.(models);
});

await sequelize.authenticate();

return {
sequelize,
models,
Expand Down
9 changes: 6 additions & 3 deletions prisma/sequelize/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const databaseUrl = config.datasource.url.fromEnvVar
? env[config.datasource.url.fromEnvVar]
: config.datasource.url.value;

export const createSequelizeInstance = async (options?: Options) => {
export const createSequelizeInstance = (options?: Options) => {
const withDefaults = mergeDeepRight({
define: {
freezeTableName: true,
Expand All @@ -27,14 +27,17 @@ export const createSequelizeInstance = async (options?: Options) => {

const sequelize = new Sequelize(databaseUrl, withDefaults(options ?? {}));

// First initialize all models
Object.keys(models).forEach((model) => {
models[model].initialize?.(sequelize);
});

// Then apply associations
Object.keys(models).forEach((model) => {
models[model].associate?.(models);
models[model].hooks?.(models);
});

await sequelize.authenticate();

return {
sequelize,
models,
Expand Down

0 comments on commit b8f4b2d

Please sign in to comment.