2019-09-27 04:03:15 +02:00
|
|
|
const Sequelize = require('sequelize');
|
|
|
|
|
|
|
|
function getSequelizeModels (sequelize) {
|
|
|
|
const User = sequelize.define('user', {
|
|
|
|
id: {
|
|
|
|
type: Sequelize.INTEGER,
|
|
|
|
primaryKey: true,
|
|
|
|
autoIncrement: true,
|
|
|
|
},
|
|
|
|
email: {
|
|
|
|
type: Sequelize.STRING,
|
|
|
|
allowNull: false,
|
|
|
|
unique: true,
|
2019-09-28 01:02:45 +02:00
|
|
|
validate: {
|
|
|
|
isEmail: true,
|
|
|
|
len: [5, 150],
|
|
|
|
},
|
2019-09-27 04:03:15 +02:00
|
|
|
},
|
|
|
|
username: {
|
|
|
|
type: Sequelize.STRING,
|
|
|
|
allowNull: false,
|
|
|
|
unique: true,
|
2019-09-28 01:02:45 +02:00
|
|
|
validate: {
|
|
|
|
is: /^[a-z0-9_]+$/i, // Is a set of characters a-z, 0-9, or _, case insensitive
|
|
|
|
len: [2, 32],
|
|
|
|
},
|
2019-09-27 04:03:15 +02:00
|
|
|
},
|
|
|
|
displayName: {
|
|
|
|
type: Sequelize.STRING,
|
|
|
|
allowNull: false,
|
|
|
|
unique: true,
|
2019-09-28 01:02:45 +02:00
|
|
|
validate: {
|
2019-10-26 19:47:15 +02:00
|
|
|
len: [1, 32],
|
2019-09-28 01:02:45 +02:00
|
|
|
},
|
2019-09-27 04:03:15 +02:00
|
|
|
},
|
|
|
|
passwordHash: {
|
|
|
|
type: Sequelize.STRING,
|
|
|
|
allowNull: false,
|
|
|
|
},
|
|
|
|
passwordSalt: {
|
|
|
|
type: Sequelize.STRING,
|
|
|
|
allowNull: false,
|
|
|
|
},
|
2019-09-28 02:40:39 +02:00
|
|
|
accountConfirm: {
|
|
|
|
type: Sequelize.STRING,
|
|
|
|
allowNull: true,
|
|
|
|
},
|
2019-09-27 04:03:15 +02:00
|
|
|
|
|
|
|
// Timestamps
|
|
|
|
createdAt: {
|
|
|
|
type: Sequelize.DATE,
|
|
|
|
allowNull: false,
|
|
|
|
defaultValue: Sequelize.NOW,
|
|
|
|
},
|
|
|
|
updatedAt: {
|
|
|
|
type: Sequelize.DATE,
|
|
|
|
allowNull: false,
|
|
|
|
defaultValue: Sequelize.NOW,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const Shelf = sequelize.define('shelf', {
|
|
|
|
id: {
|
|
|
|
type: Sequelize.INTEGER,
|
|
|
|
primaryKey: true,
|
|
|
|
autoIncrement: true,
|
|
|
|
},
|
2019-12-01 01:25:55 +01:00
|
|
|
userId: {
|
2019-09-27 04:03:15 +02:00
|
|
|
type: Sequelize.INTEGER,
|
|
|
|
references: {
|
|
|
|
model: User,
|
|
|
|
key: 'id',
|
|
|
|
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
name: {
|
|
|
|
type: Sequelize.STRING,
|
|
|
|
allowNull: false,
|
2019-12-01 19:03:55 +01:00
|
|
|
validate: {
|
|
|
|
len: [1, 32],
|
|
|
|
},
|
2019-09-27 04:03:15 +02:00
|
|
|
},
|
|
|
|
isPublic: {
|
|
|
|
type: Sequelize.BOOLEAN,
|
|
|
|
allowNull: false,
|
|
|
|
defaultValue: false,
|
|
|
|
},
|
|
|
|
isDeletable: {
|
|
|
|
type: Sequelize.BOOLEAN,
|
|
|
|
allowNull: false,
|
2019-12-01 01:25:55 +01:00
|
|
|
defaultValue: true,
|
2019-09-27 04:03:15 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Timestamps
|
|
|
|
createdAt: {
|
|
|
|
type: Sequelize.DATE,
|
|
|
|
allowNull: false,
|
|
|
|
defaultValue: Sequelize.NOW,
|
|
|
|
},
|
|
|
|
updatedAt: {
|
|
|
|
type: Sequelize.DATE,
|
|
|
|
allowNull: false,
|
|
|
|
defaultValue: Sequelize.NOW,
|
|
|
|
},
|
|
|
|
});
|
2019-12-01 01:25:55 +01:00
|
|
|
Shelf.belongsTo(User, {
|
|
|
|
foreignKey: 'userId',
|
|
|
|
onDelete: 'CASCADE',
|
|
|
|
});
|
2019-12-01 19:03:55 +01:00
|
|
|
User.hasMany(Shelf);
|
2019-09-27 04:03:15 +02:00
|
|
|
|
|
|
|
const BookReference = sequelize.define('bookReference', {
|
|
|
|
id: {
|
|
|
|
type: Sequelize.INTEGER,
|
|
|
|
primaryKey: true,
|
|
|
|
autoIncrement: true,
|
|
|
|
},
|
|
|
|
name: {
|
|
|
|
type: Sequelize.STRING,
|
|
|
|
allowNull: false,
|
|
|
|
},
|
|
|
|
description: {
|
|
|
|
type: Sequelize.STRING,
|
|
|
|
allowNull: false,
|
|
|
|
comment: 'The description is attempted to be made up of the type of work and the author',
|
|
|
|
},
|
|
|
|
sources: {
|
|
|
|
type: Sequelize.JSON,
|
|
|
|
allowNull: true,
|
|
|
|
defaultValue: [],
|
|
|
|
comment: 'A JSON array with each element being an object with named source <STRING> and source id <STRING>',
|
|
|
|
},
|
|
|
|
covers: {
|
|
|
|
type: Sequelize.JSON,
|
|
|
|
allowNull: true,
|
|
|
|
defaultValue: [],
|
|
|
|
comment: 'A JSON array with each element being an object with image url <STRING> and source id <STRING>',
|
|
|
|
},
|
|
|
|
|
|
|
|
// Timestamps
|
|
|
|
createdAt: {
|
|
|
|
type: Sequelize.DATE,
|
|
|
|
allowNull: false,
|
|
|
|
defaultValue: Sequelize.NOW,
|
|
|
|
},
|
|
|
|
updatedAt: {
|
|
|
|
type: Sequelize.DATE,
|
|
|
|
allowNull: false,
|
|
|
|
defaultValue: Sequelize.NOW,
|
|
|
|
},
|
2019-12-01 01:25:55 +01:00
|
|
|
}, {
|
|
|
|
indexes: [
|
|
|
|
{
|
|
|
|
fields: ['name', 'description'],
|
|
|
|
},
|
|
|
|
],
|
2019-09-27 04:03:15 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const ShelfItem = sequelize.define('shelfItem', {
|
|
|
|
id: {
|
|
|
|
type: Sequelize.INTEGER,
|
|
|
|
primaryKey: true,
|
|
|
|
autoIncrement: true,
|
|
|
|
},
|
2019-12-01 01:25:55 +01:00
|
|
|
shelfId: {
|
2019-09-27 04:03:15 +02:00
|
|
|
type: Sequelize.INTEGER,
|
|
|
|
references: {
|
|
|
|
model: Shelf,
|
|
|
|
key: 'id',
|
|
|
|
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE,
|
|
|
|
}
|
|
|
|
},
|
2019-12-01 01:25:55 +01:00
|
|
|
bookId: {
|
2019-09-27 04:03:15 +02:00
|
|
|
type: Sequelize.INTEGER,
|
|
|
|
references: {
|
|
|
|
model: BookReference,
|
|
|
|
key: 'id',
|
|
|
|
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
order: {
|
|
|
|
type: Sequelize.INTEGER,
|
|
|
|
defaultValue: 0,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Timestamps
|
|
|
|
createdAt: {
|
|
|
|
type: Sequelize.DATE,
|
|
|
|
allowNull: false,
|
|
|
|
defaultValue: Sequelize.NOW,
|
|
|
|
},
|
|
|
|
updatedAt: {
|
|
|
|
type: Sequelize.DATE,
|
|
|
|
allowNull: false,
|
|
|
|
defaultValue: Sequelize.NOW,
|
|
|
|
},
|
|
|
|
});
|
2019-12-01 01:25:55 +01:00
|
|
|
ShelfItem.belongsTo(Shelf, {
|
|
|
|
foreignKey: 'shelfId',
|
|
|
|
onDelete: 'CASCADE',
|
|
|
|
});
|
2019-12-01 19:03:55 +01:00
|
|
|
Shelf.hasMany(ShelfItem);
|
2019-12-01 01:25:55 +01:00
|
|
|
ShelfItem.belongsTo(BookReference, {
|
|
|
|
foreignKey: 'bookId',
|
|
|
|
onDelete: 'CASCADE',
|
|
|
|
});
|
2019-09-27 04:03:15 +02:00
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
2019-12-01 01:25:55 +01:00
|
|
|
typeId: {
|
2019-09-27 04:03:15 +02:00
|
|
|
type: Sequelize.INTEGER,
|
|
|
|
references: {
|
|
|
|
model: StatusType,
|
|
|
|
key: 'id',
|
|
|
|
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE,
|
|
|
|
}
|
|
|
|
},
|
2019-12-01 01:25:55 +01:00
|
|
|
userId: {
|
2019-09-27 04:03:15 +02:00
|
|
|
type: Sequelize.INTEGER,
|
|
|
|
references: {
|
|
|
|
model: User,
|
|
|
|
key: 'id',
|
|
|
|
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
text: {
|
|
|
|
type: Sequelize.TEXT,
|
|
|
|
allowNull: true,
|
|
|
|
},
|
|
|
|
book: {
|
|
|
|
type: Sequelize.INTEGER,
|
|
|
|
allowNull: true,
|
|
|
|
references: {
|
|
|
|
model: BookReference,
|
|
|
|
key: 'id',
|
|
|
|
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
type: Sequelize.JSON,
|
|
|
|
allowNull: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Timestamps
|
|
|
|
createdAt: {
|
|
|
|
type: Sequelize.DATE,
|
|
|
|
allowNull: false,
|
|
|
|
defaultValue: Sequelize.NOW,
|
|
|
|
},
|
|
|
|
updatedAt: {
|
|
|
|
type: Sequelize.DATE,
|
|
|
|
allowNull: false,
|
|
|
|
defaultValue: Sequelize.NOW,
|
|
|
|
},
|
|
|
|
});
|
2019-12-01 01:25:55 +01:00
|
|
|
Status.belongsTo(StatusType, {
|
|
|
|
foreignKey: 'typeId',
|
|
|
|
onDelete: 'CASCADE',
|
|
|
|
});
|
|
|
|
Status.belongsTo(User, {
|
|
|
|
foreignKey: 'userId',
|
|
|
|
onDelete: 'CASCADE',
|
|
|
|
});
|
2019-12-01 19:03:55 +01:00
|
|
|
User.hasMany(Status);
|
2019-09-27 04:03:15 +02:00
|
|
|
|
|
|
|
const Recommendation = sequelize.define('recommendation', {
|
|
|
|
id: {
|
|
|
|
type: Sequelize.INTEGER,
|
|
|
|
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,
|
|
|
|
references: {
|
|
|
|
model: User,
|
|
|
|
key: 'id',
|
|
|
|
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
text: {
|
|
|
|
type: Sequelize.TEXT,
|
|
|
|
allowNull: false,
|
|
|
|
},
|
|
|
|
book: {
|
|
|
|
type: Sequelize.INTEGER,
|
|
|
|
allowNull: false,
|
|
|
|
references: {
|
|
|
|
model: BookReference,
|
|
|
|
key: 'id',
|
|
|
|
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
type: Sequelize.JSON,
|
|
|
|
allowNull: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Timestamps
|
|
|
|
createdAt: {
|
|
|
|
type: Sequelize.DATE,
|
|
|
|
allowNull: false,
|
|
|
|
defaultValue: Sequelize.NOW,
|
|
|
|
},
|
|
|
|
updatedAt: {
|
|
|
|
type: Sequelize.DATE,
|
|
|
|
allowNull: false,
|
|
|
|
defaultValue: Sequelize.NOW,
|
|
|
|
},
|
|
|
|
});
|
2019-12-01 01:25:55 +01:00
|
|
|
Recommendation.belongsTo(User, {
|
|
|
|
foreignKey: 'fromUser',
|
|
|
|
onDelete: 'SET NULL',
|
|
|
|
});
|
|
|
|
Recommendation.belongsTo(User, {
|
|
|
|
foreignKey: 'toUser',
|
|
|
|
onDelete: 'CASCADE',
|
|
|
|
});
|
2019-12-01 19:03:55 +01:00
|
|
|
User.hasMany(Recommendation, {
|
|
|
|
foreignKey: 'toUser',
|
|
|
|
})
|
2019-09-27 04:03:15 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
User,
|
|
|
|
Shelf,
|
|
|
|
BookReference,
|
|
|
|
ShelfItem,
|
|
|
|
StatusType,
|
|
|
|
Status,
|
|
|
|
Recommendation,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = getSequelizeModels;
|