Redisk is a TypeScript ORM library for Redis.
Features:
- Store entities.
- Single relation support.
- Unique keys support.
- Retrieve entities by his primary keys or his unique keys.
- Indexes support.
- List entities with common filters, like limit, count and sort by.
- Find entities with multiple conditions ('>', '<', '=', '!=').
- Search (Similar to LIKE in SQL)
- And much more.
Quick overview
; await redisk.savenew User'::id::', 'Foo'; console.logawait redisk.getOneUser, '::id::';
Installation
npm install redisk --save
Contents
Connection
;
Options
Property | Description |
---|---|
url | URL of the Redis server. Format [redis[s]:]//[[user][:password@]][host][:port][/db-number][?db=db-number[&password=bar[&option=value]]] |
host | Host of the Redis server |
port | Port of the Redis server |
db | Number of the db (Default: 0) |
password | Password of the Redis server |
Closing connection to Redis:
await redisk.close;
Models
Model definition
Entity
Use the decorator Entity
to convert your class into a Redisk entity.
You can pass the option canBeListed to 'false' (Default is true) to save some space, but you will not be able to list user entities.
Property
The decorator Property
is used to save the fields into redis.
Optionally, you can pass the options indexed
if you want to use the field to sort or to use as a condition in the 'list' method or searchable
if you want to use pattern matching in this field.
You can also set a default value.
Both options are false by default.
Supported types
Redisk support multiple types to store and query.
- String
- Date (Will be saved as a timestamp)
- Boolean
- Number
All other types will be converted to a string.
Primary
Primary
decorator is used to define the primary key of the entity. It can only be one primary key and his value must be unique for all the same entities.
Unique
This decorator is used to make the value of this field unique for all the same entities. Then you can use it to query the entity.
Embedding other entities
You can make one to one relations with the HasOne
decorator.
Cascade inserts and updates are supported. (These options are false by default)
Queries
Save
await redisk.savenew Userid, name;
Update
;user.name = 'Bar';await redisk.saveuser;
Note: Null fields will be removed from the persisted entity, undefined fields will not be modified from persisted entity.
Get by primary key
await redisk.getOneUser, id;
Get by unique key
;;await redisk.getOneUser, value, uniqueKeyName;
Count
await redisk.countUser;
List all
Returns an array of all user entities.
await redisk.listUser;
Returns the first 10 user entities
;;await redis.listUser, limit, offset;
Return an array of user entities sorted by his creation date in descending order
await redisk.listUser, undefined, undefined, undefined, ;
List all with conditions
Simple
Returns an array of users where his color is red
type: 'AND',};await redisk.listUser, where, limit, offset;
Returns an array of users where his creation date is greater than the day 23
type: 'AND',};await redisk.listUser, where, limit, offset;
Multiple conditions
Returns an array of entities that his color field is 'red' or 'blue'.
Warning: Using multiple conditions leads to multiple queries with table intersections, to achieve high performance queries try to reduce the results with more concise conditional.
type: 'OR',};await redisk.listUser, where, limit, offset;
Returns an array of entities that his color field is 'red' and his food field is 'avocado'
type: 'AND',};await redisk.listUser, where, limit, offset;
Pattern matching
You can search entities by properties marked as searchables.
;;await redisk.searchUser, condition, maxNumberOfResults;
Delete
await redisk.deleteUser, id;
Logging
We use winston for logging, if you want to see more info, like redis command that are being executed you can set process.env.REDISK_LOG_LEVEL to 'info'.
For example:
REDISK_LOG_LEVEL=info npm test
Stay in touch
- Author - Nytyr
- Website - https://arkerlabs.com/