Sequelize.js Sequelize ORM-如何使一个模型拥有一个对象数组,这些对象总是以特定的持久顺序排列
我正在尝试制作一个包含一系列项目的sequelize模型(如下所示): 此外,我还需要确保数组中的项是按特定顺序排列的带有关系的示例Sequelize.js Sequelize ORM-如何使一个模型拥有一个对象数组,这些对象总是以特定的持久顺序排列,sequelize.js,Sequelize.js,我正在尝试制作一个包含一系列项目的sequelize模型(如下所示): 此外,我还需要确保数组中的项是按特定顺序排列的带有关系的示例 // Define Users const Users = sequelize.define('User', { id: {type: DataTypes.BIGINT, autoIncrement: true, primaryKey: true}, name: {type: DataTypes.STRING}, }); // Define Ho
// Define Users
const Users = sequelize.define('User', {
id: {type: DataTypes.BIGINT, autoIncrement: true, primaryKey: true},
name: {type: DataTypes.STRING},
});
// Define Hobbies
const UserHobbies = sequelize.define('UserHobbie', {
id: {type: DataTypes.BIGINT, autoIncrement: true, primaryKey: true},
name: {type: DataTypes.STRING},
}, {timestamps: true});
// Add Asocciation
Users.hasMany(UserHobbies, {
as: 'hobbies',
onDelete: 'cascade',
});
// Create scope 'withhobbies'
Users.addScope('withhobbies', {
include: [{ // Add join to hobbies
model: UserHobbies,
as: 'hobbies',
attributes: ['name'],
orderBy: [ // Order by hobbie creation time (autogenerate by option timestamps: true)
['createdAt', 'ASC'],
],
}],
});
// Use method to make final object
async function getUsersAndHobbies() {
const users = await Users.scope('withhobbies').findAll(); // Use scope to include hobbies
return users.map((u) => {
const plain = u.get({plain: true}); // Get plain object
return {
...plain,
hobbies: plain.hobbies.map((h) => h.name), // override hobbies with only the names
});
};
设置器和获取器示例:
// Define Users
const Users = sequelize.define('User', {
id: {type: DataTypes.BIGINT, autoIncrement: true, primaryKey: true},
name: {type: DataTypes.STRING},
defaultValue: '[]',
hobbies: {
type: DataTypes.TEXT,
get() {
return JSON.parse(this.getDataValue('hobbies'));
},
set(val) {
if (!Array.isArray(val)) {
throw new Error('hobbies must to be an array');
}
this.setDataValue('hobbies', JSON.stringify(val));
},
},
});
以身作则
// Define Users
const Users = sequelize.define('User', {
id: {type: DataTypes.BIGINT, autoIncrement: true, primaryKey: true},
name: {type: DataTypes.STRING},
});
// Define Hobbies
const UserHobbies = sequelize.define('UserHobbie', {
id: {type: DataTypes.BIGINT, autoIncrement: true, primaryKey: true},
name: {type: DataTypes.STRING},
}, {timestamps: true});
// Add Asocciation
Users.hasMany(UserHobbies, {
as: 'hobbies',
onDelete: 'cascade',
});
// Create scope 'withhobbies'
Users.addScope('withhobbies', {
include: [{ // Add join to hobbies
model: UserHobbies,
as: 'hobbies',
attributes: ['name'],
orderBy: [ // Order by hobbie creation time (autogenerate by option timestamps: true)
['createdAt', 'ASC'],
],
}],
});
// Use method to make final object
async function getUsersAndHobbies() {
const users = await Users.scope('withhobbies').findAll(); // Use scope to include hobbies
return users.map((u) => {
const plain = u.get({plain: true}); // Get plain object
return {
...plain,
hobbies: plain.hobbies.map((h) => h.name), // override hobbies with only the names
});
};
设置器和获取器示例:
// Define Users
const Users = sequelize.define('User', {
id: {type: DataTypes.BIGINT, autoIncrement: true, primaryKey: true},
name: {type: DataTypes.STRING},
defaultValue: '[]',
hobbies: {
type: DataTypes.TEXT,
get() {
return JSON.parse(this.getDataValue('hobbies'));
},
set(val) {
if (!Array.isArray(val)) {
throw new Error('hobbies must to be an array');
}
this.setDataValue('hobbies', JSON.stringify(val));
},
},
});
请参考下面的博客,我希望它能给你一些指导嗯,你需要更准确地回答你的问题,取决于你需要什么,在大多数情况下,你必须使用关系,如用户->爱好,以及使用关联、范围等。请参考下面的博客,我希望它能给你一些指导嗯,你需要更准确地回答你的问题,这取决于你需要什么,在大多数情况下,你必须使用关系,如用户->爱好,以及使用关联、范围等。非常感谢@Exos,是的,我认为这可以工作,我试图做的是将一个对象保存到数据库,
{user:{name:'某人',爱好:[{name:'爱好一',索引:0},{name:'爱好二',索引:1}]}
我希望嗜好总是按照索引的顺序排列,每个用户可以有任意数量的嗜好,但是我不希望在查询hanks a lot@Exos时返回索引,是的,我认为这可以工作,我试图做的是将一个对象保存到db,{user:{name:'someone',嗜好:[{name:'hobby one',index:0},{name:'hobby two',index:1}]}
我希望业余爱好总是按照索引的顺序排列,每个用户可以有任意数量的业余爱好,但是我不希望在查询时返回索引