Skip to content

Commit 04412f3

Browse files
authored
Merge pull request #510 from tg44/multidb-re
Multidb - sqlite support
2 parents c41057b + 6690b77 commit 04412f3

19 files changed

+365
-156
lines changed

backend/config/sqlite-test-db.json

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"database": {
3+
"engine": "knex-native",
4+
"knex": {
5+
"client": "sqlite3",
6+
"connection": {
7+
"filename": "/app/backend/config/mydb.sqlite"
8+
},
9+
"pool": {
10+
"min": 0,
11+
"max": 1,
12+
"createTimeoutMillis": 3000,
13+
"acquireTimeoutMillis": 30000,
14+
"idleTimeoutMillis": 30000,
15+
"reapIntervalMillis": 1000,
16+
"createRetryIntervalMillis": 100,
17+
"propagateCreateError": false
18+
},
19+
"migrations": {
20+
"tableName": "migrations",
21+
"stub": "src/backend/lib/migrate_template.js",
22+
"directory": "src/backend/migrations"
23+
}
24+
}
25+
}
26+
}

backend/db.js

+21-13
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,27 @@ if (!config.has('database')) {
44
throw new Error('Database config does not exist! Please read the instructions: https://github.com/jc21/nginx-proxy-manager/blob/master/doc/INSTALL.md');
55
}
66

7-
let data = {
8-
client: config.database.engine,
9-
connection: {
10-
host: config.database.host,
11-
user: config.database.user,
12-
password: config.database.password,
13-
database: config.database.name,
14-
port: config.database.port
15-
},
16-
migrations: {
17-
tableName: 'migrations'
18-
}
19-
};
7+
function generateDbConfig() {
8+
if (config.database.engine === 'knex-native') {
9+
return config.database.knex;
10+
} else
11+
return {
12+
client: config.database.engine,
13+
connection: {
14+
host: config.database.host,
15+
user: config.database.user,
16+
password: config.database.password,
17+
database: config.database.name,
18+
port: config.database.port
19+
},
20+
migrations: {
21+
tableName: 'migrations'
22+
}
23+
};
24+
}
25+
26+
27+
let data = generateDbConfig();
2028

2129
if (typeof config.database.version !== 'undefined') {
2230
data.version = config.database.version;

backend/migrations/20190227065017_settings.js

-16
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,6 @@ exports.up = function (knex/*, Promise*/) {
2222
})
2323
.then(() => {
2424
logger.info('[' + migrate_name + '] setting Table created');
25-
26-
// TODO: add settings
27-
let settingModel = require('../models/setting');
28-
29-
return settingModel
30-
.query()
31-
.insert({
32-
id: 'default-site',
33-
name: 'Default Site',
34-
description: 'What to show when Nginx is hit with an unknown Host',
35-
value: 'congratulations',
36-
meta: {}
37-
});
38-
})
39-
.then(() => {
40-
logger.info('[' + migrate_name + '] Default settings added');
4125
});
4226
};
4327

backend/models/access_list.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ const Model = require('objection').Model;
66
const User = require('./user');
77
const AccessListAuth = require('./access_list_auth');
88
const AccessListClient = require('./access_list_client');
9+
const now = require('./now_helper');
910

1011
Model.knex(db);
1112

