АГ
return sequelize.define('Users', {и 3я промежуточная (напрмиер: UserDocuments в которой будут id юзеров и документов) и будут определены связи:
id: {
field: 'id',
type: Sequelize.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
name: {
type: Sequelize.STRING,
allowNull: false
}
}, {
timestamps: false,
tableName: 'users'
});
return sequelize.define('Documents', {
id: {
field: 'id',
type: Sequelize.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
text: {
type: Sequelize.STRING,
allowNull: false
}
}, {
timestamps: false,
tableName: 'documents'
});
const UserDocuments = sequelize.define('Documents', {
id: {
field: 'id',
type: Sequelize.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
userId: {
field: 'user_id',
type: Sequelize.INTEGER,
allowNull: false
},
documentId: {
field: 'document_id',
type: Sequelize.INTEGER,
allowNull: false
}
}, {
timestamps: false,
tableName: 'documents'
});
UserDocuments.associate = function(models) {
models.Douments.belongsToMany(models.Users, {
through: UserDocuments,
as: 'users',
foreignKey: 'user_id'
})
models.Users.belongsToMany(models.Documents, {
through: UserDocuments,
as: 'documents',
foreignKey: 'document_id',
})
};
return UserDocuments
Теперь когда мне нужно связать эти таблицы то я делаюawait UserDocuments.create(userId, docId)а что бы вытащить данные использую
напрмер:
await User.findOne({
where: {
id
},
include: ['documents']
})
это
все верно?