Skip to content

Commit

Permalink
feat: cleanup generated file structure, improve model initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Korzunin committed Sep 11, 2021
1 parent 69e1d96 commit f1b5730
Show file tree
Hide file tree
Showing 17 changed files with 230 additions and 244 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ generator client {
}
```

With a custom output path (default=./models)
With a custom output path (default=./sequelize)

```prisma
generator client {
Expand Down
46 changes: 0 additions & 46 deletions plop/Model.ts.hbs

This file was deleted.

31 changes: 9 additions & 22 deletions plop/index.ts.hbs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { ModelCtor, Sequelize } from 'sequelize';
import { Sequelize } from 'sequelize';
import { tryLoadEnvs } from '@prisma/sdk';
import path from 'path';

import { findSync } from './utils';
import config from './config.json';

const dirname = findSync(process.cwd(), ['{{relativeOutputDir}}', '{{slsRelativeOutputDir}}'], ['d'], ['d'], 1)[0] || __dirname;

{{#each models}}
import { {{name}}Factory } from './{{name}}';
{{/each}}

const config = {{{config}}};
import * as models from './models';

const loadedEnv = tryLoadEnvs({
rootEnvPath: config.relativeEnvPaths.rootEnvPath && path.resolve(dirname, config.relativeEnvPaths.rootEnvPath),
Expand All @@ -28,7 +26,11 @@ export const createInstance = async () => {
},
});

const models = prepareModels(sequelize);
Object.keys(models).forEach((model) => {
models[model].initialize?.(sequelize);
models[model].associate?.(models);
models[model].hooks?.(models);
});

await sequelize.authenticate();

Expand All @@ -37,18 +39,3 @@ export const createInstance = async () => {
models,
};
};

function prepareModels(sequelize: Sequelize) {
const models = {
{{#each models}}
{{name}}: {{name}}Factory(sequelize),
{{/each}}
};

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

return models;
}
44 changes: 44 additions & 0 deletions plop/models/Model.ts.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Sequelize, Model, DataTypes, ModelCtor } from 'sequelize';

export class {{model.name}} extends Model {
static initialize(sequelize: Sequelize) {
this.init(
{
{{#each scalarFields}}
{{name}}: {
type: {{#if isList}}DataTypes.ARRAY(DataTypes.{{type}}){{else}}DataTypes.{{{type}}}{{/if}},{{#if (eq allowNull false)}}
allowNull: {{allowNull}},{{/if}}{{#if (and hasDefaultValue (eq isAutoincrement false))}}
defaultValue: '{{default}}',{{/if}}{{#if isId}}
primaryKey: {{isId}},{{/if}}{{#if isAutoincrement}}
autoIncrement: {{isAutoincrement}},{{/if}}{{#if isUnique}}
unique: {{isUnique}},{{/if}}
},
{{/each}}
},
{
sequelize,
modelName: '{{model.name}}',
tableName: '{{#if model.dbName}}{{model.dbName}}{{else}}{{model.name}}{{/if}}',
timestamps: {{or (or hasCreatedAt hasUpdatedAt) hasDeletedAt}},{{#if (or (or hasCreatedAt hasUpdatedAt) hasDeletedAt)}}{{#if (eq hasCreatedAt false)}}
createdAt: false,{{/if}}{{#if (eq hasUpdatedAt false)}}
updatedAt: false,{{/if}}{{!-- {{#if (eq hasDeletedAt false)}}
deletedAt: false,{{/if}} --}}{{#if hasDeletedAt}}
paranoid: true,{{/if}}{{/if}}
}
);
}

{{#if (or belongsToFields (or hasManyFields hasOneFields))}}
static associate(models: Record<string, ModelCtor<Model>>) {
{{#each belongsToFields}}
this.belongsTo(models.{{name}}, { as: '{{as}}', targetKey: '{{targetKey}}', foreignKey: '{{foreignKey}}' });
{{/each}}
{{#each hasManyFields}}
this.hasMany(models.{{name}}, { as: '{{as}}' });
{{/each}}
{{#each hasOneFields}}
this.hasOne(models.{{name}}, { as: '{{as}}' });
{{/each}}
}
{{/if}}
}
3 changes: 3 additions & 0 deletions plop/models/index.ts.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{#each models}}
export * from './{{name}}';
{{/each}}
14 changes: 12 additions & 2 deletions plop/plopfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,30 @@ module.exports = function (plop) {

plop.setGenerator('index.ts', {
actions: () => [
{
type: 'add',
path: 'models/index.ts',
templateFile: path.join(__dirname, './models/index.ts.hbs'),
},
{
type: 'add',
path: 'index.ts',
templateFile: path.join(__dirname, './index.ts.hbs'),
},
{
type: 'add',
path: 'config.json',
template: '{{{config}}}',
},
],
});

plop.setGenerator('Model.ts', {
actions: () => [
{
type: 'add',
path: '{{model.name}}.ts',
templateFile: path.join(__dirname, './Model.ts.hbs'),
path: 'models/{{model.name}}.ts',
templateFile: path.join(__dirname, './models/Model.ts.hbs'),
},
],
});
Expand Down
31 changes: 0 additions & 31 deletions prisma/models/Post.ts

This file was deleted.

61 changes: 0 additions & 61 deletions prisma/models/User.ts

This file was deleted.

80 changes: 0 additions & 80 deletions prisma/models/index.ts

This file was deleted.

29 changes: 29 additions & 0 deletions prisma/sequelize/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"generator": {
"name": "models",
"provider": {
"fromEnvVar": null,
"value": "node ./dist/generator.js"
},
"output": {
"value": "/Users/victor/Projects/_own/prisma-sequelize-generator/prisma/sequelize",
"fromEnvVar": "null"
},
"config": {},
"binaryTargets": [],
"previewFeatures": []
},
"relativeEnvPaths": {
"rootEnvPath": "../../.env",
"schemaEnvPath": "../../.env"
},
"datasource": {
"name": "db",
"provider": "postgresql",
"activeProvider": "postgresql",
"url": {
"fromEnvVar": "DATABASE_URL",
"value": null
}
}
}
Loading

0 comments on commit f1b5730

Please sign in to comment.