Commit 86897be7 authored by Robbie Antenesse's avatar Robbie Antenesse

DB: Split Review from Status; remove StatusTypes

parent 243a4094
......@@ -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,77 @@ 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', {
const Status = sequelize.define('status', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
name: {
type: Sequelize.STRING,
unique: true,
userId: {
type: Sequelize.INTEGER,
references: {
model: User,
key: 'id',
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE,
}
},
text: {
type: Sequelize.TEXT,
allowNull: true,
},
shelfItemId: {
type: Sequelize.INTEGER,
allowNull: true,
references: {
model: ShelfItem,
key: 'id',
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE,
}
},
progress: {
type: Sequelize.INTEGER,
allowNull: true,
},
// Timestamps
createdAt: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.NOW,
},
updatedAt: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.NOW,
},
}, {
timestamps: false,
});
User.hasMany(Status);
Status.belongsTo(User, {
foreignKey: 'userId',
onDelete: 'CASCADE',
});
ShelfItem.hasMany(Status);
Status.belongsTo(ShelfItem, {
foreignKey: 'shelfItemId',
// onDelete: 'IGNORE'
});
const Status = sequelize.define('status', {
const Review = sequelize.define('review', {
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,7 +283,7 @@ function getModels (sequelize) {
type: Sequelize.TEXT,
allowNull: true,
},
book: {
bookReferenceId: {
type: Sequelize.INTEGER,
allowNull: true,
references: {
......@@ -255,8 +292,8 @@ function getModels (sequelize) {
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE,
}
},
data: {
type: Sequelize.JSON,
rating: {
type: Sequelize.INTEGER,
allowNull: true,
},
......@@ -272,15 +309,16 @@ function getModels (sequelize) {
defaultValue: Sequelize.NOW,
},
});
Status.belongsTo(StatusType, {
foreignKey: 'typeId',
onDelete: 'CASCADE',
});
Status.belongsTo(User, {
User.hasMany(Review);
Review.belongsTo(User, {
foreignKey: 'userId',
onDelete: 'CASCADE',
});
User.hasMany(Status);
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,
}
}
......
......@@ -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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment