model-redis 0.1.3 → 0.2.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/README.md +27 -10
- package/index.js +1 -1
- package/package.json +2 -2
- package/src/redis_model.js +50 -14
package/README.md
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
# Model Redis
|
|
2
2
|
|
|
3
|
-
Simple ORM model for
|
|
4
|
-
This provides a simple ORM interface, with schema, for
|
|
3
|
+
Simple ORM model for Redis in NodsJS. The only external dependence is `redis`.
|
|
4
|
+
This provides a simple ORM interface, with schema, for Redis. This is not meant
|
|
5
5
|
for large data sets and is geared more for small, internal infrastructure based
|
|
6
6
|
projects that do not require complex data model.
|
|
7
7
|
|
|
8
8
|
|
|
9
9
|
## Getting started
|
|
10
10
|
|
|
11
|
-
`setUpTable([object])` -- *Function* to bind the
|
|
11
|
+
`setUpTable([object])` -- *Function* to bind the Redis connection
|
|
12
12
|
object to the ORM table. It takes an optional connected redis client object
|
|
13
|
-
or configuration for the
|
|
13
|
+
or configuration for the Redis module. This will return a `Table` class we
|
|
14
14
|
can use later for our model.
|
|
15
15
|
|
|
16
16
|
It is recommend you place this in a utility or lib file with in your project
|
|
17
17
|
and require it when needed.
|
|
18
18
|
|
|
19
19
|
The simplest way to use this is to pass nothing to the `setUpTable` function.
|
|
20
|
-
this will create a connected client to
|
|
20
|
+
this will create a connected client to Redis using the default settings:
|
|
21
21
|
|
|
22
22
|
```javascript
|
|
23
23
|
'use strict';
|
|
@@ -29,14 +29,14 @@ const Table = setUpTable();
|
|
|
29
29
|
module.exports = Table;
|
|
30
30
|
```
|
|
31
31
|
|
|
32
|
-
You can also pass your own configuration options to the
|
|
32
|
+
You can also pass your own configuration options to the Redis client. See the
|
|
33
33
|
redis [client configuration guide](https://github.com/redis/node-redis/blob/master/docs/client-configuration.md)
|
|
34
34
|
for available options:
|
|
35
35
|
|
|
36
36
|
```javascript
|
|
37
37
|
'use strict';
|
|
38
38
|
|
|
39
|
-
const {setUpTable} = require('model-redis')
|
|
39
|
+
const {setUpTable} = require('model-redis');
|
|
40
40
|
|
|
41
41
|
const conf = {
|
|
42
42
|
socket: {
|
|
@@ -45,7 +45,7 @@ const conf = {
|
|
|
45
45
|
},
|
|
46
46
|
username: admin,
|
|
47
47
|
password: hunter42
|
|
48
|
-
}
|
|
48
|
+
};
|
|
49
49
|
|
|
50
50
|
const Table = setUpTable({redisConf: conf});
|
|
51
51
|
|
|
@@ -53,12 +53,12 @@ module.exports = Table;
|
|
|
53
53
|
```
|
|
54
54
|
|
|
55
55
|
It can also take a Redis client object, if you would like to have more control
|
|
56
|
-
or use a custom version on
|
|
56
|
+
or use a custom version on Redis.
|
|
57
57
|
|
|
58
58
|
```javascript
|
|
59
59
|
'use strict';
|
|
60
60
|
|
|
61
|
-
const {setUpTable} = require('model-redis')
|
|
61
|
+
const {setUpTable} = require('model-redis');
|
|
62
62
|
|
|
63
63
|
const {createClient} = require('redis');
|
|
64
64
|
const client = createClient();
|
|
@@ -70,6 +70,23 @@ module.exports = Table;
|
|
|
70
70
|
|
|
71
71
|
```
|
|
72
72
|
|
|
73
|
+
### Prefix key
|
|
74
|
+
|
|
75
|
+
At some point, the Redis package removed the option to prefix a string to the
|
|
76
|
+
keys. This functionally has been added back with this package
|
|
77
|
+
|
|
78
|
+
```javascript
|
|
79
|
+
'use strict';
|
|
80
|
+
|
|
81
|
+
const {setUpTable} = require('model-redis');
|
|
82
|
+
|
|
83
|
+
const Table = setUpTable({
|
|
84
|
+
prefix: 'auth_app'
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
module.exports = Table;
|
|
88
|
+
```
|
|
89
|
+
|
|
73
90
|
Once we have have our table object, we can start building using the ORM!
|
|
74
91
|
|
|
75
92
|
## ORM API
|
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "model-redis",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Simple ORM model for redis in NodsJS",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -22,6 +22,6 @@
|
|
|
22
22
|
},
|
|
23
23
|
"homepage": "https://github.com/wmantly/model-redis#readme",
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"redis": "^4.
|
|
25
|
+
"redis": "^4.6.10"
|
|
26
26
|
}
|
|
27
27
|
}
|
package/src/redis_model.js
CHANGED
|
@@ -3,7 +3,12 @@
|
|
|
3
3
|
const objValidate = require('./object_validate');
|
|
4
4
|
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
|
|
7
|
+
function setUpTable(client, prefix=''){
|
|
8
|
+
|
|
9
|
+
function redisPrefix(key){
|
|
10
|
+
return `${prefix}${key}`;
|
|
11
|
+
}
|
|
7
12
|
|
|
8
13
|
class Table{
|
|
9
14
|
static _indexed = [];
|
|
@@ -21,7 +26,11 @@ function setUpTable(client){
|
|
|
21
26
|
index = index[this._key]
|
|
22
27
|
}
|
|
23
28
|
|
|
24
|
-
|
|
29
|
+
console.log('get', redisPrefix(`${this.prototype.constructor.name}_${index}`))
|
|
30
|
+
|
|
31
|
+
let result = await client.HGETALL(
|
|
32
|
+
redisPrefix(`${this.prototype.constructor.name}_${index}`)
|
|
33
|
+
);
|
|
25
34
|
|
|
26
35
|
if(Object.keys(result).length === 0){
|
|
27
36
|
let error = new Error('EntryNotFound');
|
|
@@ -57,13 +66,21 @@ function setUpTable(client){
|
|
|
57
66
|
// return a list of all the index keys for this table.
|
|
58
67
|
try{
|
|
59
68
|
|
|
69
|
+
|
|
70
|
+
console.log('here')
|
|
71
|
+
|
|
60
72
|
if(index_key && !this._indexed.includes(index_key)) return [];
|
|
73
|
+
console.log('here2', redisPrefix(`${this.prototype.constructor.name}_${index_key}_${value}`))
|
|
61
74
|
|
|
62
75
|
if(index_key && this._indexed.includes(index_key)){
|
|
63
|
-
return await client.SMEMBERS(
|
|
76
|
+
return await client.SMEMBERS(
|
|
77
|
+
redisPrefix(`${this.prototype.constructor.name}_${index_key}_${value}`)
|
|
78
|
+
);
|
|
64
79
|
}
|
|
80
|
+
console.log('here3', redisPrefix(this.prototype.constructor.name))
|
|
65
81
|
|
|
66
|
-
return await client.SMEMBERS(
|
|
82
|
+
return await client.SMEMBERS(
|
|
83
|
+
redisPrefix(this.prototype.constructor.name));
|
|
67
84
|
|
|
68
85
|
}catch(error){
|
|
69
86
|
throw error;
|
|
@@ -99,19 +116,25 @@ function setUpTable(client){
|
|
|
99
116
|
}
|
|
100
117
|
|
|
101
118
|
// Add the key to the members for this redis table
|
|
102
|
-
await client.SADD(
|
|
119
|
+
await client.SADD(
|
|
120
|
+
redisPrefix(this.prototype.constructor.name),
|
|
121
|
+
String(data[this._key])
|
|
122
|
+
);
|
|
103
123
|
|
|
104
124
|
// Create index keys lists
|
|
105
125
|
for(let index of this._indexed){
|
|
106
126
|
if(data[index]) await client.SADD(
|
|
107
|
-
`${this.prototype.constructor.name}_${index}_${data[index]}
|
|
127
|
+
redisPrefix(`${this.prototype.constructor.name}_${index}_${data[index]}`),
|
|
108
128
|
String(data[this._key]
|
|
109
129
|
));
|
|
110
130
|
}
|
|
111
131
|
|
|
112
132
|
// Add the values for this entry.
|
|
113
133
|
for(let key of Object.keys(data)){
|
|
114
|
-
await client.HSET(
|
|
134
|
+
await client.HSET(
|
|
135
|
+
redisPrefix(`${this.prototype.constructor.name}_${data[this._key]}`),
|
|
136
|
+
key, objValidate.parseToString(data[key])
|
|
137
|
+
);
|
|
115
138
|
}
|
|
116
139
|
|
|
117
140
|
// return the created redis entry as entry instance.
|
|
@@ -147,12 +170,12 @@ function setUpTable(client){
|
|
|
147
170
|
for(let index of this.constructor._indexed){
|
|
148
171
|
if(data[index]){
|
|
149
172
|
await client.SREM(
|
|
150
|
-
`${this.constructor.name}_${index}_${this[index]}
|
|
173
|
+
redisPrefix(`${this.constructor.name}_${index}_${this[index]}`),
|
|
151
174
|
String(this[this.constructor._key])
|
|
152
175
|
);
|
|
153
176
|
|
|
154
177
|
await client.SADD(
|
|
155
|
-
`${this.constructor.name}_${index}_${data[index]}
|
|
178
|
+
redisPrefix(`${this.constructor.name}_${index}_${data[index]}`),
|
|
156
179
|
String(data[this.constructor._key] || this[this.constructor._key])
|
|
157
180
|
);
|
|
158
181
|
}
|
|
@@ -161,7 +184,10 @@ function setUpTable(client){
|
|
|
161
184
|
// Loop over the data fields and apply them to redis
|
|
162
185
|
for(let key of Object.keys(data)){
|
|
163
186
|
this[key] = data[key];
|
|
164
|
-
await client.HSET(
|
|
187
|
+
await client.HSET(
|
|
188
|
+
redisPrefix(`${this.constructor.name}_${this[this.constructor._key]}`),
|
|
189
|
+
key, data[key]
|
|
190
|
+
);
|
|
165
191
|
}
|
|
166
192
|
}
|
|
167
193
|
|
|
@@ -179,14 +205,23 @@ function setUpTable(client){
|
|
|
179
205
|
try{
|
|
180
206
|
// Remove the index key from the tables members list.
|
|
181
207
|
|
|
182
|
-
await client.SREM(
|
|
208
|
+
await client.SREM(
|
|
209
|
+
redisPrefix(this.constructor.name),
|
|
210
|
+
this[this.constructor._key]
|
|
211
|
+
);
|
|
183
212
|
|
|
184
213
|
for(let index of this.constructor._indexed){
|
|
185
|
-
await client.SREM(
|
|
214
|
+
await client.SREM(
|
|
215
|
+
redisPrefix(`${this.constructor.name}_${index}_${data[value]}`),
|
|
216
|
+
data[this.constructor._key]
|
|
217
|
+
);
|
|
186
218
|
}
|
|
187
219
|
|
|
188
220
|
// Remove the entries hash values.
|
|
189
|
-
let count = await client.DEL(
|
|
221
|
+
let count = await client.DEL(
|
|
222
|
+
redisPrefix(
|
|
223
|
+
`${this.constructor.name}_${this[this.constructor._key]}`)
|
|
224
|
+
);
|
|
190
225
|
|
|
191
226
|
// Return the number of removed values to the caller.
|
|
192
227
|
return count;
|
|
@@ -195,8 +230,9 @@ function setUpTable(client){
|
|
|
195
230
|
throw error;
|
|
196
231
|
}
|
|
197
232
|
};
|
|
198
|
-
|
|
199
233
|
}
|
|
234
|
+
|
|
235
|
+
return Table;
|
|
200
236
|
}
|
|
201
237
|
|
|
202
238
|
module.exports = setUpTable;
|