model-redis 0.1.2 → 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/README.md CHANGED
@@ -93,14 +93,13 @@ class User extends Table{
93
93
  'updated_on': {default: function(){return (new Date).getTime()}, always: true},
94
94
  'username': {isRequired: true, type: 'string', min: 3, max: 500},
95
95
  'password': {isRequired: true, type: 'string', min: 3, max: 500},
96
- }
96
+ };
97
97
 
98
98
  static async add(data) {
99
99
  try{
100
100
  data['password'] = await bcrypt.hash(data['password'], saltRounds);
101
101
 
102
- return await super.add(data)
103
-
102
+ return await super.add(data);
104
103
  }catch(error){
105
104
  throw error;
106
105
  }
@@ -118,12 +117,11 @@ class User extends Table{
118
117
 
119
118
  static async login(data){
120
119
  try{
121
-
122
120
  let user = await User.get(data);
123
121
  let auth = await bcrypt.compare(data.password, user.password);
124
122
 
125
123
  if(auth){
126
- return user
124
+ return user;
127
125
  }else{
128
126
  throw new Error("LogginFailed");
129
127
  }
@@ -142,13 +140,13 @@ module.exports = {User};
142
140
  The table schema a required aspect of using this module. The schema is defined
143
141
  with `_key`, `_indexed` and `_keyMap`
144
142
 
145
- `static _key` *string* is required and is basically the primary key for this
143
+ * `static _key` *string* is required and is basically the primary key for this
146
144
  table. It MUST match one of the keys in the `_keyMap` schema
147
145
 
148
- `static _indexed` *array* is optional list of keys to be indexed. Indexed keys
146
+ * `static _indexed` *array* is optional list of keys to be indexed. Indexed keys
149
147
  can be searched by with the `list()` and `listDetial()` methods.
150
148
 
151
- `static _keyMap` *object* is required and defines the allowed schema for the
149
+ * `static _keyMap` *object* is required and defines the allowed schema for the
152
150
  table. Validation will be enforced based on what is defined in the schema.
153
151
 
154
152
  The `_keyMap` schema is an object where the key is the name of the field and the
@@ -162,10 +160,12 @@ value is an object with the options for that field:
162
160
 
163
161
  * `type` *string* Required The native type this field will be checked for, valid
164
162
  types are:
165
- * `string`
166
- * `number`
167
- * `boolean`
168
- * `object`
163
+
164
+ * `string`
165
+ * `number`
166
+ * `boolean`
167
+ * `object`
168
+
169
169
  * `isRequired` *boolean* If this is set to true, this must be set when a new
170
170
  entry is created. This has no effect on updates.
171
171
  * `default` *field type or function* if nothing is passed, this will be used be
@@ -174,6 +174,8 @@ value is an object with the options for that field:
174
174
  * `always` *boolean* If this is set, the `default` is set, then its value will
175
175
  always be used when calling update. This is useful for setting an updated on
176
176
  field or access count.
177
+ * `min` *number* Used with *string* or *number* type to define the lower limit
178
+ * `max` *number* Used with *string* or *number* type to define the max limit
177
179
 
178
180
  Once we have defined a `_keyMap` schema, the table can be used.
179
181
 
package/index.js CHANGED
@@ -15,7 +15,7 @@ function setUpTable(obj){
15
15
 
16
16
  // test client connection
17
17
 
18
- return table(client);
18
+ return table(client, obj.prefix);
19
19
  }
20
20
 
21
21
  module.exports = {client, setUpTable};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "model-redis",
3
- "version": "0.1.2",
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.0.1"
25
+ "redis": "^4.6.10"
26
26
  }
27
27
  }
@@ -3,7 +3,12 @@
3
3
  const objValidate = require('./object_validate');
4
4
 
5
5
 
6
- function setUpTable(client){
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
- let result = await client.HGETALL(`${this.prototype.constructor.name}_${index}`);
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(`${this.prototype.constructor.name}_${index_key}_${value}`);
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(this.prototype.constructor.name);
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(this.prototype.constructor.name, String(data[this._key]));
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(`${this.prototype.constructor.name}_${data[this._key]}`, key, objValidate.parseToString(data[key]));
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(`${this.constructor.name}_${this[this.constructor._key]}`, key, data[key]);
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(this.constructor.name, this[this.constructor._key]);
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(`${this.constructor.name}_${index}_${data[value]}`, data[this.constructor._key]);
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(`${this.constructor.name}_${this[this.constructor._key]}`);
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;