Skip to content

Commit

Permalink
refactor: rename props model name and field name to not mix them in hbs
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Korzunin committed Sep 14, 2021
1 parent 6bc2d6d commit 3962344
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 10 deletions.
8 changes: 4 additions & 4 deletions plop/models/Model.ts.hbs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Sequelize, Model, DataTypes, ModelCtor } from 'sequelize';

export class {{name}} extends Model {
export class {{modelName}} extends Model {
static initialize(sequelize: Sequelize) {
this.init(
{
{{#each scalarFields}}
{{name}}: {
{{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}}
Expand All @@ -17,8 +17,8 @@ export class {{name}} extends Model {
},
{
sequelize,
modelName: '{{name}}',
tableName: '{{#if dbName}}{{dbName}}{{else}}{{name}}{{/if}}',
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)}}
Expand Down
2 changes: 1 addition & 1 deletion plop/models/index.ts.hbs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{{#each models}}
export * from './{{name}}';
export * from './{{modelName}}';
{{/each}}
2 changes: 1 addition & 1 deletion plop/plopfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ module.exports = function (plop) {
actions: () => [
{
type: 'add',
path: 'models/{{name}}.ts',
path: 'models/{{modelName}}.ts',
templateFile: path.join(__dirname, './models/Model.ts.hbs'),
},
],
Expand Down
4 changes: 2 additions & 2 deletions src/generator/properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const PrismaTypeToSequelizeType: Record<string, string> = {
};

export interface ModelProperties {
name: string;
modelName: string;
dbName: string;
scalarFields: ScalarProperties[];
belongsToFields: RelationProperties[];
Expand All @@ -33,7 +33,7 @@ export interface ScalarProperties {
default: any;
isId: boolean;
isUnique: boolean;
name: string;
fieldName: string;
type: string;
allowNull: boolean;
isAutoincrement: boolean;
Expand Down
4 changes: 2 additions & 2 deletions src/generator/transformDMMF.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function transformDMMF(dmmf: DMMF.Document) {
default: 'default',
isId: 'isId',
isUnique: 'isUnique',
name: 'name',
fieldName: 'name',
type: (field: DMMF.Field) =>
field.kind === 'scalar'
? R.prop(field.type, PrismaTypeToSequelizeType)
Expand All @@ -37,7 +37,7 @@ export function transformDMMF(dmmf: DMMF.Document) {
});

const modelMorphism = morphism<Schema<ModelProperties, DMMF.Model>>({
name: 'name',
modelName: 'name',
dbName: 'dbName',
scalarFields: {
path: 'fields',
Expand Down

0 comments on commit 3962344

Please sign in to comment.