Skip to content

Instantly share code, notes, and snippets.

@hypeJunction
Last active March 22, 2019 07:27
Show Gist options
  • Save hypeJunction/5099e8729a796d115085777e4a691b11 to your computer and use it in GitHub Desktop.
Save hypeJunction/5099e8729a796d115085777e4a691b11 to your computer and use it in GitHub Desktop.
import { DataTypes } from 'sequelize';
export default {
Upload: {
options: {
tableName: 'uploads',
createdAt: 'date_created',
updatedAt: 'date_updated',
},
attributes: {
id: {
type: DataTypes.INTEGER(11),
allowNull: false,
primaryKey: true,
autoIncrement: true,
},
path: {
type: DataTypes.STRING(100),
allowNull: false,
},
owner_id: {
type: DataTypes.INTEGER(11),
allowNull: true,
references: {
model: 'users',
key: 'id',
},
onUpdate: 'CASCADE',
onDelete: 'CASCADE',
},
date_created: {
type: DataTypes.DATE,
allowNull: false,
},
date_updated: {
type: DataTypes.DATE,
allowNull: true,
},
},
relations: {
belongsTo: {
User: {
as: 'owner',
foreignKey: 'owner_id',
constraints: false,
},
},
},
},
User: {
options: {
tableName: 'users',
createdAt: 'date_joined',
updatedAt: 'date_updated',
},
attributes: {
id: {
type: DataTypes.INTEGER(11),
allowNull: false,
primaryKey: true,
autoIncrement: true,
},
username: {
type: DataTypes.STRING(150),
allowNull: false,
unique: true,
validate: {
len: [4, 150],
},
},
password: {
type: DataTypes.STRING(100),
allowNull: false,
validate: {
notEmpty: true,
},
},
avatar_id: {
type: DataTypes.INTEGER(11),
allowNull: true,
references: {
model: 'uploads',
key: 'id',
},
onUpdate: 'CASCADE',
onDelete: 'SET NULL',
},
date_joined: {
type: DataTypes.DATE,
allowNull: false,
},
date_updated: {
type: DataTypes.DATE,
allowNull: true,
},
},
relations: {
belongsTo: {
// We also want to make sure that we support multiple relationships to the same model
// e.g. Upload: [{ as: 'avatar' }, { as: 'cover' }]
Upload: {
as: 'avatar',
foreignKey: 'avatar_id',
constraints: false,
},
},
hasMany: {
Upload: {
as: 'uploads',
foreignKey: 'owner_id',
},
},
},
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment