model-redis 0.1.1 → 0.1.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/README.md +203 -0
- package/index.js +8 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,3 +4,206 @@ Simple ORM model for redis in NodsJS. The only external dependence is `redis`.
|
|
|
4
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
|
+
|
|
8
|
+
|
|
9
|
+
## Getting started
|
|
10
|
+
|
|
11
|
+
`setUpTable([object])` -- *Function* to bind the redis connection
|
|
12
|
+
object to the ORM table. It takes an optional connected redis client object
|
|
13
|
+
or configuration for the redis module. This will return a `Table` class we
|
|
14
|
+
can use later for our model.
|
|
15
|
+
|
|
16
|
+
It is recommend you place this in a utility or lib file with in your project
|
|
17
|
+
and require it when needed.
|
|
18
|
+
|
|
19
|
+
The simplest way to use this is to pass nothing to the `setUpTable` function.
|
|
20
|
+
this will create a connected client to redis using the default settings:
|
|
21
|
+
|
|
22
|
+
```javascript
|
|
23
|
+
'use strict';
|
|
24
|
+
|
|
25
|
+
const {setUpTable} = require('model-redis')
|
|
26
|
+
|
|
27
|
+
const Table = setUpTable();
|
|
28
|
+
|
|
29
|
+
module.exports = Table;
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
You can also pass your own configuration options to the redis client. See the
|
|
33
|
+
redis [client configuration guide](https://github.com/redis/node-redis/blob/master/docs/client-configuration.md)
|
|
34
|
+
for available options:
|
|
35
|
+
|
|
36
|
+
```javascript
|
|
37
|
+
'use strict';
|
|
38
|
+
|
|
39
|
+
const {setUpTable} = require('model-redis')
|
|
40
|
+
|
|
41
|
+
const conf = {
|
|
42
|
+
socket: {
|
|
43
|
+
host: '10.10.10.10'
|
|
44
|
+
port: 7676
|
|
45
|
+
},
|
|
46
|
+
username: admin,
|
|
47
|
+
password: hunter42
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const Table = setUpTable({redisConf: conf});
|
|
51
|
+
|
|
52
|
+
module.exports = Table;
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
It can also take a Redis client object, if you would like to have more control
|
|
56
|
+
or use a custom version on redis.
|
|
57
|
+
|
|
58
|
+
```javascript
|
|
59
|
+
'use strict';
|
|
60
|
+
|
|
61
|
+
const {setUpTable} = require('model-redis')
|
|
62
|
+
|
|
63
|
+
const {createClient} = require('redis');
|
|
64
|
+
const client = createClient();
|
|
65
|
+
client.connect();
|
|
66
|
+
|
|
67
|
+
const Table = setUpTable({redisClient: client});
|
|
68
|
+
|
|
69
|
+
module.exports = Table;
|
|
70
|
+
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Once we have have our table object, we can start building using the ORM!
|
|
74
|
+
|
|
75
|
+
## ORM API
|
|
76
|
+
|
|
77
|
+
The Table class implements static and bound functions to perform normal ORM
|
|
78
|
+
operations. For the rest of these examples, we will implement a simple user
|
|
79
|
+
backing. This will show some usage and extenabilty:
|
|
80
|
+
|
|
81
|
+
``` javascript
|
|
82
|
+
const Table = require('../utils/redis_model'); // Path to where the 'model-redis module is loaded and configured'
|
|
83
|
+
const {Token, InviteToken} = require('./token');
|
|
84
|
+
const bcrypt = require('bcrypt'); // We will use this for passwords later
|
|
85
|
+
const saltRounds = 10;
|
|
86
|
+
|
|
87
|
+
class User extends Table{
|
|
88
|
+
static _key = 'username';
|
|
89
|
+
static _keyMap = {
|
|
90
|
+
'created_by': {isRequired: true, type: 'string', min: 3, max: 500},
|
|
91
|
+
'created_on': {default: function(){return (new Date).getTime()}},
|
|
92
|
+
'updated_by': {default:"__NONE__", type: 'string',},
|
|
93
|
+
'updated_on': {default: function(){return (new Date).getTime()}, always: true},
|
|
94
|
+
'username': {isRequired: true, type: 'string', min: 3, max: 500},
|
|
95
|
+
'password': {isRequired: true, type: 'string', min: 3, max: 500},
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
static async add(data) {
|
|
99
|
+
try{
|
|
100
|
+
data['password'] = await bcrypt.hash(data['password'], saltRounds);
|
|
101
|
+
|
|
102
|
+
return await super.add(data)
|
|
103
|
+
|
|
104
|
+
}catch(error){
|
|
105
|
+
throw error;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async setPassword(data){
|
|
110
|
+
try{
|
|
111
|
+
data['password'] = await bcrypt.hash(data['password'], saltRounds);
|
|
112
|
+
|
|
113
|
+
return this.update(data);
|
|
114
|
+
}catch(error){
|
|
115
|
+
throw error;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
static async login(data){
|
|
120
|
+
try{
|
|
121
|
+
|
|
122
|
+
let user = await User.get(data);
|
|
123
|
+
let auth = await bcrypt.compare(data.password, user.password);
|
|
124
|
+
|
|
125
|
+
if(auth){
|
|
126
|
+
return user
|
|
127
|
+
}else{
|
|
128
|
+
throw new Error("LogginFailed");
|
|
129
|
+
}
|
|
130
|
+
}catch(error){
|
|
131
|
+
throw new Error("LogginFailed")
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
module.exports = {User};
|
|
137
|
+
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### Table schema
|
|
141
|
+
|
|
142
|
+
The table schema a required aspect of using this module. The schema is defined
|
|
143
|
+
with `_key`, `_indexed` and `_keyMap`
|
|
144
|
+
|
|
145
|
+
`static _key` *string* is required and is basically the primary key for this
|
|
146
|
+
table. It MUST match one of the keys in the `_keyMap` schema
|
|
147
|
+
|
|
148
|
+
`static _indexed` *array* is optional list of keys to be indexed. Indexed keys
|
|
149
|
+
can be searched by with the `list()` and `listDetial()` methods.
|
|
150
|
+
|
|
151
|
+
`static _keyMap` *object* is required and defines the allowed schema for the
|
|
152
|
+
table. Validation will be enforced based on what is defined in the schema.
|
|
153
|
+
|
|
154
|
+
The `_keyMap` schema is an object where the key is the name of the field and the
|
|
155
|
+
value is an object with the options for that field:
|
|
156
|
+
```javascript
|
|
157
|
+
'username': {isRequired: true, type: 'string', min: 3, max: 500}
|
|
158
|
+
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
#### Field options:
|
|
162
|
+
|
|
163
|
+
* `type` *string* Required The native type this field will be checked for, valid
|
|
164
|
+
types are:
|
|
165
|
+
* `string`
|
|
166
|
+
* `number`
|
|
167
|
+
* `boolean`
|
|
168
|
+
* `object`
|
|
169
|
+
* `isRequired` *boolean* If this is set to true, this must be set when a new
|
|
170
|
+
entry is created. This has no effect on updates.
|
|
171
|
+
* `default` *field type or function* if nothing is passed, this will be used be
|
|
172
|
+
used. If a function is placed here, it will be called and its return value
|
|
173
|
+
used.
|
|
174
|
+
* `always` *boolean* If this is set, the `default` is set, then its value will
|
|
175
|
+
always be used when calling update. This is useful for setting an updated on
|
|
176
|
+
field or access count.
|
|
177
|
+
|
|
178
|
+
Once we have defined a `_keyMap` schema, the table can be used.
|
|
179
|
+
|
|
180
|
+
#### Methods
|
|
181
|
+
|
|
182
|
+
Static methods are used to query data and create new entries.
|
|
183
|
+
|
|
184
|
+
* `await add(data)` Creates and returns a new entry. The passed data object
|
|
185
|
+
will be validated and a validation error(complete will all the key errors)
|
|
186
|
+
will be thrown if validation fails. Any key passed in the data object that
|
|
187
|
+
is not in the `_keyMap` schema will be dropped.
|
|
188
|
+
|
|
189
|
+
* `await list([index_field, [index value]])` Returns a list of the primary keys in
|
|
190
|
+
the table. If you pass `index_field` and `index_value`, only those matching
|
|
191
|
+
will be returned.
|
|
192
|
+
|
|
193
|
+
* `await listDetial([index_field, [index value]])` same as `list`, but will
|
|
194
|
+
return a list of Table instances.
|
|
195
|
+
|
|
196
|
+
* `await get(pk)` returns a Table instance for the passed object. If none is,
|
|
197
|
+
found a not found error is thrown
|
|
198
|
+
|
|
199
|
+
* `await exists(pk)` Returns `true` or `false` if the passed PK exists.
|
|
200
|
+
|
|
201
|
+
Instances of a Table have the following methods:
|
|
202
|
+
|
|
203
|
+
* `await update(data)` updates the current instance with the newly passed data
|
|
204
|
+
and returns a new instance with the updated data. Data validation is also.
|
|
205
|
+
|
|
206
|
+
* `await remove()` Deletes the current Table instance and returns the delete
|
|
207
|
+
count, this should be 1.
|
|
208
|
+
|
|
209
|
+
All of these methods are extendable so proper business logic can be implemented.
|
package/index.js
CHANGED
|
@@ -1,20 +1,21 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
const
|
|
2
|
+
const table = require('./src/redis_model')
|
|
3
3
|
var client = null
|
|
4
4
|
|
|
5
|
-
function
|
|
5
|
+
function setUpTable(obj){
|
|
6
|
+
obj = obj || {};
|
|
6
7
|
|
|
7
|
-
if(
|
|
8
|
-
client =
|
|
8
|
+
if(obj.redisClient){
|
|
9
|
+
client = obj.redisClient;
|
|
9
10
|
}else{
|
|
10
11
|
const {createClient} = require('redis');
|
|
11
|
-
client = createClient(
|
|
12
|
+
client = createClient(obj.redisConf || {});
|
|
12
13
|
client.connect();
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
// test client connection
|
|
16
17
|
|
|
17
|
-
return
|
|
18
|
+
return table(client);
|
|
18
19
|
}
|
|
19
20
|
|
|
20
|
-
module.exports = {
|
|
21
|
+
module.exports = {client, setUpTable};
|