ueberdb2 4.1.23 → 4.1.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cassandra_db.d.ts +1 -0
- package/dist/couch_db.d.ts +1 -0
- package/dist/databases/cassandra_db.cjs +1 -0
- package/dist/databases/couch_db.cjs +1 -0
- package/dist/databases/dirty_db.cjs +1 -0
- package/dist/databases/dirty_git_db.cjs +1 -0
- package/dist/databases/elasticsearch_db.cjs +1 -0
- package/dist/databases/memory_db.cjs +1 -0
- package/dist/databases/mock_db.cjs +1 -0
- package/dist/databases/mongodb_db.cjs +1 -0
- package/dist/databases/mssql_db.cjs +5 -0
- package/dist/databases/mysql_db.cjs +1 -0
- package/dist/databases/postgres_db.cjs +1 -0
- package/dist/databases/postgrespool_db.cjs +1 -0
- package/dist/databases/redis_db.cjs +1 -0
- package/dist/databases/rethink_db.cjs +1 -0
- package/dist/databases/sqlite_db.cjs +4 -0
- package/dist/dirty_db.d.ts +1 -0
- package/dist/dirty_git_db.d.ts +1 -0
- package/dist/elasticsearch_db.d.ts +1 -0
- package/dist/index.d.ts +85 -0
- package/dist/lib/AbstractDatabase.cjs +1 -0
- package/dist/lib/CacheAndBufferLayer.cjs +1 -0
- package/dist/lib/logging.cjs +1 -0
- package/dist/memory_db.d.ts +1 -0
- package/dist/mock_db.d.ts +1 -0
- package/dist/mongodb_db.d.ts +1 -0
- package/dist/mssql_db.d.ts +1 -0
- package/dist/mysql_db.d.ts +1 -0
- package/dist/postgres_db.d.ts +1 -0
- package/dist/postgrespool_db.d.ts +1 -0
- package/dist/redis_db.d.ts +1 -0
- package/dist/rethink_db.d.ts +1 -0
- package/dist/sqlite_db.d.ts +1 -0
- package/package.json +7 -6
- package/dist/databases/cassandra_db.js +0 -235
- package/dist/databases/couch_db.js +0 -173
- package/dist/databases/dirty_db.js +0 -75
- package/dist/databases/dirty_git_db.js +0 -59
- package/dist/databases/elasticsearch_db.js +0 -243
- package/dist/databases/memory_db.js +0 -37
- package/dist/databases/mock_db.js +0 -41
- package/dist/databases/mongodb_db.js +0 -134
- package/dist/databases/mssql_db.js +0 -185
- package/dist/databases/mysql_db.js +0 -167
- package/dist/databases/postgres_db.js +0 -190
- package/dist/databases/postgrespool_db.js +0 -12
- package/dist/databases/redis_db.js +0 -120
- package/dist/databases/rethink_db.js +0 -121
- package/dist/databases/sqlite_db.js +0 -138
- package/dist/index.js +0 -193
- package/dist/lib/AbstractDatabase.js +0 -40
- package/dist/lib/CacheAndBufferLayer.js +0 -658
- package/dist/lib/logging.js +0 -27
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var AbstractDatabase = require('../lib/AbstractDatabase.js');
|
|
4
|
-
var redis = require('redis');
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* 2011 Peter 'Pita' Martischka
|
|
8
|
-
*
|
|
9
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
10
|
-
* you may not use this file except in compliance with the License.
|
|
11
|
-
* You may obtain a copy of the License at
|
|
12
|
-
*
|
|
13
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
14
|
-
*
|
|
15
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
16
|
-
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
17
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
18
|
-
* See the License for the specific language governing permissions and
|
|
19
|
-
* limitations under the License.
|
|
20
|
-
*/
|
|
21
|
-
const Database = class RedisDB extends AbstractDatabase {
|
|
22
|
-
_client;
|
|
23
|
-
constructor(settings) {
|
|
24
|
-
super();
|
|
25
|
-
this._client = null;
|
|
26
|
-
this.settings = settings || {};
|
|
27
|
-
}
|
|
28
|
-
get isAsync() { return true; }
|
|
29
|
-
async init() {
|
|
30
|
-
if (this.settings.url) {
|
|
31
|
-
this._client = redis.createClient({ url: this.settings.url });
|
|
32
|
-
}
|
|
33
|
-
else if (this.settings.host) {
|
|
34
|
-
const options = {
|
|
35
|
-
socket: {
|
|
36
|
-
host: this.settings.host,
|
|
37
|
-
port: Number(this.settings.port),
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
if (this.settings.password) {
|
|
41
|
-
options.password = this.settings.password;
|
|
42
|
-
}
|
|
43
|
-
if (this.settings.user) {
|
|
44
|
-
options.username = this.settings.user;
|
|
45
|
-
}
|
|
46
|
-
this._client = redis.createClient(options);
|
|
47
|
-
}
|
|
48
|
-
if (this._client) {
|
|
49
|
-
await this._client.connect();
|
|
50
|
-
await this._client.ping();
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
async get(key) {
|
|
54
|
-
if (this._client == null)
|
|
55
|
-
return null;
|
|
56
|
-
return await this._client.get(key);
|
|
57
|
-
}
|
|
58
|
-
async findKeys(key, notKey) {
|
|
59
|
-
if (this._client == null)
|
|
60
|
-
return null;
|
|
61
|
-
const [type] = /^([^:*]+):\*$/.exec(key) || [];
|
|
62
|
-
if (type != null && ['*:*:*', `${key}:*`].includes(notKey)) {
|
|
63
|
-
// Performance optimization for a common Etherpad case.
|
|
64
|
-
return await this._client.sMembers(`ueberDB:keys:${type}`);
|
|
65
|
-
}
|
|
66
|
-
let keys = await this._client.keys(key.replace(/[?[\]\\]/g, '\\$&'));
|
|
67
|
-
if (notKey != null) {
|
|
68
|
-
const regex = this.createFindRegex(key, notKey);
|
|
69
|
-
keys = keys.filter((k) => regex.test(k));
|
|
70
|
-
}
|
|
71
|
-
return keys;
|
|
72
|
-
}
|
|
73
|
-
async set(key, value) {
|
|
74
|
-
if (this._client == null)
|
|
75
|
-
return null;
|
|
76
|
-
const matches = /^([^:]+):([^:]+)$/.exec(key);
|
|
77
|
-
await Promise.all([
|
|
78
|
-
matches && this._client.sAdd(`ueberDB:keys:${matches[1]}`, matches[0]),
|
|
79
|
-
this._client.set(key, value),
|
|
80
|
-
]);
|
|
81
|
-
}
|
|
82
|
-
async remove(key) {
|
|
83
|
-
if (this._client == null)
|
|
84
|
-
return null;
|
|
85
|
-
const matches = /^([^:]+):([^:]+)$/.exec(key);
|
|
86
|
-
await Promise.all([
|
|
87
|
-
matches && this._client.sRem(`ueberDB:keys:${matches[1]}`, matches[0]),
|
|
88
|
-
this._client.del(key),
|
|
89
|
-
]);
|
|
90
|
-
}
|
|
91
|
-
async doBulk(bulk) {
|
|
92
|
-
if (this._client == null)
|
|
93
|
-
return null;
|
|
94
|
-
const multi = this._client.multi();
|
|
95
|
-
for (const { key, type, value } of bulk) {
|
|
96
|
-
const matches = /^([^:]+):([^:]+)$/.exec(key);
|
|
97
|
-
if (type === 'set') {
|
|
98
|
-
if (matches) {
|
|
99
|
-
multi.sAdd(`ueberDB:keys:${matches[1]}`, matches[0]);
|
|
100
|
-
}
|
|
101
|
-
multi.set(key, value);
|
|
102
|
-
}
|
|
103
|
-
else if (type === 'remove') {
|
|
104
|
-
if (matches) {
|
|
105
|
-
multi.sRem(`ueberDB:keys:${matches[1]}`, matches[0]);
|
|
106
|
-
}
|
|
107
|
-
multi.del(key);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
await multi.exec();
|
|
111
|
-
}
|
|
112
|
-
async close() {
|
|
113
|
-
if (this._client == null)
|
|
114
|
-
return null;
|
|
115
|
-
await this._client.quit();
|
|
116
|
-
this._client = null;
|
|
117
|
-
}
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
exports.Database = Database;
|
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var AbstractDatabase = require('../lib/AbstractDatabase.js');
|
|
4
|
-
var r = require('rethinkdb');
|
|
5
|
-
var async = require('async');
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* 2016 Remi Arnaud
|
|
9
|
-
*
|
|
10
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
11
|
-
* you may not use this file except in compliance with the License.
|
|
12
|
-
* You may obtain a copy of the License at
|
|
13
|
-
*
|
|
14
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
15
|
-
*
|
|
16
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
17
|
-
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
18
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
19
|
-
* See the License for the specific language governing permissions and
|
|
20
|
-
* limitations under the License.
|
|
21
|
-
*/
|
|
22
|
-
const Database = class extends AbstractDatabase {
|
|
23
|
-
host;
|
|
24
|
-
db;
|
|
25
|
-
port;
|
|
26
|
-
table;
|
|
27
|
-
connection;
|
|
28
|
-
constructor(settings) {
|
|
29
|
-
super();
|
|
30
|
-
if (!settings)
|
|
31
|
-
settings = {};
|
|
32
|
-
if (!settings.host) {
|
|
33
|
-
settings.host = 'localhost';
|
|
34
|
-
}
|
|
35
|
-
if (!settings.port) {
|
|
36
|
-
settings.port = 28015;
|
|
37
|
-
}
|
|
38
|
-
if (!settings.db) {
|
|
39
|
-
settings.db = 'test';
|
|
40
|
-
}
|
|
41
|
-
if (!settings.table) {
|
|
42
|
-
settings.table = 'test';
|
|
43
|
-
}
|
|
44
|
-
this.host = settings.host;
|
|
45
|
-
this.db = settings.db;
|
|
46
|
-
this.port = settings.port;
|
|
47
|
-
this.table = settings.table;
|
|
48
|
-
this.connection = null;
|
|
49
|
-
}
|
|
50
|
-
init(callback) {
|
|
51
|
-
// @ts-ignore
|
|
52
|
-
r.connect(this, (err, conn) => {
|
|
53
|
-
if (err)
|
|
54
|
-
throw err;
|
|
55
|
-
this.connection = conn;
|
|
56
|
-
r.table(this.table).run(this.connection, (err, cursor) => {
|
|
57
|
-
if (err) {
|
|
58
|
-
// assuming table does not exists
|
|
59
|
-
// @ts-ignore
|
|
60
|
-
r.tableCreate(this.table).run(this.connection, callback);
|
|
61
|
-
}
|
|
62
|
-
else if (callback) {
|
|
63
|
-
callback(null, cursor);
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
get(key, callback) {
|
|
69
|
-
// @ts-ignore
|
|
70
|
-
r.table(this.table).get(key).run(this.connection, (err, item) => {
|
|
71
|
-
// @ts-ignore
|
|
72
|
-
callback(err, (item ? item.content : item));
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
findKeys(key, notKey, callback) {
|
|
76
|
-
const keys = [];
|
|
77
|
-
const regex = this.createFindRegex(key, notKey);
|
|
78
|
-
// @ts-ignore
|
|
79
|
-
r.filter((item) => {
|
|
80
|
-
if (item.id.search(regex) !== -1) {
|
|
81
|
-
keys.push(item.id);
|
|
82
|
-
}
|
|
83
|
-
}).run(this.connection, callback);
|
|
84
|
-
}
|
|
85
|
-
set(key, value, callback) {
|
|
86
|
-
r.table(this.table)
|
|
87
|
-
.insert({ id: key, content: value }, { conflict: 'replace' })
|
|
88
|
-
.run(this.connection, callback);
|
|
89
|
-
}
|
|
90
|
-
doBulk(bulk, callback) {
|
|
91
|
-
const _in = [];
|
|
92
|
-
const _out = [];
|
|
93
|
-
for (const i in bulk) {
|
|
94
|
-
if (bulk[i].type === 'set') {
|
|
95
|
-
_in.push({ id: bulk[i].key, content: bulk[i].value });
|
|
96
|
-
}
|
|
97
|
-
else if (bulk[i].type === 'remove') {
|
|
98
|
-
_out.push(bulk[i].key);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
async.parallel([
|
|
102
|
-
(cb) => {
|
|
103
|
-
r.table(this.table).insert(_in, { conflict: 'replace' }).run(this.connection, cb);
|
|
104
|
-
},
|
|
105
|
-
(cb) => {
|
|
106
|
-
r.table(this.table).getAll(_out).delete().run(this.connection, cb);
|
|
107
|
-
},
|
|
108
|
-
], callback);
|
|
109
|
-
}
|
|
110
|
-
remove(key, callback) {
|
|
111
|
-
// @ts-ignore
|
|
112
|
-
r.table(this.table).get(key).delete().run(this.connection, callback);
|
|
113
|
-
}
|
|
114
|
-
close(callback) {
|
|
115
|
-
if (this.connection) {
|
|
116
|
-
this.connection.close(callback);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
exports.Database = Database;
|
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var AbstractDatabase = require('../lib/AbstractDatabase.js');
|
|
4
|
-
var util = require('util');
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* 2011 Peter 'Pita' Martischka
|
|
8
|
-
*
|
|
9
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
10
|
-
* you may not use this file except in compliance with the License.
|
|
11
|
-
* You may obtain a copy of the License at
|
|
12
|
-
*
|
|
13
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
14
|
-
*
|
|
15
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
16
|
-
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
17
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
18
|
-
* See the License for the specific language governing permissions and
|
|
19
|
-
* limitations under the License.
|
|
20
|
-
*/
|
|
21
|
-
let SQDB;
|
|
22
|
-
try {
|
|
23
|
-
SQDB = require('sqlite3').Database;
|
|
24
|
-
}
|
|
25
|
-
catch (err) {
|
|
26
|
-
throw new Error('sqlite3 not found. It was removed from ueberdb\'s dependencies because it requires ' +
|
|
27
|
-
'compilation which fails on several systems. If you still want to use sqlite, run ' +
|
|
28
|
-
'"npm install sqlite3" in your etherpad-lite ./src directory.');
|
|
29
|
-
}
|
|
30
|
-
const escape = (val) => `'${val.replace(/'/g, "''")}'`;
|
|
31
|
-
const Database = class SQLiteDB extends AbstractDatabase {
|
|
32
|
-
db;
|
|
33
|
-
constructor(settings) {
|
|
34
|
-
super();
|
|
35
|
-
this.db = null;
|
|
36
|
-
if (!settings || !settings.filename) {
|
|
37
|
-
settings = { filename: ':memory:' };
|
|
38
|
-
}
|
|
39
|
-
this.settings = settings;
|
|
40
|
-
// set settings for the dbWrapper
|
|
41
|
-
if (settings.filename === ':memory:') {
|
|
42
|
-
this.settings.cache = 0;
|
|
43
|
-
this.settings.writeInterval = 0;
|
|
44
|
-
this.settings.json = true;
|
|
45
|
-
}
|
|
46
|
-
else {
|
|
47
|
-
this.settings.cache = 1000;
|
|
48
|
-
this.settings.writeInterval = 100;
|
|
49
|
-
this.settings.json = true;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
init(callback) {
|
|
53
|
-
util.callbackify(async () => {
|
|
54
|
-
this.db = new SQDB(this.settings.filename);
|
|
55
|
-
await this._query('CREATE TABLE IF NOT EXISTS store (key TEXT PRIMARY KEY, value TEXT)');
|
|
56
|
-
// @ts-ignore
|
|
57
|
-
})(callback);
|
|
58
|
-
}
|
|
59
|
-
async _query(sql, params = []) {
|
|
60
|
-
// It is unclear how util.promisify() deals with variadic functions, so it is not used here.
|
|
61
|
-
return await new Promise((resolve, reject) => {
|
|
62
|
-
// According to sqlite3's documentation, .run() method (and maybe .all() and .get(); the
|
|
63
|
-
// documentation is unclear) might call the callback multiple times. That's OK -- ECMAScript
|
|
64
|
-
// guarantees that it is safe to call a Promise executor's resolve and reject functions
|
|
65
|
-
// multiple times. The subsequent calls are ignored, except Node.js's 'process' object emits a
|
|
66
|
-
// 'multipleResolves' event to aid in debugging.
|
|
67
|
-
this.db && this.db.all(sql, params, (err, rows) => {
|
|
68
|
-
if (err != null)
|
|
69
|
-
return reject(err);
|
|
70
|
-
resolve(rows);
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
// Temporary callbackified version of _query. This will be removed once all database objects are
|
|
75
|
-
// asyncified.
|
|
76
|
-
_queryCb(sql, params, callback) {
|
|
77
|
-
// It is unclear how util.callbackify() handles optional parameters, so it is not used here.
|
|
78
|
-
const p = this._query(sql, params);
|
|
79
|
-
if (callback)
|
|
80
|
-
p.then((rows) => callback(null, rows), (err) => callback(err || new Error(err)));
|
|
81
|
-
}
|
|
82
|
-
get(key, callback) {
|
|
83
|
-
this._queryCb('SELECT value FROM store WHERE key = ?', [key], (err, rows) => callback(err, err == null && rows && rows.length ? rows[0].value : null));
|
|
84
|
-
}
|
|
85
|
-
findKeys(key, notKey, callback) {
|
|
86
|
-
let query = 'SELECT key FROM store WHERE key LIKE ?';
|
|
87
|
-
const params = [];
|
|
88
|
-
// desired keys are %key:%, e.g. pad:%
|
|
89
|
-
key = key.replace(/\*/g, '%');
|
|
90
|
-
params.push(key);
|
|
91
|
-
if (notKey != null) {
|
|
92
|
-
// not desired keys are notKey:%, e.g. %:%:%
|
|
93
|
-
notKey = notKey.replace(/\*/g, '%');
|
|
94
|
-
query += ' AND key NOT LIKE ?';
|
|
95
|
-
params.push(notKey);
|
|
96
|
-
}
|
|
97
|
-
this._queryCb(query, params, (err, results) => {
|
|
98
|
-
const value = [];
|
|
99
|
-
if (!err && Object.keys(results).length > 0) {
|
|
100
|
-
results.forEach((val) => {
|
|
101
|
-
value.push(val.key);
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
callback(err, value);
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
set(key, value, callback) {
|
|
108
|
-
this._queryCb('REPLACE INTO store VALUES (?,?)', [key, value], callback);
|
|
109
|
-
}
|
|
110
|
-
remove(key, callback) {
|
|
111
|
-
this._queryCb('DELETE FROM store WHERE key = ?', [key], callback);
|
|
112
|
-
}
|
|
113
|
-
doBulk(bulk, callback) {
|
|
114
|
-
let sql = 'BEGIN TRANSACTION;\n';
|
|
115
|
-
for (const i in bulk) {
|
|
116
|
-
if (bulk[i].type === 'set') {
|
|
117
|
-
sql += `REPLACE INTO store VALUES (${escape(bulk[i].key)}, ${escape(bulk[i].value)});\n`;
|
|
118
|
-
}
|
|
119
|
-
else if (bulk[i].type === 'remove') {
|
|
120
|
-
sql += `DELETE FROM store WHERE key = ${escape(bulk[i].key)};\n`;
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
sql += 'END TRANSACTION;';
|
|
124
|
-
this.db && this.db.exec(sql, (err) => {
|
|
125
|
-
if (err) {
|
|
126
|
-
console.error('ERROR WITH SQL: ');
|
|
127
|
-
console.error(sql);
|
|
128
|
-
}
|
|
129
|
-
callback(err);
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
close(callback) {
|
|
133
|
-
callback();
|
|
134
|
-
this.db && this.db.close((err) => callback(err));
|
|
135
|
-
}
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
exports.Database = Database;
|
package/dist/index.js
DELETED
|
@@ -1,193 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var CacheAndBufferLayer = require('./lib/CacheAndBufferLayer.js');
|
|
4
|
-
var logging = require('./lib/logging.js');
|
|
5
|
-
var util = require('util');
|
|
6
|
-
|
|
7
|
-
const cbDb = {
|
|
8
|
-
init: () => { },
|
|
9
|
-
flush: () => { },
|
|
10
|
-
get: () => { },
|
|
11
|
-
remove: () => { },
|
|
12
|
-
findKeys: () => { },
|
|
13
|
-
close: () => { },
|
|
14
|
-
getSub: () => { },
|
|
15
|
-
setSub: () => { },
|
|
16
|
-
};
|
|
17
|
-
const fns = ['close', 'findKeys', 'flush', 'get', 'getSub', 'init', 'remove', 'set', 'setSub'];
|
|
18
|
-
for (const fn of fns) {
|
|
19
|
-
// @ts-ignore
|
|
20
|
-
cbDb[fn] = util.callbackify(CacheAndBufferLayer.Database.prototype[fn]);
|
|
21
|
-
}
|
|
22
|
-
const makeDoneCallback = (callback, deprecated) => (err) => {
|
|
23
|
-
if (callback)
|
|
24
|
-
callback(err);
|
|
25
|
-
if (deprecated)
|
|
26
|
-
deprecated(err);
|
|
27
|
-
if (err != null && callback == null && deprecated == null)
|
|
28
|
-
throw err;
|
|
29
|
-
};
|
|
30
|
-
const Database = class {
|
|
31
|
-
type;
|
|
32
|
-
dbModule;
|
|
33
|
-
dbSettings;
|
|
34
|
-
wrapperSettings;
|
|
35
|
-
logger;
|
|
36
|
-
db;
|
|
37
|
-
metrics;
|
|
38
|
-
/**
|
|
39
|
-
* @param type The type of the database
|
|
40
|
-
* @param dbSettings The settings for that specific database type
|
|
41
|
-
* @param wrapperSettings
|
|
42
|
-
* @param logger Optional logger object. If no logger object is provided no logging will occur.
|
|
43
|
-
* The logger object is expected to be a log4js logger object or `console`. A logger object
|
|
44
|
-
* from another logging library should also work, but performance may be reduced if the logger
|
|
45
|
-
* object does not have is${Level}Enabled() methods (isDebugEnabled(), etc.).
|
|
46
|
-
*/
|
|
47
|
-
constructor(type, dbSettings, wrapperSettings, logger = null) {
|
|
48
|
-
if (!type) {
|
|
49
|
-
type = 'sqlite';
|
|
50
|
-
dbSettings = null;
|
|
51
|
-
wrapperSettings = null;
|
|
52
|
-
}
|
|
53
|
-
// saves all settings and require the db module
|
|
54
|
-
this.type = type;
|
|
55
|
-
this.dbSettings = dbSettings;
|
|
56
|
-
this.wrapperSettings = wrapperSettings;
|
|
57
|
-
this.logger = logging.normalizeLogger(logger);
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
|
|
61
|
-
*/
|
|
62
|
-
async init(callback = null) {
|
|
63
|
-
this.dbModule = await import(`./databases/${this.type}_db`);
|
|
64
|
-
const db = new this.dbModule.Database(this.dbSettings);
|
|
65
|
-
db.logger = this.logger;
|
|
66
|
-
this.db = new CacheAndBufferLayer.Database(db, this.wrapperSettings, this.logger);
|
|
67
|
-
this.metrics = this.db.metrics;
|
|
68
|
-
if (callback != null) {
|
|
69
|
-
return cbDb.init.call(this.db);
|
|
70
|
-
}
|
|
71
|
-
return this.db.init();
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Wrapper functions
|
|
75
|
-
*/
|
|
76
|
-
/**
|
|
77
|
-
* Deprecated synonym of flush().
|
|
78
|
-
*
|
|
79
|
-
* @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
|
|
80
|
-
*/
|
|
81
|
-
doShutdown(callback = null) {
|
|
82
|
-
return this.flush(callback);
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Writes any unsaved changes to the underlying database.
|
|
86
|
-
*
|
|
87
|
-
* @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
|
|
88
|
-
*/
|
|
89
|
-
flush(callback = null) {
|
|
90
|
-
if (!cbDb || !cbDb.flush === undefined)
|
|
91
|
-
return null;
|
|
92
|
-
if (callback != null) { // @ts-ignore
|
|
93
|
-
return cbDb.flush.call(this.db, callback);
|
|
94
|
-
}
|
|
95
|
-
return this.db.flush();
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* @param key
|
|
99
|
-
* @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
|
|
100
|
-
*/
|
|
101
|
-
get(key, callback = null) {
|
|
102
|
-
if (callback != null) { // @ts-ignore
|
|
103
|
-
return cbDb.get.call(this.db, key, callback);
|
|
104
|
-
}
|
|
105
|
-
return this.db.get(key);
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* @param key
|
|
109
|
-
* @param notKey
|
|
110
|
-
* @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
|
|
111
|
-
*/
|
|
112
|
-
findKeys(key, notKey, callback = null) {
|
|
113
|
-
if (callback != null) { // @ts-ignore
|
|
114
|
-
return cbDb.findKeys.call(this.db, key, notKey, callback);
|
|
115
|
-
}
|
|
116
|
-
return this.db.findKeys(key, notKey);
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Removes an entry from the database if present.
|
|
120
|
-
*
|
|
121
|
-
* @param key
|
|
122
|
-
* @param cb Deprecated. Node-style callback. Called when the write has been committed to the
|
|
123
|
-
* underlying database driver. If null, a Promise is returned.
|
|
124
|
-
* @param deprecated Deprecated callback that is called just after cb. Ignored if cb is null.
|
|
125
|
-
*/
|
|
126
|
-
remove(key, cb = null, deprecated = null) {
|
|
127
|
-
if (cb != null) { // @ts-ignore
|
|
128
|
-
return cbDb.remove.call(this.db, key, makeDoneCallback(cb, deprecated));
|
|
129
|
-
}
|
|
130
|
-
return this.db.remove(key);
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Adds or changes the value of an entry.
|
|
134
|
-
*
|
|
135
|
-
* @param key
|
|
136
|
-
* @param value
|
|
137
|
-
* @param cb Deprecated. Node-style callback. Called when the write has been committed to the
|
|
138
|
-
* underlying database driver. If null, a Promise is returned.
|
|
139
|
-
* @param deprecated Deprecated callback that is called just after cb. Ignored if cb is null.
|
|
140
|
-
*/
|
|
141
|
-
set(key, value, cb = null, deprecated = null) {
|
|
142
|
-
if (cb != null) { // @ts-ignore
|
|
143
|
-
return cbDb.get.call(this.db, key, value, makeDoneCallback(cb, deprecated));
|
|
144
|
-
}
|
|
145
|
-
return this.db.set(key, value);
|
|
146
|
-
}
|
|
147
|
-
/**
|
|
148
|
-
* @param key
|
|
149
|
-
* @param sub
|
|
150
|
-
* @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
|
|
151
|
-
*/
|
|
152
|
-
getSub(key, sub, callback = null) {
|
|
153
|
-
if (callback != null) { // @ts-ignore
|
|
154
|
-
return cbDb.getSub.call(this.db, key, sub, callback);
|
|
155
|
-
}
|
|
156
|
-
return this.db.getSub(key, sub);
|
|
157
|
-
}
|
|
158
|
-
/**
|
|
159
|
-
* Adds or changes a subvalue of an entry.
|
|
160
|
-
*
|
|
161
|
-
* @param key
|
|
162
|
-
* @param sub
|
|
163
|
-
* @param value
|
|
164
|
-
* @param cb Deprecated. Node-style callback. Called when the write has been committed to the
|
|
165
|
-
* underlying database driver. If null, a Promise is returned.
|
|
166
|
-
* @param deprecated Deprecated callback that is called just after cb. Ignored if cb is null.
|
|
167
|
-
*/
|
|
168
|
-
setSub(key, sub, value, cb = null, deprecated = null) {
|
|
169
|
-
if (cb != null) {
|
|
170
|
-
// @ts-ignore
|
|
171
|
-
return cbDb.setSub.call(this.db, key, sub, value, makeDoneCallback(cb, deprecated));
|
|
172
|
-
}
|
|
173
|
-
return this.db.setSub(key, sub, value);
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* Flushes unwritten changes then closes the connection to the underlying database. After this
|
|
177
|
-
* returns, any future call to a method on this object may result in an error.
|
|
178
|
-
*
|
|
179
|
-
* @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
|
|
180
|
-
*/
|
|
181
|
-
close(callback = null) {
|
|
182
|
-
if (callback != null) { // @ts-ignore
|
|
183
|
-
return cbDb.close.call(this.db, callback);
|
|
184
|
-
}
|
|
185
|
-
return this.db.close();
|
|
186
|
-
}
|
|
187
|
-
};
|
|
188
|
-
/**
|
|
189
|
-
* Deprecated synonym of Database.
|
|
190
|
-
*/
|
|
191
|
-
exports.database = exports.Database;
|
|
192
|
-
|
|
193
|
-
exports.Database = Database;
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var logging = require('./logging.js');
|
|
4
|
-
|
|
5
|
-
const nullLogger = logging.normalizeLogger(null);
|
|
6
|
-
// Format: All characters match themselves except * matches any zero or more characters. No
|
|
7
|
-
// backslash escaping is supported, so it is impossible to create a pattern that matches only the
|
|
8
|
-
// '*' character.
|
|
9
|
-
const simpleGlobToRegExp = (s) => s.replace(/[.+?^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '.*');
|
|
10
|
-
class AbstractDatabase {
|
|
11
|
-
logger;
|
|
12
|
-
// @ts-ignore
|
|
13
|
-
settings;
|
|
14
|
-
constructor() {
|
|
15
|
-
if (new.target === module.exports) {
|
|
16
|
-
throw new TypeError('cannot instantiate Abstract Database directly');
|
|
17
|
-
}
|
|
18
|
-
for (const fn of ['init', 'close', 'get', 'findKeys', 'remove', 'set']) {
|
|
19
|
-
// @ts-ignore
|
|
20
|
-
if (typeof this[fn] !== 'function')
|
|
21
|
-
throw new TypeError(`method ${fn} not defined`);
|
|
22
|
-
}
|
|
23
|
-
this.logger = nullLogger;
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* For findKey regex. Used by document dbs like mongodb or dirty.
|
|
27
|
-
*/
|
|
28
|
-
createFindRegex(key, notKey) {
|
|
29
|
-
let regex = `^(?=${simpleGlobToRegExp(key)}$)`;
|
|
30
|
-
if (notKey != null)
|
|
31
|
-
regex += `(?!${simpleGlobToRegExp(notKey)}$)`;
|
|
32
|
-
return new RegExp(regex);
|
|
33
|
-
}
|
|
34
|
-
doBulk(operations, cb) {
|
|
35
|
-
throw new Error('the doBulk method must be implemented if write caching is enabled');
|
|
36
|
-
}
|
|
37
|
-
get isAsync() { return false; }
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
module.exports = AbstractDatabase;
|