redisk
Advanced tools
Comparing version 1.1.2 to 1.2.0
@@ -6,5 +6,5 @@ "use strict"; | ||
return (target) => { | ||
let canBeListed = false; | ||
if (options !== undefined && options.canBeListed === true) { | ||
canBeListed = true; | ||
let canBeListed = true; | ||
if (options !== undefined && options.canBeListed === false) { | ||
canBeListed = false; | ||
} | ||
@@ -11,0 +11,0 @@ metadata_storage_1.MetadataStorage.getGlobal().canBeListed[target.name] = canBeListed; |
@@ -5,1 +5,2 @@ export * from './redisk'; | ||
export * from './metadata'; | ||
export * from './client'; |
@@ -9,1 +9,2 @@ "use strict"; | ||
__export(require("./metadata")); | ||
__export(require("./client")); |
import { Type } from './metadata/type'; | ||
import * as redis from 'redis'; | ||
import { Metadata } from './metadata/metadata'; | ||
import { Condition } from './interfaces/condition'; | ||
import { OrderBy } from './interfaces/orderby'; | ||
import { ClientOptions, Client } from './client'; | ||
export declare class Redisk { | ||
private readonly metadata; | ||
private readonly client; | ||
constructor(metadata: Metadata, client: redis.RedisClient); | ||
constructor(metadata: Metadata, client: Client); | ||
static init(options: ClientOptions): Redisk; | ||
close(): Promise<void>; | ||
getClient(): Client; | ||
save<T>(entity: T): Promise<void>; | ||
@@ -11,0 +14,0 @@ count<T>(entityType: Type<T>): Promise<number>; |
@@ -12,4 +12,4 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const redis = require("redis"); | ||
const bluebird = require("bluebird"); | ||
const metadata_1 = require("./metadata/metadata"); | ||
const client_1 = require("./client"); | ||
class Redisk { | ||
@@ -19,4 +19,14 @@ constructor(metadata, client) { | ||
this.client = client; | ||
bluebird.promisifyAll(redis); | ||
} | ||
static init(options) { | ||
return new Redisk(new metadata_1.Metadata(), new client_1.RedisClient(options)); | ||
} | ||
close() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield this.client.closeConnection(); | ||
}); | ||
} | ||
getClient() { | ||
return this.client; | ||
} | ||
save(entity) { | ||
@@ -33,3 +43,3 @@ return __awaiter(this, void 0, void 0, function* () { | ||
if (entity[property.name] === null) { | ||
yield this.client.hdelAsync(hashKey, property.name); | ||
yield this.client.hdel(hashKey, property.name); | ||
} | ||
@@ -40,6 +50,6 @@ if (hasOneRelations !== undefined && hasOneRelations[property.name] && hasOneRelations[property.name].cascadeUpdate && entity[property.name] !== null) { | ||
if (property.searchable) { | ||
yield this.client.sremAsync(this.getSearchableKeyName(name, property.name), this.getSearchableValuePrefix(entity[primary]) + persistedEntity[property.name].toLowerCase()); | ||
yield this.client.srem(this.getSearchableKeyName(name, property.name), this.getSearchableValuePrefix(entity[primary]) + persistedEntity[property.name].toLowerCase()); | ||
} | ||
if (property.sortable) { | ||
yield this.client.zremAsync(this.getSortableKeyName(name, property.name), persistedEntity[property.name]); | ||
yield this.client.zrem(this.getSortableKeyName(name, property.name), persistedEntity[property.name]); | ||
} | ||
@@ -68,3 +78,3 @@ } | ||
if (entity[uniqueName] !== null) { | ||
yield this.client.setAsync(this.getUniqueKeyName(name, uniqueName) + ':' + entity[uniqueName], entity[primary]); | ||
yield this.client.set(this.getUniqueKeyName(name, uniqueName) + ':' + entity[uniqueName], entity[primary]); | ||
} | ||
@@ -87,10 +97,10 @@ } | ||
if (property.sortable === true) { | ||
yield this.client.zaddAsync(this.getSortableKeyName(name, property.name), this.convertPropertyTypeToPrimitive(property, entity[property.name]), entity[primary]); | ||
yield this.client.zadd(this.getSortableKeyName(name, property.name), this.convertPropertyTypeToPrimitive(property, entity[property.name]), entity[primary]); | ||
} | ||
if (property.searchable === true) { | ||
yield this.client.saddAsync(this.getSearchableKeyName(name, property.name), this.getSearchableValuePrefix(entity[primary]) + entity[property.name].toLowerCase()); | ||
yield this.client.sadd(this.getSearchableKeyName(name, property.name), this.getSearchableValuePrefix(entity[primary]) + entity[property.name].toLowerCase()); | ||
} | ||
} | ||
} | ||
yield this.client.hmsetAsync(hashKey, valuesToStore); | ||
yield this.client.hmset(hashKey, valuesToStore); | ||
if (indexes) { | ||
@@ -104,3 +114,3 @@ for (const indexName of indexes) { | ||
if (value !== null) { | ||
yield this.client.saddAsync(this.getIndexKeyName(name, indexName, value), entity[primary]); | ||
yield this.client.sadd(this.getIndexKeyName(name, indexName, value), entity[primary]); | ||
} | ||
@@ -110,3 +120,3 @@ } | ||
if (canBeListed && persistedEntity === null) { | ||
yield this.client.rpushAsync(this.getListKeyName(name), entity[primary]); | ||
yield this.client.rpush(this.getListKeyName(name), entity[primary]); | ||
} | ||
@@ -120,3 +130,3 @@ return null; | ||
const keyName = this.getListKeyName(name); | ||
return yield this.client.llenAsync(keyName); | ||
return yield this.client.llen(keyName); | ||
}); | ||
@@ -174,5 +184,5 @@ } | ||
while (!finishedScanning) { | ||
const scanResponse = (yield this.client.sscanAsync(key, cursor, 'MATCH', value)); | ||
cursor = Number(scanResponse[0]); | ||
response.push(...scanResponse[1].map((id) => id.match(/.+?(?=\:_id_:)/g)[0])); | ||
const scanResponse = (yield this.client.sscan(key, cursor, value)); | ||
cursor = scanResponse.cursor; | ||
response.push(...scanResponse.data.map((id) => id.match(/.+?(?=\:_id_:)/g)[0])); | ||
if (cursor === 0 || response.length === limit) { | ||
@@ -196,6 +206,6 @@ finishedScanning = true; | ||
if (type === 'AND') { | ||
return yield this.client.sinterAsync(keyNames); | ||
return yield this.client.sinter(keyNames); | ||
} | ||
else { | ||
return yield this.client.sunionAsync(keyNames); | ||
return yield this.client.sunion(keyNames); | ||
} | ||
@@ -222,9 +232,9 @@ }); | ||
if (orderBy.strategy === 'ASC') { | ||
return yield this.client.zrangeAsync(sortableKey, start, stop); | ||
return yield this.client.zrange(sortableKey, start, stop); | ||
} | ||
else { | ||
return yield this.client.zrevrangeAsync(sortableKey, start, stop); | ||
return yield this.client.zrevrange(sortableKey, start, stop); | ||
} | ||
} | ||
return yield this.client.lrangeAsync(keyName, start, stop); | ||
return yield this.client.lrange(keyName, start, stop); | ||
}); | ||
@@ -244,7 +254,7 @@ } | ||
if (canBeListed) { | ||
yield this.client.lremAsync(this.getListKeyName(name), 1, id); | ||
yield this.client.lrem(this.getListKeyName(name), 1, id); | ||
} | ||
yield this.dropSortables(persistedEntity); | ||
yield this.dropSearchables(persistedEntity); | ||
yield this.client.delAsync(hashKey); | ||
yield this.client.del(hashKey); | ||
}); | ||
@@ -268,3 +278,3 @@ } | ||
} | ||
id = yield this.client.getAsync(indexKey + ':' + valueAsString); | ||
id = yield this.client.get(indexKey + ':' + valueAsString); | ||
} | ||
@@ -275,3 +285,3 @@ else { | ||
const hashKey = name + ':' + id; | ||
const result = yield this.client.hmgetAsync(hashKey, properties.map((property) => property.name)); | ||
const result = yield this.client.hmget(hashKey, properties.map((property) => property.name)); | ||
let index = 0; | ||
@@ -318,3 +328,3 @@ for (const resultKey of result) { | ||
for (const uniqueName of uniques) { | ||
yield this.client.delAsync(this.getUniqueKeyName(name, uniqueName) + ':' + entity[uniqueName]); | ||
yield this.client.del(this.getUniqueKeyName(name, uniqueName) + ':' + entity[uniqueName]); | ||
} | ||
@@ -333,3 +343,3 @@ }); | ||
} | ||
yield this.client.sremAsync(this.getIndexKeyName(name, indexName, value), id); | ||
yield this.client.srem(this.getIndexKeyName(name, indexName, value), id); | ||
} | ||
@@ -344,3 +354,3 @@ } | ||
if (property.searchable === true) { | ||
yield this.client.sremAsync(this.getSearchableKeyName(name, property.name), this.getSearchableValuePrefix(entity[primary]) + entity[property.name].toLowerCase()); | ||
yield this.client.srem(this.getSearchableKeyName(name, property.name), this.getSearchableValuePrefix(entity[primary]) + entity[property.name].toLowerCase()); | ||
} | ||
@@ -355,3 +365,3 @@ } | ||
if (property.sortable === true) { | ||
yield this.client.zremAsync(this.getSortableKeyName(name, property.name), entity[primary]); | ||
yield this.client.zrem(this.getSortableKeyName(name, property.name), entity[primary]); | ||
} | ||
@@ -358,0 +368,0 @@ } |
{ | ||
"name": "redisk", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"description": "TypeScript ORM for Redis.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
239
README.md
@@ -21,8 +21,87 @@ Redisk | ||
## Getting started | ||
## Quick overview | ||
```ts | ||
const redisk = Redisk.init({url: 'redis://127.0.0.1:6379/0'}); | ||
@Entity('user') | ||
export class User { | ||
@Primary() | ||
@Property() | ||
public readonly id: string; | ||
@Property() | ||
public readonly name: string; | ||
constructor( | ||
id: string, | ||
name: string, | ||
) { | ||
this.id = id; | ||
this.name = name; | ||
} | ||
} | ||
await redisk.save(new User('::id::', 'Foo')); | ||
console.log(await redisk.getOne(User, '::id::')); | ||
``` | ||
## Installation | ||
```bash | ||
npm install redisk --save | ||
``` | ||
## Examples with a User model | ||
## Contents | ||
- [Connection](#connection) | ||
- - [Options](#options) | ||
- [Models](#models) | ||
- - [Model definition](#model-definition) | ||
- - [Entity](#entity) | ||
- - [Property](#property) | ||
- - - [Supported types](#supported-types) | ||
- - [Primary](#primary) | ||
- - [Unique](#unique) | ||
- - [Index](#index) | ||
- - [Embedding other entities](#embedding-other-entities) | ||
- [Queries](#queries) | ||
- - [Save and Update](#save-and-update) | ||
- - [Get by primary key](#get-by-primary-key) | ||
- - [Get by unique key](#get-by-unique-key) | ||
- - [Count](#count) | ||
- - [List all](#list-all) | ||
- - [Find all by index](#find-all-by-index) | ||
- - - [Simple](#simple) | ||
- - - [Multiple conditions](#multiple-conditions) | ||
- - [Pattern matching](#pattern-matching) | ||
- - [Delete](#delete) | ||
## Connection | ||
```ts | ||
const redisk = Redisk.init(options); | ||
``` | ||
### `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: | ||
```ts | ||
await redisk.close(); | ||
``` | ||
## Models | ||
### Model definition | ||
@@ -73,44 +152,127 @@ ```ts | ||
### Init Redisk | ||
### 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. | ||
```ts | ||
import * as redis from 'redis'; | ||
@Entity('user', { canBeListed: true }) | ||
export class User { | ||
} | ||
``` | ||
const redisk = new Redisk(new Metadata(), redis.createClient({url: 'redis://127.0.0.1:6379/0'})); | ||
### Property | ||
The decorator `Property` is used to save the fields into redis. | ||
Optionally, you can pass the options `sortable` if you want to use the field to sort in the 'list' method or `searchable` if you want to use pattern matching in this field. | ||
Both options are false by default. | ||
```ts | ||
@Entity('user') | ||
export class User { | ||
@Property({sortable: true, searchable: false}) | ||
public readonly created: Date; | ||
} | ||
``` | ||
### Store one user | ||
#### 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. | ||
```ts | ||
await redisk.save<User>(new User(id, name, email, color, created)); | ||
@Entity('user') | ||
export class User { | ||
@Primary() | ||
@Property() | ||
public readonly id: string; | ||
} | ||
``` | ||
### Get one user by his primary key | ||
### 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. | ||
```ts | ||
@Entity('user') | ||
export class User { | ||
@Unique() | ||
@Property() | ||
public readonly email: string; | ||
} | ||
``` | ||
### Index | ||
Use the decorator `Index` on the fields that you want to query later with the find() method. | ||
```ts | ||
await redisk.getOne<User>(User, id); | ||
@Entity('user') | ||
export class User { | ||
@Index() | ||
@Property() | ||
public readonly color: string; | ||
} | ||
``` | ||
### Get one user by his email unique key | ||
### Embedding other entities | ||
You can make one to one relations with the `HasOne` decorator. | ||
Cascade inserts and updates are supported. | ||
```ts | ||
await redisk.getOne<User>(User, 'john@doe.com', 'email'); | ||
@Entity('user') | ||
export class User { | ||
@HasOne(Group, {cascadeInsert: true, cascadeUpdate: true}) | ||
@Property() | ||
public readonly group: Group; | ||
} | ||
``` | ||
### Count all users | ||
## Queries | ||
### Save and update | ||
```ts | ||
await redisk.count<User>(User); | ||
await redisk.save(new User(id, name)); | ||
``` | ||
### List all users | ||
### Get by primary key | ||
```ts | ||
await redisk.list<User>(User); // Returns an array of entities | ||
await redisk.getOne(User, id); | ||
``` | ||
### Get by unique key | ||
```ts | ||
const value = 'john@doe.com'; | ||
const uniqueKeyName = 'email'; | ||
await redisk.getOne(User, value, uniqueKeyName); | ||
``` | ||
### Count | ||
```ts | ||
await redisk.count(User); | ||
``` | ||
### List all | ||
```ts | ||
await redisk.list(User); // Returns an array of entities | ||
const limit = 10; | ||
const offset = 0; | ||
await redis.list<User>(User, limit, offset); // Returns 10 user entities | ||
await redis.list(User, limit, offset); // Returns 10 user entities | ||
await redisk.list<User>(User, undefined, undefined, { | ||
await redisk.list(User, undefined, undefined, { | ||
field: 'created', | ||
@@ -121,3 +283,4 @@ strategy: 'DESC', | ||
### Find all users by one index | ||
### Find all by index | ||
#### Simple | ||
```ts | ||
@@ -130,7 +293,37 @@ const conditions = [ | ||
]; | ||
await redisk.find<User>(User, conditions, limit, offset); // Returns an array of entites that match the conditions | ||
await redisk.find(User, conditions, limit, offset); // Returns an array of entities that match the conditions | ||
``` | ||
### Search users by his name | ||
#### Multiple conditions | ||
```ts | ||
const conditions = [ | ||
{ | ||
key: 'color', | ||
value: 'red', | ||
}, | ||
{ | ||
key: 'color', | ||
value: 'blue', | ||
}, | ||
]; | ||
await redisk.find(User, conditions, limit, offset, 'OR'); // Returns an array of entities that his color field is 'red' or 'blue' | ||
``` | ||
```ts | ||
const conditions = [ | ||
{ | ||
key: 'color', | ||
value: 'red', | ||
}, | ||
{ | ||
key: 'food', | ||
value: 'avocado', | ||
}, | ||
]; | ||
await redisk.find(User, conditions, limit, offset, 'AND'); // Returns an array of entities that his color field is 'red' and his food field is 'avocado' | ||
``` | ||
### Pattern matching | ||
You can search entities by properties marked as searchables. | ||
```ts | ||
@@ -142,9 +335,9 @@ const condition = { | ||
const maxNumberOfResults = 10; | ||
await redisk.search<User>(User, condition, maxNumberOfResults); | ||
await redisk.search(User, condition, maxNumberOfResults); | ||
``` | ||
### Delete one user | ||
### Delete | ||
```ts | ||
await redisk.delete<User>(User, id); | ||
await redisk.delete(User, id); | ||
``` |
@@ -5,5 +5,5 @@ import { MetadataStorage } from '../metadata/metadata.storage'; | ||
return (target: any) => { | ||
let canBeListed = false; | ||
if (options !== undefined && options.canBeListed === true) { | ||
canBeListed = true; | ||
let canBeListed = true; | ||
if (options !== undefined && options.canBeListed === false) { | ||
canBeListed = false; | ||
} | ||
@@ -10,0 +10,0 @@ MetadataStorage.getGlobal().canBeListed[target.name] = canBeListed; |
import { Type } from './metadata/type'; | ||
import * as redis from 'redis'; | ||
import * as bluebird from 'bluebird'; | ||
import { Metadata } from './metadata/metadata'; | ||
@@ -8,2 +6,3 @@ import { PropertyMetadata } from './metadata/property.metadata'; | ||
import { OrderBy } from './interfaces/orderby'; | ||
import { ClientOptions, Client, RedisClient } from './client'; | ||
@@ -14,12 +13,18 @@ export class Redisk { | ||
private readonly metadata: Metadata, | ||
private readonly client: redis.RedisClient, | ||
private readonly client: Client, | ||
) { | ||
bluebird.promisifyAll(redis); | ||
} | ||
/* | ||
TODO | ||
- Relations (Reference [Comment contains User]) | ||
*/ | ||
static init(options: ClientOptions) { | ||
return new Redisk(new Metadata(), new RedisClient(options)); | ||
} | ||
async close() { | ||
await this.client.closeConnection(); | ||
} | ||
getClient(): Client { | ||
return this.client; | ||
} | ||
async save<T>(entity: T): Promise<void> { | ||
@@ -40,3 +45,3 @@ | ||
if (entity[property.name] === null) { | ||
await this.client.hdelAsync(hashKey, property.name); | ||
await this.client.hdel(hashKey, property.name); | ||
} | ||
@@ -49,3 +54,3 @@ | ||
if (property.searchable) { | ||
await this.client.sremAsync( | ||
await this.client.srem( | ||
this.getSearchableKeyName(name, property.name), | ||
@@ -56,3 +61,3 @@ this.getSearchableValuePrefix(entity[primary]) + persistedEntity[property.name].toLowerCase(), | ||
if (property.sortable) { | ||
await this.client.zremAsync(this.getSortableKeyName(name, property.name), persistedEntity[property.name]); | ||
await this.client.zrem(this.getSortableKeyName(name, property.name), persistedEntity[property.name]); | ||
} | ||
@@ -85,3 +90,3 @@ } | ||
if (entity[uniqueName] !== null) { | ||
await this.client.setAsync( | ||
await this.client.set( | ||
this.getUniqueKeyName(name, uniqueName) + ':' + entity[uniqueName], | ||
@@ -113,3 +118,3 @@ entity[primary], | ||
if (property.sortable === true) { | ||
await this.client.zaddAsync( | ||
await this.client.zadd( | ||
this.getSortableKeyName(name, property.name), | ||
@@ -122,3 +127,3 @@ this.convertPropertyTypeToPrimitive(property, entity[property.name]), | ||
if (property.searchable === true) { | ||
await this.client.saddAsync( | ||
await this.client.sadd( | ||
this.getSearchableKeyName(name, property.name), | ||
@@ -130,3 +135,3 @@ this.getSearchableValuePrefix(entity[primary]) + entity[property.name].toLowerCase(), | ||
} | ||
await this.client.hmsetAsync(hashKey, valuesToStore); | ||
await this.client.hmset(hashKey, valuesToStore); | ||
@@ -141,3 +146,3 @@ if (indexes) { | ||
if (value !== null) { | ||
await this.client.saddAsync(this.getIndexKeyName(name, indexName, value), entity[primary]); | ||
await this.client.sadd(this.getIndexKeyName(name, indexName, value), entity[primary]); | ||
} | ||
@@ -148,3 +153,3 @@ } | ||
if (canBeListed && persistedEntity === null) { | ||
await this.client.rpushAsync(this.getListKeyName(name), entity[primary]); | ||
await this.client.rpush(this.getListKeyName(name), entity[primary]); | ||
} | ||
@@ -159,3 +164,3 @@ | ||
return await this.client.llenAsync(keyName); | ||
return await this.client.llen(keyName); | ||
} | ||
@@ -223,7 +228,7 @@ | ||
while (!finishedScanning) { | ||
const scanResponse = (await this.client.sscanAsync(key, cursor, 'MATCH', value)); | ||
cursor = Number(scanResponse[0]); | ||
const scanResponse = (await this.client.sscan(key, cursor, value)); | ||
cursor = scanResponse.cursor; | ||
response.push( | ||
...scanResponse[1].map((id: string) => id.match(/.+?(?=\:_id_:)/g)[0]), | ||
...scanResponse.data.map((id: string) => id.match(/.+?(?=\:_id_:)/g)[0]), | ||
); | ||
@@ -254,5 +259,5 @@ | ||
if (type === 'AND') { | ||
return await this.client.sinterAsync(keyNames); | ||
return await this.client.sinter(keyNames); | ||
} else { | ||
return await this.client.sunionAsync(keyNames); | ||
return await this.client.sunion(keyNames); | ||
} | ||
@@ -286,9 +291,9 @@ | ||
if (orderBy.strategy === 'ASC') { | ||
return await this.client.zrangeAsync(sortableKey, start, stop); | ||
return await this.client.zrange(sortableKey, start, stop); | ||
} else { | ||
return await this.client.zrevrangeAsync(sortableKey, start, stop); | ||
return await this.client.zrevrange(sortableKey, start, stop); | ||
} | ||
} | ||
return await this.client.lrangeAsync(keyName, start, stop); | ||
return await this.client.lrange(keyName, start, stop); | ||
} | ||
@@ -309,3 +314,3 @@ | ||
if (canBeListed) { | ||
await this.client.lremAsync(this.getListKeyName(name), 1, id); | ||
await this.client.lrem(this.getListKeyName(name), 1, id); | ||
} | ||
@@ -316,3 +321,3 @@ | ||
await this.client.delAsync(hashKey); | ||
await this.client.del(hashKey); | ||
} | ||
@@ -337,3 +342,3 @@ | ||
} | ||
id = await this.client.getAsync(indexKey + ':' + valueAsString); | ||
id = await this.client.get(indexKey + ':' + valueAsString); | ||
} else { | ||
@@ -345,3 +350,3 @@ id = valueAsString; | ||
const result = await this.client.hmgetAsync(hashKey, properties.map((property: PropertyMetadata) => property.name)); | ||
const result = await this.client.hmget(hashKey, properties.map((property: PropertyMetadata) => property.name)); | ||
let index = 0; | ||
@@ -390,3 +395,3 @@ for (const resultKey of result) { | ||
for (const uniqueName of uniques) { | ||
await this.client.delAsync(this.getUniqueKeyName(name, uniqueName) + ':' + entity[uniqueName]); | ||
await this.client.del(this.getUniqueKeyName(name, uniqueName) + ':' + entity[uniqueName]); | ||
} | ||
@@ -404,3 +409,3 @@ } | ||
} | ||
await this.client.sremAsync(this.getIndexKeyName(name, indexName, value), id); | ||
await this.client.srem(this.getIndexKeyName(name, indexName, value), id); | ||
} | ||
@@ -414,3 +419,3 @@ } | ||
if (property.searchable === true) { | ||
await this.client.sremAsync( | ||
await this.client.srem( | ||
this.getSearchableKeyName(name, property.name), | ||
@@ -427,3 +432,3 @@ this.getSearchableValuePrefix(entity[primary]) + entity[property.name].toLowerCase(), | ||
if (property.sortable === true) { | ||
await this.client.zremAsync( | ||
await this.client.zrem( | ||
this.getSortableKeyName(name, property.name), | ||
@@ -430,0 +435,0 @@ entity[primary], |
@@ -30,15 +30,15 @@ import { RediskTestUtils } from './utils/redisk-test-utils'; | ||
await utils.redisk.delete(User, users[0].id); | ||
expect(await utils.connection.hgetallAsync('user:' + users[0].id)).toBeNull(); | ||
expect(await utils.redisk.getClient().hgetall('user:' + users[0].id)).toBeNull(); | ||
expect(await utils.connection.getAsync('user:unique:email:' + users[0].email)).toBeNull(); | ||
expect(await utils.redisk.getClient().get('user:unique:email:' + users[0].email)).toBeNull(); | ||
expect(await utils.connection.lrangeAsync('user:list', 0, -1)).toEqual([ users[1].id, users[2].id ]); | ||
expect(await utils.redisk.getClient().lrange('user:list', 0, -1)).toEqual([ users[1].id, users[2].id ]); | ||
expect(await utils.connection.sinterAsync('user:index:color:' + users[0].color)).toEqual([]); | ||
expect(await utils.connection.sinterAsync('user:index:food:' + users[0].food)).toEqual([ users[2].id ]); | ||
expect(await utils.connection.sinterAsync('user:index:group:' + groups[0].id)).toEqual([ users[1].id ]); | ||
expect(await utils.redisk.getClient().sinter('user:index:color:' + users[0].color)).toEqual([]); | ||
expect(await utils.redisk.getClient().sinter('user:index:food:' + users[0].food)).toEqual([ users[2].id ]); | ||
expect(await utils.redisk.getClient().sinter('user:index:group:' + groups[0].id)).toEqual([ users[1].id ]); | ||
expect(await utils.connection.zrangeAsync('user:sort:created', 0, -1)).toEqual([ users[1].id, users[2].id ]); | ||
expect(await utils.redisk.getClient().zrange('user:sort:created', 0, -1)).toEqual([ users[1].id, users[2].id ]); | ||
expect(await utils.connection.smembersAsync('user:search:name')).toEqual( | ||
expect(await utils.redisk.getClient().smembers('user:search:name')).toEqual( | ||
[ users[2].id + ':_id_:' + users[2].name.toLowerCase(), users[1].id + ':_id_:' + users[1].name.toLowerCase() ] | ||
@@ -45,0 +45,0 @@ ); |
@@ -54,3 +54,3 @@ import { RediskTestUtils } from './utils/redisk-test-utils'; | ||
], | ||
)).sort()).toEqual([users[0], users[1]].sort()); | ||
)).sort()).toEqual([users[1], users[0]].sort()); | ||
}); | ||
@@ -110,4 +110,4 @@ }); | ||
'OR', | ||
)).sort()).toEqual([users[0], users[1], users[4]]); | ||
)).sort()).toEqual([users[1], users[0], users[4]]); | ||
}); | ||
}); |
@@ -39,3 +39,3 @@ import { RediskTestUtils } from './utils/redisk-test-utils'; | ||
const storedUser = await utils.connection.hgetallAsync('user:' + id); | ||
const storedUser = await utils.redisk.getClient().hgetall('user:' + id); | ||
expect(storedUser).toEqual({ | ||
@@ -50,3 +50,3 @@ id, | ||
}); | ||
const storedGroup = await utils.connection.hgetallAsync('group:' + group.id); | ||
const storedGroup = await utils.redisk.getClient().hgetall('group:' + group.id); | ||
expect(storedGroup).toEqual({ | ||
@@ -57,13 +57,13 @@ id: group.id, | ||
expect(await utils.connection.getAsync('user:unique:email:' + email)).toEqual(id); | ||
expect(await utils.redisk.getClient().get('user:unique:email:' + email)).toEqual(id); | ||
expect(await utils.connection.lrangeAsync('user:list', 0, -1)).toEqual([ users[0].id, id ]); | ||
expect(await utils.redisk.getClient().lrange('user:list', 0, -1)).toEqual([ users[0].id, id ]); | ||
expect(await utils.connection.sinterAsync('user:index:color:' + color)).toEqual([ id, users[0].id ]); | ||
expect(await utils.connection.sinterAsync('user:index:food:' + food)).toEqual([ id ]); | ||
expect(await utils.redisk.getClient().sinter('user:index:color:' + color)).toEqual([ users[0].id, id ]); | ||
expect(await utils.redisk.getClient().sinter('user:index:food:' + food)).toEqual([ id ]); | ||
expect(await utils.connection.zrangeAsync('user:sort:created', 0, -1)).toEqual([ id, users[0].id ]); | ||
expect(await utils.redisk.getClient().zrange('user:sort:created', 0, -1)).toEqual([ id, users[0].id ]); | ||
expect(await utils.connection.smembersAsync('user:search:name')).toEqual( | ||
[ id + ':_id_:' + name.toLowerCase(), users[0].id + ':_id_:' + users[0].name.toLowerCase() ] | ||
expect(await utils.redisk.getClient().smembers('user:search:name')).toEqual( | ||
[ users[0].id + ':_id_:' + users[0].name.toLowerCase(), id + ':_id_:' + name.toLowerCase() ] | ||
); | ||
@@ -103,3 +103,3 @@ }); | ||
const storedUser = await utils.connection.hgetallAsync('user:' + id); | ||
const storedUser = await utils.redisk.getClient().hgetall('user:' + id); | ||
expect(storedUser).toEqual({ | ||
@@ -114,3 +114,3 @@ id, | ||
}); | ||
const storedGroup = await utils.connection.hgetallAsync('group:' + group.id); | ||
const storedGroup = await utils.redisk.getClient().hgetall('group:' + group.id); | ||
expect(storedGroup).toEqual({ | ||
@@ -121,15 +121,15 @@ id: group.id, | ||
expect(await utils.connection.getAsync('user:unique:email:' + newEmail)).toEqual(id); | ||
expect(await utils.connection.getAsync('user:unique:email:' + email)).toBeNull(); | ||
expect(await utils.redisk.getClient().get('user:unique:email:' + newEmail)).toEqual(id); | ||
expect(await utils.redisk.getClient().get('user:unique:email:' + email)).toBeNull(); | ||
expect(await utils.connection.lrangeAsync('user:list', 0, -1)).toEqual([ users[0].id, id ]); | ||
expect(await utils.redisk.getClient().lrange('user:list', 0, -1)).toEqual([ users[0].id, id ]); | ||
expect(await utils.connection.sinterAsync('user:index:color:' + color)).toEqual([ users[0].id ]); | ||
expect(await utils.connection.sinterAsync('user:index:color:' + newColor)).toEqual([ id ]); | ||
expect(await utils.connection.sinterAsync('user:index:food:' + food)).toEqual([ id ]); | ||
expect(await utils.redisk.getClient().sinter('user:index:color:' + color)).toEqual([ users[0].id ]); | ||
expect(await utils.redisk.getClient().sinter('user:index:color:' + newColor)).toEqual([ id ]); | ||
expect(await utils.redisk.getClient().sinter('user:index:food:' + food)).toEqual([ id ]); | ||
expect(await utils.connection.zrangeAsync('user:sort:created', 0, -1)).toEqual([ users[0].id, id ]); | ||
expect(await utils.redisk.getClient().zrange('user:sort:created', 0, -1)).toEqual([ users[0].id, id ]); | ||
expect(await utils.connection.smembersAsync('user:search:name')).toEqual( | ||
[ id + ':_id_:' + newName.toLowerCase(), users[0].id + ':_id_:' + users[0].name.toLowerCase() ] | ||
expect(await utils.redisk.getClient().smembers('user:search:name')).toEqual( | ||
[ users[0].id + ':_id_:' + users[0].name.toLowerCase(), id + ':_id_:' + newName.toLowerCase() ] | ||
); | ||
@@ -151,3 +151,3 @@ }); | ||
const storedUser = await utils.connection.hgetallAsync('user:' + id); | ||
const storedUser = await utils.redisk.getClient().hgetall('user:' + id); | ||
expect(storedUser).toEqual({ | ||
@@ -162,16 +162,16 @@ id, | ||
expect(await utils.connection.getAsync('user:unique:email:' + newEmail)).toEqual(id); | ||
expect(await utils.redisk.getClient().get('user:unique:email:' + newEmail)).toEqual(id); | ||
expect(await utils.connection.lrangeAsync('user:list', 0, -1)).toEqual([ users[0].id, id ]); | ||
expect(await utils.redisk.getClient().lrange('user:list', 0, -1)).toEqual([ users[0].id, id ]); | ||
expect(await utils.connection.sinterAsync('user:index:color:' + color)).toEqual([ users[0].id ]); | ||
expect(await utils.connection.sinterAsync('user:index:color:' + newColor)).toEqual([ id ]); | ||
expect(await utils.connection.sinterAsync('user:index:food:' + food)).toEqual([ id ]); | ||
expect(await utils.redisk.getClient().sinter('user:index:color:' + color)).toEqual([ users[0].id ]); | ||
expect(await utils.redisk.getClient().sinter('user:index:color:' + newColor)).toEqual([ id ]); | ||
expect(await utils.redisk.getClient().sinter('user:index:food:' + food)).toEqual([ id ]); | ||
expect(await utils.connection.zrangeAsync('user:sort:created', 0, -1)).toEqual([ users[0].id, id ]); | ||
expect(await utils.redisk.getClient().zrange('user:sort:created', 0, -1)).toEqual([ users[0].id, id ]); | ||
expect(await utils.connection.smembersAsync('user:search:name')).toEqual( | ||
[ id + ':_id_:' + newName.toLowerCase(), users[0].id + ':_id_:' + users[0].name.toLowerCase() ] | ||
expect(await utils.redisk.getClient().smembers('user:search:name')).toEqual( | ||
[ users[0].id + ':_id_:' + users[0].name.toLowerCase(), id + ':_id_:' + newName.toLowerCase() ] | ||
); | ||
}); | ||
}); |
@@ -9,3 +9,2 @@ import { Redisk } from '../../src/redisk'; | ||
public connection; | ||
public redisk: Redisk; | ||
@@ -15,13 +14,12 @@ | ||
async beforeAll() { | ||
this.connection = redis.createClient({url: this.redisURL}); | ||
this.redisk = new Redisk(new Metadata(), this.connection); | ||
this.redisk = Redisk.init({url: this.redisURL}); | ||
} | ||
async afterEach() { | ||
await this.connection.flushdbAsync(); | ||
await this.redisk.getClient().flushdb(); | ||
} | ||
async afterAll() { | ||
await this.connection.end(false); | ||
await this.redisk.close(); | ||
} | ||
} |
93469
83
2083
338