DB: Split Review from Status; remove StatusTypes

This commit is contained in:
Robbie Antenesse 2020-01-12 22:55:38 -07:00
parent 243a4094d0
commit 86897be78a
2 changed files with 73 additions and 55 deletions

View File

@ -178,6 +178,11 @@ function getModels (sequelize) {
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE,
}
},
bookEdition: {
type: Sequelize.JSON,
allowNull: true,
comment: 'An object with properties `source` and `id`',
},
order: {
type: Sequelize.INTEGER,
defaultValue: 0,
@ -195,45 +200,22 @@ function getModels (sequelize) {
defaultValue: Sequelize.NOW,
},
});
Shelf.hasMany(ShelfItem);
ShelfItem.belongsTo(Shelf, {
foreignKey: 'shelfId',
onDelete: 'CASCADE',
});
Shelf.hasMany(ShelfItem);
ShelfItem.belongsTo(BookReference, {
foreignKey: 'bookId',
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', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
typeId: {
type: Sequelize.INTEGER,
references: {
model: StatusType,
key: 'id',
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE,
}
},
userId: {
type: Sequelize.INTEGER,
references: {
@ -246,17 +228,17 @@ function getModels (sequelize) {
type: Sequelize.TEXT,
allowNull: true,
},
book: {
shelfItemId: {
type: Sequelize.INTEGER,
allowNull: true,
references: {
model: BookReference,
model: ShelfItem,
key: 'id',
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE,
}
},
data: {
type: Sequelize.JSON,
progress: {
type: Sequelize.INTEGER,
allowNull: true,
},
@ -272,15 +254,71 @@ function getModels (sequelize) {
defaultValue: Sequelize.NOW,
},
});
Status.belongsTo(StatusType, {
foreignKey: 'typeId',
onDelete: 'CASCADE',
});
User.hasMany(Status);
Status.belongsTo(User, {
foreignKey: 'userId',
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', {
id: {
@ -288,16 +326,6 @@ function getModels (sequelize) {
primaryKey: 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: {
type: Sequelize.INTEGER,
allowNull: false,
@ -356,6 +384,7 @@ function getModels (sequelize) {
ShelfItem,
StatusType,
Status,
Review,
Recommendation,
}
}

View File

@ -61,21 +61,10 @@ if (!force) {
}
}
const Models = require('./getModels')(sequelize);
console.log(`Installing database tables${force ? ', dropping existing ones first' : ''}...`);
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();
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 => {
if (err) {
console.error(err);