static migrate() { const run_migrate = ()=>{ // https://github.com/sequelize/umzug const Umzug = require('umzug'); const Sequelize = require('sequelize'); const umzugs = [Photo, Thumbnail, Preview, Album].map((model)=>{ return new Umzug({ storage: 'Sequelize', storageOptions: { model: model, columnName: 'migration', columnType: new Sequelize.STRING(100) } }); }); console.log('database file', DbFileName); umzugs.forEach((umzug)=>{ umzug.execute({migrations:[''], method: 'up'}) .then(umzug, console.log) }); } Photo.count().catch((e)=>{ run_migrate(); }); }
/** * Adds the where claus of the query * @param {Object} attributes attributes, their values, and the operator * @param {Object} [attributes.model=this.model] the attribute model * @param {Object} attributes.operator the comparison operator * @param {Object} attributes.value the value to match against * @return {QueryGenerator} queryGenerator */ where(attributes) { Object.keys(attributes).forEach(key => { let model = !Util.isEmptyObject(attributes[key].model) ? attributes[key].model : this.model; let operator = attributes[key].operator; let field = QueryGenerator.col(model.attributes[key].field, model); let value = attributes[key].value; if (operator === 'ilike') { operator = 'ILIKE'; // If the operator is ILIKE and the field type is not String, Char, or Text, cast it to Text. if ([Sequelize.STRING, Sequelize.CHAR, Sequelize.TEXT].every(type => !(model.attributes[key].type instanceof type))) field = QueryGenerator.cast(field); value = '%' + value + '%'; } value = value instanceof Fn ? value.build() : '\'' + value + '\''; this.query.where.push(field + ' ' + operator + ' ' + value); }); return this; }
const getDB = async () => { const Op = SQL.Op; const operatorsAliases = { $in: Op.in, }; const db = new SQL('', '', '', { dialect: 'sqlite', storage: './db.sqlite', operatorsAliases, logging: false, }); const users = db.define('user', { id: { type: SQL.INTEGER, primaryKey: true, autoIncrement: true, }, name: SQL.STRING, createdAt: SQL.DATE, updatedAt: SQL.DATE, }); return { users }; }
router.post('/', function (req, res, next) { var report = sequelize.define('rdm_report', { reportId: Sequelize.STRING, formType: Sequelize.STRING, reportName: Sequelize.STRING, reportStatistics: Sequelize.STRING, reportStatisticsType: Sequelize.STRING, reportSecond: Sequelize.STRING, reportEnd: Sequelize.STRING, reportSort: Sequelize.STRING }); sequelize.sync().then(function () { return report.create({ reportId: req.body.reportId, formType: req.body.formType, reportName: req.body.reportName, reportStatistics: req.body.reportStatistics, reportStatisticsType: req.body.reportStatisticsType, reportSecond: req.body.reportSecond, reportEnd: req.body.reportEnd, reportSort: req.body.reportSort, }); }).then(function (jane) { console.log(jane.get({ plain: true })); // 跳转到首页 res.redirect('/reportRoutes'); }); });
}, name: { type: Sequelize.STRING(20), allowNull: false
sequelize.define('recipe', { name: { type: Sequelize.STRING(30) } }, { freezeTableName: true, paranoid: false, timestamps: false, classMethods: { findById: function(recipeId) { return Recipe.findOne({ where: { id: recipeId } }); } } })
datasource.define('user', { email: { type: Sequelize.STRING, unique: true, validate: { isEmail: { msg: 'Not a valid email' } } }, password: { type: Sequelize.STRING, allowNull: false, validate: { isString: function (value) { if (!(typeof value === 'string' || value instanceof String)) { throw new Error('Password must be a string') } } } }, firstName: { type: Sequelize.STRING }, lastName: { type: Sequelize.STRING } })