1213
class AccessList extends Model {
1314
$beforeInsert () {
14-
this.created_on = Model.raw('NOW()');
15-
this.modified_on = Model.raw('NOW()');
15+
this.created_on = now();
16+
this.modified_on = now();
1617

1718
// Default for meta
1819
if (typeof this.meta === 'undefined') {
@@ -21,7 +22,7 @@ class AccessList extends Model {
2122
}
2223

2324
$beforeUpdate () {
24-
this.modified_on = Model.raw('NOW()');
25+
this.modified_on = now();
2526
}
2627

2728
static get name () {

backend/models/access_list_auth.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33

44
const db = require('../db');
55
const Model = require('objection').Model;
6+
const now = require('./now_helper');
67

78
Model.knex(db);
89

910
class AccessListAuth extends Model {
1011
$beforeInsert () {
11-
this.created_on = Model.raw('NOW()');
12-
this.modified_on = Model.raw('NOW()');
12+
this.created_on = now();
13+
this.modified_on = now();
1314

1415
// Default for meta
1516
if (typeof this.meta === 'undefined') {
@@ -18,7 +19,7 @@ class AccessListAuth extends Model {
1819
}
1920

2021
$beforeUpdate () {
21-
this.modified_on = Model.raw('NOW()');
22+
this.modified_on = now();
2223
}
2324

2425
static get name () {

backend/models/access_list_client.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33

44
const db = require('../db');
55
const Model = require('objection').Model;
6+
const now = require('./now_helper');
67

78
Model.knex(db);
89

910
class AccessListClient extends Model {
1011
$beforeInsert () {
11-
this.created_on = Model.raw('NOW()');
12-
this.modified_on = Model.raw('NOW()');
12+
this.created_on = now();
13+
this.modified_on = now();
1314

1415
// Default for meta
1516
if (typeof this.meta === 'undefined') {
@@ -18,7 +19,7 @@ class AccessListClient extends Model {
1819
}
1920

2021
$beforeUpdate () {
21-
this.modified_on = Model.raw('NOW()');
22+
this.modified_on = now();
2223
}
2324

2425
static get name () {

backend/models/audit-log.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
const db = require('../db');
55
const Model = require('objection').Model;
66
const User = require('./user');
7+
const now = require('./now_helper');
78

89
Model.knex(db);
910

1011
class AuditLog extends Model {
1112
$beforeInsert () {
12-
this.created_on = Model.raw('NOW()');
13-
this.modified_on = Model.raw('NOW()');
13+
this.created_on = now();
14+
this.modified_on = now();
1415

1516
// Default for meta
1617
if (typeof this.meta === 'undefined') {
@@ -19,7 +20,7 @@ class AuditLog extends Model {
1920
}
2021

2122
$beforeUpdate () {
22-
this.modified_on = Model.raw('NOW()');
23+
this.modified_on = now();
2324
}
2425

2526
static get name () {

backend/models/auth.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const bcrypt = require('bcrypt');
55
const db = require('../db');
66
const Model = require('objection').Model;
77
const User = require('./user');
8+
const now = require('./now_helper');
89

910
Model.knex(db);
1011

@@ -24,8 +25,8 @@ function encryptPassword () {
2425

2526
class Auth extends Model {
2627
$beforeInsert (queryContext) {
27-
this.created_on = Model.raw('NOW()');
28-
this.modified_on = Model.raw('NOW()');
28+
this.created_on = now();
29+
this.modified_on = now();
2930

3031
// Default for meta
3132
if (typeof this.meta === 'undefined') {
@@ -36,7 +37,7 @@ class Auth extends Model {
3637
}
3738

3839
$beforeUpdate (queryContext) {
39-
this.modified_on = Model.raw('NOW()');
40+
this.modified_on = now();
4041
return encryptPassword.apply(this, queryContext);
4142
}
4243

backend/models/certificate.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@
44
const db = require('../db');
55
const Model = require('objection').Model;
66
const User = require('./user');
7+
const now = require('./now_helper');
78

89
Model.knex(db);
910

1011
class Certificate extends Model {
1112
$beforeInsert () {
12-
this.created_on = Model.raw('NOW()');
13-
this.modified_on = Model.raw('NOW()');
13+
this.created_on = now();
14+
this.modified_on = now();
1415

1516
// Default for expires_on
1617
if (typeof this.expires_on === 'undefined') {
17-
this.expires_on = Model.raw('NOW()');
18+
this.expires_on = now();
1819
}
1920

2021
// Default for domain_names
@@ -31,7 +32,7 @@ class Certificate extends Model {
3132
}
3233

3334
$beforeUpdate () {
34-
this.modified_on = Model.raw('NOW()');
35+
this.modified_on = now();
3536

3637
// Sort domain_names
3738
if (typeof this.domain_names !== 'undefined') {

backend/models/dead_host.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ const db = require('../db');
55
const Model = require('objection').Model;
66
const User = require('./user');
77
const Certificate = require('./certificate');
8+
const now = require('./now_helper');
89

910
Model.knex(db);
1011

1112
class DeadHost extends Model {
1213
$beforeInsert () {
13-
this.created_on = Model.raw('NOW()');
14-
this.modified_on = Model.raw('NOW()');
14+
this.created_on = now();
15+
this.modified_on = now();
1516

1617
// Default for domain_names
1718
if (typeof this.domain_names === 'undefined') {
@@ -27,7 +28,7 @@ class DeadHost extends Model {
2728
}
2829

2930
$beforeUpdate () {
30-
this.modified_on = Model.raw('NOW()');
31+
this.modified_on = now();
3132

3233
// Sort domain_names
3334
if (typeof this.domain_names !== 'undefined') {

backend/models/now_helper.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const db = require('../db');
2+
const config = require('config');
3+
const Model = require('objection').Model;
4+
5+
Model.knex(db);
6+
7+
module.exports = function () {
8+
if (config.database.knex && config.database.knex.client === 'sqlite3') {
9+
return Model.raw('date(\'now\')');
10+
} else {
11+
return Model.raw('NOW()');
12+
}
13+
};

backend/models/proxy_host.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ const Model = require('objection').Model;
66
const User = require('./user');
77
const AccessList = require('./access_list');
88
const Certificate = require('./certificate');
9+
const now = require('./now_helper');
910

1011
Model.knex(db);
1112

1213
class ProxyHost extends Model {
1314
$beforeInsert () {
14-
this.created_on = Model.raw('NOW()');
15-
this.modified_on = Model.raw('NOW()');
15+
this.created_on = now();
16+
this.modified_on = now();
1617

1718
// Default for domain_names
1819
if (typeof this.domain_names === 'undefined') {
@@ -28,7 +29,7 @@ class ProxyHost extends Model {
2829
}
2930

3031
$beforeUpdate () {
31-
this.modified_on = Model.raw('NOW()');
32+
this.modified_on = now();
3233

3334
// Sort domain_names
3435
if (typeof this.domain_names !== 'undefined') {

backend/models/redirection_host.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ const db = require('../db');
55
const Model = require('objection').Model;
66
const User = require('./user');
77
const Certificate = require('./certificate');
8+
const now = require('./now_helper');
89

910
Model.knex(db);
1011

1112
class RedirectionHost extends Model {
1213
$beforeInsert () {
13-
this.created_on = Model.raw('NOW()');
14-
this.modified_on = Model.raw('NOW()');
14+
this.created_on = now();
15+
this.modified_on = now();
1516

1617
// Default for domain_names
1718
if (typeof this.domain_names === 'undefined') {
@@ -27,7 +28,7 @@ class RedirectionHost extends Model {
2728
}
2829

2930
$beforeUpdate () {
30-
this.modified_on = Model.raw('NOW()');
31+
this.modified_on = now();
3132

3233
// Sort domain_names
3334
if (typeof this.domain_names !== 'undefined') {

backend/models/stream.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
const db = require('../db');
55
const Model = require('objection').Model;
66
const User = require('./user');
7+
const now = require('./now_helper');
78

89
Model.knex(db);
910

1011
class Stream extends Model {
1112
$beforeInsert () {
12-
this.created_on = Model.raw('NOW()');
13-
this.modified_on = Model.raw('NOW()');
13+
this.created_on = now();
14+
this.modified_on = now();
1415

1516
// Default for meta
1617
if (typeof this.meta === 'undefined') {
@@ -19,7 +20,7 @@ class Stream extends Model {
1920
}
2021

2122
$beforeUpdate () {
22-
this.modified_on = Model.raw('NOW()');
23+
this.modified_on = now();
2324
}
2425

2526
static get name () {

backend/models/user.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
const db = require('../db');
55
const Model = require('objection').Model;
66
const UserPermission = require('./user_permission');
7+
const now = require('./now_helper');
78

89
Model.knex(db);
910

1011
class User extends Model {
1112
$beforeInsert () {
12-
this.created_on = Model.raw('NOW()');
13-
this.modified_on = Model.raw('NOW()');
13+
this.created_on = now();
14+
this.modified_on = now();
1415

1516
// Default for roles
1617
if (typeof this.roles === 'undefined') {
@@ -19,7 +20,7 @@ class User extends Model {
1920
}
2021

2122
$beforeUpdate () {
22-
this.modified_on = Model.raw('NOW()');
23+
this.modified_on = now();
2324
}
2425

2526
static get name () {

0 commit comments

Comments
 (0)