deepbase-redis 1.0.6
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/LICENSE +21 -0
- package/README.md +97 -0
- package/demo/demo.mjs +54 -0
- package/index.mjs +135 -0
- package/package.json +45 -0
- package/test/test.mjs +136 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Martin Clasen
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# 🌳 DeepBase Redis
|
|
2
|
+
|
|
3
|
+
DeepBaseRedis is an innovative and efficient module designed to seamlessly integrate with Redis Stack, providing a robust solution for managing and interacting with databases. With DeepBaseRedis, you can effortlessly perform CRUD operations and manipulate data stored in Redis Stack (json module) collections.
|
|
4
|
+
|
|
5
|
+
For simplicity you may be interested in the version of DeepBase that persists in JSON files. https://www.npmjs.com/package/deepbase
|
|
6
|
+
|
|
7
|
+
## 📦 Installation
|
|
8
|
+
```shell
|
|
9
|
+
npm install deepbase
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## 🔧 Usage
|
|
13
|
+
```js
|
|
14
|
+
const DeepBase = require("deepbase");
|
|
15
|
+
const mem = new DeepBase({ name: "db" }); // "db" redis prefix
|
|
16
|
+
await mem.connect();
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
### ✍️ Setting Values
|
|
20
|
+
```js
|
|
21
|
+
await mem.set("config", "lang", "en");
|
|
22
|
+
|
|
23
|
+
const configLang = await mem.get("config", "lang");
|
|
24
|
+
console.log(configLang); // "en"
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### ✅ Adding Rows
|
|
28
|
+
```js
|
|
29
|
+
const path = await mem.add("user", { name: "martin" });
|
|
30
|
+
|
|
31
|
+
// add() will create a secure key (ie. "iKid4OCK")
|
|
32
|
+
console.log(path) // [ 'user', 'iKid4OCK' ]
|
|
33
|
+
|
|
34
|
+
const userName = await mem.get(...path, "name");
|
|
35
|
+
console.log(userName); // "martin"
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### 🔢 Increment fields
|
|
39
|
+
```js
|
|
40
|
+
await mem.inc(...path, "balance", 160);
|
|
41
|
+
await mem.inc(...path, "balance", 420);
|
|
42
|
+
|
|
43
|
+
const userBalance = await mem.get(...path, "balance");
|
|
44
|
+
console.log(userBalance); // 580
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### ⚗️ Update
|
|
48
|
+
```js
|
|
49
|
+
await mem.upd("config", "lang", v => v.toUpperCase());
|
|
50
|
+
const lang = await mem.get("config", "lang"); // EN
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### 🔥 Finally
|
|
54
|
+
```js
|
|
55
|
+
await mem.add("user", { name: "anya" });
|
|
56
|
+
|
|
57
|
+
const userIds = await mem.keys("user")
|
|
58
|
+
console.log(userIds) // [ 'iKid4OCKds', 'F3wORv_Jsd' ]
|
|
59
|
+
|
|
60
|
+
console.log(await mem.get())
|
|
61
|
+
// {
|
|
62
|
+
// config: { lang: 'EN' },
|
|
63
|
+
// user: {
|
|
64
|
+
// iKid4OCKds: { name: 'martin', balance: 580 },
|
|
65
|
+
// F3wORv_Jsd: { name: 'anya' }
|
|
66
|
+
// }
|
|
67
|
+
// }
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## 🤯 Features
|
|
71
|
+
- 🔍 Easily access and modify nested objects in JSON storage.
|
|
72
|
+
- 📁 Provides an easy-to-use interface for connecting to Redis JSON and performing data operations, saving development.
|
|
73
|
+
- 🌱 Simple and intuitive API for managing complex JSON structures.
|
|
74
|
+
|
|
75
|
+
## 🤔 Why DeepBase
|
|
76
|
+
- ⚡ Fastest and simplest way to add persistence to your projects.
|
|
77
|
+
- 📖 Offers advanced querying capabilities, nested value retrieval, and seamless update processes.
|
|
78
|
+
- 🧠 Easy to use and understand.
|
|
79
|
+
|
|
80
|
+
## 🤝 Contributing
|
|
81
|
+
Contributions to DeepBase are welcome! If you have an idea or a bug to report, please open an issue. If you would like to contribute to the code, please open a pull request.
|
|
82
|
+
|
|
83
|
+
## 🎬 Conclusion
|
|
84
|
+
DeepBaseRedis is built with efficiency and performance in mind, leveraging the power of the Redis Stack driver and optimizing data access operations. Whether you're building a small-scale application or a complex system, DeepBaseRedis empowers you to interact with Redis Stack effortlessly, making your development process smoother and more efficient.
|
|
85
|
+
|
|
86
|
+
🚀 Try DeepBaseRedis today and experience the convenience and power it brings to your Redis Stack data management workflow!
|
|
87
|
+
|
|
88
|
+
## 📄 License
|
|
89
|
+
The MIT License (MIT)
|
|
90
|
+
|
|
91
|
+
Copyright (c) Martin Clasen
|
|
92
|
+
|
|
93
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
94
|
+
|
|
95
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
96
|
+
|
|
97
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/demo/demo.mjs
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import DeepBase from '../index.mjs';
|
|
2
|
+
|
|
3
|
+
const mem = new DeepBase({ name: "demo" });
|
|
4
|
+
|
|
5
|
+
await mem.connect();
|
|
6
|
+
|
|
7
|
+
// SET
|
|
8
|
+
await mem.set("config", "lang", "en");
|
|
9
|
+
|
|
10
|
+
const configLang = await mem.get("config", "lang");
|
|
11
|
+
console.log(configLang); // "en"
|
|
12
|
+
|
|
13
|
+
// ADD
|
|
14
|
+
const path = await mem.add("user", { name: "martin" });
|
|
15
|
+
console.log(path) // [ 'user', 'iKid4OCKds' ] / iKid4OCKds is a random string
|
|
16
|
+
|
|
17
|
+
const userName = await mem.get(...path, "name");
|
|
18
|
+
console.log(userName); // "martin"
|
|
19
|
+
|
|
20
|
+
// INC
|
|
21
|
+
await mem.inc(...path, "count", 1);
|
|
22
|
+
await mem.inc(...path, "count", 1);
|
|
23
|
+
|
|
24
|
+
const userBalance = await mem.get(...path, "count");
|
|
25
|
+
console.log(userBalance); // 2
|
|
26
|
+
|
|
27
|
+
await mem.add("user", { name: "anya" });
|
|
28
|
+
|
|
29
|
+
const userIds = await mem.keys("user")
|
|
30
|
+
console.log(userIds) // [ 'CqtOILTDUg', 'MXOlTBSmEf' ]
|
|
31
|
+
|
|
32
|
+
// UPDATE
|
|
33
|
+
await mem.upd("config", "lang", v => v.toUpperCase());
|
|
34
|
+
const lang = await mem.get("config", "lang"); // EN
|
|
35
|
+
|
|
36
|
+
console.log({
|
|
37
|
+
config: await mem.get("config"),
|
|
38
|
+
user: await mem.get("user")
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
// {
|
|
42
|
+
// "config": {
|
|
43
|
+
// "lang": "EN"
|
|
44
|
+
// },
|
|
45
|
+
// "user": {
|
|
46
|
+
// "CqtOILTDUg": {
|
|
47
|
+
// "name": "martin",
|
|
48
|
+
// "count": 2
|
|
49
|
+
// },
|
|
50
|
+
// "MXOlTBSmEf": {
|
|
51
|
+
// "name": "anya"
|
|
52
|
+
// }
|
|
53
|
+
// }
|
|
54
|
+
// }
|
package/index.mjs
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { createClient } from 'redis';
|
|
2
|
+
import { customAlphabet } from 'nanoid'
|
|
3
|
+
|
|
4
|
+
export default class DeepBaseRedis {
|
|
5
|
+
|
|
6
|
+
constructor(opts = {}) {
|
|
7
|
+
this.nidAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
8
|
+
this.nidLength = 10;
|
|
9
|
+
this.name = "db";
|
|
10
|
+
this.url = "redis://localhost:6379";
|
|
11
|
+
Object.assign(this, opts)
|
|
12
|
+
|
|
13
|
+
this.nanoid = customAlphabet(this.nidAlphabet, this.nidLength)
|
|
14
|
+
this.client = createClient({ url: this.url });
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async disconnect() {
|
|
18
|
+
await this.client.disconnect();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async connect() {
|
|
22
|
+
await this.client.connect();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async set(...args) {
|
|
26
|
+
if (args.length < 2) return
|
|
27
|
+
const keys = args.slice(0, -1)
|
|
28
|
+
const value = args[args.length - 1]
|
|
29
|
+
|
|
30
|
+
const key = keys.shift()
|
|
31
|
+
const keyPath = keys.length == 0 ? "." : keys.join('.')
|
|
32
|
+
await this._set(key, keyPath, value);
|
|
33
|
+
return args.slice(0, -1)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async _set(key, path, value) {
|
|
37
|
+
|
|
38
|
+
if (value === undefined) {
|
|
39
|
+
await this.client.json.del(this.name + ":" + key, path);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
let r = null;
|
|
44
|
+
try {
|
|
45
|
+
r = await this.client.json.set(this.name + ":" + key, path, value);
|
|
46
|
+
} catch (error) {
|
|
47
|
+
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (r === null) {
|
|
51
|
+
const keys = path.split('.');
|
|
52
|
+
keys.pop();
|
|
53
|
+
const keyPath = keys.length == 0 ? "." : keys.join('.')
|
|
54
|
+
await this._set(key, keyPath, {});
|
|
55
|
+
await this._set(key, path, value);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async get(...args) {
|
|
60
|
+
|
|
61
|
+
if (args.length == 0) return null
|
|
62
|
+
|
|
63
|
+
const key = args.shift()
|
|
64
|
+
const path = args.length == 0 ? "." : args.join('.');
|
|
65
|
+
|
|
66
|
+
let r = null
|
|
67
|
+
try {
|
|
68
|
+
r = await this.client.json.get(this.name + ":" + key, { path });
|
|
69
|
+
} catch (error) {
|
|
70
|
+
|
|
71
|
+
}
|
|
72
|
+
return r;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async keys(...args) {
|
|
76
|
+
const r = await this.get(...args)
|
|
77
|
+
return (r !== null && typeof r === "object") ? Object.keys(r) : [];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async upd(...args) {
|
|
81
|
+
const func = args.pop()
|
|
82
|
+
return this.set(...args, func(await this.get(...args)))
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async inc(...args) {
|
|
86
|
+
|
|
87
|
+
const i = args.pop()
|
|
88
|
+
const key = args.shift()
|
|
89
|
+
const path = args.length == 0 ? "." : args.join('.')
|
|
90
|
+
|
|
91
|
+
try {
|
|
92
|
+
await this.client.json.numIncrBy(this.name + ":" + key, path, i);
|
|
93
|
+
return args.slice(0, -1)
|
|
94
|
+
} catch (error) {
|
|
95
|
+
args.unshift(key)
|
|
96
|
+
return this.upd(...args, n => n + i)
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async dec(...args) {
|
|
101
|
+
const i = args.pop()
|
|
102
|
+
args.push(-i)
|
|
103
|
+
return this.inc(...args)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async del(...args) {
|
|
107
|
+
const key = args.shift()
|
|
108
|
+
const path = args.length == 0 ? "." : args.join('.')
|
|
109
|
+
await this.client.json.del(this.name + ":" + key, path);
|
|
110
|
+
return [key, ...args]
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async add(...keys) {
|
|
114
|
+
const value = keys.pop();
|
|
115
|
+
const id = this.nanoid();
|
|
116
|
+
await this.set(...[...keys, id, value])
|
|
117
|
+
return [...keys, id]
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// const mem = new DeepBaseRedis();
|
|
122
|
+
|
|
123
|
+
// await mem.del('a')
|
|
124
|
+
|
|
125
|
+
// await mem.add('a', 'b', 'c', 1)
|
|
126
|
+
// await mem.add('a', 'b', 'c', 1)
|
|
127
|
+
// await mem.get('a', 'b', 'c')
|
|
128
|
+
|
|
129
|
+
// await mem.set('a', 'b', 'a', 1)
|
|
130
|
+
// await mem.set('a', 'c', 'a', 1)
|
|
131
|
+
// await mem.del('a', 'c', 'a', 1)
|
|
132
|
+
|
|
133
|
+
// console.log(await mem.get('a'))
|
|
134
|
+
|
|
135
|
+
// await client.quit();
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "deepbase-redis",
|
|
3
|
+
"version": "1.0.6",
|
|
4
|
+
"description": "⚡ Fastest and simplest way to add Redis persistence to your projects.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"dependencies": {
|
|
7
|
+
"nanoid": "^5.0.4",
|
|
8
|
+
"redis": "^4.6.12"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"test": "mocha"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/clasen/DeepBaseRedis.git"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"redis",
|
|
19
|
+
"redis-json",
|
|
20
|
+
"redis-stack",
|
|
21
|
+
"json",
|
|
22
|
+
"database",
|
|
23
|
+
"persist",
|
|
24
|
+
"nested",
|
|
25
|
+
"objects",
|
|
26
|
+
"user-friendly",
|
|
27
|
+
"intuitive",
|
|
28
|
+
"reliable",
|
|
29
|
+
"synchronization",
|
|
30
|
+
"structure",
|
|
31
|
+
"lowdb"
|
|
32
|
+
],
|
|
33
|
+
"author": "Martin Clasen",
|
|
34
|
+
"license": "MIT",
|
|
35
|
+
"bugs": {
|
|
36
|
+
"url": "https://github.com/clasen/DeepBaseRedis/issues"
|
|
37
|
+
},
|
|
38
|
+
"homepage": "https://github.com/clasen/DeepBaseRedis#readme",
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"mocha": "^10.2.0"
|
|
41
|
+
},
|
|
42
|
+
"directories": {
|
|
43
|
+
"test": "test"
|
|
44
|
+
}
|
|
45
|
+
}
|
package/test/test.mjs
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/* eslint-env mocha */
|
|
2
|
+
|
|
3
|
+
import DeepBase from '../index.mjs';
|
|
4
|
+
import assert from 'assert';
|
|
5
|
+
|
|
6
|
+
describe('DeepBaseRedis', () => {
|
|
7
|
+
let db;
|
|
8
|
+
|
|
9
|
+
beforeEach(async () => {
|
|
10
|
+
db = new DeepBase({ name: 'test' });
|
|
11
|
+
await db.connect();
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
afterEach(async () => {
|
|
15
|
+
await db.del('foo');
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
describe('#set()', () => {
|
|
19
|
+
it('should set a value at a given path', async () => {
|
|
20
|
+
await db.set('foo', 'bar', 'baz');
|
|
21
|
+
const baz = await db.get('foo', 'bar');
|
|
22
|
+
assert.deepEqual(baz, 'baz');
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('should overwrite an existing value at the same path', async () => {
|
|
26
|
+
await db.set('foo', 'bar', 'baz');
|
|
27
|
+
await db.set('foo', 'bar', 'qux');
|
|
28
|
+
const qux = await db.get('foo', 'bar')
|
|
29
|
+
assert.deepEqual(qux, 'qux');
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('should do nothing if no value is provided', async () => {
|
|
33
|
+
await db.set('foo', 'bar');
|
|
34
|
+
await db.set('foo', 'bar', undefined);
|
|
35
|
+
const udf = await db.get('foo', 'bar');
|
|
36
|
+
assert.deepEqual(udf, undefined);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('should save changes to Redis', async () => {
|
|
40
|
+
await db.set('foo', 'bar', 'baz');
|
|
41
|
+
db = new DeepBase({ name: 'test' }); // create a new instance to reload the saved data
|
|
42
|
+
await db.connect();
|
|
43
|
+
const baz = await db.get('foo', 'bar');
|
|
44
|
+
assert.deepEqual(baz, 'baz');
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe('#get()', () => {
|
|
49
|
+
it('should retrieve a value at a given path', async () => {
|
|
50
|
+
await db.set('foo', 'bar', 'baz');
|
|
51
|
+
assert.deepEqual(await db.get('foo', 'bar'), 'baz');
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('should return null if the value does not exist', async () => {
|
|
55
|
+
assert.deepEqual(await db.get('foo', 'bar'), null);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('should return null if no keys are provided', async () => {
|
|
59
|
+
assert.deepEqual(await db.get(), null);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
describe('#del()', () => {
|
|
64
|
+
it('should delete a value at a given path', async () => {
|
|
65
|
+
await db.set('foo', 'bar', 'baz');
|
|
66
|
+
await db.del('foo', 'bar');
|
|
67
|
+
assert.deepEqual(await db.get('foo', 'bar'), null);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('should do nothing if the value does not exist', async () => {
|
|
71
|
+
await db.del('foo', 'bar');
|
|
72
|
+
assert.deepEqual(await db.get('foo', 'bar'), null);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('should save changes to Redis', async () => {
|
|
76
|
+
await db.set('foo', 'bar', 'baz');
|
|
77
|
+
await db.del('foo', 'bar');
|
|
78
|
+
db = new DeepBase({ name: 'test' }); // create a new instance to reload the saved data
|
|
79
|
+
await db.connect();
|
|
80
|
+
assert.deepEqual(await db.get('foo', 'bar'), null);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
describe('#add()', () => {
|
|
85
|
+
|
|
86
|
+
it('should save changes to Redis', async () => {
|
|
87
|
+
const obj = { bar: 'baz' }
|
|
88
|
+
const path = await db.add('foo', obj);
|
|
89
|
+
assert.deepEqual(await db.get(...path), obj);
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
describe('#inc()', () => {
|
|
94
|
+
|
|
95
|
+
it('should increment a value at a given path', async () => {
|
|
96
|
+
await db.set('foo', 'bar', 1);
|
|
97
|
+
await db.inc('foo', 'bar', 2);
|
|
98
|
+
assert.deepEqual(await db.get('foo', 'bar'), 3);
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
describe('#dec()', () => {
|
|
103
|
+
|
|
104
|
+
it('should save changes to Redis', async () => {
|
|
105
|
+
await db.set('foo', 'bar', 3);
|
|
106
|
+
await db.dec('foo', 'bar', 2);
|
|
107
|
+
db = new DeepBase({ name: 'test' }); // create a new instance to reload the saved data
|
|
108
|
+
await db.connect();
|
|
109
|
+
assert.deepEqual(await db.get('foo', 'bar'), 1);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('should set the value to -1 if it does not exist', async () => {
|
|
113
|
+
await db.dec('foo', 'bar', 2);
|
|
114
|
+
assert.deepEqual(await db.get('foo', 'bar'), -2);
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
describe('#keys()', () => {
|
|
119
|
+
|
|
120
|
+
it('should return keys', async () => {
|
|
121
|
+
await db.set('foo', 'bar', 1);
|
|
122
|
+
await db.set('foo', 'quux', 1);
|
|
123
|
+
assert.deepEqual(await db.keys('foo'), ['bar', 'quux']);
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
describe('#upd()', async () => {
|
|
128
|
+
|
|
129
|
+
it('should update field keys', async () => {
|
|
130
|
+
await db.set('foo', 'bar', 2);
|
|
131
|
+
await db.upd('foo', 'bar', n => n * 3);
|
|
132
|
+
assert.deepEqual(await db.get('foo', 'bar'), 6);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
});
|
|
136
|
+
});
|