-
Notifications
You must be signed in to change notification settings - Fork 95
/
index.js
173 lines (154 loc) · 5.95 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
'use strict';
/**
* 2011 Peter 'Pita' Martischka
* 2020 John McLear
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const cacheAndBufferLayer = require('./lib/CacheAndBufferLayer');
const logging = require('./lib/logging');
const util = require('util');
const cbDb = {};
const fns = ['close', 'findKeys', 'flush', 'get', 'getSub', 'init', 'remove', 'set', 'setSub'];
for (const fn of fns) cbDb[fn] = util.callbackify(cacheAndBufferLayer.Database.prototype[fn]);
const makeDoneCallback = (callback, deprecated) => (err) => {
if (callback) callback(err);
if (deprecated) deprecated(err);
if (err != null && callback == null && deprecated == null) throw err;
};
exports.Database = class {
/**
* @param logger Optional logger object. If no logger object is provided no logging will occur.
* The logger object is expected to be a log4js logger object or `console`. A logger object
* from another logging library should also work, but performance may be reduced if the logger
* object does not have is${Level}Enabled() methods (isDebugEnabled(), etc.).
*/
constructor(type, dbSettings, wrapperSettings, logger = null) {
if (!type) {
type = 'sqlite';
dbSettings = null;
wrapperSettings = null;
}
// saves all settings and require the db module
this.type = type;
this.dbModule = require(`./databases/${type}_db`);
this.dbSettings = dbSettings;
this.wrapperSettings = wrapperSettings;
this.logger = logging.normalizeLogger(logger);
const db = new this.dbModule.Database(this.dbSettings);
db.logger = this.logger;
this.db = new cacheAndBufferLayer.Database(db, this.wrapperSettings, this.logger);
// Expose the cache wrapper's metrics to the user. See lib/CacheAndBufferLayer.js for details.
//
// WARNING: This feature is EXPERIMENTAL -- do not assume it will continue to exist in its
// current form in a future version.
this.metrics = this.db.metrics;
}
/**
* @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
*/
init(callback = null) {
if (callback != null) return cbDb.init.call(this.db, callback);
return this.db.init();
}
/**
* Wrapper functions
*/
/**
* Deprecated synonym of flush().
*
* @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
*/
doShutdown(callback = null) {
return this.flush(callback);
}
/**
* Writes any unsaved changes to the underlying database.
*
* @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
*/
flush(callback = null) {
if (callback != null) return cbDb.flush.call(this.db, callback);
return this.db.flush();
}
/**
* @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
*/
get(key, callback = null) {
if (callback != null) return cbDb.get.call(this.db, key, callback);
return this.db.get(key);
}
/**
* @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
*/
findKeys(key, notKey, callback = null) {
if (callback != null) return cbDb.findKeys.call(this.db, key, notKey, callback);
return this.db.findKeys(key, notKey);
}
/**
* Removes an entry from the database if present.
*
* @param cb Deprecated. Node-style callback. Called when the write has been committed to the
* underlying database driver. If null, a Promise is returned.
* @param deprecated Deprecated callback that is called just after cb. Ignored if cb is null.
*/
remove(key, cb = null, deprecated = null) {
if (cb != null) return cbDb.remove.call(this.db, key, makeDoneCallback(cb, deprecated));
return this.db.remove(key);
}
/**
* Adds or changes the value of an entry.
*
* @param cb Deprecated. Node-style callback. Called when the write has been committed to the
* underlying database driver. If null, a Promise is returned.
* @param deprecated Deprecated callback that is called just after cb. Ignored if cb is null.
*/
set(key, value, cb = null, deprecated = null) {
if (cb != null) return cbDb.set.call(this.db, key, value, makeDoneCallback(cb, deprecated));
return this.db.set(key, value);
}
/**
* @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
*/
getSub(key, sub, callback = null) {
if (callback != null) return cbDb.getSub.call(this.db, key, sub, callback);
return this.db.getSub(key, sub);
}
/**
* Adds or changes a subvalue of an entry.
*
* @param cb Deprecated. Node-style callback. Called when the write has been committed to the
* underlying database driver. If null, a Promise is returned.
* @param deprecated Deprecated callback that is called just after cb. Ignored if cb is null.
*/
setSub(key, sub, value, cb = null, deprecated = null) {
if (cb != null) {
return cbDb.setSub.call(this.db, key, sub, value, makeDoneCallback(cb, deprecated));
}
return this.db.setSub(key, sub, value);
}
/**
* Flushes unwritten changes then closes the connection to the underlying database. After this
* returns, any future call to a method on this object may result in an error.
*
* @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
*/
close(callback = null) {
if (callback != null) return cbDb.close.call(this.db, callback);
return this.db.close();
}
};
/**
* Deprecated synonym of Database.
*/
exports.database = exports.Database;