Skip to content

Commit

Permalink
feat(generator): generate primary key and timestamps and other field …
Browse files Browse the repository at this point in the history
…props properly
  • Loading branch information
Victor Korzunin committed Aug 21, 2021
1 parent 7ed9a04 commit 23b1ece
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 12 deletions.
13 changes: 11 additions & 2 deletions .plop/Model.ts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@ export const {{model.name}}Factory = (sequelize: Sequelize) => {
const {{model.name}} = sequelize.define('{{model.name}}', {
{{#each scalarFields}}
{{name}}: {
type: DataTypes.{{type}},
type: {{#if isList}}DataTypes.ARRAY(DataTypes.{{type}}){{else}}DataTypes.{{type}}{{/if}},{{#if (eq allowNull false)}}
allowNull: {{allowNull}},{{/if}}{{#if isId}}
primaryKey: {{isId}},{{/if}}{{#if isAutoincrement}}
autoIncrement: {{isAutoincrement}},{{/if}}{{#if isUnique}}
unique: {{isUnique}},{{/if}}
},
{{/each}}
}, {
tableName: '{{#if model.dbName}}{{model.dbName}}{{else}}{{model.name}}{{/if}}'
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}}
});

return {{model.name}};
Expand Down
13 changes: 13 additions & 0 deletions .plop/plopfile.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
const path = require('path');

module.exports = function (plop) {
plop.setHelper('eq', (v1, v2) => v1 === v2);
plop.setHelper('ne', (v1, v2) => v1 !== v2);
plop.setHelper('lt', (v1, v2) => v1 < v2);
plop.setHelper('gt', (v1, v2) => v1 > v2);
plop.setHelper('lte', (v1, v2) => v1 <= v2);
plop.setHelper('gte', (v1, v2) => v1 >= v2);
plop.setHelper('and', function () {
return Array.prototype.every.call(arguments, Boolean);
});
plop.setHelper('or', function () {
return Array.prototype.slice.call(arguments, 0, -1).some(Boolean);
});

plop.setGenerator('utils', {
actions: () => [
{
Expand Down
6 changes: 5 additions & 1 deletion prisma/models/Post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ export const PostFactory = (sequelize: Sequelize) => {
const Post = sequelize.define('Post', {
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true,
},
userId: {
type: DataTypes.INTEGER,
},
}, {
tableName: 'Posts'
tableName: 'Posts',
timestamps: false,
});

return Post;
Expand Down
16 changes: 11 additions & 5 deletions prisma/models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ export const UserFactory = (sequelize: Sequelize) => {
const User = sequelize.define('User', {
id: {
type: DataTypes.INTEGER,
},
createdAt: {
type: DataTypes.DATE,
allowNull: false,
primaryKey: true,
autoIncrement: true,
},
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
},
weight: {
type: DataTypes.FLOAT,
Expand All @@ -24,13 +26,17 @@ export const UserFactory = (sequelize: Sequelize) => {
type: DataTypes.INTEGER,
},
keywords: {
type: DataTypes.STRING,
type: DataTypes.ARRAY(DataTypes.STRING),
allowNull: false,
},
biography: {
type: DataTypes.JSONB,
allowNull: false,
},
}, {
tableName: 'User'
tableName: 'User',
timestamps: true,
updatedAt: false,
});

return User;
Expand Down
17 changes: 13 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,26 @@ generatorHandler({
relativeOutputDir,
slsRelativeOutputDir,
}),
...options.dmmf.datamodel.models.map((model) =>
modelGenerator.runActions({
...options.dmmf.datamodel.models.map((model) => {
const attributes = model.fields.map((field) => field.name);
return modelGenerator.runActions({
model,
scalarFields: model.fields
.filter((field) => field.kind === 'scalar')
.filter((field) => !['createdAt', 'updatedAt', 'deletedAt'].includes(field.name))
.map((field) => ({
...field,
name: field.name,
type: PrismaTypeToSequelizeType[field.type],
allowNull: !field.isRequired,
isAutoincrement:
field.hasDefaultValue && typeof field.default === 'object' && field.default.name === 'autoincrement',
})),
})
),
hasCreatedAt: attributes.includes('createdAt'),
hasUpdatedAt: attributes.includes('updatedAt'),
hasDeletedAt: attributes.includes('deletedAt'),
});
}),
]);
} catch (e) {
console.error('Error: unable to write files for Prisma Sequelize Generator');
Expand Down

0 comments on commit 23b1ece

Please sign in to comment.