Last active
October 21, 2018 10:59
-
-
Save harrisrobin/1415a91630bcfaefb6193d721c435b95 to your computer and use it in GitHub Desktop.
feathers-sequelize many-to-many association
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
| 'use strict'; | |
| // company-model.js - A sequelize model | |
| // | |
| // See http://docs.sequelizejs.com/en/latest/docs/models-definition/ | |
| // for more of what you can do here. | |
| const Sequelize = require('sequelize'); | |
| module.exports = function(sequelize) { | |
| const app = this; | |
| const Company = sequelize.define('Company', { | |
| name: { | |
| type: Sequelize.TEXT, | |
| allowNull: true, | |
| unique: true | |
| }, | |
| title: { | |
| type: Sequelize.TEXT, | |
| allowNull: true | |
| }, | |
| subtitle: { | |
| type: Sequelize.TEXT, | |
| allowNull: true | |
| }, | |
| description: { | |
| type: Sequelize.TEXT, | |
| allowNull: true | |
| }, | |
| logo: { | |
| type: Sequelize.JSON, | |
| allowNull: true | |
| }, | |
| gallery: { | |
| type: Sequelize.JSON, | |
| allowNull: true | |
| }, | |
| }, { | |
| freezeTableName: true, | |
| classMethods: { | |
| associate() { | |
| Company.belongsToMany(sequelize.models.User, {as: 'users', through: 'User_Company', onDelete: 'NO ACTION'}); | |
| } | |
| } | |
| }); | |
| return Company; | |
| }; |
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
| 'use strict'; | |
| const authentication = require('./authentication'); | |
| const company = require('./company'); | |
| const user = require('./user'); | |
| const Sequelize = require('sequelize'); | |
| const verifyReset = require('feathers-service-verify-reset').service; | |
| const userNotifier = require('../utils/verifyReset/userNotifier'); | |
| module.exports = function() { | |
| const app = this; | |
| const sequelize = new Sequelize(app.get('postgres'), { | |
| dialect: 'postgres', | |
| logging: true | |
| }); | |
| app.set('sequelize', sequelize); | |
| app.configure(authentication); | |
| app.configure(verifyReset({ userNotifier })); | |
| app.configure(user); | |
| app.configure(company); | |
| // Associate all of our models | |
| Object.keys(sequelize.models) | |
| .map(name => sequelize.models[name]) | |
| .filter(model => model.associate !== undefined) | |
| .forEach(model => model.associate()); | |
| sequelize.sync(); | |
| }; |
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
| 'use strict'; | |
| const sendVerificationNotification = require('./sendVerificationNotification'); | |
| const customizeGoogleLogin = require('./customizeGoogleLogin'); | |
| const customizeLinkedinLogin = require('./customizeLinkedinLogin'); | |
| const customizeFacebookLogin = require('./customizeFacebookLogin'); | |
| const globalHooks = require('../../../hooks'); | |
| const verifyHooks = require('feathers-service-verify-reset').hooks; | |
| const hooks = require('feathers-hooks-common'); | |
| const authentication = require('feathers-authentication'); | |
| const local = require('feathers-authentication-local'); | |
| const permissions = require('feathers-permissions'); | |
| exports.before = { | |
| all: [], | |
| find: [ | |
| authentication.hooks.authenticate('jwt'), | |
| ], | |
| get: [ | |
| authentication.hooks.authenticate('jwt'), | |
| ], | |
| create: [ | |
| customizeFacebookLogin(), | |
| local.hooks.hashPassword(), | |
| customizeLinkedinLogin(), | |
| customizeGoogleLogin(), | |
| verifyHooks.addVerification() | |
| ], | |
| update: [ | |
| authentication.hooks.authenticate('jwt'), | |
| local.hooks.hashPassword(), | |
| customizeFacebookLogin(), | |
| customizeLinkedinLogin(), | |
| customizeGoogleLogin() | |
| ], | |
| patch: [], | |
| remove: [] | |
| }; | |
| exports.after = { | |
| all: [hooks.remove('password')], | |
| find: [], | |
| get: [], | |
| create: [ | |
| verifyHooks.removeVerification(), | |
| sendVerificationNotification() | |
| ], | |
| update: [], | |
| patch: [], | |
| remove: [] | |
| }; |
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
| 'use strict'; | |
| // user-model.js - A sequelize model | |
| // | |
| // See http://docs.sequelizejs.com/en/latest/docs/models-definition/ | |
| // for more of what you can do here. | |
| const Sequelize = require('sequelize'); | |
| module.exports = function(sequelize) { | |
| const app = this; | |
| const User = sequelize.define('User', { | |
| firstName: { | |
| type: Sequelize.STRING, | |
| allowNull: true | |
| }, | |
| lastName: { | |
| type: Sequelize.STRING, | |
| allowNull: true | |
| }, | |
| email: { | |
| type: Sequelize.STRING, | |
| allowNull: false, | |
| unique: true | |
| }, | |
| password: { | |
| type: Sequelize.STRING, | |
| allowNull: true | |
| }, | |
| online: { | |
| type: Sequelize.BOOLEAN, | |
| defaultValue: false | |
| }, | |
| role: { | |
| type: Sequelize.ENUM, | |
| values: ['seeker', 'employer'] | |
| }, | |
| pictures: { | |
| type: Sequelize.JSON, | |
| allowNull: true | |
| }, | |
| bitbucketId: { | |
| type: Sequelize.STRING, | |
| allowNull: true | |
| }, | |
| facebookId: { | |
| type: Sequelize.STRING, | |
| allowNull: true | |
| }, | |
| githubId: { | |
| type: Sequelize.STRING, | |
| allowNull: true | |
| }, | |
| googleId: { | |
| type: Sequelize.STRING, | |
| allowNull: true | |
| }, | |
| linkedinId: { | |
| type: Sequelize.STRING, | |
| allowNull: true | |
| }, | |
| isVerified: { | |
| type: Sequelize.BOOLEAN, | |
| allowNull: true, | |
| defaultValue: true | |
| }, | |
| verifyToken: { | |
| type: Sequelize.STRING, | |
| allowNull: true | |
| }, | |
| verifyExpires: { | |
| type: Sequelize.DATE | |
| }, | |
| resetToken: { | |
| type: Sequelize.STRING, | |
| allowNull: true | |
| }, | |
| resetExpires: { | |
| type: Sequelize.DATE | |
| } | |
| }, { | |
| freezeTableName: true, | |
| classMethods: { | |
| associate() { | |
| User.belongsToMany(sequelize.models.Company, { as: 'companies', through: 'User_Company', onDelete: 'NO ACTION' }); | |
| } | |
| } | |
| }); | |
| return User; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment