From a9e7222e5e1b1b1458efc7628a87bf04d426b522 Mon Sep 17 00:00:00 2001 From: Gergo Torcsvari Date: Sun, 19 Jul 2020 16:37:44 +0200 Subject: [PATCH] introduced now_helper for multidb capabilities --- backend/models/access_list.js | 7 ++++--- backend/models/access_list_auth.js | 7 ++++--- backend/models/access_list_client.js | 7 ++++--- backend/models/audit-log.js | 7 ++++--- backend/models/auth.js | 7 ++++--- backend/models/certificate.js | 9 +++++---- backend/models/dead_host.js | 7 ++++--- backend/models/now_helper.js | 13 +++++++++++++ backend/models/proxy_host.js | 7 ++++--- backend/models/redirection_host.js | 7 ++++--- backend/models/stream.js | 7 ++++--- backend/models/user.js | 7 ++++--- backend/models/user_permission.js | 7 ++++--- 13 files changed, 62 insertions(+), 37 deletions(-) create mode 100644 backend/models/now_helper.js diff --git a/backend/models/access_list.js b/backend/models/access_list.js index 1af9e832..8e63a2ae 100644 --- a/backend/models/access_list.js +++ b/backend/models/access_list.js @@ -6,13 +6,14 @@ const Model = require('objection').Model; const User = require('./user'); const AccessListAuth = require('./access_list_auth'); const AccessListClient = require('./access_list_client'); +const now = require('./now_helper'); Model.knex(db); class AccessList extends Model { $beforeInsert () { - this.created_on = Model.raw('NOW()'); - this.modified_on = Model.raw('NOW()'); + this.created_on = now(); + this.modified_on = now(); // Default for meta if (typeof this.meta === 'undefined') { @@ -21,7 +22,7 @@ class AccessList extends Model { } $beforeUpdate () { - this.modified_on = Model.raw('NOW()'); + this.modified_on = now(); } static get name () { diff --git a/backend/models/access_list_auth.js b/backend/models/access_list_auth.js index 20436ccd..932371f3 100644 --- a/backend/models/access_list_auth.js +++ b/backend/models/access_list_auth.js @@ -3,13 +3,14 @@ const db = require('../db'); const Model = require('objection').Model; +const now = require('./now_helper'); Model.knex(db); class AccessListAuth extends Model { $beforeInsert () { - this.created_on = Model.raw('NOW()'); - this.modified_on = Model.raw('NOW()'); + this.created_on = now(); + this.modified_on = now(); // Default for meta if (typeof this.meta === 'undefined') { @@ -18,7 +19,7 @@ class AccessListAuth extends Model { } $beforeUpdate () { - this.modified_on = Model.raw('NOW()'); + this.modified_on = now(); } static get name () { diff --git a/backend/models/access_list_client.js b/backend/models/access_list_client.js index 0386395c..e257213a 100644 --- a/backend/models/access_list_client.js +++ b/backend/models/access_list_client.js @@ -3,13 +3,14 @@ const db = require('../db'); const Model = require('objection').Model; +const now = require('./now_helper'); Model.knex(db); class AccessListClient extends Model { $beforeInsert () { - this.created_on = Model.raw('NOW()'); - this.modified_on = Model.raw('NOW()'); + this.created_on = now(); + this.modified_on = now(); // Default for meta if (typeof this.meta === 'undefined') { @@ -18,7 +19,7 @@ class AccessListClient extends Model { } $beforeUpdate () { - this.modified_on = Model.raw('NOW()'); + this.modified_on = now(); } static get name () { diff --git a/backend/models/audit-log.js b/backend/models/audit-log.js index 3d473fc1..a3a318c8 100644 --- a/backend/models/audit-log.js +++ b/backend/models/audit-log.js @@ -4,13 +4,14 @@ const db = require('../db'); const Model = require('objection').Model; const User = require('./user'); +const now = require('./now_helper'); Model.knex(db); class AuditLog extends Model { $beforeInsert () { - this.created_on = Model.raw('NOW()'); - this.modified_on = Model.raw('NOW()'); + this.created_on = now(); + this.modified_on = now(); // Default for meta if (typeof this.meta === 'undefined') { @@ -19,7 +20,7 @@ class AuditLog extends Model { } $beforeUpdate () { - this.modified_on = Model.raw('NOW()'); + this.modified_on = now(); } static get name () { diff --git a/backend/models/auth.js b/backend/models/auth.js index 9db62f5f..5ba5f380 100644 --- a/backend/models/auth.js +++ b/backend/models/auth.js @@ -5,6 +5,7 @@ const bcrypt = require('bcrypt'); const db = require('../db'); const Model = require('objection').Model; const User = require('./user'); +const now = require('./now_helper'); Model.knex(db); @@ -24,8 +25,8 @@ function encryptPassword () { class Auth extends Model { $beforeInsert (queryContext) { - this.created_on = Model.raw('NOW()'); - this.modified_on = Model.raw('NOW()'); + this.created_on = now(); + this.modified_on = now(); // Default for meta if (typeof this.meta === 'undefined') { @@ -36,7 +37,7 @@ class Auth extends Model { } $beforeUpdate (queryContext) { - this.modified_on = Model.raw('NOW()'); + this.modified_on = now(); return encryptPassword.apply(this, queryContext); } diff --git a/backend/models/certificate.js b/backend/models/certificate.js index 2dcb7505..6084a995 100644 --- a/backend/models/certificate.js +++ b/backend/models/certificate.js @@ -4,17 +4,18 @@ const db = require('../db'); const Model = require('objection').Model; const User = require('./user'); +const now = require('./now_helper'); Model.knex(db); class Certificate extends Model { $beforeInsert () { - this.created_on = Model.raw('NOW()'); - this.modified_on = Model.raw('NOW()'); + this.created_on = now(); + this.modified_on = now(); // Default for expires_on if (typeof this.expires_on === 'undefined') { - this.expires_on = Model.raw('NOW()'); + this.expires_on = now(); } // Default for domain_names @@ -31,7 +32,7 @@ class Certificate extends Model { } $beforeUpdate () { - this.modified_on = Model.raw('NOW()'); + this.modified_on = now(); // Sort domain_names if (typeof this.domain_names !== 'undefined') { diff --git a/backend/models/dead_host.js b/backend/models/dead_host.js index dc1109c8..6de42a33 100644 --- a/backend/models/dead_host.js +++ b/backend/models/dead_host.js @@ -5,13 +5,14 @@ const db = require('../db'); const Model = require('objection').Model; const User = require('./user'); const Certificate = require('./certificate'); +const now = require('./now_helper'); Model.knex(db); class DeadHost extends Model { $beforeInsert () { - this.created_on = Model.raw('NOW()'); - this.modified_on = Model.raw('NOW()'); + this.created_on = now(); + this.modified_on = now(); // Default for domain_names if (typeof this.domain_names === 'undefined') { @@ -27,7 +28,7 @@ class DeadHost extends Model { } $beforeUpdate () { - this.modified_on = Model.raw('NOW()'); + this.modified_on = now(); // Sort domain_names if (typeof this.domain_names !== 'undefined') { diff --git a/backend/models/now_helper.js b/backend/models/now_helper.js new file mode 100644 index 00000000..a1258a86 --- /dev/null +++ b/backend/models/now_helper.js @@ -0,0 +1,13 @@ +const db = require('../db'); +const config = require('config'); +const Model = require('objection').Model; + +Model.knex(db); + +module.exports = function () { + if (config.database.knex && config.database.knex.client === 'sqlite3') { + return Model.raw('date(\'now\')'); + } else { + return Model.raw('NOW()'); + } +}; diff --git a/backend/models/proxy_host.js b/backend/models/proxy_host.js index a2c9beee..a7583088 100644 --- a/backend/models/proxy_host.js +++ b/backend/models/proxy_host.js @@ -6,13 +6,14 @@ const Model = require('objection').Model; const User = require('./user'); const AccessList = require('./access_list'); const Certificate = require('./certificate'); +const now = require('./now_helper'); Model.knex(db); class ProxyHost extends Model { $beforeInsert () { - this.created_on = Model.raw('NOW()'); - this.modified_on = Model.raw('NOW()'); + this.created_on = now(); + this.modified_on = now(); // Default for domain_names if (typeof this.domain_names === 'undefined') { @@ -28,7 +29,7 @@ class ProxyHost extends Model { } $beforeUpdate () { - this.modified_on = Model.raw('NOW()'); + this.modified_on = now(); // Sort domain_names if (typeof this.domain_names !== 'undefined') { diff --git a/backend/models/redirection_host.js b/backend/models/redirection_host.js index 029b0c08..dd149b76 100644 --- a/backend/models/redirection_host.js +++ b/backend/models/redirection_host.js @@ -5,13 +5,14 @@ const db = require('../db'); const Model = require('objection').Model; const User = require('./user'); const Certificate = require('./certificate'); +const now = require('./now_helper'); Model.knex(db); class RedirectionHost extends Model { $beforeInsert () { - this.created_on = Model.raw('NOW()'); - this.modified_on = Model.raw('NOW()'); + this.created_on = now(); + this.modified_on = now(); // Default for domain_names if (typeof this.domain_names === 'undefined') { @@ -27,7 +28,7 @@ class RedirectionHost extends Model { } $beforeUpdate () { - this.modified_on = Model.raw('NOW()'); + this.modified_on = now(); // Sort domain_names if (typeof this.domain_names !== 'undefined') { diff --git a/backend/models/stream.js b/backend/models/stream.js index 7b6c8a4b..ed65de0f 100644 --- a/backend/models/stream.js +++ b/backend/models/stream.js @@ -4,13 +4,14 @@ const db = require('../db'); const Model = require('objection').Model; const User = require('./user'); +const now = require('./now_helper'); Model.knex(db); class Stream extends Model { $beforeInsert () { - this.created_on = Model.raw('NOW()'); - this.modified_on = Model.raw('NOW()'); + this.created_on = now(); + this.modified_on = now(); // Default for meta if (typeof this.meta === 'undefined') { @@ -19,7 +20,7 @@ class Stream extends Model { } $beforeUpdate () { - this.modified_on = Model.raw('NOW()'); + this.modified_on = now(); } static get name () { diff --git a/backend/models/user.js b/backend/models/user.js index 8d1ef123..c76f7dbf 100644 --- a/backend/models/user.js +++ b/backend/models/user.js @@ -4,13 +4,14 @@ const db = require('../db'); const Model = require('objection').Model; const UserPermission = require('./user_permission'); +const now = require('./now_helper'); Model.knex(db); class User extends Model { $beforeInsert () { - this.created_on = Model.raw('NOW()'); - this.modified_on = Model.raw('NOW()'); + this.created_on = now(); + this.modified_on = now(); // Default for roles if (typeof this.roles === 'undefined') { @@ -19,7 +20,7 @@ class User extends Model { } $beforeUpdate () { - this.modified_on = Model.raw('NOW()'); + this.modified_on = now(); } static get name () { diff --git a/backend/models/user_permission.js b/backend/models/user_permission.js index 836a23f5..bb87d5dc 100644 --- a/backend/models/user_permission.js +++ b/backend/models/user_permission.js @@ -3,17 +3,18 @@ const db = require('../db'); const Model = require('objection').Model; +const now = require('./now_helper'); Model.knex(db); class UserPermission extends Model { $beforeInsert () { - this.created_on = Model.raw('NOW()'); - this.modified_on = Model.raw('NOW()'); + this.created_on = now(); + this.modified_on = now(); } $beforeUpdate () { - this.modified_on = Model.raw('NOW()'); + this.modified_on = now(); } static get name () {