Skip to content

Commit

Permalink
fix: loadModel import esm system
Browse files Browse the repository at this point in the history
- fix: User.js wronged formated schema
  • Loading branch information
iwaduarte committed Oct 13, 2022
1 parent bbcbacb commit c5b9779
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
11 changes: 6 additions & 5 deletions templates/database/loadModels.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
`${
opts.esm
? `import { dirname } from 'path';
import { fileURLToPath } from 'url';
? `import { fileURLToPath, pathToFileURL } from 'url';
import path from 'path';
import fs from 'fs';
const __dirname = dirname(fileURLToPath(import.meta.url));`
const __dirname = path.dirname(fileURLToPath(import.meta.url));`
: `const path = require('path');
const fs = require('fs');
`
}
const loadModels = ${opts.esm ? 'async' : ''} (sequelize, dataTypes) => {
if (!sequelize) throw new Error('Missing sequelize connection');
const files = fs
.readdirSync(__dirname + '/models')
.readdirSync(path.join(__dirname , '/models'))
.filter(file => file.indexOf('.') !== 0 && file.slice(-3) === '.js');
${
opts.esm
? ` let Models = {};
await Promise.all(
files.map(file => {
return import(path.join(__dirname, 'models', file)).then( ({ default: modelFn }) => {
return import(pathToFileURL(path.join(__dirname, '/models', file))).then( ({ default: modelFn }) => {
const model = modelFn(sequelize, dataTypes);
Models[model.name] = model;
});
Expand Down
29 changes: 15 additions & 14 deletions templates/database/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const generatePasswordHash = password => {
return bcrypt.hash(password, saltRounds);
};
const user = (sequelize, DataTypes) => {
const User = sequelize.define(
const User = (sequelize, DataTypes) => {
const _User = sequelize.define(
'User',
{
email: {
Expand All @@ -20,16 +20,17 @@ const user = (sequelize, DataTypes) => {
validate: {
notNull: {
msg: 'Email not provided'
},
isEmail: {
msg: 'It is not a valid email'
}
},
isEmail: {
msg: 'It is not a valid email'
unique: {
args: true,
msg: 'The email is already in use'
}
},
unique: {
args: true,
msg: 'The email is already in use'
},
password: {
type: DataTypes.STRING,
allowNull: false,
Expand Down Expand Up @@ -74,33 +75,33 @@ const user = (sequelize, DataTypes) => {
}
);
User.beforeValidate(async data => {
_User.beforeValidate(async data => {
Object.keys(dataValues).forEach(key => {
if (data[key] === '') {
Object.assign(data, { [key]: null });
}
});
});
User.beforeCreate(async data => {
_User.beforeCreate(async data => {
const password = await generatePasswordHash(data.password);
const lastLogin = new Date();
Object.assign(data, { password, lastLogin });
});
User.beforeUpdate(async data => {
_User.beforeUpdate(async data => {
if (data.password && data.changed('password')) {
const password = await generatePasswordHash(data.password);
Object.assign(data, { password });
}
});
User.associate = models => {
_User.associate = models => {
// you can create associations with other models here
// See https://sequelize.org/docs/v6/core-concepts/assocs/
};
return User;
return _User;
};
${opts.esm ? 'export default user' : 'module.exports = user'};`;
${opts.esm ? 'export default User' : 'module.exports = User'};`;

0 comments on commit c5b9779

Please sign in to comment.