ueberdb2 1.4.19 → 2.0.1
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/CHANGELOG.md +10 -0
- package/README.md +1 -0
- package/databases/memory_db.js +41 -0
- package/index.js +5 -3
- package/package.json +1 -1
- package/test/lib/databases.js +1 -0
- package/test/test_tojson.js +40 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# Notable Changes
|
|
2
2
|
|
|
3
|
+
## v2.0.0
|
|
4
|
+
|
|
5
|
+
* When saving an object that has a `.toJSON()` method, the value returned from
|
|
6
|
+
that method is saved to the database instead of the object itself. This
|
|
7
|
+
matches [the behavior of
|
|
8
|
+
`JSON.stringify()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#tojson_behavior).
|
|
9
|
+
The `.toJSON()` method is used even if the chosen database driver never
|
|
10
|
+
actually converts anything to JSON.
|
|
11
|
+
* New `memory` database driver that stores values in memory only.
|
|
12
|
+
|
|
3
13
|
## v1.4.19
|
|
4
14
|
|
|
5
15
|
Updated database (and other) dependencies:
|
package/README.md
CHANGED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const AbstractDatabase = require('../lib/AbstractDatabase');
|
|
4
|
+
|
|
5
|
+
exports.Database = class extends AbstractDatabase {
|
|
6
|
+
constructor(settings) {
|
|
7
|
+
super();
|
|
8
|
+
this.settings = settings;
|
|
9
|
+
settings.json = false;
|
|
10
|
+
settings.cache = 0;
|
|
11
|
+
settings.writeInterval = 0;
|
|
12
|
+
this._data = null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
get isAsync() { return true; }
|
|
16
|
+
|
|
17
|
+
close() {
|
|
18
|
+
this._data = null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
findKeys(key, notKey) {
|
|
22
|
+
const regex = this.createFindRegex(key, notKey);
|
|
23
|
+
return [...this._data.keys()].filter((k) => regex.test(k));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
get(key) {
|
|
27
|
+
return this._data.get(key);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
init() {
|
|
31
|
+
this._data = new Map();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
remove(key) {
|
|
35
|
+
this._data.delete(key);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
set(key, value) {
|
|
39
|
+
this._data.set(key, value);
|
|
40
|
+
}
|
|
41
|
+
};
|
package/index.js
CHANGED
|
@@ -153,10 +153,12 @@ exports.Database = class {
|
|
|
153
153
|
}
|
|
154
154
|
};
|
|
155
155
|
|
|
156
|
-
const clone = (obj) => {
|
|
156
|
+
const clone = (obj, key = '') => {
|
|
157
157
|
// Handle the 3 simple types, and null or undefined
|
|
158
158
|
if (null == obj || 'object' !== typeof obj) return obj;
|
|
159
159
|
|
|
160
|
+
if (typeof obj.toJSON === 'function') return clone(obj.toJSON(key));
|
|
161
|
+
|
|
160
162
|
// Handle Date
|
|
161
163
|
if (obj instanceof Date) {
|
|
162
164
|
const copy = new Date();
|
|
@@ -168,7 +170,7 @@ const clone = (obj) => {
|
|
|
168
170
|
if (obj instanceof Array) {
|
|
169
171
|
const copy = [];
|
|
170
172
|
for (let i = 0, len = obj.length; i < len; ++i) {
|
|
171
|
-
copy[i] = clone(obj[i]);
|
|
173
|
+
copy[i] = clone(obj[i], String(i));
|
|
172
174
|
}
|
|
173
175
|
return copy;
|
|
174
176
|
}
|
|
@@ -177,7 +179,7 @@ const clone = (obj) => {
|
|
|
177
179
|
if (obj instanceof Object) {
|
|
178
180
|
const copy = {};
|
|
179
181
|
for (const attr in obj) {
|
|
180
|
-
if (Object.prototype.hasOwnProperty.call(obj, attr)) copy[attr] = clone(obj[attr]);
|
|
182
|
+
if (Object.prototype.hasOwnProperty.call(obj, attr)) copy[attr] = clone(obj[attr], attr);
|
|
181
183
|
}
|
|
182
184
|
return copy;
|
|
183
185
|
}
|
package/package.json
CHANGED
package/test/lib/databases.js
CHANGED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const assert = require('assert').strict;
|
|
4
|
+
const ueberdb = require('../index');
|
|
5
|
+
const util = require('util');
|
|
6
|
+
|
|
7
|
+
describe(__filename, function () {
|
|
8
|
+
let db = null;
|
|
9
|
+
|
|
10
|
+
before(async function () {
|
|
11
|
+
const udb = new ueberdb.Database('memory', {}, {});
|
|
12
|
+
db = {};
|
|
13
|
+
for (const fn of ['init', 'close', 'set', 'get']) db[fn] = util.promisify(udb[fn].bind(udb));
|
|
14
|
+
await db.init();
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
after(async function () {
|
|
18
|
+
await db.close();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('no .toJSON method', async function () {
|
|
22
|
+
await db.set('key', {prop: 'value'});
|
|
23
|
+
assert.deepEqual(await db.get('key'), {prop: 'value'});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('direct', async function () {
|
|
27
|
+
await db.set('key', {toJSON: (arg) => `toJSON ${arg}`});
|
|
28
|
+
assert.equal(await db.get('key'), 'toJSON ');
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('object property', async function () {
|
|
32
|
+
await db.set('key', {prop: {toJSON: (arg) => `toJSON ${arg}`}});
|
|
33
|
+
assert.deepEqual(await db.get('key'), {prop: 'toJSON prop'});
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('array entry', async function () {
|
|
37
|
+
await db.set('key', [{toJSON: (arg) => `toJSON ${arg}`}]);
|
|
38
|
+
assert.deepEqual(await db.get('key'), ['toJSON 0']);
|
|
39
|
+
});
|
|
40
|
+
});
|