-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cleanup generated file structure, improve model initialization
- Loading branch information
Victor Korzunin
committed
Sep 11, 2021
1 parent
69e1d96
commit f1b5730
Showing
17 changed files
with
230 additions
and
244 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{{#each models}} | ||
export * from './{{name}}'; | ||
{{/each}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
Oops, something went wrong.