Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/appellation/rejects
Easy object storage in Redis using native types.
https://github.com/appellation/rejects
Last synced: about 8 hours ago
JSON representation
Easy object storage in Redis using native types.
- Host: GitHub
- URL: https://github.com/appellation/rejects
- Owner: appellation
- Created: 2018-03-23T23:44:21.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-08-24T18:35:54.000Z (about 6 years ago)
- Last Synced: 2024-10-28T14:23:28.266Z (18 days ago)
- Language: TypeScript
- Homepage:
- Size: 110 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Redis Objects
Simple abstractions for storing complex JSON data in Redis. Supports storage of complex objects and arrays.
## Example
```js
const Redis = require('ioredis');
const { Rejects } = require('rejects');const r = new Redis();
const s = new Rejects(r);s.set('some key', {
id: 'some id',
array: [
'array',
'of',
'primitives',
{
and: 'an',
object: 'too',
},
],
data: {
complex: 'nested',
data: 'here',
with: [
'an',
'array',
],
},
});
```Nested data can be directly accessed and modified by concatentating properties together with a `.`. For example:
```js
const data = await s.get('some key'); // { id: 'some id', ... }
const with = await s.get('some key.data.with', { type: 'arr' }); // ['an', 'array']
const update = await s.set('some key.data.with', ['new', 'array']);
const added = await s.upsert('some key.data.with', ['element']);
```Note that the type must be explicitly set to `arr` when directly accessing an array. Upserts are faster than sets, as they do not have to clear out the existing data before adding new data.
References to other data can be created:
```js
const { Reference } = require('rejects');await s.set('some other key', {
value: 'hello world',
});await s.set('some key', {
id: 'an id',
reference: new Reference('some other key.value'),
});const data = await s.get('some key'); // { id: 'an id', reference: 'hello world' }
```References can be created within arrays to create arrays of references. The `Reference` constructor takes a second parameter of either `obj` or `arr` to differentiate between references to arrays and objects (default to `obj`).
## Caveats
- Array entries are not actually stored as arrays, and as such order will not be preserved when fetching. It also follows that:
- Array entries can never be overridden unless you delete the entire entry and re-add the elements.## Reference
### `default`
- **`constructor(redis: Redis)`** - make a new instance and give it an [ioredis](https://github.com/luin/ioredis) client
- `get(key: string, { full = true, type = 'obj', depth = -1 })` - get a complex object. Valid type values are `arr` and `obj` depending on the anticipated data type of the fetched data. If full is true, will resolve all references to nested data. If full is false, will set the property to a string of the form `ref::` where type is `arr` or `obj`, and `key` points to the Redis key where the data is stored. Setting a depth option of 0 or greater will ensure recursion to only the specified depth.
- `set(key: string, data: any)` - set complex data; will overwrite and remove existing data that is changed or removed in the new data.
- `upsert(key: string, data: any)` - set complex data; will overwrite but not remove existing data.
- `delete(key: string)` - delete all complex data at a given key.
- `keys(key: string)` - get the keys of the object at `key`.
- `size(key: string)` - get the size of the object at `key`.
- `incr(key: string, count = 1)` - increment the value at `key` by `count`.