ueberdb2 4.0.10 → 4.0.15

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.
Files changed (47) hide show
  1. package/.eslintignore +2 -0
  2. package/.eslintrc.cjs +44 -5
  3. package/databases/{cassandra_db.js → cassandra_db.ts} +45 -30
  4. package/databases/{couch_db.js → couch_db.ts} +78 -31
  5. package/databases/{dirty_db.js → dirty_db.ts} +19 -14
  6. package/databases/{dirty_git_db.js → dirty_git_db.ts} +19 -15
  7. package/databases/{elasticsearch_db.js → elasticsearch_db.ts} +30 -21
  8. package/databases/{memory_db.js → memory_db.ts} +8 -8
  9. package/databases/mock_db.ts +43 -0
  10. package/databases/{mongodb_db.js → mongodb_db.ts} +22 -16
  11. package/databases/{mssql_db.js → mssql_db.ts} +29 -21
  12. package/databases/{mysql_db.js → mysql_db.ts} +20 -15
  13. package/databases/{postgres_db.js → postgres_db.ts} +37 -22
  14. package/databases/{postgrespool_db.js → postgrespool_db.ts} +3 -3
  15. package/databases/redis_db.ts +129 -0
  16. package/databases/{rethink_db.js → rethink_db.ts} +35 -19
  17. package/databases/{sqlite_db.js → sqlite_db.ts} +37 -36
  18. package/docker-compose.yml +44 -0
  19. package/{index.js → index.ts} +76 -25
  20. package/lib/AbstractDatabase.ts +79 -0
  21. package/lib/{CacheAndBufferLayer.js → CacheAndBufferLayer.ts} +17 -16
  22. package/lib/{logging.js → logging.ts} +10 -6
  23. package/package.json +17 -3
  24. package/test/lib/{databases.js → databases.ts} +8 -5
  25. package/test/test.ts +328 -0
  26. package/test/test_bulk.ts +69 -0
  27. package/test/{test_elasticsearch.js → test_elasticsearch.ts} +48 -53
  28. package/test/{test_findKeys.js → test_findKeys.ts} +15 -17
  29. package/test/{test_flush.js → test_flush.ts} +16 -22
  30. package/test/test_getSub.ts +28 -0
  31. package/test/test_lru.ts +151 -0
  32. package/test/test_memory.ts +32 -0
  33. package/test/{test_metrics.js → test_metrics.ts} +73 -68
  34. package/test/{test_mysql.js → test_mysql.ts} +16 -22
  35. package/test/test_postgres.ts +16 -0
  36. package/test/{test_setSub.js → test_setSub.ts} +8 -12
  37. package/test/test_tojson.ts +34 -0
  38. package/databases/mock_db.js +0 -42
  39. package/databases/redis_db.js +0 -96
  40. package/lib/AbstractDatabase.js +0 -37
  41. package/test/test.js +0 -328
  42. package/test/test_bulk.js +0 -69
  43. package/test/test_getSub.js +0 -31
  44. package/test/test_lru.js +0 -145
  45. package/test/test_memory.js +0 -31
  46. package/test/test_postgres.js +0 -16
  47. package/test/test_tojson.js +0 -37
@@ -17,28 +17,50 @@
17
17
  * limitations under the License.
18
18
  */
19
19
 
20
- const cacheAndBufferLayer = require('./lib/CacheAndBufferLayer');
21
- const logging = require('./lib/logging');
22
- const util = require('util');
23
-
24
- const cbDb = {};
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
+ };
25
36
  const fns = ['close', 'findKeys', 'flush', 'get', 'getSub', 'init', 'remove', 'set', 'setSub'];
26
- for (const fn of fns) cbDb[fn] = util.callbackify(cacheAndBufferLayer.Database.prototype[fn]);
27
-
28
- const makeDoneCallback = (callback, deprecated) => (err) => {
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) => {
29
41
  if (callback) callback(err);
30
42
  if (deprecated) deprecated(err);
31
43
  if (err != null && callback == null && deprecated == null) throw err;
32
44
  };
33
45
 
