]>
Commit | Line | Data |
---|---|---|
e02643f3 C |
1 | import * as Sequelize from 'sequelize' |
2 | ||
74889a71 | 3 | import { addMethodsToModel } from '../utils' |
e02643f3 | 4 | import { |
e02643f3 C |
5 | OAuthClientInstance, |
6 | OAuthClientAttributes, | |
7 | ||
8 | OAuthClientMethods | |
9 | } from './oauth-client-interface' | |
10 | ||
11 | let OAuthClient: Sequelize.Model<OAuthClientInstance, OAuthClientAttributes> | |
12 | let countTotal: OAuthClientMethods.CountTotal | |
13 | let loadFirstClient: OAuthClientMethods.LoadFirstClient | |
14 | let getByIdAndSecret: OAuthClientMethods.GetByIdAndSecret | |
15 | ||
127944aa C |
16 | export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { |
17 | OAuthClient = sequelize.define<OAuthClientInstance, OAuthClientAttributes>('OAuthClient', | |
feb4bdfd C |
18 | { |
19 | clientId: { | |
67bf9b96 C |
20 | type: DataTypes.STRING, |
21 | allowNull: false | |
feb4bdfd C |
22 | }, |
23 | clientSecret: { | |
67bf9b96 C |
24 | type: DataTypes.STRING, |
25 | allowNull: false | |
feb4bdfd C |
26 | }, |
27 | grants: { | |
28 | type: DataTypes.ARRAY(DataTypes.STRING) | |
29 | }, | |
30 | redirectUris: { | |
31 | type: DataTypes.ARRAY(DataTypes.STRING) | |
32 | } | |
33 | }, | |
34 | { | |
319d072e C |
35 | indexes: [ |
36 | { | |
37 | fields: [ 'clientId' ], | |
38 | unique: true | |
39 | }, | |
40 | { | |
41 | fields: [ 'clientId', 'clientSecret' ], | |
42 | unique: true | |
43 | } | |
e02643f3 | 44 | ] |
feb4bdfd C |
45 | } |
46 | ) | |
47 | ||
e02643f3 C |
48 | const classMethods = [ |
49 | associate, | |
50 | ||
51 | countTotal, | |
52 | getByIdAndSecret, | |
53 | loadFirstClient | |
54 | ] | |
55 | addMethodsToModel(OAuthClient, classMethods) | |
56 | ||
feb4bdfd | 57 | return OAuthClient |
69b0a27c C |
58 | } |
59 | ||
69b0a27c C |
60 | // --------------------------------------------------------------------------- |
61 | ||
e02643f3 C |
62 | function associate (models) { |
63 | OAuthClient.hasMany(models.OAuthToken, { | |
64 | foreignKey: 'oAuthClientId', | |
65 | onDelete: 'cascade' | |
66 | }) | |
67 | } | |
68 | ||
6fcd19ba C |
69 | countTotal = function () { |
70 | return OAuthClient.count() | |
69b0a27c C |
71 | } |
72 | ||
6fcd19ba C |
73 | loadFirstClient = function () { |
74 | return OAuthClient.findOne() | |
69b0a27c C |
75 | } |
76 | ||
69818c93 | 77 | getByIdAndSecret = function (clientId: string, clientSecret: string) { |
feb4bdfd C |
78 | const query = { |
79 | where: { | |
80 | clientId: clientId, | |
81 | clientSecret: clientSecret | |
82 | } | |
83 | } | |
84 | ||
e02643f3 | 85 | return OAuthClient.findOne(query) |
69b0a27c | 86 | } |