mirror of
https://gitlab.com/Alamantus/Readlebee.git
synced 2025-06-27 11:44:18 +02:00
Update sequelize models to add foreign keys
This commit is contained in:
parent
b5ef4a9bb5
commit
da2425a73d
1 changed files with 40 additions and 12 deletions
|
@ -65,7 +65,7 @@ function getSequelizeModels (sequelize) {
|
||||||
primaryKey: true,
|
primaryKey: true,
|
||||||
autoIncrement: true,
|
autoIncrement: true,
|
||||||
},
|
},
|
||||||
user: {
|
userId: {
|
||||||
type: Sequelize.INTEGER,
|
type: Sequelize.INTEGER,
|
||||||
references: {
|
references: {
|
||||||
model: User,
|
model: User,
|
||||||
|
@ -77,12 +77,6 @@ function getSequelizeModels (sequelize) {
|
||||||
type: Sequelize.STRING,
|
type: Sequelize.STRING,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
},
|
},
|
||||||
isUnique: {
|
|
||||||
type: Sequelize.BOOLEAN,
|
|
||||||
allowNull: false,
|
|
||||||
defaultValue: false,
|
|
||||||
comment: 'If true, books on this shelf cannot be in other Unique shelves.',
|
|
||||||
},
|
|
||||||
isPublic: {
|
isPublic: {
|
||||||
type: Sequelize.BOOLEAN,
|
type: Sequelize.BOOLEAN,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
|
@ -91,7 +85,7 @@ function getSequelizeModels (sequelize) {
|
||||||
isDeletable: {
|
isDeletable: {
|
||||||
type: Sequelize.BOOLEAN,
|
type: Sequelize.BOOLEAN,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
defaultValue: false,
|
defaultValue: true,
|
||||||
},
|
},
|
||||||
|
|
||||||
// Timestamps
|
// Timestamps
|
||||||
|
@ -106,6 +100,10 @@ function getSequelizeModels (sequelize) {
|
||||||
defaultValue: Sequelize.NOW,
|
defaultValue: Sequelize.NOW,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
Shelf.belongsTo(User, {
|
||||||
|
foreignKey: 'userId',
|
||||||
|
onDelete: 'CASCADE',
|
||||||
|
});
|
||||||
|
|
||||||
const BookReference = sequelize.define('bookReference', {
|
const BookReference = sequelize.define('bookReference', {
|
||||||
id: {
|
id: {
|
||||||
|
@ -146,6 +144,12 @@ function getSequelizeModels (sequelize) {
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
defaultValue: Sequelize.NOW,
|
defaultValue: Sequelize.NOW,
|
||||||
},
|
},
|
||||||
|
}, {
|
||||||
|
indexes: [
|
||||||
|
{
|
||||||
|
fields: ['name', 'description'],
|
||||||
|
},
|
||||||
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
const ShelfItem = sequelize.define('shelfItem', {
|
const ShelfItem = sequelize.define('shelfItem', {
|
||||||
|
@ -154,7 +158,7 @@ function getSequelizeModels (sequelize) {
|
||||||
primaryKey: true,
|
primaryKey: true,
|
||||||
autoIncrement: true,
|
autoIncrement: true,
|
||||||
},
|
},
|
||||||
shelf: {
|
shelfId: {
|
||||||
type: Sequelize.INTEGER,
|
type: Sequelize.INTEGER,
|
||||||
references: {
|
references: {
|
||||||
model: Shelf,
|
model: Shelf,
|
||||||
|
@ -162,7 +166,7 @@ function getSequelizeModels (sequelize) {
|
||||||
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE,
|
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
book: {
|
bookId: {
|
||||||
type: Sequelize.INTEGER,
|
type: Sequelize.INTEGER,
|
||||||
references: {
|
references: {
|
||||||
model: BookReference,
|
model: BookReference,
|
||||||
|
@ -187,6 +191,14 @@ function getSequelizeModels (sequelize) {
|
||||||
defaultValue: Sequelize.NOW,
|
defaultValue: Sequelize.NOW,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
ShelfItem.belongsTo(Shelf, {
|
||||||
|
foreignKey: 'shelfId',
|
||||||
|
onDelete: 'CASCADE',
|
||||||
|
});
|
||||||
|
ShelfItem.belongsTo(BookReference, {
|
||||||
|
foreignKey: 'bookId',
|
||||||
|
onDelete: 'CASCADE',
|
||||||
|
});
|
||||||
|
|
||||||
const StatusType = sequelize.define('statusType', {
|
const StatusType = sequelize.define('statusType', {
|
||||||
id: {
|
id: {
|
||||||
|
@ -209,7 +221,7 @@ function getSequelizeModels (sequelize) {
|
||||||
primaryKey: true,
|
primaryKey: true,
|
||||||
autoIncrement: true,
|
autoIncrement: true,
|
||||||
},
|
},
|
||||||
type: {
|
typeId: {
|
||||||
type: Sequelize.INTEGER,
|
type: Sequelize.INTEGER,
|
||||||
references: {
|
references: {
|
||||||
model: StatusType,
|
model: StatusType,
|
||||||
|
@ -217,7 +229,7 @@ function getSequelizeModels (sequelize) {
|
||||||
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE,
|
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
user: {
|
userId: {
|
||||||
type: Sequelize.INTEGER,
|
type: Sequelize.INTEGER,
|
||||||
references: {
|
references: {
|
||||||
model: User,
|
model: User,
|
||||||
|
@ -255,6 +267,14 @@ function getSequelizeModels (sequelize) {
|
||||||
defaultValue: Sequelize.NOW,
|
defaultValue: Sequelize.NOW,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
Status.belongsTo(StatusType, {
|
||||||
|
foreignKey: 'typeId',
|
||||||
|
onDelete: 'CASCADE',
|
||||||
|
});
|
||||||
|
Status.belongsTo(User, {
|
||||||
|
foreignKey: 'userId',
|
||||||
|
onDelete: 'CASCADE',
|
||||||
|
});
|
||||||
|
|
||||||
const Recommendation = sequelize.define('recommendation', {
|
const Recommendation = sequelize.define('recommendation', {
|
||||||
id: {
|
id: {
|
||||||
|
@ -311,6 +331,14 @@ function getSequelizeModels (sequelize) {
|
||||||
defaultValue: Sequelize.NOW,
|
defaultValue: Sequelize.NOW,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
Recommendation.belongsTo(User, {
|
||||||
|
foreignKey: 'fromUser',
|
||||||
|
onDelete: 'SET NULL',
|
||||||
|
});
|
||||||
|
Recommendation.belongsTo(User, {
|
||||||
|
foreignKey: 'toUser',
|
||||||
|
onDelete: 'CASCADE',
|
||||||
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
User,
|
User,
|
||||||
|
|
Loading…
Add table
Reference in a new issue