javascript – Sequelize动态播种
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – Sequelize动态播种,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2850字,纯文字阅读大概需要5分钟。
内容图文
![javascript – Sequelize动态播种](/upload/InfoBanner/zyjiaocheng/811/da81ed23372b4a909e98eb01b0f7781e.jpg)
我目前正在使用Sequelize.js播种数据,并使用硬编码值作为关联ID.这不理想,因为我真的应该能够动态地做到这一点吗?例如,将用户和配置文件与“拥有一个”和“属于”关联相关联.我不一定想用硬编码的profileId为用户播种.在创建配置文件后,我宁愿在配置文件种子中执行此操作.创建配置文件后,动态地将profileId添加到用户.使用Sequelize.js时,这是否可行?或者,在使用Sequelize进行播种时,硬编码关联ID会更常见吗?
也许我正在播种错误?我是否应该使用Sequelize使用一对一的种子文件和迁移文件?在Rails中,通常只有1个种子文件,如果需要,您可以选择分成多个文件.
一般来说,只需在这里寻找指导和建议.这些是我的文件:
users.js
// User seeds
'use strict';
module.exports = {
up: function (queryInterface, Sequelize) {
/*
Add altering commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.bulkInsert('Person', [{
name: 'John Doe',
isBetaMember: false
}], {});
*/
var users = [];
for (let i = 0; i < 10; i++) {
users.push({
fname: "Foo",
lname: "Bar",
username: `foobar${i}`,
email: `foobar${i}@gmail.com`,
profileId: i + 1
});
}
return queryInterface.bulkInsert('Users', users);
},
down: function (queryInterface, Sequelize) {
/*
Add reverting commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.bulkDelete('Person', null, {});
*/
return queryInterface.bulkDelete('Users', null, {});
}
};
profiles.js
// Profile seeds
'use strict';
var models = require('./../models');
var User = models.User;
var Profile = models.Profile;
module.exports = {
up: function (queryInterface, Sequelize) {
/*
Add altering commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.bulkInsert('Person', [{
name: 'John Doe',
isBetaMember: false
}], {});
*/
var profiles = [];
var genders = ['m', 'f'];
for (let i = 0; i < 10; i++) {
profiles.push({
birthday: new Date(),
gender: genders[Math.round(Math.random())],
occupation: 'Dev',
description: 'Cool yo',
userId: i + 1
});
}
return queryInterface.bulkInsert('Profiles', profiles);
},
down: function (queryInterface, Sequelize) {
/*
Add reverting commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.bulkDelete('Person', null, {});
*/
return queryInterface.bulkDelete('Profiles', null, {});
}
};
正如你所看到的,我只是使用硬编码的for循环(非理想).
解决方法:
您可以使用sequelizes create-with-association功能将它们组合在一个文件中,而不是为用户和配置文件使用不同的种子.
另外,当使用一系列create()时,必须将它们包装在Promise.all()中,因为种子接口需要Promise作为返回值.
up: function (queryInterface, Sequelize) {
return Promise.all([
models.Profile.create({
data: 'profile stuff',
users: [{
name: "name",
...
}, {
name: 'another user',
...
}]}, {
include: [ model.users]
}
),
models.Profile.create({
data: 'another profile',
users: [{
name: "more users",
...
}, {
name: 'another user',
...
}]}, {
include: [ model.users]
}
)
])
}
不确定这是否真的是最好的解决方案,但这就是我如何在播种文件中自己维护外键.
内容总结
以上是互联网集市为您收集整理的javascript – Sequelize动态播种全部内容,希望文章能够帮你解决javascript – Sequelize动态播种所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。