Created
May 2, 2016 20:43
-
-
Save vsnikkil/d2b0b4516a6ad7b320217ede48475279 to your computer and use it in GitHub Desktop.
Sequelize snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import User from './User' | |
import Session from './Session' | |
/* Associations */ | |
User.hasMany(Session) | |
/* Hooks */ | |
User.afterDestroy(async (user, options) => { | |
await Session.destroy({ | |
where: { userId: user.id }, | |
individualHooks: true | |
}) | |
}) | |
export { User, Session } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Simple model for user session */ | |
import Sequelize from 'sequelize' | |
import sequelize from '../sequelize' | |
var Session = sequelize.define('session', { | |
token: { | |
allowNull: false, | |
type: Sequelize.STRING(256), | |
unique: true, | |
primaryKey: true | |
}, | |
ip: { | |
allowNull: false, | |
type: Sequelize.STRING(50) | |
} | |
}) | |
export default Session |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Simple model for user */ | |
import Sequelize from 'sequelize' | |
import sequelize from '../sequelize' | |
var User = sequelize.define('user', { | |
username: { | |
type: Sequelize.STRING(32), | |
allowNull: false, | |
unique: true, | |
validate: { | |
len: [3, 32] | |
} | |
}, | |
firstName: { | |
type: Sequelize.STRING(64), | |
allowNull: false, | |
validate: { | |
len: [1, 64] | |
} | |
}, | |
lastName: { | |
type: Sequelize.STRING(64), | |
allowNull: false, | |
validate: { | |
len: [1, 64] | |
} | |
}, | |
email: { | |
type: Sequelize.STRING(254), | |
allowNull: false, | |
unique: true, | |
validate: { | |
isEmail: true | |
} | |
}, | |
profileDescription: { | |
type: Sequelize.STRING(2000), | |
allowNull: true, | |
validate: { | |
len: [0, 2000] | |
} | |
}, | |
hash: { | |
type: Sequelize.STRING(128).BINARY | |
} | |
}) | |
export default User |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment