model-redis
Advanced tools
Comparing version 0.1.3 to 0.2.0
@@ -18,5 +18,5 @@ 'use strict'; | ||
return table(client); | ||
return table(client, obj.prefix); | ||
} | ||
module.exports = {client, setUpTable}; |
{ | ||
"name": "model-redis", | ||
"version": "0.1.3", | ||
"version": "0.2.0", | ||
"description": "Simple ORM model for redis in NodsJS", | ||
@@ -25,4 +25,4 @@ "main": "index.js", | ||
"dependencies": { | ||
"redis": "^4.0.1" | ||
"redis": "^4.6.10" | ||
} | ||
} |
@@ -6,4 +6,9 @@ 'use strict'; | ||
function setUpTable(client){ | ||
function setUpTable(client, prefix=''){ | ||
function redisPrefix(key){ | ||
return `${prefix}${key}`; | ||
} | ||
class Table{ | ||
@@ -25,4 +30,8 @@ static _indexed = []; | ||
let result = await client.HGETALL(`${this.prototype.constructor.name}_${index}`); | ||
console.log('get', redisPrefix(`${this.prototype.constructor.name}_${index}`)) | ||
let result = await client.HGETALL( | ||
redisPrefix(`${this.prototype.constructor.name}_${index}`) | ||
); | ||
if(Object.keys(result).length === 0){ | ||
@@ -62,9 +71,17 @@ let error = new Error('EntryNotFound'); | ||
console.log('here') | ||
if(index_key && !this._indexed.includes(index_key)) return []; | ||
console.log('here2', redisPrefix(`${this.prototype.constructor.name}_${index_key}_${value}`)) | ||
if(index_key && this._indexed.includes(index_key)){ | ||
return await client.SMEMBERS(`${this.prototype.constructor.name}_${index_key}_${value}`); | ||
return await client.SMEMBERS( | ||
redisPrefix(`${this.prototype.constructor.name}_${index_key}_${value}`) | ||
); | ||
} | ||
console.log('here3', redisPrefix(this.prototype.constructor.name)) | ||
return await client.SMEMBERS(this.prototype.constructor.name); | ||
return await client.SMEMBERS( | ||
redisPrefix(this.prototype.constructor.name)); | ||
@@ -105,3 +122,6 @@ }catch(error){ | ||
// Add the key to the members for this redis table | ||
await client.SADD(this.prototype.constructor.name, String(data[this._key])); | ||
await client.SADD( | ||
redisPrefix(this.prototype.constructor.name), | ||
String(data[this._key]) | ||
); | ||
@@ -111,3 +131,3 @@ // Create index keys lists | ||
if(data[index]) await client.SADD( | ||
`${this.prototype.constructor.name}_${index}_${data[index]}`, | ||
redisPrefix(`${this.prototype.constructor.name}_${index}_${data[index]}`), | ||
String(data[this._key] | ||
@@ -119,3 +139,6 @@ )); | ||
for(let key of Object.keys(data)){ | ||
await client.HSET(`${this.prototype.constructor.name}_${data[this._key]}`, key, objValidate.parseToString(data[key])); | ||
await client.HSET( | ||
redisPrefix(`${this.prototype.constructor.name}_${data[this._key]}`), | ||
key, objValidate.parseToString(data[key]) | ||
); | ||
} | ||
@@ -156,3 +179,3 @@ | ||
await client.SREM( | ||
`${this.constructor.name}_${index}_${this[index]}`, | ||
redisPrefix(`${this.constructor.name}_${index}_${this[index]}`), | ||
String(this[this.constructor._key]) | ||
@@ -162,3 +185,3 @@ ); | ||
await client.SADD( | ||
`${this.constructor.name}_${index}_${data[index]}`, | ||
redisPrefix(`${this.constructor.name}_${index}_${data[index]}`), | ||
String(data[this.constructor._key] || this[this.constructor._key]) | ||
@@ -172,3 +195,6 @@ ); | ||
this[key] = data[key]; | ||
await client.HSET(`${this.constructor.name}_${this[this.constructor._key]}`, key, data[key]); | ||
await client.HSET( | ||
redisPrefix(`${this.constructor.name}_${this[this.constructor._key]}`), | ||
key, data[key] | ||
); | ||
} | ||
@@ -191,10 +217,19 @@ } | ||
await client.SREM(this.constructor.name, this[this.constructor._key]); | ||
await client.SREM( | ||
redisPrefix(this.constructor.name), | ||
this[this.constructor._key] | ||
); | ||
for(let index of this.constructor._indexed){ | ||
await client.SREM(`${this.constructor.name}_${index}_${data[value]}`, data[this.constructor._key]); | ||
await client.SREM( | ||
redisPrefix(`${this.constructor.name}_${index}_${data[value]}`), | ||
data[this.constructor._key] | ||
); | ||
} | ||
// Remove the entries hash values. | ||
let count = await client.DEL(`${this.constructor.name}_${this[this.constructor._key]}`); | ||
let count = await client.DEL( | ||
redisPrefix( | ||
`${this.constructor.name}_${this[this.constructor._key]}`) | ||
); | ||
@@ -208,6 +243,7 @@ // Return the number of removed values to the caller. | ||
}; | ||
} | ||
} | ||
return Table; | ||
} | ||
module.exports = setUpTable; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16518
267
Updatedredis@^4.6.10