34
- exports.Database = class {
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;
35
54
  /**
55
+ * @param type The type of the database
56
+ * @param dbSettings The settings for that specific database type
57
+ * @param wrapperSettings
36
58
  * @param logger Optional logger object. If no logger object is provided no logging will occur.
37
59
  * The logger object is expected to be a log4js logger object or `console`. A logger object
38
60
  * from another logging library should also work, but performance may be reduced if the logger
39
61
  * object does not have is${Level}Enabled() methods (isDebugEnabled(), etc.).
40
62
  */
41
- constructor(type, dbSettings, wrapperSettings, logger = null) {
63
+ constructor(type: undefined | string, dbSettings: Settings | null | string, wrapperSettings?: null | {}, logger:any = null) {
42
64
  if (!type) {
43
65
  type = 'sqlite';
44
66
  dbSettings = null;
@@ -50,7 +72,7 @@ exports.Database = class {
50
72
  this.dbModule = require(`./databases/${type}_db`);
51
73
  this.dbSettings = dbSettings;
52
74
  this.wrapperSettings = wrapperSettings;
53
- this.logger = logging.normalizeLogger(logger);
75
+ this.logger = normalizeLogger(logger);
54
76
  const db = new this.dbModule.Database(this.dbSettings);
55
77
  db.logger = this.logger;
56
78
  this.db = new cacheAndBufferLayer.Database(db, this.wrapperSettings, this.logger);
@@ -66,7 +88,9 @@ exports.Database = class {
66
88
  * @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
67
89
  */
68
90
  init(callback = null) {
69
- if (callback != null) return cbDb.init.call(this.db, callback);
91
+ if (callback != null) {
92
+ return cbDb.init.call(this.db);
93
+ }
70
94
  return this.db.init();
71
95
  }
72
96
 
@@ -89,67 +113,92 @@ exports.Database = class {
89
113
  * @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
90
114
  */
91
115
  flush(callback = null) {
92
- if (callback != null) return cbDb.flush.call(this.db, callback);
116
+ if (!cbDb || !cbDb.flush === undefined) return null;
117
+ if (callback != null) { // @ts-ignore
118
+ return cbDb.flush.call(this.db, callback);
119
+ }
93
120
  return this.db.flush();
94
121
  }
95
122
 
96
123
  /**
124
+ * @param key
97
125
  * @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
98
126
  */
99
- get(key, callback = null) {
100
- if (callback != null) return cbDb.get.call(this.db, key, callback);
127
+ get(key:string, callback = null) {
128
+ if (callback != null) { // @ts-ignore
129
+ return cbDb.get.call(this.db, key, callback);
130
+ }
101
131
  return this.db.get(key);
102
132
  }
103
133
 
104
134
  /**
135
+ * @param key
136
+ * @param notKey
105
137
  * @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
106
138
  */
107
- findKeys(key, notKey, callback = null) {
108
- if (callback != null) return cbDb.findKeys.call(this.db, key, notKey, callback);
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
+ }
109
143
  return this.db.findKeys(key, notKey);
110
144
  }
111
145
 
112
146
  /**
113
147
  * Removes an entry from the database if present.
114
148
  *
149
+ * @param key
115
150
  * @param cb Deprecated. Node-style callback. Called when the write has been committed to the
116
151
  * underlying database driver. If null, a Promise is returned.
117
152
  * @param deprecated Deprecated callback that is called just after cb. Ignored if cb is null.
118
153
  */
119
- remove(key, cb = null, deprecated = null) {
120
- if (cb != null) return cbDb.remove.call(this.db, key, makeDoneCallback(cb, deprecated));
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
+ }
121
158
  return this.db.remove(key);
122
159
  }
123
160
 
124
161
  /**
125
162
  * Adds or changes the value of an entry.
126
163
  *
164
+ * @param key
165
+ * @param value
127
166
  * @param cb Deprecated. Node-style callback. Called when the write has been committed to the
128
167
  * underlying database driver. If null, a Promise is returned.
129
168
  * @param deprecated Deprecated callback that is called just after cb. Ignored if cb is null.
130
169
  */
131
- set(key, value, cb = null, deprecated = null) {
132
- if (cb != null) return cbDb.set.call(this.db, key, value, makeDoneCallback(cb, deprecated));
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
+ }
133
174
  return this.db.set(key, value);
134
175
  }
135
176
 
136
177
  /**
178
+ * @param key
179
+ * @param sub
137
180
  * @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
138
181
  */
139
- getSub(key, sub, callback = null) {
140
- if (callback != null) return cbDb.getSub.call(this.db, key, sub, callback);
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
+ }
141
186
  return this.db.getSub(key, sub);
142
187
  }
143
188
 
144
189
  /**
145
190
  * Adds or changes a subvalue of an entry.
146
191
  *
192
+ * @param key
193
+ * @param sub
194
+ * @param value
147
195
  * @param cb Deprecated. Node-style callback. Called when the write has been committed to the
148
196
  * underlying database driver. If null, a Promise is returned.
149
197
  * @param deprecated Deprecated callback that is called just after cb. Ignored if cb is null.
150
198
  */
151
- setSub(key, sub, value, cb = null, deprecated = null) {
199
+ setSub(key:string, sub:string, value:string, cb = null, deprecated = null) {
152
200
  if (cb != null) {
201
+ // @ts-ignore
153
202
  return cbDb.setSub.call(this.db, key, sub, value, makeDoneCallback(cb, deprecated));
154
203
  }
155
204
  return this.db.setSub(key, sub, value);
@@ -162,7 +211,9 @@ exports.Database = class {
162
211
  * @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
163
212
  */
164
213
  close(callback = null) {
165
- if (callback != null) return cbDb.close.call(this.db, callback);
214
+ if (callback != null) { // @ts-ignore
215
+ return cbDb.close.call(this.db, callback);
216
+ }
166
217
  return this.db.close();
167
218
  }
168
219
  };
@@ -0,0 +1,79 @@
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;
@@ -1,4 +1,5 @@
1
- 'use strict';
1
+ // @ts-nocheck
2
+
2
3
  /**
3
4
  * 2011 Peter 'Pita' Martischka
4
5
  *
@@ -116,20 +117,20 @@ class SelfContainedPromise extends Promise {
116
117
  }
117
118
 
118
119
  const defaultSettings =
119
- {
120
- // Maximum number of operations that can be passed to the wrapped database's doBulk() method.
121
- // Falsy means no limit. EXPERIMENTAL.
122
- bulkLimit: 0,
123
- // the number of elements that should be cached. To Disable cache just set it to zero
124
- cache: 10000,
125
- // the interval in ms the wrapper writes to the database. To Disable interval writes just set it
126
- // to zero
127
- writeInterval: 100,
128
- // a flag if the data sould be serialized/deserialized to json
129
- json: true,
130
- // use utf8mb4 as default
131
- charset: 'utf8mb4',
132
- };
120
+ {
121
+ // Maximum number of operations that can be passed to the wrapped database's doBulk() method.
122
+ // Falsy means no limit. EXPERIMENTAL.
123
+ bulkLimit: 0,
124
+ // the number of elements that should be cached. To Disable cache just set it to zero
125
+ cache: 10000,
126
+ // the interval in ms the wrapper writes to the database. To Disable interval writes just set it
127
+ // to zero
128
+ writeInterval: 100,
129
+ // a flag if the data sould be serialized/deserialized to json
130
+ json: true,
131
+ // use utf8mb4 as default
132
+ charset: 'utf8mb4',
133
+ };
133
134
 
134
135
  exports.Database = class {
135
136
  /**
@@ -316,7 +317,7 @@ exports.Database = class {
316
317
  ++this.metrics.readsFromCache;
317
318
  if (this.logger.isDebugEnabled()) {
318
319
  this.logger.debug(`GET - ${key} - ${JSON.stringify(entry.value)} - ` +
319
- `from ${entry.dirty ? 'dirty buffer' : 'cache'}`);
320
+ `from ${entry.dirty ? 'dirty buffer' : 'cache'}`);
320
321
  }
321
322
  return entry.value;
322
323
  }
@@ -1,7 +1,5 @@
1
- 'use strict';
2
-
3
- const {Console} = require('console');
4
- const {stdout, stderr} = require('process');
1
+ import {Console} from 'console';
2
+ import {stdout, stderr} from 'process';
5
3
 
6
4
  class ConsoleLogger extends Console {
7
5
  constructor(opts = {}) { super({stdout, stderr, inspectOptions: {depth: Infinity}, ...opts}); }
@@ -11,19 +9,25 @@ class ConsoleLogger extends Console {
11
9
  isErrorEnabled() { return true; }
12
10
  }
13
11
 
14
- exports.ConsoleLogger = ConsoleLogger;
15
12
 
16
- exports.normalizeLogger = (logger) => {
13
+ export const normalizeLogger = (logger: null | Function) => {
17
14
  const logLevelsUsed = ['debug', 'info', 'warn', 'error'];
18
15
  logger = Object.create(logger || {});
19
16
  for (const level of logLevelsUsed) {
20
17
  const enabledFnName = `is${level.charAt(0).toUpperCase() + level.slice(1)}Enabled`;
18
+ // @ts-ignore
21
19
  if (typeof logger[level] !== 'function') {
20
+ // @ts-ignore
22
21
  logger[level] = () => {};
22
+ // @ts-ignore
23
23
  logger[enabledFnName] = () => false;
24
+ // @ts-ignore
24
25
  } else if (typeof logger[enabledFnName] !== 'function') {
26
+ // @ts-ignore
25
27
  logger[enabledFnName] = () => true;
26
28
  }
27
29
  }
28
30
  return logger;
29
31
  };
32
+
33
+ export default {ConsoleLogger};
package/package.json CHANGED
@@ -25,6 +25,7 @@
25
25
  "cassandra-driver": "^4.6.4",
26
26
  "dirty": "^1.1.3",
27
27
  "elasticsearch7": "npm:@elastic/elasticsearch@^7.17.0",
28
+ "eslint-plugin-import": "^2.26.0",
28
29
  "mongodb": "^3.7.3",
29
30
  "mssql": "^9.1.1",
30
31
  "mysql": "2.18.1",
@@ -39,11 +40,21 @@
39
40
  "sqlite3": "^5.1.6"
40
41
  },
41
42
  "devDependencies": {
43
+ "@types/async": "^3.2.20",
44
+ "@types/mocha": "^10.0.1",
45
+ "@types/mongodb": "^4.0.7",
46
+ "@types/mssql": "^8.1.2",
47
+ "@types/mysql": "^2.15.21",
48
+ "@types/node": "^20.3.2",
49
+ "@types/pg": "^8.10.2",
50
+ "@types/rethinkdb": "^2.3.17",
42
51
  "cli-table": "^0.3.11",
43
52
  "eslint": "^8.43.0",
44
53
  "eslint-config-etherpad": "^3.0.13",
45
54
  "mocha": "^10.2.0",
46
55
  "randexp": "^0.5.3",
56
+ "ts-migrate": "^0.1.35",
57
+ "ts-node": "^10.9.1",
47
58
  "typescript": "^4.9.5",
48
59
  "wtfnode": "^0.9.1"
49
60
  },
@@ -51,8 +62,8 @@
51
62
  "type": "git",
52
63
  "url": "https://github.com/ether/ueberDB.git"
53
64
  },
54
- "main": "./index",
55
- "version": "4.0.10",
65
+ "main": "./dist/index",
66
+ "version": "4.0.15",
56
67
  "bugs": {
57
68
  "url": "https://github.com/ether/ueberDB/issues"
58
69
  },
@@ -60,7 +71,10 @@
60
71
  "scripts": {
61
72
  "lint": "eslint .",
62
73
  "lint:fix": "eslint --fix .",
63
- "test": "mocha test/test*.js"
74
+ "test": "tsc && mocha ./dist/test/test*.js",
75
+ "test:watch": "mocha ./dist/test/test*.js --watch",
76
+ "test-debug": "tsc && mocha --inspect-brk ./dist/test/test*.js",
77
+ "build": "tsc"
64
78
  },
65
79
  "_npmUser": {
66
80
  "name": "johnyma22",
@@ -1,9 +1,10 @@
1
- 'use strict';
1
+ const os = require('os');
2
2
 
3
- exports.databases = {
3
+
4
+ export const databases = {
4
5
  memory: {},
5
6
  dirty: {
6
- filename: '/tmp/ueberdb-test.db',
7
+ filename: `${os.tmpdir()}/ueberdb-test.db`,
7
8
  speeds: {
8
9
  setMax: 1,
9
10
  getMax: 0.1,
@@ -11,7 +12,7 @@ exports.databases = {
11
12
  },
12
13
  },
13
14
  sqlite: {
14
- filename: '/tmp/ueberdb-test.sqlite',
15
+ filename: `${os.tmpdir()}/ueberdb-test.sqlite`,
15
16
  speeds: {
16
17
  setMax: 0.6,
17
18
  getMax: 0.5,
@@ -65,6 +66,8 @@ exports.databases = {
65
66
  base_index: 'ueberdb_test',
66
67
  speeds: {
67
68
  findKeysMax: 30,
68
- },
69
+ }, host: '127.0.0.1',
70
+ port: '9200',
71
+
69
72
  },
70
73
  };