mirror of
https://gitlab.com/Alamantus/Readlebee.git
synced 2025-06-28 04:04:18 +02:00
DB: Split Review from Status; remove StatusTypes
This commit is contained in:
parent
243a4094d0
commit
86897be78a
2 changed files with 73 additions and 55 deletions
|
@ -178,6 +178,11 @@ function getModels (sequelize) {
|
||||||
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE,
|
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
bookEdition: {
|
||||||
|
type: Sequelize.JSON,
|
||||||
|
allowNull: true,
|
||||||
|
comment: 'An object with properties `source` and `id`',
|
||||||
|
},
|
||||||
order: {
|
order: {
|
||||||
type: Sequelize.INTEGER,
|
type: Sequelize.INTEGER,
|
||||||
defaultValue: 0,
|
defaultValue: 0,
|
||||||
|
@ -195,45 +200,22 @@ function getModels (sequelize) {
|
||||||
defaultValue: Sequelize.NOW,
|
defaultValue: Sequelize.NOW,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
Shelf.hasMany(ShelfItem);
|
||||||
ShelfItem.belongsTo(Shelf, {
|
ShelfItem.belongsTo(Shelf, {
|
||||||
foreignKey: 'shelfId',
|
foreignKey: 'shelfId',
|
||||||
onDelete: 'CASCADE',
|
onDelete: 'CASCADE',
|
||||||
});
|
});
|
||||||
Shelf.hasMany(ShelfItem);
|
|
||||||
ShelfItem.belongsTo(BookReference, {
|
ShelfItem.belongsTo(BookReference, {
|
||||||
foreignKey: 'bookId',
|
foreignKey: 'bookId',
|
||||||
onDelete: 'CASCADE',
|
onDelete: 'CASCADE',
|
||||||
});
|
});
|
||||||
|
|
||||||
const StatusType = sequelize.define('statusType', {
|
|
||||||
id: {
|
|
||||||
type: Sequelize.INTEGER,
|
|
||||||
primaryKey: true,
|
|
||||||
autoIncrement: true,
|
|
||||||
},
|
|
||||||
name: {
|
|
||||||
type: Sequelize.STRING,
|
|
||||||
unique: true,
|
|
||||||
allowNull: false,
|
|
||||||
},
|
|
||||||
}, {
|
|
||||||
timestamps: false,
|
|
||||||
});
|
|
||||||
|
|
||||||
const Status = sequelize.define('status', {
|
const Status = sequelize.define('status', {
|
||||||
id: {
|
id: {
|
||||||
type: Sequelize.INTEGER,
|
type: Sequelize.INTEGER,
|
||||||
primaryKey: true,
|
primaryKey: true,
|
||||||
autoIncrement: true,
|
autoIncrement: true,
|
||||||
},
|
},
|
||||||
typeId: {
|
|
||||||
type: Sequelize.INTEGER,
|
|
||||||
references: {
|
|
||||||
model: StatusType,
|
|
||||||
key: 'id',
|
|
||||||
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE,
|
|
||||||
}
|
|
||||||
},
|
|
||||||
userId: {
|
userId: {
|
||||||
type: Sequelize.INTEGER,
|
type: Sequelize.INTEGER,
|
||||||
references: {
|
references: {
|
||||||
|
@ -246,17 +228,17 @@ function getModels (sequelize) {
|
||||||
type: Sequelize.TEXT,
|
type: Sequelize.TEXT,
|
||||||
allowNull: true,
|
allowNull: true,
|
||||||
},
|
},
|
||||||
book: {
|
shelfItemId: {
|
||||||
type: Sequelize.INTEGER,
|
type: Sequelize.INTEGER,
|
||||||
allowNull: true,
|
allowNull: true,
|
||||||
references: {
|
references: {
|
||||||
model: BookReference,
|
model: ShelfItem,
|
||||||
key: 'id',
|
key: 'id',
|
||||||
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE,
|
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
data: {
|
progress: {
|
||||||
type: Sequelize.JSON,
|
type: Sequelize.INTEGER,
|
||||||
allowNull: true,
|
allowNull: true,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -272,15 +254,71 @@ function getModels (sequelize) {
|
||||||
defaultValue: Sequelize.NOW,
|
defaultValue: Sequelize.NOW,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
Status.belongsTo(StatusType, {
|
User.hasMany(Status);
|
||||||
foreignKey: 'typeId',
|
|
||||||
onDelete: 'CASCADE',
|
|
||||||
});
|
|
||||||
Status.belongsTo(User, {
|
Status.belongsTo(User, {
|
||||||
foreignKey: 'userId',
|
foreignKey: 'userId',
|
||||||
onDelete: 'CASCADE',
|
onDelete: 'CASCADE',
|
||||||
});
|
});
|
||||||
User.hasMany(Status);
|
ShelfItem.hasMany(Status);
|
||||||
|
Status.belongsTo(ShelfItem, {
|
||||||
|
foreignKey: 'shelfItemId',
|
||||||
|
// onDelete: 'IGNORE'
|
||||||
|
});
|
||||||
|
|
||||||
|
const Review = sequelize.define('review', {
|
||||||
|
id: {
|
||||||
|
type: Sequelize.INTEGER,
|
||||||
|
primaryKey: true,
|
||||||
|
autoIncrement: true,
|
||||||
|
},
|
||||||
|
userId: {
|
||||||
|
type: Sequelize.INTEGER,
|
||||||
|
references: {
|
||||||
|
model: User,
|
||||||
|
key: 'id',
|
||||||
|
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
text: {
|
||||||
|
type: Sequelize.TEXT,
|
||||||
|
allowNull: true,
|
||||||
|
},
|
||||||
|
bookReferenceId: {
|
||||||
|
type: Sequelize.INTEGER,
|
||||||
|
allowNull: true,
|
||||||
|
references: {
|
||||||
|
model: BookReference,
|
||||||
|
key: 'id',
|
||||||
|
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
rating: {
|
||||||
|
type: Sequelize.INTEGER,
|
||||||
|
allowNull: true,
|
||||||
|
},
|
||||||
|
|
||||||
|
// Timestamps
|
||||||
|
createdAt: {
|
||||||
|
type: Sequelize.DATE,
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: Sequelize.NOW,
|
||||||
|
},
|
||||||
|
updatedAt: {
|
||||||
|
type: Sequelize.DATE,
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: Sequelize.NOW,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
User.hasMany(Review);
|
||||||
|
Review.belongsTo(User, {
|
||||||
|
foreignKey: 'userId',
|
||||||
|
onDelete: 'CASCADE',
|
||||||
|
});
|
||||||
|
BookReference.hasMany(Review);
|
||||||
|
Review.belongsTo(BookReference, {
|
||||||
|
foreignKey: 'shelfItemId',
|
||||||
|
// onDelete: 'IGNORE'
|
||||||
|
});
|
||||||
|
|
||||||
const Recommendation = sequelize.define('recommendation', {
|
const Recommendation = sequelize.define('recommendation', {
|
||||||
id: {
|
id: {
|
||||||
|
@ -288,16 +326,6 @@ function getModels (sequelize) {
|
||||||
primaryKey: true,
|
primaryKey: true,
|
||||||
autoIncrement: true,
|
autoIncrement: true,
|
||||||
},
|
},
|
||||||
fromUser: {
|
|
||||||
type: Sequelize.INTEGER,
|
|
||||||
allowNull: true,
|
|
||||||
references: {
|
|
||||||
model: User,
|
|
||||||
key: 'id',
|
|
||||||
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE,
|
|
||||||
},
|
|
||||||
comment: 'If null, check data for arbitrary from user text.',
|
|
||||||
},
|
|
||||||
toUser: {
|
toUser: {
|
||||||
type: Sequelize.INTEGER,
|
type: Sequelize.INTEGER,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
|
@ -356,6 +384,7 @@ function getModels (sequelize) {
|
||||||
ShelfItem,
|
ShelfItem,
|
||||||
StatusType,
|
StatusType,
|
||||||
Status,
|
Status,
|
||||||
|
Review,
|
||||||
Recommendation,
|
Recommendation,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,21 +61,10 @@ if (!force) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const Models = require('./getModels')(sequelize);
|
|
||||||
|
|
||||||
console.log(`Installing database tables${force ? ', dropping existing ones first' : ''}...`);
|
console.log(`Installing database tables${force ? ', dropping existing ones first' : ''}...`);
|
||||||
sequelize.sync({ force }).then(() => {
|
sequelize.sync({ force }).then(() => {
|
||||||
console.log('Tables installed! Adding Status Types...')
|
|
||||||
const promises = [ // Default status types to use in Statuses
|
|
||||||
{ name: 'update' },
|
|
||||||
{ name: 'progress' },
|
|
||||||
{ name: 'rating' },
|
|
||||||
{ name: 'review' },
|
|
||||||
].map(statusType => Models.StatusType.create(statusType).catch(() => console.log(statusType.name, 'already exists')));
|
|
||||||
return Promise.all(promises);
|
|
||||||
}).then(() => {
|
|
||||||
sequelize.close();
|
sequelize.close();
|
||||||
console.log(`Status Types installed! Writing database version to ${dbVersionPath}...`);
|
console.log(`Tables installed! Writing database version to ${dbVersionPath}...`);
|
||||||
fs.writeFile(dbVersionPath, migration.dbVersion, err => {
|
fs.writeFile(dbVersionPath, migration.dbVersion, err => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
|
|
Loading…
Add table
Reference in a new issue