model-redis 0.1.3 → 0.2.0
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.js +1 -1
- package/package.json +2 -2
- package/src/redis_model.js +50 -14
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "model-redis",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
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;
|