deepbase-redis 1.1.1 → 1.2.2
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.cjs +1 -0
- package/index.js +43 -38
- package/package.json +14 -5
- package/test/{test.mjs → test.js} +9 -0
- /package/demo/{demo.mjs → demo.js} +0 -0
package/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('./index.js').default;
|
package/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { createClient } from "redis";
|
|
2
|
+
import { customAlphabet } from "nanoid";
|
|
3
3
|
|
|
4
4
|
class DeepBaseRedis {
|
|
5
5
|
|
|
@@ -9,8 +9,8 @@ class DeepBaseRedis {
|
|
|
9
9
|
this.name = "db";
|
|
10
10
|
this.url = "redis://localhost:6379";
|
|
11
11
|
this.client = createClient({ url: this.url });
|
|
12
|
-
this.nanoid = customAlphabet(this.nidAlphabet, this.nidLength)
|
|
13
|
-
Object.assign(this, opts)
|
|
12
|
+
this.nanoid = customAlphabet(this.nidAlphabet, this.nidLength);
|
|
13
|
+
Object.assign(this, opts);
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
async disconnect() {
|
|
@@ -22,14 +22,14 @@ class DeepBaseRedis {
|
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
async set(...args) {
|
|
25
|
-
if (args.length < 2) return
|
|
26
|
-
const keys = args.slice(0, -1)
|
|
27
|
-
const value = args[args.length - 1]
|
|
25
|
+
if (args.length < 2) return;
|
|
26
|
+
const keys = args.slice(0, -1);
|
|
27
|
+
const value = args[args.length - 1];
|
|
28
28
|
|
|
29
|
-
const key = keys.shift()
|
|
30
|
-
const keyPath = keys.length == 0 ? "." : keys.join(
|
|
29
|
+
const key = keys.shift();
|
|
30
|
+
const keyPath = keys.length == 0 ? "." : keys.join(".");
|
|
31
31
|
await this._set(key, keyPath, value);
|
|
32
|
-
return args.slice(0, -1)
|
|
32
|
+
return args.slice(0, -1);
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
async _set(key, path, value) {
|
|
@@ -49,7 +49,7 @@ class DeepBaseRedis {
|
|
|
49
49
|
if (r === null) {
|
|
50
50
|
const keys = path.split('.');
|
|
51
51
|
keys.pop();
|
|
52
|
-
const keyPath = keys.length == 0 ? "." : keys.join(
|
|
52
|
+
const keyPath = keys.length == 0 ? "." : keys.join(".");
|
|
53
53
|
await this._set(key, keyPath, {});
|
|
54
54
|
await this._set(key, path, value);
|
|
55
55
|
}
|
|
@@ -59,17 +59,17 @@ class DeepBaseRedis {
|
|
|
59
59
|
|
|
60
60
|
if (args.length === 0) {
|
|
61
61
|
|
|
62
|
-
const dic = {}
|
|
62
|
+
const dic = {};
|
|
63
63
|
for (let key of await this._zeroKeys()) {
|
|
64
64
|
dic[key] = await this.get(key);
|
|
65
65
|
}
|
|
66
66
|
return dic;
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
const key = args.shift()
|
|
70
|
-
const path = args.length == 0 ? "." : args.join(
|
|
69
|
+
const key = args.shift();
|
|
70
|
+
const path = args.length == 0 ? "." : args.join(".");
|
|
71
71
|
|
|
72
|
-
let r = null
|
|
72
|
+
let r = null;
|
|
73
73
|
try {
|
|
74
74
|
r = await this.client.json.get(this.name + ":" + key, { path });
|
|
75
75
|
} catch (error) {
|
|
@@ -79,49 +79,54 @@ class DeepBaseRedis {
|
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
async keys(...args) {
|
|
82
|
-
const r = await this.get(...args)
|
|
82
|
+
const r = await this.get(...args);
|
|
83
83
|
return (r !== null && typeof r === "object") ? Object.keys(r) : [];
|
|
84
84
|
}
|
|
85
85
|
|
|
86
86
|
async values(...args) {
|
|
87
|
-
const r = await this.get(...args)
|
|
87
|
+
const r = await this.get(...args);
|
|
88
88
|
return (r !== null && typeof r === "object") ? Object.values(r) : [];
|
|
89
89
|
}
|
|
90
90
|
|
|
91
|
+
async entries(...args) {
|
|
92
|
+
const r = await this.get(...args);
|
|
93
|
+
return (r !== null && typeof r === "object") ? Object.entries(r) : [];
|
|
94
|
+
}
|
|
95
|
+
|
|
91
96
|
async upd(...args) {
|
|
92
|
-
const func = args.pop()
|
|
93
|
-
return this.set(...args, func(await this.get(...args)))
|
|
97
|
+
const func = args.pop();
|
|
98
|
+
return this.set(...args, func(await this.get(...args)));
|
|
94
99
|
}
|
|
95
100
|
|
|
96
101
|
async inc(...args) {
|
|
97
102
|
|
|
98
|
-
const i = args.pop()
|
|
99
|
-
const key = args.shift()
|
|
100
|
-
const path = args.length == 0 ? "." : args.join(
|
|
103
|
+
const i = args.pop();
|
|
104
|
+
const key = args.shift();
|
|
105
|
+
const path = args.length == 0 ? "." : args.join(".");
|
|
101
106
|
|
|
102
107
|
try {
|
|
103
108
|
await this.client.json.numIncrBy(this.name + ":" + key, path, i);
|
|
104
|
-
return args.slice(0, -1)
|
|
109
|
+
return args.slice(0, -1);
|
|
105
110
|
} catch (error) {
|
|
106
|
-
args.unshift(key)
|
|
107
|
-
return this.upd(...args, n => n + i)
|
|
111
|
+
args.unshift(key);
|
|
112
|
+
return this.upd(...args, n => n + i);
|
|
108
113
|
}
|
|
109
114
|
}
|
|
110
115
|
|
|
111
116
|
async dec(...args) {
|
|
112
|
-
const i = args.pop()
|
|
113
|
-
args.push(-i)
|
|
114
|
-
return this.inc(...args)
|
|
117
|
+
const i = args.pop();
|
|
118
|
+
args.push(-i);
|
|
119
|
+
return this.inc(...args);
|
|
115
120
|
}
|
|
116
121
|
|
|
117
122
|
async _zeroKeys() {
|
|
118
123
|
const scan = {
|
|
119
|
-
TYPE:
|
|
120
|
-
MATCH: this.name +
|
|
124
|
+
TYPE: "ReJSON-RL",
|
|
125
|
+
MATCH: this.name + ":*",
|
|
121
126
|
COUNT: 1000,
|
|
122
127
|
}
|
|
123
128
|
|
|
124
|
-
const keys = []
|
|
129
|
+
const keys = [];
|
|
125
130
|
for await (let key of this.client.scanIterator(scan)) {
|
|
126
131
|
keys.push(key.substring(this.name.length + 1));
|
|
127
132
|
}
|
|
@@ -138,27 +143,27 @@ class DeepBaseRedis {
|
|
|
138
143
|
}
|
|
139
144
|
}
|
|
140
145
|
|
|
141
|
-
const key = args.shift()
|
|
142
|
-
const path = args.length == 0 ? "." : args.join(
|
|
146
|
+
const key = args.shift();
|
|
147
|
+
const path = args.length == 0 ? "." : args.join(".");
|
|
143
148
|
await this.client.json.del(this.name + ":" + key, path);
|
|
144
|
-
return [key, ...args]
|
|
149
|
+
return [key, ...args];
|
|
145
150
|
}
|
|
146
151
|
|
|
147
152
|
async add(...keys) {
|
|
148
153
|
const value = keys.pop();
|
|
149
154
|
const id = this.nanoid();
|
|
150
|
-
await this.set(...[...keys, id, value])
|
|
151
|
-
return [...keys, id]
|
|
155
|
+
await this.set(...[...keys, id, value]);
|
|
156
|
+
return [...keys, id];
|
|
152
157
|
}
|
|
153
158
|
|
|
154
159
|
use(plugin) {
|
|
155
160
|
const prototype = Object.getPrototypeOf(plugin);
|
|
156
161
|
Object.getOwnPropertyNames(prototype).forEach(method => {
|
|
157
|
-
if (method !==
|
|
162
|
+
if (method !== "constructor") {
|
|
158
163
|
this[method] = plugin[method].bind(this);
|
|
159
164
|
}
|
|
160
165
|
});
|
|
161
166
|
}
|
|
162
167
|
}
|
|
163
168
|
|
|
164
|
-
|
|
169
|
+
export default DeepBaseRedis;
|
package/package.json
CHANGED
|
@@ -1,11 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "deepbase-redis",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.2",
|
|
4
4
|
"description": "⚡ Fastest and simplest way to add Redis Stack persistence to your projects.",
|
|
5
5
|
"main": "index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"module": "./index.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"require": "./index.cjs",
|
|
11
|
+
"import": "./index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
6
14
|
"dependencies": {
|
|
7
|
-
"nanoid": "^
|
|
8
|
-
"redis": "^4.
|
|
15
|
+
"nanoid": "^5.1.5",
|
|
16
|
+
"redis": "^4.7.0"
|
|
9
17
|
},
|
|
10
18
|
"scripts": {
|
|
11
19
|
"test": "mocha"
|
|
@@ -28,7 +36,8 @@
|
|
|
28
36
|
"reliable",
|
|
29
37
|
"synchronization",
|
|
30
38
|
"structure",
|
|
31
|
-
"lowdb"
|
|
39
|
+
"lowdb",
|
|
40
|
+
"clasen"
|
|
32
41
|
],
|
|
33
42
|
"author": "Martin Clasen",
|
|
34
43
|
"license": "MIT",
|
|
@@ -37,7 +46,7 @@
|
|
|
37
46
|
},
|
|
38
47
|
"homepage": "https://github.com/clasen/DeepBaseRedis#readme",
|
|
39
48
|
"devDependencies": {
|
|
40
|
-
"mocha": "^
|
|
49
|
+
"mocha": "^11.1.0"
|
|
41
50
|
},
|
|
42
51
|
"directories": {
|
|
43
52
|
"test": "test"
|
|
@@ -136,6 +136,15 @@ describe('DeepBaseRedis', () => {
|
|
|
136
136
|
});
|
|
137
137
|
});
|
|
138
138
|
|
|
139
|
+
describe('#entries()', () => {
|
|
140
|
+
|
|
141
|
+
it('should return entries', async () => {
|
|
142
|
+
await db.set('foo', 'bar', 1);
|
|
143
|
+
await db.set('foo', 'quux', 2);
|
|
144
|
+
assert.deepEqual(await db.entries('foo'), [['bar', 1], ['quux', 2]]);
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
|
|
139
148
|
describe('#upd()', async () => {
|
|
140
149
|
|
|
141
150
|
it('should update field keys', async () => {
|
|
File without changes
|