redis-json
Advanced tools
Comparing version 5.0.0 to 6.0.0
@@ -0,1 +1,7 @@ | ||
# v6.0.0 | ||
* improves efficiency by combining multiple commands into one `multi` command | ||
* fixes code smells | ||
* replace the entire array when an array is present in the set command | ||
* removes all `<set|del|rewrite|incr>T` methods and integrates transaction within `<set|del|rewrite|incr>` method itself | ||
# v5.0.0 | ||
@@ -2,0 +8,0 @@ * supports empty string as pre-fixes |
@@ -178,3 +178,3 @@ import { promisify } from 'util'; | ||
const getValueOf = (val, stringifier = {}) => { | ||
var _a, _b; | ||
var _a; | ||
if (typeof val === 'object') { | ||
@@ -185,3 +185,3 @@ // if the val is null | ||
} | ||
const constructorName = (_b = (_a = val) === null || _a === void 0 ? void 0 : _a.constructor) === null || _b === void 0 ? void 0 : _b.name; | ||
const constructorName = (_a = val === null || val === void 0 ? void 0 : val.constructor) === null || _a === void 0 ? void 0 : _a.name; | ||
return isKnownContructor(constructorName) | ||
@@ -233,2 +233,3 @@ // if the val is {} or [] | ||
typeInfo: {}, | ||
arrayInfo: {}, | ||
}); | ||
@@ -300,2 +301,5 @@ /** | ||
return result; | ||
if (Array.isArray(target)) { | ||
result.arrayInfo[basePath] = true; | ||
} | ||
const entries = Object.entries(target); | ||
@@ -356,3 +360,6 @@ if (entries.length > 0) { | ||
const SCAN_COUNT = 100; | ||
const Config = { | ||
SCAN_COUNT: 100, | ||
}; | ||
/** | ||
@@ -401,2 +408,12 @@ * JSONCache eases the difficulties in storing a JSON in redis. | ||
hincrbyfloat: promisify(redisClient.hincrbyfloat).bind(redisClient), | ||
multi: (commands) => { | ||
return new Promise((resolve, reject) => { | ||
redisClient.multi(commands).exec((err, results) => { | ||
if (err) | ||
reject(err); | ||
else | ||
resolve(results); | ||
}); | ||
}); | ||
}, | ||
}; | ||
@@ -416,35 +433,13 @@ this.flattener = new Flattener(options.stringifier, options.parser); | ||
const flattened = this.flattener.flatten(obj); | ||
yield Promise.all([ | ||
this.redisClientInt.hmset(this.getKey(key), flattened.data), | ||
this.redisClientInt.hmset(this.getTypeKey(key), flattened.typeInfo), | ||
]); | ||
const commands = yield this.getKeysToBeRemoved(key, flattened); | ||
commands.push(['hmset', this.getKey(key), flattened.data]); | ||
commands.push(['hmset', this.getTypeKey(key), flattened.typeInfo]); | ||
if (options.expire) { | ||
yield Promise.all([ | ||
this.redisClientInt.expire(this.getKey(key), options.expire), | ||
this.redisClientInt.expire(this.getTypeKey(key), options.expire), | ||
]); | ||
commands.push(['expire', this.getKey(key), options.expire]); | ||
commands.push(['expire', this.getTypeKey(key), options.expire]); | ||
} | ||
yield this.execCommand(commands, options.transaction); | ||
}); | ||
} | ||
/** | ||
* Flattens the given json object and | ||
* stores it in Redis hashset using | ||
* the given transaction | ||
* | ||
* @param transaction redis transaction | ||
* @param key Redis key | ||
* @param obj JSON object to be stored | ||
* @param options | ||
*/ | ||
setT(transaction, key, obj, options = {}) { | ||
const flattened = this.flattener.flatten(obj); | ||
transaction.hmset(this.getKey(key), flattened.data); | ||
transaction.hmset(this.getTypeKey(key), flattened.typeInfo); | ||
if (options.expire) { | ||
transaction.expire(this.getKey(key), options.expire); | ||
transaction.expire(this.getTypeKey(key), options.expire); | ||
} | ||
return transaction; | ||
} | ||
/** | ||
* Retrieves the hashset from redis and | ||
@@ -493,3 +488,3 @@ * unflattens it back to the original Object | ||
else { | ||
result = { data, typeInfo }; | ||
result = { data, typeInfo, arrayInfo: {} }; | ||
} | ||
@@ -505,5 +500,8 @@ return this.flattener.unflatten(result); | ||
*/ | ||
rewrite(key, obj, options) { | ||
rewrite(key, obj, options = {}) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield this.redisClientInt.del(this.getKey(key)); | ||
const commands = [ | ||
['del', this.getKey(key)], | ||
]; | ||
yield this.execCommand(commands, options.transaction); | ||
yield this.set(key, obj, options); | ||
@@ -513,13 +511,2 @@ }); | ||
/** | ||
* Replace the entire hashset for the given key | ||
* | ||
* @param transaction Redis transaction | ||
* @param key Redis key | ||
* @param obj JSON Object of type T | ||
*/ | ||
rewriteT(transaction, key, obj, options) { | ||
transaction.del(this.getKey(key)); | ||
return this.setT(transaction, key, obj, options); | ||
} | ||
/** | ||
* Removes/deletes all the keys in the JSON Cache, | ||
@@ -533,3 +520,3 @@ * having the prefix. | ||
do { | ||
[cursor, keys] = yield this.redisClientInt.scan(cursor, 'MATCH', `${this.options.prefix}*`, 'COUNT', SCAN_COUNT); | ||
[cursor, keys] = yield this.redisClientInt.scan(cursor, 'MATCH', `${this.options.prefix}*`, 'COUNT', Config.SCAN_COUNT); | ||
if (keys.length > 0) { | ||
@@ -553,30 +540,12 @@ yield this.redisClientInt.del(...keys); | ||
*/ | ||
del(key) { | ||
del(key, options = {}) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield Promise.all([ | ||
this.redisClientInt.del(this.getKey(key)), | ||
this.redisClientInt.del(this.getTypeKey(key)), | ||
]); | ||
const commands = [ | ||
['del', this.getKey(key)], | ||
['del', this.getTypeKey(key)], | ||
]; | ||
yield this.execCommand(commands, options.transaction); | ||
}); | ||
} | ||
/** | ||
* Removes the given key from Redis | ||
* using the given transaction | ||
* | ||
* Please use this method instead of | ||
* directly using `redis.del` as this method | ||
* ensures that even the corresponding type info | ||
* is removed. It also ensures that prefix is | ||
* added to key, ensuring no other key is | ||
* removed unintentionally | ||
* | ||
* @param transaction Redis transaction | ||
* @param key Redis key | ||
*/ | ||
delT(transaction, key) { | ||
transaction.del(this.getKey(key)); | ||
transaction.del(this.getTypeKey(key)); | ||
return transaction; | ||
} | ||
/** | ||
* Increments the value of a variable in the JSON | ||
@@ -596,6 +565,7 @@ * Note: You can increment multiple variables in the | ||
*/ | ||
incr(key, obj) { | ||
incr(key, obj, options = {}) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const flattened = this.flattener.flatten(obj); | ||
yield Promise.all(Object.entries(flattened.data).map(([path, incrVal]) => { | ||
const commands = []; | ||
Object.entries(flattened.data).forEach(([path, incrVal]) => { | ||
// This check is needed to avoid redis errors. | ||
@@ -608,23 +578,37 @@ // It also helps while the user wants to increment the value | ||
} | ||
return this.redisClientInt.hincrbyfloat(this.getKey(key), path, incrVal); | ||
})); | ||
commands.push(['hincrbyfloat', this.getKey(key), path, incrVal]); | ||
}); | ||
yield this.execCommand(commands, options.transaction); | ||
}); | ||
} | ||
incrT(transaction, key, obj) { | ||
const flattened = this.flattener.flatten(obj); | ||
Object.entries(flattened.data).forEach(([path, incrVal]) => { | ||
// This check is needed to avoid redis errors. | ||
// It also helps while the user wants to increment the value | ||
// within an array. | ||
// Ex: rand: [null, null, 1] => this will increment the 3rd index by 1 | ||
if (flattened.typeInfo[path] !== TYPE.NUMBER) { | ||
return; | ||
/****************** | ||
* PRIVATE METHODS | ||
******************/ | ||
getKeysToBeRemoved(key, flattened) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const commands = []; | ||
// Check if the given obj has arrays and if it does | ||
// then we must remove the current array stored in | ||
// Cache and then set this array in the Cache | ||
if (Object.keys(flattened.arrayInfo).length > 0) { | ||
const currentObj = yield this.get(key); | ||
if (currentObj) { | ||
const currrentObjFlattened = this.flattener.flatten(currentObj).data; | ||
const keysToBeRemoved = []; | ||
// Get all paths matching the parent array path | ||
Object.keys(flattened.arrayInfo).forEach(path => { | ||
Object.keys(currrentObjFlattened).forEach(objPath => { | ||
if (objPath.startsWith(path)) { | ||
keysToBeRemoved.push(objPath); | ||
} | ||
}); | ||
}); | ||
if (keysToBeRemoved.length > 0) { | ||
commands.push(['hdel', this.getKey(key), ...keysToBeRemoved]); | ||
} | ||
} | ||
} | ||
transaction.hincrbyfloat(this.getKey(key), path, incrVal); | ||
return commands; | ||
}); | ||
return transaction; | ||
} | ||
/****************** | ||
* PRIVATE METHODS | ||
******************/ | ||
/** | ||
@@ -651,4 +635,22 @@ * Returns the redis storage key for storing data | ||
} | ||
execTransactionCommands(commands, transaction) { | ||
commands.forEach(command => { | ||
const [action, ...args] = command; | ||
transaction[action](...args); | ||
}); | ||
} | ||
execCommand(commands, transaction) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (transaction) { | ||
this.execTransactionCommands(commands, transaction); | ||
return transaction; | ||
} | ||
else { | ||
const result = yield this.redisClientInt.multi(commands); | ||
return result; | ||
} | ||
}); | ||
} | ||
} | ||
export default JSONCache; |
@@ -180,3 +180,3 @@ 'use strict'; | ||
const getValueOf = (val, stringifier = {}) => { | ||
var _a, _b; | ||
var _a; | ||
if (typeof val === 'object') { | ||
@@ -187,3 +187,3 @@ // if the val is null | ||
} | ||
const constructorName = (_b = (_a = val) === null || _a === void 0 ? void 0 : _a.constructor) === null || _b === void 0 ? void 0 : _b.name; | ||
const constructorName = (_a = val === null || val === void 0 ? void 0 : val.constructor) === null || _a === void 0 ? void 0 : _a.name; | ||
return isKnownContructor(constructorName) | ||
@@ -235,2 +235,3 @@ // if the val is {} or [] | ||
typeInfo: {}, | ||
arrayInfo: {}, | ||
}); | ||
@@ -302,2 +303,5 @@ /** | ||
return result; | ||
if (Array.isArray(target)) { | ||
result.arrayInfo[basePath] = true; | ||
} | ||
const entries = Object.entries(target); | ||
@@ -358,3 +362,6 @@ if (entries.length > 0) { | ||
const SCAN_COUNT = 100; | ||
const Config = { | ||
SCAN_COUNT: 100, | ||
}; | ||
/** | ||
@@ -403,2 +410,12 @@ * JSONCache eases the difficulties in storing a JSON in redis. | ||
hincrbyfloat: util.promisify(redisClient.hincrbyfloat).bind(redisClient), | ||
multi: (commands) => { | ||
return new Promise((resolve, reject) => { | ||
redisClient.multi(commands).exec((err, results) => { | ||
if (err) | ||
reject(err); | ||
else | ||
resolve(results); | ||
}); | ||
}); | ||
}, | ||
}; | ||
@@ -418,35 +435,13 @@ this.flattener = new Flattener(options.stringifier, options.parser); | ||
const flattened = this.flattener.flatten(obj); | ||
yield Promise.all([ | ||
this.redisClientInt.hmset(this.getKey(key), flattened.data), | ||
this.redisClientInt.hmset(this.getTypeKey(key), flattened.typeInfo), | ||
]); | ||
const commands = yield this.getKeysToBeRemoved(key, flattened); | ||
commands.push(['hmset', this.getKey(key), flattened.data]); | ||
commands.push(['hmset', this.getTypeKey(key), flattened.typeInfo]); | ||
if (options.expire) { | ||
yield Promise.all([ | ||
this.redisClientInt.expire(this.getKey(key), options.expire), | ||
this.redisClientInt.expire(this.getTypeKey(key), options.expire), | ||
]); | ||
commands.push(['expire', this.getKey(key), options.expire]); | ||
commands.push(['expire', this.getTypeKey(key), options.expire]); | ||
} | ||
yield this.execCommand(commands, options.transaction); | ||
}); | ||
} | ||
/** | ||
* Flattens the given json object and | ||
* stores it in Redis hashset using | ||
* the given transaction | ||
* | ||
* @param transaction redis transaction | ||
* @param key Redis key | ||
* @param obj JSON object to be stored | ||
* @param options | ||
*/ | ||
setT(transaction, key, obj, options = {}) { | ||
const flattened = this.flattener.flatten(obj); | ||
transaction.hmset(this.getKey(key), flattened.data); | ||
transaction.hmset(this.getTypeKey(key), flattened.typeInfo); | ||
if (options.expire) { | ||
transaction.expire(this.getKey(key), options.expire); | ||
transaction.expire(this.getTypeKey(key), options.expire); | ||
} | ||
return transaction; | ||
} | ||
/** | ||
* Retrieves the hashset from redis and | ||
@@ -495,3 +490,3 @@ * unflattens it back to the original Object | ||
else { | ||
result = { data, typeInfo }; | ||
result = { data, typeInfo, arrayInfo: {} }; | ||
} | ||
@@ -507,5 +502,8 @@ return this.flattener.unflatten(result); | ||
*/ | ||
rewrite(key, obj, options) { | ||
rewrite(key, obj, options = {}) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield this.redisClientInt.del(this.getKey(key)); | ||
const commands = [ | ||
['del', this.getKey(key)], | ||
]; | ||
yield this.execCommand(commands, options.transaction); | ||
yield this.set(key, obj, options); | ||
@@ -515,13 +513,2 @@ }); | ||
/** | ||
* Replace the entire hashset for the given key | ||
* | ||
* @param transaction Redis transaction | ||
* @param key Redis key | ||
* @param obj JSON Object of type T | ||
*/ | ||
rewriteT(transaction, key, obj, options) { | ||
transaction.del(this.getKey(key)); | ||
return this.setT(transaction, key, obj, options); | ||
} | ||
/** | ||
* Removes/deletes all the keys in the JSON Cache, | ||
@@ -535,3 +522,3 @@ * having the prefix. | ||
do { | ||
[cursor, keys] = yield this.redisClientInt.scan(cursor, 'MATCH', `${this.options.prefix}*`, 'COUNT', SCAN_COUNT); | ||
[cursor, keys] = yield this.redisClientInt.scan(cursor, 'MATCH', `${this.options.prefix}*`, 'COUNT', Config.SCAN_COUNT); | ||
if (keys.length > 0) { | ||
@@ -555,30 +542,12 @@ yield this.redisClientInt.del(...keys); | ||
*/ | ||
del(key) { | ||
del(key, options = {}) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield Promise.all([ | ||
this.redisClientInt.del(this.getKey(key)), | ||
this.redisClientInt.del(this.getTypeKey(key)), | ||
]); | ||
const commands = [ | ||
['del', this.getKey(key)], | ||
['del', this.getTypeKey(key)], | ||
]; | ||
yield this.execCommand(commands, options.transaction); | ||
}); | ||
} | ||
/** | ||
* Removes the given key from Redis | ||
* using the given transaction | ||
* | ||
* Please use this method instead of | ||
* directly using `redis.del` as this method | ||
* ensures that even the corresponding type info | ||
* is removed. It also ensures that prefix is | ||
* added to key, ensuring no other key is | ||
* removed unintentionally | ||
* | ||
* @param transaction Redis transaction | ||
* @param key Redis key | ||
*/ | ||
delT(transaction, key) { | ||
transaction.del(this.getKey(key)); | ||
transaction.del(this.getTypeKey(key)); | ||
return transaction; | ||
} | ||
/** | ||
* Increments the value of a variable in the JSON | ||
@@ -598,6 +567,7 @@ * Note: You can increment multiple variables in the | ||
*/ | ||
incr(key, obj) { | ||
incr(key, obj, options = {}) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const flattened = this.flattener.flatten(obj); | ||
yield Promise.all(Object.entries(flattened.data).map(([path, incrVal]) => { | ||
const commands = []; | ||
Object.entries(flattened.data).forEach(([path, incrVal]) => { | ||
// This check is needed to avoid redis errors. | ||
@@ -610,23 +580,37 @@ // It also helps while the user wants to increment the value | ||
} | ||
return this.redisClientInt.hincrbyfloat(this.getKey(key), path, incrVal); | ||
})); | ||
commands.push(['hincrbyfloat', this.getKey(key), path, incrVal]); | ||
}); | ||
yield this.execCommand(commands, options.transaction); | ||
}); | ||
} | ||
incrT(transaction, key, obj) { | ||
const flattened = this.flattener.flatten(obj); | ||
Object.entries(flattened.data).forEach(([path, incrVal]) => { | ||
// This check is needed to avoid redis errors. | ||
// It also helps while the user wants to increment the value | ||
// within an array. | ||
// Ex: rand: [null, null, 1] => this will increment the 3rd index by 1 | ||
if (flattened.typeInfo[path] !== TYPE.NUMBER) { | ||
return; | ||
/****************** | ||
* PRIVATE METHODS | ||
******************/ | ||
getKeysToBeRemoved(key, flattened) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const commands = []; | ||
// Check if the given obj has arrays and if it does | ||
// then we must remove the current array stored in | ||
// Cache and then set this array in the Cache | ||
if (Object.keys(flattened.arrayInfo).length > 0) { | ||
const currentObj = yield this.get(key); | ||
if (currentObj) { | ||
const currrentObjFlattened = this.flattener.flatten(currentObj).data; | ||
const keysToBeRemoved = []; | ||
// Get all paths matching the parent array path | ||
Object.keys(flattened.arrayInfo).forEach(path => { | ||
Object.keys(currrentObjFlattened).forEach(objPath => { | ||
if (objPath.startsWith(path)) { | ||
keysToBeRemoved.push(objPath); | ||
} | ||
}); | ||
}); | ||
if (keysToBeRemoved.length > 0) { | ||
commands.push(['hdel', this.getKey(key), ...keysToBeRemoved]); | ||
} | ||
} | ||
} | ||
transaction.hincrbyfloat(this.getKey(key), path, incrVal); | ||
return commands; | ||
}); | ||
return transaction; | ||
} | ||
/****************** | ||
* PRIVATE METHODS | ||
******************/ | ||
/** | ||
@@ -653,4 +637,22 @@ * Returns the redis storage key for storing data | ||
} | ||
execTransactionCommands(commands, transaction) { | ||
commands.forEach(command => { | ||
const [action, ...args] = command; | ||
transaction[action](...args); | ||
}); | ||
} | ||
execCommand(commands, transaction) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (transaction) { | ||
this.execTransactionCommands(commands, transaction); | ||
return transaction; | ||
} | ||
else { | ||
const result = yield this.redisClientInt.multi(commands); | ||
return result; | ||
} | ||
}); | ||
} | ||
} | ||
module.exports = JSONCache; |
{ | ||
"name": "redis-json", | ||
"version": "5.0.0", | ||
"version": "6.0.0", | ||
"description": "A wrapper library to store JSON Objects in redis-hashsets and retrieve it back as JSON objects", | ||
@@ -5,0 +5,0 @@ "sideEffects": false, |
# redis-json [![npm version](https://badge.fury.io/js/redis-json.svg)](https://badge.fury.io/js/redis-json) [![Build Status](https://travis-ci.com/AkashBabu/redis-json.svg?branch=master)](https://travis-ci.com/AkashBabu/redis-json) [![Coverage Status](https://coveralls.io/repos/github/AkashBabu/redis-json/badge.svg?branch=master)](https://coveralls.io/github/AkashBabu/redis-json?branch=master) [![Maintainability](https://api.codeclimate.com/v1/badges/0015747bb31d085adae8/maintainability)](https://codeclimate.com/github/AkashBabu/redis-json/maintainability) | ||
Nodejs library to store/retreive JSON Objects in RedisDB | ||
Nodejs library to store/retrieve JSON Objects in RedisDB without loosing type information, i.e. WYSIWYG (What You Store Is What You Get) | ||
## Description | ||
Every time `set` is called JSON object is flattened(embeded objects are converted to path keys) and then stored in Redis(just like a normal hashset), on `get` the hashset is unflattened and converted back to the original JSON object(with the same types as the original object). | ||
Every time `set` is called JSON object is flattened(embeded objects are converted to path keys) and then stored in Redis(just like a normal hashset), on `get` the hashset is unflattened and converted back to the original JSON object(with the same types as was in the original object). | ||
## What's new in v4.3.0? | ||
- In response to issue: [#17](https://github.com/AkashBabu/redis-json/issues/17), we now support incrementing values with the methods `incr()` & `incrT()`(for transactions) | ||
## What's new in v6.0.0? | ||
- In response to issue: [#24](https://github.com/AkashBabu/redis-json/issues/24), we now replace the array in the cache when array is found in `set` object. | ||
If you are on V5 then please check this [Migration Guide to V6](docs/migrationV6.md) | ||
## Installation | ||
> npm install redis-json --save | ||
## API | ||
Please visit [this page](docs/README.md) for detailed API documentation. | ||
Please visit [this page](docs/api/README.md) for detailed API documentation. | ||
@@ -29,12 +26,2 @@ ## Usage | ||
const jsonCache = new JSONCache<{ | ||
name: string; | ||
age: number; | ||
address: { | ||
doorNo: string; | ||
locality: string; | ||
pincode: number; | ||
} | ||
}>(redis, {prefix: 'cache:'}); | ||
const user = { | ||
@@ -51,2 +38,5 @@ name: 'redis-json', | ||
const jsonCache = new JSONCache<typeof user>(redis, {prefix: 'cache:'}); | ||
await jsonCache.set('123', user) | ||
@@ -143,5 +133,5 @@ | ||
jsonCache.setT(transaction, 'test', {name: 'testing'}) | ||
jsonCache.delT(transaction, 'test1') | ||
jsonCache.rewriteT(transaction, 'test2', {name: 'testing', age: 25}) | ||
await jsonCache.set('test', {name: 'testing'}, {transaction}) | ||
await jsonCache.del('test1', {transaction}) | ||
await jsonCache.rewrite('test2', {name: 'testing', age: 25}, {transaction}) | ||
@@ -153,10 +143,5 @@ transaction | ||
``` | ||
Please note that only `setT()`, `rewriteT()` & `delT()` supports transaction, where as `get()` & `clearAll()` do NOT support transaction because we process those results before returning it to the calling function. Moreover there is no real usecase in adding `get` methods to a transaction! | ||
Please note that only `set()`, `rewrite()`, `del()` & `incr()` supports transaction, where as `get()` & `clearAll()` do NOT support transaction because we process those results before returning it to the calling function. Moreover there is no real usecase in supporting transaction in `get()` & `clearAll()` methods! | ||
## Since v4.0.0 | ||
Types of the data are retained when retrieved from Redis. | ||
## Changelogs | ||
@@ -163,0 +148,0 @@ |
module.exports = { | ||
"name": "redis-json", | ||
"mode": "file", | ||
"out": "docs", | ||
"out": "docs/api", | ||
"excludePrivate": true, | ||
@@ -16,2 +16,3 @@ "excludeProtected": true, | ||
], | ||
ignoreCompilerErrors: true, | ||
} |
@@ -1,39 +0,2 @@ | ||
export interface ISetOptions { | ||
expire?: number; | ||
} | ||
/** | ||
* Stringifier will be used to convert a custom | ||
* object to a string when `set` is called | ||
*/ | ||
export interface IStringifier { | ||
[constructorName: string]: (val: any) => string; | ||
} | ||
/** | ||
* Parser will be used to convert the string | ||
* back to custom object when `get` is called | ||
*/ | ||
export interface IParser { | ||
[constructorName: string]: (val: string) => any; | ||
} | ||
/** | ||
* JSONCache options | ||
*/ | ||
export interface IOptions { | ||
/** | ||
* Custom prefix to be used for storage | ||
* namespace separation | ||
*/ | ||
prefix?: string; | ||
/** | ||
* Stringifier will be used to convert a custom | ||
* object to a string when `set` is called | ||
*/ | ||
stringifier?: IStringifier; | ||
/** | ||
* Parser will be used to convert the string | ||
* back to custom object when `get` is called | ||
*/ | ||
parser?: IParser; | ||
} | ||
/** | ||
* @hidden | ||
@@ -50,2 +13,9 @@ */ | ||
typeInfo: IObj<string>; | ||
arrayInfo: IObj<boolean>; | ||
} | ||
/** | ||
* @hidden | ||
*/ | ||
export declare type RecursivePartial<T> = { | ||
[P in keyof T]?: T[P] extends any[] ? Array<RecursivePartial<T[P]>> : T[P] extends any ? RecursivePartial<T[P]> : T[P]; | ||
}; |
@@ -1,2 +0,3 @@ | ||
import { IObj, IStringifier, IResult, IParser } from '../interfaces'; | ||
import { IObj, IResult } from '../interfaces'; | ||
import type { IStringifier, IParser } from './jsonCache.types'; | ||
/** | ||
@@ -3,0 +4,0 @@ * @internal |
@@ -1,6 +0,3 @@ | ||
import { IOptions, ISetOptions } from '../interfaces'; | ||
declare type Transaction = any; | ||
declare type RecursivePartial<T> = { | ||
[P in keyof T]?: T[P] extends any[] ? Array<RecursivePartial<T[P]>> : T[P] extends any ? RecursivePartial<T[P]> : T[P]; | ||
}; | ||
import type { RecursivePartial } from '../interfaces'; | ||
import type { IOptions, ISetOptions, IDelOptions } from './jsonCache.types'; | ||
interface IJSONCache<T> { | ||
@@ -11,8 +8,4 @@ set(key: string, obj: T, options: ISetOptions): Promise<any>; | ||
clearAll(): Promise<any>; | ||
del(key: string): Promise<any>; | ||
incr(key: string, obj: RecursivePartial<T>): Promise<any>; | ||
setT(transaction: Transaction, key: string, obj: T, options: ISetOptions): Transaction; | ||
rewriteT(transaction: Transaction, key: string, obj: T, options?: ISetOptions): Transaction; | ||
delT(transaction: Transaction, key: string): Transaction; | ||
incrT(transaction: Transaction, key: string, obj: RecursivePartial<T>): Transaction; | ||
del(key: string, options?: IDelOptions): Promise<any>; | ||
incr(key: string, obj: RecursivePartial<T>, options?: IDelOptions): Promise<any>; | ||
} | ||
@@ -65,13 +58,2 @@ /** | ||
/** | ||
* Flattens the given json object and | ||
* stores it in Redis hashset using | ||
* the given transaction | ||
* | ||
* @param transaction redis transaction | ||
* @param key Redis key | ||
* @param obj JSON object to be stored | ||
* @param options | ||
*/ | ||
setT(transaction: Transaction, key: string, obj: T, options?: ISetOptions): Transaction; | ||
/** | ||
* Retrieves the hashset from redis and | ||
@@ -96,10 +78,2 @@ * unflattens it back to the original Object | ||
/** | ||
* Replace the entire hashset for the given key | ||
* | ||
* @param transaction Redis transaction | ||
* @param key Redis key | ||
* @param obj JSON Object of type T | ||
*/ | ||
rewriteT(transaction: Transaction, key: string, obj: T, options?: ISetOptions): Transaction; | ||
/** | ||
* Removes/deletes all the keys in the JSON Cache, | ||
@@ -121,19 +95,4 @@ * having the prefix. | ||
*/ | ||
del(key: string): Promise<any>; | ||
del(key: string, options?: IDelOptions): Promise<any>; | ||
/** | ||
* Removes the given key from Redis | ||
* using the given transaction | ||
* | ||
* Please use this method instead of | ||
* directly using `redis.del` as this method | ||
* ensures that even the corresponding type info | ||
* is removed. It also ensures that prefix is | ||
* added to key, ensuring no other key is | ||
* removed unintentionally | ||
* | ||
* @param transaction Redis transaction | ||
* @param key Redis key | ||
*/ | ||
delT(transaction: Transaction, key: string): Transaction; | ||
/** | ||
* Increments the value of a variable in the JSON | ||
@@ -153,7 +112,7 @@ * Note: You can increment multiple variables in the | ||
*/ | ||
incr(key: string, obj: RecursivePartial<T>): Promise<any>; | ||
incrT(transaction: Transaction, key: string, obj: RecursivePartial<T>): Transaction; | ||
incr(key: string, obj: RecursivePartial<T>, options?: IDelOptions): Promise<any>; | ||
/****************** | ||
* PRIVATE METHODS | ||
******************/ | ||
private getKeysToBeRemoved; | ||
/** | ||
@@ -176,3 +135,5 @@ * Returns the redis storage key for storing data | ||
private getTypeKey; | ||
private execTransactionCommands; | ||
private execCommand; | ||
} | ||
export {}; |
@@ -1,2 +0,2 @@ | ||
import { IStringifier, IParser } from '../interfaces'; | ||
import type { IStringifier, IParser } from '../lib/jsonCache.types'; | ||
export declare enum TYPE { | ||
@@ -30,3 +30,3 @@ OBJECT = "0", | ||
*/ | ||
export declare const getTypeOf: (val: any) => string; | ||
export declare const getTypeOf: (val: any) => (TYPE | string); | ||
/** | ||
@@ -56,2 +56,2 @@ * Returns the stringified version of the given value. | ||
*/ | ||
export declare const getTypedVal: (type: string, val: string, parser?: IParser) => any; | ||
export declare const getTypedVal: (type: TYPE | string, val: string, parser?: IParser) => any; |
79498
23
1608
164