Created
August 4, 2021 05:47
-
-
Save rjaus/cbb932cdbb6967268c30975b63908766 to your computer and use it in GitHub Desktop.
sequelize model & sync
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
'use strict'; | |
const { | |
Model | |
} = require('sequelize'); | |
module.exports = (sequelize, DataTypes) => { | |
class Service extends Model { | |
/** | |
* Helper method for defining associations. | |
* This method is not a part of Sequelize lifecycle. | |
* The `models/index` file will call this method automatically. | |
*/ | |
static associate(models) { | |
// define association here | |
this.hasMany(models.Proof) | |
this.hasMany(models.Parser) | |
} | |
}; | |
Service.init({ | |
name: DataTypes.STRING, | |
public: { | |
type: DataTypes.BOOLEAN, | |
defaultValue: false | |
} | |
}, { | |
sequelize, | |
modelName: 'Service', | |
}); | |
return Service; | |
}; | |
.... | |
/* | |
Later on, after you've instantiated all your models | |
*/ | |
// I usually have an additional SYNC_DB env flag here, as with force: true it's going to wipe the db constantly. | |
if (process.env.NODE_ENV == 'development') { | |
db.sequelize.sync({ force: true }) | |
.then(() => { | |
// Seed your database here | |
devSeeds(db); | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment