ueberdb2 1.4.16

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/index.js ADDED
@@ -0,0 +1,191 @@
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
+ const cacheAndBufferLayer = require('./lib/CacheAndBufferLayer');
21
+ const util = require('util');
22
+
23
+ // Returns a logger derived from the given logger (which may be null) that has debug() and
24
+ // isDebugEnabled() methods.
25
+ const normalizeLogger = (logger) => {
26
+ const logLevelsUsed = ['debug', 'error'];
27
+ logger = Object.create(logger || {});
28
+ for (const level of logLevelsUsed) {
29
+ const enabledFnName = `is${level.charAt(0).toUpperCase() + level.slice(1)}Enabled`;
30
+ if (typeof logger[level] !== 'function') {
31
+ logger[level] = () => {};
32
+ logger[enabledFnName] = () => false;
33
+ } else if (typeof logger[enabledFnName] !== 'function') {
34
+ logger[enabledFnName] = () => true;
35
+ }
36
+ }
37
+ return logger;
38
+ };
39
+
40
+ const makeDoneCallback = (callback, deprecated) => (err) => {
41
+ if (callback) callback(err);
42
+ if (deprecated) deprecated(err);
43
+ if (err != null && callback == null && deprecated == null) throw err;
44
+ };
45
+
46
+ exports.Database = class {
47
+ /**
48
+ * @param logger Optional logger object. If no logger object is provided no logging will occur.
49
+ * The logger object is expected to be a log4js logger object or `console`. A logger object
50
+ * from another logging library should also work, but performance may be reduced if the logger
51
+ * object does not have is${Level}Enabled() methods (isDebugEnabled(), etc.).
52
+ */
53
+ constructor(type, dbSettings, wrapperSettings, logger = null) {
54
+ if (!type) {
55
+ type = 'sqlite';
56
+ dbSettings = null;
57
+ wrapperSettings = null;
58
+ }
59
+
60
+ // saves all settings and require the db module
61
+ this.type = type;
62
+ this.dbModule = require(`./databases/${type}_db`);
63
+ this.dbSettings = dbSettings;
64
+ this.wrapperSettings = wrapperSettings;
65
+ this.logger = normalizeLogger(logger);
66
+ const db = new this.dbModule.Database(this.dbSettings);
67
+ this.db = new cacheAndBufferLayer.Database(db, this.wrapperSettings, this.logger);
68
+
69
+ // Expose the cache wrapper's metrics to the user. See lib/CacheAndBufferLayer.js for details.
70
+ //
71
+ // WARNING: This feature is EXPERIMENTAL -- do not assume it will continue to exist in its
72
+ // current form in a future version.
73
+ this.metrics = this.db.metrics;
74
+ }
75
+
76
+ init(callback) {
77
+ if (callback) {
78
+ util.callbackify(this.db.init.bind(this.db))(callback);
79
+ } else {
80
+ return this.db.init();
81
+ }
82
+ }
83
+
84
+ /**
85
+ * Wrapper functions
86
+ */
87
+
88
+ /**
89
+ * Deprecated synonym of flush().
90
+ */
91
+ doShutdown(callback) {
92
+ this.flush(callback);
93
+ }
94
+
95
+ /**
96
+ * Writes any unsaved changes to the underlying database.
97
+ */
98
+ flush(callback) {
99
+ util.callbackify(this.db.flush.bind(this.db))(callback);
100
+ }
101
+
102
+ get(key, callback) {
103
+ util.callbackify(this.db.get.bind(this.db))(key, (err, val) => callback(err, clone(val)));
104
+ }
105
+
106
+ findKeys(key, notKey, callback) {
107
+ util.callbackify(this.db.findKeys.bind(this.db))(key, notKey, (e, v) => callback(e, clone(v)));
108
+ }
109
+
110
+ /**
111
+ * Removes an entry from the database if present.
112
+ *
113
+ * @param cb Called when the write has been committed to the underlying database driver.
114
+ * @param deprecated Deprecated callback that is called just after cb.
115
+ */
116
+ remove(key, cb, deprecated = null) {
117
+ util.callbackify(this.db.remove.bind(this.db))(key, makeDoneCallback(cb, deprecated));
118
+ }
119
+
120
+ /**
121
+ * Adds or changes the value of an entry.
122
+ *
123
+ * @param cb Called when the write has been committed to the underlying database driver.
124
+ * @param deprecated Deprecated callback that is called just after cb.
125
+ */
126
+ set(key, value, cb, deprecated = null) {
127
+ util.callbackify(this.db.set.bind(this.db))(
128
+ key, clone(value), makeDoneCallback(cb, deprecated));
129
+ }
130
+
131
+ getSub(key, sub, callback) {
132
+ util.callbackify(this.db.getSub.bind(this.db))(
133
+ key, sub, (err, val) => callback(err, clone(val)));
134
+ }
135
+
136
+ /**
137
+ * Adds or changes a subvalue of an entry.
138
+ *
139
+ * @param cb Called when the write has been committed to the underlying database driver.
140
+ * @param deprecated Deprecated callback that is called just after cb.
141
+ */
142
+ setSub(key, sub, value, cb, deprecated = null) {
143
+ util.callbackify(this.db.setSub.bind(this.db))(
144
+ key, sub, clone(value), makeDoneCallback(cb, deprecated));
145
+ }
146
+
147
+ /**
148
+ * Flushes unwritten changes then closes the connection to the underlying database. After this
149
+ * returns, any future call to a method on this object may result in an error.
150
+ */
151
+ close(callback) {
152
+ util.callbackify(this.db.close.bind(this.db))(callback);
153
+ }
154
+ };
155
+
156
+ const clone = (obj) => {
157
+ // Handle the 3 simple types, and null or undefined
158
+ if (null == obj || 'object' !== typeof obj) return obj;
159
+
160
+ // Handle Date
161
+ if (obj instanceof Date) {
162
+ const copy = new Date();
163
+ copy.setTime(obj.getTime());
164
+ return copy;
165
+ }
166
+
167
+ // Handle Array
168
+ if (obj instanceof Array) {
169
+ const copy = [];
170
+ for (let i = 0, len = obj.length; i < len; ++i) {
171
+ copy[i] = clone(obj[i]);
172
+ }
173
+ return copy;
174
+ }
175
+
176
+ // Handle Object
177
+ if (obj instanceof Object) {
178
+ const copy = {};
179
+ for (const attr in obj) {
180
+ if (Object.prototype.hasOwnProperty.call(obj, attr)) copy[attr] = clone(obj[attr]);
181
+ }
182
+ return copy;
183
+ }
184
+
185
+ throw new Error("Unable to copy obj! Its type isn't supported.");
186
+ };
187
+
188
+ /**
189
+ * Deprecated synonym of Database.
190
+ */
191
+ exports.database = exports.Database;
@@ -0,0 +1,32 @@
1
+ 'use strict';
2
+
3
+ module.exports = class AbstractDatabase {
4
+ constructor() {
5
+ if (new.target === module.exports) {
6
+ throw new TypeError('cannot instantiate Abstract Database directly');
7
+ }
8
+ for (const fn of ['init', 'close', 'get', 'findKeys', 'remove', 'set']) {
9
+ if (typeof this[fn] !== 'function') throw new TypeError(`method ${fn} not defined`);
10
+ }
11
+ }
12
+
13
+ /**
14
+ * For findKey regex. Used by document dbs like mongodb or dirty.
15
+ */
16
+ createFindRegex(key, notKey) {
17
+ let regex = '';
18
+ key = key.replace(/\*/g, '.*');
19
+ regex = `(?=^${key}$)`;
20
+ if (notKey != null) {
21
+ notKey = notKey.replace(/\*/g, '.*');
22
+ regex += `(?!${notKey}$)`;
23
+ }
24
+ return new RegExp(regex);
25
+ }
26
+
27
+ doBulk(operations, cb) {
28
+ throw new Error('the doBulk method must be implemented if write caching is enabled');
29
+ }
30
+
31
+ get isAsync() { return false; }
32
+ };