model-redis 0.3.0 → 0.4.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
@@ -7,6 +7,7 @@ projects that do not require complex data models.
7
7
 
8
8
  ## Features
9
9
 
10
+ - 📦 **CommonJS & ESM compatible** - Works seamlessly with both module systems
10
11
  - 📋 Schema-based validation with type checking
11
12
  - 🔑 Primary key and indexed field support
12
13
  - 🔗 Model relationships (one-to-one, one-to-many)
@@ -23,15 +24,20 @@ npm install model-redis
23
24
 
24
25
  ## Getting Started
25
26
 
26
- `setUpTable([object])` - *Async Function* to bind the Redis connection
27
+ `setUpTable([object])` - Function to bind the Redis connection
27
28
  to the ORM table. It takes an optional connected redis client object
28
29
  or configuration for the Redis module. This will return a `Table` class we
29
30
  can use later for our models.
30
31
 
32
+ The function returns synchronously, making it compatible with both CommonJS and ESM.
33
+ Redis connection happens in the background, and operations automatically await the connection.
34
+
31
35
  It is recommended you place this in a utility or lib file within your project
32
36
  and require it when needed.
33
37
 
34
- The simplest way to use this is to pass nothing to the `setUpTable` function.
38
+ ### CommonJS Usage
39
+
40
+ The simplest way to use this in CommonJS is to pass nothing to the `setUpTable` function.
35
41
  This will create a connected client to Redis using the default settings:
36
42
 
37
43
  ```javascript
@@ -39,11 +45,23 @@ This will create a connected client to Redis using the default settings:
39
45
 
40
46
  const {setUpTable} = require('model-redis');
41
47
 
42
- const Table = await setUpTable();
48
+ const Table = setUpTable();
43
49
 
44
50
  module.exports = Table;
45
51
  ```
46
52
 
