From feb4bdfd9b46e87aadfa7c0d5338cde887d1f58c Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sun, 11 Dec 2016 21:50:51 +0100 Subject: First version with PostgreSQL --- server/models/oauth-client.js | 72 ++++++++++++++++++++++++++++++------------- 1 file changed, 51 insertions(+), 21 deletions(-) (limited to 'server/models/oauth-client.js') diff --git a/server/models/oauth-client.js b/server/models/oauth-client.js index a1aefa985..15118591a 100644 --- a/server/models/oauth-client.js +++ b/server/models/oauth-client.js @@ -1,33 +1,63 @@ -const mongoose = require('mongoose') - -// --------------------------------------------------------------------------- - -const OAuthClientSchema = mongoose.Schema({ - clientSecret: String, - grants: Array, - redirectUris: Array -}) - -OAuthClientSchema.path('clientSecret').required(true) - -OAuthClientSchema.statics = { - getByIdAndSecret, - list, - loadFirstClient +module.exports = function (sequelize, DataTypes) { + const OAuthClient = sequelize.define('OAuthClient', + { + clientId: { + type: DataTypes.STRING + }, + clientSecret: { + type: DataTypes.STRING + }, + grants: { + type: DataTypes.ARRAY(DataTypes.STRING) + }, + redirectUris: { + type: DataTypes.ARRAY(DataTypes.STRING) + } + }, + { + classMethods: { + associate, + + getByIdAndSecret, + list, + loadFirstClient + } + } + ) + + return OAuthClient } -mongoose.model('OAuthClient', OAuthClientSchema) +// TODO: validation +// OAuthClientSchema.path('clientSecret').required(true) // --------------------------------------------------------------------------- +function associate (models) { + this.hasMany(models.OAuthToken, { + foreignKey: { + name: 'oAuthClientId', + allowNull: false + }, + onDelete: 'cascade' + }) +} + function list (callback) { - return this.find(callback) + return this.findAll().asCallback(callback) } function loadFirstClient (callback) { - return this.findOne({}, callback) + return this.findOne().asCallback(callback) } -function getByIdAndSecret (id, clientSecret) { - return this.findOne({ _id: id, clientSecret: clientSecret }).exec() +function getByIdAndSecret (clientId, clientSecret) { + const query = { + where: { + clientId: clientId, + clientSecret: clientSecret + } + } + + return this.findOne(query) } -- cgit v1.2.3 From 67bf9b96bbcd92b069fe86d9223fe0f8b9c6e677 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 28 Dec 2016 15:49:23 +0100 Subject: Server: add database field validations --- server/models/oauth-client.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'server/models/oauth-client.js') diff --git a/server/models/oauth-client.js b/server/models/oauth-client.js index 15118591a..b56838d4c 100644 --- a/server/models/oauth-client.js +++ b/server/models/oauth-client.js @@ -1,11 +1,15 @@ +'use strict' + module.exports = function (sequelize, DataTypes) { const OAuthClient = sequelize.define('OAuthClient', { clientId: { - type: DataTypes.STRING + type: DataTypes.STRING, + allowNull: false }, clientSecret: { - type: DataTypes.STRING + type: DataTypes.STRING, + allowNull: false }, grants: { type: DataTypes.ARRAY(DataTypes.STRING) @@ -28,9 +32,6 @@ module.exports = function (sequelize, DataTypes) { return OAuthClient } -// TODO: validation -// OAuthClientSchema.path('clientSecret').required(true) - // --------------------------------------------------------------------------- function associate (models) { -- cgit v1.2.3 From 319d072e8eb7266cd8d33e0bb2fb5ebe76c487d1 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 29 Dec 2016 09:33:28 +0100 Subject: Server: Add postgresql indexes --- server/models/oauth-client.js | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) (limited to 'server/models/oauth-client.js') diff --git a/server/models/oauth-client.js b/server/models/oauth-client.js index b56838d4c..758c4cf2f 100644 --- a/server/models/oauth-client.js +++ b/server/models/oauth-client.js @@ -19,9 +19,17 @@ module.exports = function (sequelize, DataTypes) { } }, { + indexes: [ + { + fields: [ 'clientId' ], + unique: true + }, + { + fields: [ 'clientId', 'clientSecret' ], + unique: true + } + ], classMethods: { - associate, - getByIdAndSecret, list, loadFirstClient @@ -34,16 +42,6 @@ module.exports = function (sequelize, DataTypes) { // --------------------------------------------------------------------------- -function associate (models) { - this.hasMany(models.OAuthToken, { - foreignKey: { - name: 'oAuthClientId', - allowNull: false - }, - onDelete: 'cascade' - }) -} - function list (callback) { return this.findAll().asCallback(callback) } -- cgit v1.2.3 From 4712081f2a5f48749cf125d729e78b926ab28d6d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 29 Dec 2016 10:33:36 +0100 Subject: Server: add association between author and user --- server/models/oauth-client.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'server/models/oauth-client.js') diff --git a/server/models/oauth-client.js b/server/models/oauth-client.js index 758c4cf2f..021a34007 100644 --- a/server/models/oauth-client.js +++ b/server/models/oauth-client.js @@ -30,8 +30,8 @@ module.exports = function (sequelize, DataTypes) { } ], classMethods: { + countTotal, getByIdAndSecret, - list, loadFirstClient } } @@ -42,8 +42,8 @@ module.exports = function (sequelize, DataTypes) { // --------------------------------------------------------------------------- -function list (callback) { - return this.findAll().asCallback(callback) +function countTotal (callback) { + return this.count().asCallback(callback) } function loadFirstClient (callback) { -- cgit v1.2.3