ueberdb2 4.1.1 → 4.1.3
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/package.json +5 -3
- package/.eslintignore +0 -2
- package/.eslintrc.cjs +0 -66
- package/.github/dependabot.yml +0 -11
- package/.github/workflows/npmpublish.yml +0 -134
- package/.travis.yml +0 -46
- package/CHANGELOG.md +0 -304
- package/CONTRIBUTING.md +0 -103
- package/SECURITY.md +0 -5
- package/databases/cassandra_db.ts +0 -265
- package/databases/couch_db.ts +0 -189
- package/databases/dirty_db.ts +0 -85
- package/databases/dirty_git_db.ts +0 -82
- package/databases/elasticsearch_db.ts +0 -257
- package/databases/memory_db.ts +0 -41
- package/databases/mock_db.ts +0 -43
- package/databases/mongodb_db.ts +0 -142
- package/databases/mssql_db.ts +0 -226
- package/databases/mysql_db.ts +0 -183
- package/databases/postgres_db.ts +0 -213
- package/databases/postgrespool_db.ts +0 -11
- package/databases/redis_db.ts +0 -129
- package/databases/rethink_db.ts +0 -114
- package/databases/sqlite_db.ts +0 -159
- package/docker-compose.yml +0 -44
- package/index.ts +0 -224
- package/lib/AbstractDatabase.ts +0 -79
- package/lib/CacheAndBufferLayer.ts +0 -665
- package/lib/logging.ts +0 -33
- package/test/lib/databases.ts +0 -73
- package/test/lib/mysql.sql +0 -84
- package/test/test.ts +0 -328
- package/test/test_bulk.ts +0 -69
- package/test/test_elasticsearch.ts +0 -128
- package/test/test_findKeys.ts +0 -41
- package/test/test_flush.ts +0 -55
- package/test/test_getSub.ts +0 -28
- package/test/test_lru.ts +0 -151
- package/test/test_memory.ts +0 -32
- package/test/test_metrics.ts +0 -734
- package/test/test_mysql.ts +0 -62
- package/test/test_postgres.ts +0 -16
- package/test/test_setSub.ts +0 -19
- package/test/test_tojson.ts +0 -34
package/databases/rethink_db.ts
DELETED
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 2016 Remi Arnaud
|
|
3
|
-
*
|
|
4
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
* you may not use this file except in compliance with the License.
|
|
6
|
-
* You may obtain a copy of the License at
|
|
7
|
-
*
|
|
8
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
*
|
|
10
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
12
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
* See the License for the specific language governing permissions and
|
|
14
|
-
* limitations under the License.
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
import AbstractDatabase, {Settings} from '../lib/AbstractDatabase';
|
|
18
|
-
import r from 'rethinkdb';
|
|
19
|
-
import async from 'async';
|
|
20
|
-
import {BulkObject} from './cassandra_db';
|
|
21
|
-
|
|
22
|
-
export const Database = class extends AbstractDatabase {
|
|
23
|
-
private host: string;
|
|
24
|
-
private db: string;
|
|
25
|
-
private port: number | string;
|
|
26
|
-
private table: string;
|
|
27
|
-
private connection: r.Connection | null;
|
|
28
|
-
constructor(settings:Settings) {
|
|
29
|
-
super();
|
|
30
|
-
if (!settings) settings = {};
|
|
31
|
-
if (!settings.host) { settings.host = 'localhost'; }
|
|
32
|
-
if (!settings.port) { settings.port = 28015; }
|
|
33
|
-
if (!settings.db) { settings.db = 'test'; }
|
|
34
|
-
if (!settings.table) { settings.table = 'test'; }
|
|
35
|
-
|
|
36
|
-
this.host = settings.host;
|
|
37
|
-
this.db = settings.db;
|
|
38
|
-
this.port = settings.port;
|
|
39
|
-
this.table = settings.table;
|
|
40
|
-
this.connection = null;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
init(callback: (p: any, cursor: any)=>{}) {
|
|
44
|
-
// @ts-ignore
|
|
45
|
-
r.connect(this, (err, conn) => {
|
|
46
|
-
if (err) throw err;
|
|
47
|
-
this.connection = conn;
|
|
48
|
-
|
|
49
|
-
r.table(this.table).run(this.connection, (err, cursor) => {
|
|
50
|
-
if (err) {
|
|
51
|
-
// assuming table does not exists
|
|
52
|
-
// @ts-ignore
|
|
53
|
-
r.tableCreate(this.table).run(this.connection, callback);
|
|
54
|
-
} else if (callback) { callback(null, cursor); }
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
get(key:string, callback: (err: Error, p: any)=>{}) {
|
|
60
|
-
// @ts-ignore
|
|
61
|
-
r.table(this.table).get(key).run(this.connection, (err, item) => {
|
|
62
|
-
// @ts-ignore
|
|
63
|
-
callback(err, (item ? item.content : item));
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
findKeys(key:string, notKey:string, callback:()=>{}) {
|
|
68
|
-
const keys = [];
|
|
69
|
-
const regex = this.createFindRegex(key, notKey);
|
|
70
|
-
// @ts-ignore
|
|
71
|
-
r.filter((item) => {
|
|
72
|
-
if (item.id.search(regex) !== -1) {
|
|
73
|
-
keys.push(item.id);
|
|
74
|
-
}
|
|
75
|
-
}).run(this.connection, callback);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
set(key:string, value:string, callback:()=>{}) {
|
|
79
|
-
r.table(this.table)
|
|
80
|
-
.insert({id: key, content: value}, {conflict: 'replace'})
|
|
81
|
-
.run(this.connection as r.Connection, callback);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
doBulk(bulk: BulkObject[], callback: ()=>{}) {
|
|
85
|
-
const _in: any[] = [];
|
|
86
|
-
const _out: string | string[] | r.Expression<any> = [];
|
|
87
|
-
|
|
88
|
-
for (const i in bulk) {
|
|
89
|
-
if (bulk[i].type === 'set') {
|
|
90
|
-
_in.push({id: bulk[i].key, content: bulk[i].value});
|
|
91
|
-
} else if (bulk[i].type === 'remove') {
|
|
92
|
-
_out.push(bulk[i].key);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
async.parallel([
|
|
97
|
-
(cb) => { // @ts-ignore
|
|
98
|
-
r.table(this.table).insert(_in, {conflict: 'replace'}).run(this.connection, cb);
|
|
99
|
-
},
|
|
100
|
-
(cb) => { // @ts-ignore
|
|
101
|
-
r.table(this.table).getAll(_out).delete().run(this.connection, cb);
|
|
102
|
-
},
|
|
103
|
-
], callback);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
remove(key:string, callback:()=>{}) {
|
|
107
|
-
// @ts-ignore
|
|
108
|
-
r.table(this.table).get(key).delete().run(this.connection, callback);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
close(callback:()=>{}) {
|
|
112
|
-
if (this.connection) { this.connection.close(callback); }
|
|
113
|
-
}
|
|
114
|
-
};
|
package/databases/sqlite_db.ts
DELETED
|
@@ -1,159 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
import {BulkObject} from "./cassandra_db";
|
|
3
|
-
import {Settings} from "../lib/AbstractDatabase";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* 2011 Peter 'Pita' Martischka
|
|
7
|
-
*
|
|
8
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
9
|
-
* you may not use this file except in compliance with the License.
|
|
10
|
-
* You may obtain a copy of the License at
|
|
11
|
-
*
|
|
12
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
13
|
-
*
|
|
14
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
15
|
-
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
16
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
17
|
-
* See the License for the specific language governing permissions and
|
|
18
|
-
* limitations under the License.
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
let SQDB: any;
|
|
22
|
-
try {
|
|
23
|
-
SQDB = require('sqlite3').Database;
|
|
24
|
-
} catch (err) {
|
|
25
|
-
throw new Error(
|
|
26
|
-
'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
|
-
|
|
31
|
-
import AbstractDatabase from '../lib/AbstractDatabase';
|
|
32
|
-
import util from 'util';
|
|
33
|
-
|
|
34
|
-
const escape = (val:string) => `'${val.replace(/'/g, "''")}'`;
|
|
35
|
-
|
|
36
|
-
export const Database = class SQLiteDB extends AbstractDatabase {
|
|
37
|
-
private db: typeof SQDB|null;
|
|
38
|
-
constructor(settings:Settings) {
|
|
39
|
-
super();
|
|
40
|
-
this.db = null;
|
|
41
|
-
|
|
42
|
-
if (!settings || !settings.filename) {
|
|
43
|
-
settings = {filename: ':memory:'};
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
this.settings = settings;
|
|
47
|
-
|
|
48
|
-
// set settings for the dbWrapper
|
|
49
|
-
if (settings.filename === ':memory:') {
|
|
50
|
-
this.settings.cache = 0;
|
|
51
|
-
this.settings.writeInterval = 0;
|
|
52
|
-
this.settings.json = true;
|
|
53
|
-
} else {
|
|
54
|
-
this.settings.cache = 1000;
|
|
55
|
-
this.settings.writeInterval = 100;
|
|
56
|
-
this.settings.json = true;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
init(callback:Function) {
|
|
61
|
-
util.callbackify(async () => {
|
|
62
|
-
this.db = new SQDB(this.settings.filename as string)
|
|
63
|
-
await this._query('CREATE TABLE IF NOT EXISTS store (key TEXT PRIMARY KEY, value TEXT)');
|
|
64
|
-
// @ts-ignore
|
|
65
|
-
})(callback);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
async _query(sql:string, params = []) {
|
|
69
|
-
// It is unclear how util.promisify() deals with variadic functions, so it is not used here.
|
|
70
|
-
return await new Promise((resolve, reject) => {
|
|
71
|
-
// According to sqlite3's documentation, .run() method (and maybe .all() and .get(); the
|
|
72
|
-
// documentation is unclear) might call the callback multiple times. That's OK -- ECMAScript
|
|
73
|
-
// guarantees that it is safe to call a Promise executor's resolve and reject functions
|
|
74
|
-
// multiple times. The subsequent calls are ignored, except Node.js's 'process' object emits a
|
|
75
|
-
// 'multipleResolves' event to aid in debugging.
|
|
76
|
-
this.db&&this.db.all(sql, params, (err:Error, rows:any) => {
|
|
77
|
-
if (err != null) return reject(err);
|
|
78
|
-
resolve(rows);
|
|
79
|
-
});
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// Temporary callbackified version of _query. This will be removed once all database objects are
|
|
84
|
-
// asyncified.
|
|
85
|
-
_queryCb(sql: string, params: string[], callback: Function) {
|
|
86
|
-
// It is unclear how util.callbackify() handles optional parameters, so it is not used here.
|
|
87
|
-
const p = this._query(sql, params as []);
|
|
88
|
-
if (callback) p.then((rows) => callback(null, rows), (err) => callback(err || new Error(err)));
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
get(key:string, callback:Function) {
|
|
94
|
-
this._queryCb(
|
|
95
|
-
'SELECT value FROM store WHERE key = ?', [key],
|
|
96
|
-
(err:Error, rows:any) => callback(err, err == null && rows && rows.length ? rows[0].value : null));
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
findKeys(key:string, notKey:string, callback:Function) {
|
|
100
|
-
let query = 'SELECT key FROM store WHERE key LIKE ?';
|
|
101
|
-
const params = [];
|
|
102
|
-
// desired keys are %key:%, e.g. pad:%
|
|
103
|
-
key = key.replace(/\*/g, '%');
|
|
104
|
-
params.push(key);
|
|
105
|
-
|
|
106
|
-
if (notKey != null) {
|
|
107
|
-
// not desired keys are notKey:%, e.g. %:%:%
|
|
108
|
-
notKey = notKey.replace(/\*/g, '%');
|
|
109
|
-
query += ' AND key NOT LIKE ?';
|
|
110
|
-
params.push(notKey);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
this._queryCb(query, params, (err: Error, results: { val: string, key: string }[]) => {
|
|
114
|
-
const value: string[] = [];
|
|
115
|
-
|
|
116
|
-
if (!err && Object.keys(results).length > 0) {
|
|
117
|
-
results.forEach((val) => {
|
|
118
|
-
value.push(val.key);
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
callback(err, value);
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
set(key:string, value:string, callback:Function) {
|
|
127
|
-
this._queryCb('REPLACE INTO store VALUES (?,?)', [key, value], callback);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
remove(key:string, callback:Function) {
|
|
131
|
-
this._queryCb('DELETE FROM store WHERE key = ?', [key], callback);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
doBulk(bulk:BulkObject[], callback:Function) {
|
|
135
|
-
let sql = 'BEGIN TRANSACTION;\n';
|
|
136
|
-
for (const i in bulk) {
|
|
137
|
-
if (bulk[i].type === 'set') {
|
|
138
|
-
sql += `REPLACE INTO store VALUES (${escape(bulk[i].key)}, ${escape(bulk[i].value as string)});\n`;
|
|
139
|
-
} else if (bulk[i].type === 'remove') {
|
|
140
|
-
sql += `DELETE FROM store WHERE key = ${escape(bulk[i].key)};\n`;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
sql += 'END TRANSACTION;';
|
|
144
|
-
|
|
145
|
-
this.db&&this.db.exec(sql, (err:any) => {
|
|
146
|
-
if (err) {
|
|
147
|
-
console.error('ERROR WITH SQL: ');
|
|
148
|
-
console.error(sql);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
callback(err);
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
close(callback: Function) {
|
|
156
|
-
callback()
|
|
157
|
-
this.db&&this.db.close((err:any) => callback(err));
|
|
158
|
-
}
|
|
159
|
-
};
|
package/docker-compose.yml
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
# Docker compose setup for testing with multiple databases
|
|
2
|
-
version: '3.8'
|
|
3
|
-
services:
|
|
4
|
-
couchdb:
|
|
5
|
-
image: couchdb
|
|
6
|
-
ports:
|
|
7
|
-
- "5984:5984"
|
|
8
|
-
environment:
|
|
9
|
-
COUCHDB_USER: ueberdb
|
|
10
|
-
COUCHDB_PASSWORD: ueberdb
|
|
11
|
-
elasticsearch:
|
|
12
|
-
image: elasticsearch:7.17.3
|
|
13
|
-
ports:
|
|
14
|
-
- 9200:9200
|
|
15
|
-
environment:
|
|
16
|
-
discovery.type: single-node
|
|
17
|
-
mongo:
|
|
18
|
-
image: mongo
|
|
19
|
-
ports:
|
|
20
|
-
- 27017:27017
|
|
21
|
-
environment:
|
|
22
|
-
MONGO_INITDB_DATABASE: mydb_test
|
|
23
|
-
mysql:
|
|
24
|
-
image: mariadb
|
|
25
|
-
ports:
|
|
26
|
-
- 3306:3306
|
|
27
|
-
environment:
|
|
28
|
-
MYSQL_ROOT_PASSWORD: password
|
|
29
|
-
MYSQL_USER: ueberdb
|
|
30
|
-
MYSQL_PASSWORD: ueberdb
|
|
31
|
-
MYSQL_DATABASE: ueberdb
|
|
32
|
-
postgres:
|
|
33
|
-
image: postgres:14-alpine
|
|
34
|
-
ports:
|
|
35
|
-
- 5432:5432
|
|
36
|
-
environment:
|
|
37
|
-
POSTGRES_USER: ueberdb
|
|
38
|
-
POSTGRES_PASSWORD: ueberdb
|
|
39
|
-
POSTGRES_DB: ueberdb
|
|
40
|
-
POSTGRES_HOST_AUTH_METHOD: "trust"
|
|
41
|
-
redis:
|
|
42
|
-
image: redis
|
|
43
|
-
ports:
|
|
44
|
-
- "6379:6379"
|
package/index.ts
DELETED
|
@@ -1,224 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 2011 Peter 'Pita' Martischka
|
|
5
|
-
* 2020 John McLear
|
|
6
|
-
*
|
|
7
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
-
* you may not use this file except in compliance with the License.
|
|
9
|
-
* You may obtain a copy of the License at
|
|
10
|
-
*
|
|
11
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
-
*
|
|
13
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
14
|
-
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
15
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
-
* See the License for the specific language governing permissions and
|
|
17
|
-
* limitations under the License.
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
// @ts-ignore
|
|
21
|
-
import cacheAndBufferLayer from './lib/CacheAndBufferLayer';
|
|
22
|
-
import {normalizeLogger} from './lib/logging';
|
|
23
|
-
import {callbackify} from 'util';
|
|
24
|
-
import {Settings} from './lib/AbstractDatabase';
|
|
25
|
-
|
|
26
|
-
const cbDb = {
|
|
27
|
-
init: () => {},
|
|
28
|
-
flush: () => {},
|
|
29
|
-
get: () => {},
|
|
30
|
-
remove: () => {},
|
|
31
|
-
findKeys: () => {},
|
|
32
|
-
close: () => {},
|
|
33
|
-
getSub: () => {},
|
|
34
|
-
setSub: () => {},
|
|
35
|
-
};
|
|
36
|
-
const fns = ['close', 'findKeys', 'flush', 'get', 'getSub', 'init', 'remove', 'set', 'setSub'];
|
|
37
|
-
for (const fn of fns) { // @ts-ignore
|
|
38
|
-
cbDb[fn] = callbackify(cacheAndBufferLayer.Database.prototype[fn]);
|
|
39
|
-
}
|
|
40
|
-
const makeDoneCallback = (callback: (err?:any)=>{}, deprecated:(err:any)=>{}) => (err: null) => {
|
|
41
|
-
if (callback) callback(err);
|
|
42
|
-
if (deprecated) deprecated(err);
|
|
43
|
-
if (err != null && callback == null && deprecated == null) throw err;
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
export const Database = class {
|
|
47
|
-
private type: any;
|
|
48
|
-
private dbModule: any;
|
|
49
|
-
private readonly dbSettings: any;
|
|
50
|
-
private readonly wrapperSettings: any | {};
|
|
51
|
-
private readonly logger: Function | null;
|
|
52
|
-
private readonly db: any;
|
|
53
|
-
private metrics: any;
|
|
54
|
-
/**
|
|
55
|
-
* @param type The type of the database
|
|
56
|
-
* @param dbSettings The settings for that specific database type
|
|
57
|
-
* @param wrapperSettings
|
|
58
|
-
* @param logger Optional logger object. If no logger object is provided no logging will occur.
|
|
59
|
-
* The logger object is expected to be a log4js logger object or `console`. A logger object
|
|
60
|
-
* from another logging library should also work, but performance may be reduced if the logger
|
|
61
|
-
* object does not have is${Level}Enabled() methods (isDebugEnabled(), etc.).
|
|
62
|
-
*/
|
|
63
|
-
constructor(type: undefined | string, dbSettings: Settings | null | string, wrapperSettings?: null | {}, logger:any = null) {
|
|
64
|
-
if (!type) {
|
|
65
|
-
type = 'sqlite';
|
|
66
|
-
dbSettings = null;
|
|
67
|
-
wrapperSettings = null;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// saves all settings and require the db module
|
|
71
|
-
this.type = type;
|
|
72
|
-
this.dbModule = require(`./databases/${type}_db`);
|
|
73
|
-
this.dbSettings = dbSettings;
|
|
74
|
-
this.wrapperSettings = wrapperSettings;
|
|
75
|
-
this.logger = normalizeLogger(logger);
|
|
76
|
-
const db = new this.dbModule.Database(this.dbSettings);
|
|
77
|
-
db.logger = this.logger;
|
|
78
|
-
this.db = new cacheAndBufferLayer.Database(db, this.wrapperSettings, this.logger);
|
|
79
|
-
|
|
80
|
-
// Expose the cache wrapper's metrics to the user. See lib/CacheAndBufferLayer.js for details.
|
|
81
|
-
//
|
|
82
|
-
// WARNING: This feature is EXPERIMENTAL -- do not assume it will continue to exist in its
|
|
83
|
-
// current form in a future version.
|
|
84
|
-
this.metrics = this.db.metrics;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
|
|
89
|
-
*/
|
|
90
|
-
init(callback = null) {
|
|
91
|
-
if (callback != null) {
|
|
92
|
-
return cbDb.init.call(this.db);
|
|
93
|
-
}
|
|
94
|
-
return this.db.init();
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Wrapper functions
|
|
99
|
-
*/
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Deprecated synonym of flush().
|
|
103
|
-
*
|
|
104
|
-
* @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
|
|
105
|
-
*/
|
|
106
|
-
doShutdown(callback = null) {
|
|
107
|
-
return this.flush(callback);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Writes any unsaved changes to the underlying database.
|
|
112
|
-
*
|
|
113
|
-
* @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
|
|
114
|
-
*/
|
|
115
|
-
flush(callback = null) {
|
|
116
|
-
if (!cbDb || !cbDb.flush === undefined) return null;
|
|
117
|
-
if (callback != null) { // @ts-ignore
|
|
118
|
-
return cbDb.flush.call(this.db, callback);
|
|
119
|
-
}
|
|
120
|
-
return this.db.flush();
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* @param key
|
|
125
|
-
* @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
|
|
126
|
-
*/
|
|
127
|
-
get(key:string, callback = null) {
|
|
128
|
-
if (callback != null) { // @ts-ignore
|
|
129
|
-
return cbDb.get.call(this.db, key, callback);
|
|
130
|
-
}
|
|
131
|
-
return this.db.get(key);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* @param key
|
|
136
|
-
* @param notKey
|
|
137
|
-
* @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
|
|
138
|
-
*/
|
|
139
|
-
findKeys(key:string, notKey:string, callback = null) {
|
|
140
|
-
if (callback != null) { // @ts-ignore
|
|
141
|
-
return cbDb.findKeys.call(this.db, key, notKey, callback);
|
|
142
|
-
}
|
|
143
|
-
return this.db.findKeys(key, notKey);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* Removes an entry from the database if present.
|
|
148
|
-
*
|
|
149
|
-
* @param key
|
|
150
|
-
* @param cb Deprecated. Node-style callback. Called when the write has been committed to the
|
|
151
|
-
* underlying database driver. If null, a Promise is returned.
|
|
152
|
-
* @param deprecated Deprecated callback that is called just after cb. Ignored if cb is null.
|
|
153
|
-
*/
|
|
154
|
-
remove(key:string, cb = null, deprecated = null) {
|
|
155
|
-
if (cb != null) { // @ts-ignore
|
|
156
|
-
return cbDb.remove.call(this.db, key, makeDoneCallback(cb, deprecated));
|
|
157
|
-
}
|
|
158
|
-
return this.db.remove(key);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* Adds or changes the value of an entry.
|
|
163
|
-
*
|
|
164
|
-
* @param key
|
|
165
|
-
* @param value
|
|
166
|
-
* @param cb Deprecated. Node-style callback. Called when the write has been committed to the
|
|
167
|
-
* underlying database driver. If null, a Promise is returned.
|
|
168
|
-
* @param deprecated Deprecated callback that is called just after cb. Ignored if cb is null.
|
|
169
|
-
*/
|
|
170
|
-
set(key:string, value:string, cb = null, deprecated = null) {
|
|
171
|
-
if (cb != null) { // @ts-ignore
|
|
172
|
-
return cbDb.get.call(this.db, key, value, makeDoneCallback(cb, deprecated));
|
|
173
|
-
}
|
|
174
|
-
return this.db.set(key, value);
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* @param key
|
|
179
|
-
* @param sub
|
|
180
|
-
* @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
|
|
181
|
-
*/
|
|
182
|
-
getSub(key:string, sub:string, callback = null) {
|
|
183
|
-
if (callback != null) { // @ts-ignore
|
|
184
|
-
return cbDb.getSub.call(this.db, key, sub, callback);
|
|
185
|
-
}
|
|
186
|
-
return this.db.getSub(key, sub);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* Adds or changes a subvalue of an entry.
|
|
191
|
-
*
|
|
192
|
-
* @param key
|
|
193
|
-
* @param sub
|
|
194
|
-
* @param value
|
|
195
|
-
* @param cb Deprecated. Node-style callback. Called when the write has been committed to the
|
|
196
|
-
* underlying database driver. If null, a Promise is returned.
|
|
197
|
-
* @param deprecated Deprecated callback that is called just after cb. Ignored if cb is null.
|
|
198
|
-
*/
|
|
199
|
-
setSub(key:string, sub:string, value:string, cb = null, deprecated = null) {
|
|
200
|
-
if (cb != null) {
|
|
201
|
-
// @ts-ignore
|
|
202
|
-
return cbDb.setSub.call(this.db, key, sub, value, makeDoneCallback(cb, deprecated));
|
|
203
|
-
}
|
|
204
|
-
return this.db.setSub(key, sub, value);
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* Flushes unwritten changes then closes the connection to the underlying database. After this
|
|
209
|
-
* returns, any future call to a method on this object may result in an error.
|
|
210
|
-
*
|
|
211
|
-
* @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
|
|
212
|
-
*/
|
|
213
|
-
close(callback = null) {
|
|
214
|
-
if (callback != null) { // @ts-ignore
|
|
215
|
-
return cbDb.close.call(this.db, callback);
|
|
216
|
-
}
|
|
217
|
-
return this.db.close();
|
|
218
|
-
}
|
|
219
|
-
};
|
|
220
|
-
|
|
221
|
-
/**
|
|
222
|
-
* Deprecated synonym of Database.
|
|
223
|
-
*/
|
|
224
|
-
exports.database = exports.Database;
|
package/lib/AbstractDatabase.ts
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import {normalizeLogger} from './logging';
|
|
2
|
-
|
|
3
|
-
const nullLogger = normalizeLogger(null);
|
|
4
|
-
|
|
5
|
-
// Format: All characters match themselves except * matches any zero or more characters. No
|
|
6
|
-
// backslash escaping is supported, so it is impossible to create a pattern that matches only the
|
|
7
|
-
// '*' character.
|
|
8
|
-
const simpleGlobToRegExp = (s:string) => s.replace(/[.+?^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '.*');
|
|
9
|
-
|
|
10
|
-
export type Settings = {
|
|
11
|
-
data?: any;
|
|
12
|
-
table?: string;
|
|
13
|
-
db?: string;
|
|
14
|
-
idleTimeoutMillis?: any;
|
|
15
|
-
min?: any;
|
|
16
|
-
max?: any;
|
|
17
|
-
engine?: string;
|
|
18
|
-
charset?: string;
|
|
19
|
-
server?: string | undefined;
|
|
20
|
-
requestTimeout?: number;
|
|
21
|
-
bulkLimit?: number;
|
|
22
|
-
queryTimeout?: number;
|
|
23
|
-
connectionString?: string;
|
|
24
|
-
parseJSON?: boolean;
|
|
25
|
-
dbName?: string;
|
|
26
|
-
collection?: string;
|
|
27
|
-
url?: string;
|
|
28
|
-
mock?: any;
|
|
29
|
-
base_index?: string;
|
|
30
|
-
migrate_to_newer_schema?: boolean;
|
|
31
|
-
api?: string
|
|
32
|
-
filename?: string;
|
|
33
|
-
database?: string;
|
|
34
|
-
password?: string;
|
|
35
|
-
user?: string;
|
|
36
|
-
port?: number | string;
|
|
37
|
-
host?: string;
|
|
38
|
-
maxListeners?: number | undefined;
|
|
39
|
-
json?: boolean;
|
|
40
|
-
cache?: number;
|
|
41
|
-
writeInterval?: number;
|
|
42
|
-
logger?: any;
|
|
43
|
-
columnFamily?: any;
|
|
44
|
-
clientOptions?: any;
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
class AbstractDatabase {
|
|
49
|
-
logger: any;
|
|
50
|
-
// @ts-ignore
|
|
51
|
-
settings: Settings;
|
|
52
|
-
constructor() {
|
|
53
|
-
if (new.target === module.exports) {
|
|
54
|
-
throw new TypeError('cannot instantiate Abstract Database directly');
|
|
55
|
-
}
|
|
56
|
-
for (const fn of ['init', 'close', 'get', 'findKeys', 'remove', 'set']) {
|
|
57
|
-
// @ts-ignore
|
|
58
|
-
if (typeof this[fn] !== 'function') throw new TypeError(`method ${fn} not defined`);
|
|
59
|
-
}
|
|
60
|
-
this.logger = nullLogger;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* For findKey regex. Used by document dbs like mongodb or dirty.
|
|
65
|
-
*/
|
|
66
|
-
createFindRegex(key:string, notKey?:string) {
|
|
67
|
-
let regex = `^(?=${simpleGlobToRegExp(key)}$)`;
|
|
68
|
-
if (notKey != null) regex += `(?!${simpleGlobToRegExp(notKey)}$)`;
|
|
69
|
-
return new RegExp(regex);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
doBulk(operations:any, cb: ()=>{}) {
|
|
73
|
-
throw new Error('the doBulk method must be implemented if write caching is enabled');
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
get isAsync() { return false; }
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export default AbstractDatabase;
|