-
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: support js code models generation
- Loading branch information
Victor Korzunin
committed
Sep 15, 2021
1 parent
059ce9c
commit bb915d0
Showing
12 changed files
with
134 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const { Sequelize } = require('sequelize'); | ||
const { tryLoadEnvs } = require('@prisma/sdk'); | ||
const { mergeDeepRight } = require('ramda'); | ||
const path = require('path'); | ||
|
||
const config = require('./config.json'); | ||
const models = require('./models'); | ||
|
||
const loadedEnv = tryLoadEnvs({ | ||
rootEnvPath: config.relativeEnvPaths.rootEnvPath && path.resolve(__dirname, config.relativeEnvPaths.rootEnvPath), | ||
schemaEnvPath: config.relativeEnvPaths.schemaEnvPath && path.resolve(__dirname, config.relativeEnvPaths.schemaEnvPath), | ||
}); | ||
const env = { ...(loadedEnv ? loadedEnv.parsed : {}), ...process.env }; | ||
const databaseUrl = config.datasource.url.fromEnvVar | ||
? env[config.datasource.url.fromEnvVar] | ||
: config.datasource.url.value; | ||
|
||
module.exports.createSequelizeInstance = (options) => { | ||
const withDefaults = mergeDeepRight({ | ||
define: { | ||
freezeTableName: true, | ||
}, | ||
}); | ||
|
||
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); | ||
}); | ||
|
||
return { | ||
sequelize, | ||
models, | ||
}; | ||
}; |
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 @@ | ||
const { Model, DataTypes } = require('sequelize'); | ||
|
||
module.exports.{{modelName}} = class extends Model { | ||
static initialize(sequelize) { | ||
this.init( | ||
{ | ||
{{#each scalarFields}} | ||
{{fieldName}}: { | ||
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: '{{modelName}}', | ||
tableName: '{{#if dbName}}{{dbName}}{{else}}{{modelName}}{{/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) { | ||
{{#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,9 @@ | ||
{{#each models}} | ||
const { {{modelName}} } = require('./{{modelName}}'); | ||
{{/each}} | ||
|
||
module.exports = { | ||
{{#each models}} | ||
{{modelName}}, | ||
{{/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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
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