53
+ ### ESM Usage
54
+
55
+ For ESM projects, you can still use `await` if preferred (though it's no longer required):
56
+
57
+ ```javascript
58
+ import {setUpTable} from 'model-redis';
59
+
60
+ const Table = await setUpTable();
61
+
62
+ export default Table;
63
+ ```
64
+
47
65
  You can also pass your own configuration options to the Redis client. See the
48
66
  redis [client configuration guide](https://github.com/redis/node-redis/blob/master/docs/client-configuration.md)
49
67
  for available options:
@@ -62,7 +80,7 @@ const conf = {
62
80
  password: 'hunter42'
63
81
  };
64
82
 
65
- const Table = await setUpTable({redisConf: conf});
83
+ const Table = setUpTable({redisConf: conf});
66
84
 
67
85
  module.exports = Table;
68
86
  ```
@@ -79,11 +97,13 @@ const {createClient} = require('redis');
79
97
  const client = createClient();
80
98
  await client.connect();
81
99
 
82
- const Table = await setUpTable({redisClient: client});
100
+ const Table = setUpTable({redisClient: client});
83
101
 
84
102
  module.exports = Table;
85
103
  ```
86
104
 
105
+ **Note:** When passing a custom client, ensure it's connected before passing it to `setUpTable`.
106
+
87
107
  ### Prefix Key
88
108
 
89
109
  At some point, the Redis package removed the option to prefix a string to the
@@ -94,7 +114,7 @@ keys. This functionality has been added back with this package:
94
114
 
95
115
  const {setUpTable} = require('model-redis');
96
116
 
97
- const Table = await setUpTable({
117
+ const Table = setUpTable({
98
118
  prefix: 'auth_app:'
99
119
  });
100
120
 
@@ -262,7 +282,7 @@ All of these methods are extensible so proper business logic can be implemented.
262
282
  Model Redis supports relationships between models through the model registry system:
263
283
 
264
284
  ```javascript
265
- const Table = await setUpTable();
285
+ const Table = setUpTable();
266
286
 
267
287
  // Define User model
268
288
  class User extends Table {
package/index.js CHANGED
@@ -2,20 +2,24 @@
2
2
  const table = require('./src/redis_model')
3
3
  var client = null
4
4
 
5
- async function setUpTable(obj){
5
+ function setUpTable(obj){
6
6
  obj = obj || {};
7
7
 
8
+ let connectionPromise;
9
+
8
10
  if(obj.redisClient){
9
11
  client = obj.redisClient;
12
+ // If a client is provided, assume it's already connected or will be connected externally
13
+ connectionPromise = Promise.resolve(client);
10
14
  }else{
11
15
  const {createClient} = require('redis');
12
16
  client = createClient(obj.redisConf || {});
13
- await client.connect();
17
+ // Connect in background and store the promise
18
+ connectionPromise = client.connect().then(() => client);
14
19
  }
15
20
 
16
- // test client connection
17
-
18
- return table(client, obj.prefix);
21
+ // Return Table class immediately with connection promise injected
22
+ return table(client, obj.prefix, connectionPromise);
19
23
  }
20
24
 
21
25
  module.exports = {client, setUpTable};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "model-redis",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Simple ORM model for Redis in Node.js",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -21,12 +21,19 @@ class QueryHelper{
21
21
  }
22
22
  }
23
23
 
24
- function setUpTable(client, prefix=''){
24
+ function setUpTable(client, prefix='', connectionPromise=null){
25
25
 
26
26
  function redisPrefix(key){
27
27
  return `${prefix}${key}`;
28
28
  }
29
29
 
30
+ // Helper function to await connection if promise exists
31
+ async function ensureClientReady(){
32
+ if(connectionPromise){
33
+ await connectionPromise;
34
+ }
35
+ }
36
+
30
37
  class Table{
31
38
  static errors = {
32
39
  ObjectValidateError: objValidate.ObjectValidateError,
@@ -60,6 +67,9 @@ function setUpTable(client, prefix=''){
60
67
 
61
68
  static async get(index, queryHelper){
62
69
  try{
70
+ // Ensure client is connected before proceeding
71
+ await ensureClientReady();
72
+
63
73
  if(typeof index === 'object'){
64
74
  index = index[this._key];
65
75
  }
@@ -117,6 +127,9 @@ function setUpTable(client, prefix=''){
117
127
  }
118
128
 
119
129
  static async exists(index){
130
+ // Ensure client is connected before proceeding
131
+ await ensureClientReady();
132
+
120
133
  if(typeof index === 'object'){
121
134
  index = index[this._key];
122
135
  }
@@ -130,6 +143,9 @@ function setUpTable(client, prefix=''){
130
143
  static async list(){
131
144
  // return a list of all the index keys for this table.
132
145
  try{
146
+ // Ensure client is connected before proceeding
147
+ await ensureClientReady();
148
+
133
149
  return await client.SMEMBERS(
134
150
  redisPrefix(this.prototype.constructor.name)
135
151
  );
@@ -166,6 +182,8 @@ function setUpTable(client, prefix=''){
166
182
  static async create(data){
167
183
  // Add a entry to this redis table.
168
184
  try{
185
+ // Ensure client is connected before proceeding
186
+ await ensureClientReady();
169
187
 
170
188
  // Validate the passed data by the keyMap schema.
171
189
  data = objValidate.processKeys(this._keyMap, data);
@@ -210,6 +228,9 @@ function setUpTable(client, prefix=''){
210
228
  async update(data){
211
229
  // Update an existing entry.
212
230
  try{
231
+ // Ensure client is connected before proceeding
232
+ await ensureClientReady();
233
+
213
234
  // Validate the passed data, ignoring required fields.
214
235
  data = objValidate.processKeys(this.constructor._keyMap, data, true);
215
236
 
@@ -271,6 +292,9 @@ function setUpTable(client, prefix=''){
271
292
  // Remove an entry from this table.
272
293
 
273
294
  try{
295
+ // Ensure client is connected before proceeding
296
+ await ensureClientReady();
297
+
274
298
  // Remove the index key from the tables members list.
275
299
  await client.SREM(
276
300
  redisPrefix(this.constructor.name),