Skip to content

Commit

Permalink
users-scopes many-to-many association
Browse files Browse the repository at this point in the history
  • Loading branch information
Smarthard committed Jul 26, 2020
1 parent 3aafcb5 commit 9fe21ae
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 22 deletions.
8 changes: 0 additions & 8 deletions migrations/20200709131700-create-scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@ module.exports = {
value: {
allowNull: false,
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
Expand Down
7 changes: 0 additions & 7 deletions migrations/20200709131739-create-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ module.exports = {
allowNull: false,
type: Sequelize.STRING
},
scopes: {
references: {
model: 'Scopes',
key: 'id'
},
type: Sequelize.INTEGER
},
shikimoriId: {
type: Sequelize.STRING
},
Expand Down
26 changes: 26 additions & 0 deletions migrations/20200726184442-associate-users-and-scopes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('UserScopes', {
userId: {
references: {
model: 'Users',
key: 'id'
},
type: Sequelize.INTEGER
},
scopeId: {
references: {
model: 'Scopes',
key: 'id'
},
type: Sequelize.INTEGER
}
})
},

down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('UserScopes', { cascade: true })
}
};
11 changes: 7 additions & 4 deletions src/models/Scope.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import {Column, ForeignKey, Model, Table} from 'sequelize-typescript';
import {BelongsToMany, Column, Model, Table} from 'sequelize-typescript';
import {UserScopes} from "./UserScopes";
import {User} from "./User";

@Table({
createdAt: false,
updatedAt: false
})
export class Scope extends Model<Scope> {

@ForeignKey(() => User)
userId!: number;

@Column
value!: string;

@BelongsToMany(() => User, () => UserScopes, 'scopeId')
scopes!: Scope[];

}
9 changes: 7 additions & 2 deletions src/models/User.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import {Column, HasMany, Model, Table} from 'sequelize-typescript';
import {BelongsToMany, Column, Model, PrimaryKey, Table} from 'sequelize-typescript';
import {Scope} from './Scope';
import {UserScopes} from './UserScopes';

@Table({
updatedAt: false
})
export class User extends Model<User> {

@PrimaryKey
@Column
id!: number;

@Column
name!: string;

Expand All @@ -18,7 +23,7 @@ export class User extends Model<User> {
@Column
email!: string;

@HasMany(() => Scope)
@BelongsToMany(() => Scope, () => UserScopes, 'userId')
scopes!: Scope[];

@Column
Expand Down
18 changes: 18 additions & 0 deletions src/models/UserScopes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {Column, ForeignKey, Model, Table} from 'sequelize-typescript';
import {User} from './User';
import {Scope} from './Scope';

@Table({
createdAt: false,
updatedAt: false
})
export class UserScopes extends Model<UserScopes> {

@ForeignKey(() => User)
@Column
userId!: number;

@ForeignKey(() => Scope)
@Column
scopeId!: number
}
3 changes: 2 additions & 1 deletion src/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {User} from './User';
import {Scope} from './Scope';
import {UserScopes} from './UserScopes';

export const models = [User, Scope];
export const models = [User, Scope, UserScopes];

0 comments on commit 9fe21ae

Please sign in to comment.