gpt4 book ai didi

sequelize.js - Sequelize js orm迁移与seeder

转载 作者:行者123 更新时间:2023-12-02 01:34:56 24 4
gpt4 key购买 nike

有没有办法填充数据库(迁移+种子)使用 sequelizejs ORM?

我的迁移

'use strict';

/**
* Module dependencies.
*/
var Schema = require('../src/schemas/users');

module.exports = {

up: function (queryInterface, Sequelize) {
return queryInterface.createTable(
Schema.name,
Schema.definition(Sequelize)
);
},

down: function (queryInterface, Sequelize) {
return queryInterface.dropTable(Schema.name);
}

};

看看Node Sequelize migrations/models is it possible to share the same code?

第一次尝试

'use strict';

/**
* Module dependencies.
*/
var Async = require('async');
var Models = require('../src/models');
var User = Models.users;

/**
* Users to insert.
*/
var users = [
{

firstname: 'abcdefghijklmnopqrstuvwxyz',
lastname: 'abcdefghijklmnopqrstuvwxyz',
email : 'abcdefghijklmnopqrstuvwxyz@abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz',
username:'abcdefghijklmnopqrstuvwxyz',
password:'abcdefghijklmnopqrstuvwxyz'
}
];

var calls = [];

function createUserPromise(current, index, users) {
calls.push(function(callback) {
User
.create(current)
.then(function(user) {
callback(null, user);
})
.catch(function(err){
return callback(err);
});
});
}
users.forEach(createUserPromise);

Async.parallel(calls , function(err, results){
if(err){
console.log(err);
}
else{
var info = results.map(function(result){
return result.dataValues;
});
console.log(info);
}
process.exit();
});

最佳答案

按照来自 sequelize-cliseed:create 搭建的模板:

$ node_modules/.bin/sequelize seed:create --name 我的种子名称

然后运行你的种子:

$ node_modules/.bin/sequelize db:seed

关于sequelize.js - Sequelize js orm迁移与seeder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31812009/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com