aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/oauth/oauth-client.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2017-12-12 17:53:50 +0100
committerChocobozzz <me@florianbigard.com>2017-12-13 16:50:33 +0100
commit3fd3ab2d34d512b160a5e6084d7609be7b4f4452 (patch)
treee5ca358287fca6ecacce83defcf23af1e8e9f419 /server/models/oauth/oauth-client.ts
parentc893d4514e6ecbf282c7985fe5f82b8acd8a1137 (diff)
downloadPeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.tar.gz
PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.tar.zst
PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.zip
Move models to typescript-sequelize
Diffstat (limited to 'server/models/oauth/oauth-client.ts')
-rw-r--r--server/models/oauth/oauth-client.ts112
1 files changed, 44 insertions, 68 deletions
diff --git a/server/models/oauth/oauth-client.ts b/server/models/oauth/oauth-client.ts
index 9cc68771d..42c59bb79 100644
--- a/server/models/oauth/oauth-client.ts
+++ b/server/models/oauth/oauth-client.ts
@@ -1,86 +1,62 @@
1import * as Sequelize from 'sequelize' 1import { AllowNull, Column, CreatedAt, DataType, HasMany, Model, Table, UpdatedAt } from 'sequelize-typescript'
2import { OAuthTokenModel } from './oauth-token'
2 3
3import { addMethodsToModel } from '../utils' 4@Table({
4import { 5 tableName: 'oAuthClient',
5 OAuthClientInstance, 6 indexes: [
6 OAuthClientAttributes,
7
8 OAuthClientMethods
9} from './oauth-client-interface'
10
11let OAuthClient: Sequelize.Model<OAuthClientInstance, OAuthClientAttributes>
12let countTotal: OAuthClientMethods.CountTotal
13let loadFirstClient: OAuthClientMethods.LoadFirstClient
14let getByIdAndSecret: OAuthClientMethods.GetByIdAndSecret
15
16export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
17 OAuthClient = sequelize.define<OAuthClientInstance, OAuthClientAttributes>('OAuthClient',
18 { 7 {
19 clientId: { 8 fields: [ 'clientId' ],
20 type: DataTypes.STRING, 9 unique: true
21 allowNull: false
22 },
23 clientSecret: {
24 type: DataTypes.STRING,
25 allowNull: false
26 },
27 grants: {
28 type: DataTypes.ARRAY(DataTypes.STRING)
29 },
30 redirectUris: {
31 type: DataTypes.ARRAY(DataTypes.STRING)
32 }
33 }, 10 },
34 { 11 {
35 indexes: [ 12 fields: [ 'clientId', 'clientSecret' ],
36 { 13 unique: true
37 fields: [ 'clientId' ],
38 unique: true
39 },
40 {
41 fields: [ 'clientId', 'clientSecret' ],
42 unique: true
43 }
44 ]
45 } 14 }
46 ) 15 ]
16})
17export class OAuthClientModel extends Model<OAuthClientModel> {
47 18
48 const classMethods = [ 19 @AllowNull(false)
49 associate, 20 @Column
21 clientId: string
50 22
51 countTotal, 23 @AllowNull(false)
52 getByIdAndSecret, 24 @Column
53 loadFirstClient 25 clientSecret: string
54 ]
55 addMethodsToModel(OAuthClient, classMethods)
56 26
57 return OAuthClient 27 @Column(DataType.ARRAY(DataType.STRING))
58} 28 grants: string[]
29
30 @Column(DataType.ARRAY(DataType.STRING))
31 redirectUris: string[]
32
33 @CreatedAt
34 createdAt: Date
59 35
60// --------------------------------------------------------------------------- 36 @UpdatedAt
37 updatedAt: Date
61 38
62function associate (models) { 39 @HasMany(() => OAuthTokenModel, {
63 OAuthClient.hasMany(models.OAuthToken, {
64 foreignKey: 'oAuthClientId',
65 onDelete: 'cascade' 40 onDelete: 'cascade'
66 }) 41 })
67} 42 OAuthTokens: OAuthTokenModel[]
68 43
69countTotal = function () { 44 static countTotal () {
70 return OAuthClient.count() 45 return OAuthClientModel.count()
71} 46 }
72 47
73loadFirstClient = function () { 48 static loadFirstClient () {
74 return OAuthClient.findOne() 49 return OAuthClientModel.findOne()
75} 50 }
76 51
77getByIdAndSecret = function (clientId: string, clientSecret: string) { 52 static getByIdAndSecret (clientId: string, clientSecret: string) {
78 const query = { 53 const query = {
79 where: { 54 where: {
80 clientId: clientId, 55 clientId: clientId,
81 clientSecret: clientSecret 56 clientSecret: clientSecret
57 }
82 } 58 }
83 }
84 59
85 return OAuthClient.findOne(query) 60 return OAuthClientModel.findOne(query)
61 }
86} 62}