如何使用 sequelize + node js 播种 pivot tablet
How to seed pivot tablet with sequelize + node js
我有两个 table 具有多对多关系,角色 table 和权限 table,我需要播种我的 table roles_permission,在权限播种器中设置了所有初始权限后,我在 sequelize 的文档中搜索了如何执行此操作,但我还没有找到任何其他可以帮助我的东西
我的角色种子
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.bulkInsert('roles', [{
name: 'SUPER_ROLE',
created_at: new Date(),
updated_at: new Date()
}],{});
},
down: async (queryInterface, Sequelize) => {
return queryInterface.bulkDelete('roles', null, {});
}
};
我的权限种子
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
return queryInterface.bulkInsert('permissions', [{
name: 'create_products',
created_at: new Date(),
updated_at: new Date()
}, {
name: 'edit_products',
created_at: new Date(),
updated_at: new Date()
}, {
name: 'delete_products',
created_at: new Date(),
updated_at: new Date()
}, {
name: 'view_products',
created_at: new Date(),
updated_at: new Date()
}],
{});
},
down: async (queryInterface, Sequelize) => {
return await queryInterface.bulkDelete('permissions', null, {});
}
};
我的支点table迁移
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
return queryInterface.createTable('roles_permissions', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false
},
role_id: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'roles', key: 'id'
},
onUpdate: 'CASCADE',
onDelete: 'CASCADE'
},
permission_id: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'permissions', key: 'id'
},
onUpdate: 'CASCADE',
onDelete: 'CASCADE'
},
created_at: {
type: Sequelize.DATE,
allowNull: false,
},
updated_at: {
type: Sequelize.DATE,
allowNull: false,
},
})
},
down: async (queryInterface, Sequelize) => {
return queryInterface.dropTable('roles_permissions');
}
};
您可以使用 queryInterface
查询数据库并找到您的角色和权限,然后尝试动态创建批量插入。
参见 示例。
我有两个 table 具有多对多关系,角色 table 和权限 table,我需要播种我的 table roles_permission,在权限播种器中设置了所有初始权限后,我在 sequelize 的文档中搜索了如何执行此操作,但我还没有找到任何其他可以帮助我的东西
我的角色种子
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.bulkInsert('roles', [{
name: 'SUPER_ROLE',
created_at: new Date(),
updated_at: new Date()
}],{});
},
down: async (queryInterface, Sequelize) => {
return queryInterface.bulkDelete('roles', null, {});
}
};
我的权限种子
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
return queryInterface.bulkInsert('permissions', [{
name: 'create_products',
created_at: new Date(),
updated_at: new Date()
}, {
name: 'edit_products',
created_at: new Date(),
updated_at: new Date()
}, {
name: 'delete_products',
created_at: new Date(),
updated_at: new Date()
}, {
name: 'view_products',
created_at: new Date(),
updated_at: new Date()
}],
{});
},
down: async (queryInterface, Sequelize) => {
return await queryInterface.bulkDelete('permissions', null, {});
}
};
我的支点table迁移
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
return queryInterface.createTable('roles_permissions', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false
},
role_id: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'roles', key: 'id'
},
onUpdate: 'CASCADE',
onDelete: 'CASCADE'
},
permission_id: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'permissions', key: 'id'
},
onUpdate: 'CASCADE',
onDelete: 'CASCADE'
},
created_at: {
type: Sequelize.DATE,
allowNull: false,
},
updated_at: {
type: Sequelize.DATE,
allowNull: false,
},
})
},
down: async (queryInterface, Sequelize) => {
return queryInterface.dropTable('roles_permissions');
}
};
您可以使用 queryInterface
查询数据库并找到您的角色和权限,然后尝试动态创建批量插入。
参见