Using the magic of joins

This commit is contained in:
Tom Moor
2017-07-03 21:35:17 -07:00
parent 91e09e3853
commit f90d170497
3 changed files with 41 additions and 39 deletions

View File

@ -115,7 +115,31 @@ const Document = sequelize.define(
},
classMethods: {
associate: models => {
Document.belongsTo(models.User);
Document.belongsTo(models.Collection, {
foreignKey: 'atlasId',
});
Document.belongsTo(models.User, {
as: 'createdBy',
foreignKey: 'createdById',
});
Document.belongsTo(models.User, {
as: 'updatedBy',
foreignKey: 'lastModifiedById',
});
Document.hasMany(models.Star, {
as: 'starred',
});
Document.addScope(
'defaultScope',
{
include: [
{ model: models.Collection },
{ model: models.User, as: 'createdBy' },
{ model: models.User, as: 'updatedBy' },
],
},
{ override: true }
);
},
findById: async id => {
if (isUUID(id)) {