ts-redis-orm
Advanced tools
Comparing version 0.1.4 to 0.1.5
{ | ||
"name": "ts-redis-orm", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"description": "A full functional Redis Orm library written in Typescript.", | ||
@@ -10,3 +10,3 @@ "main": "build/index.js", | ||
"test-general": "mocha --exit --timeout 10000 -r ts-node/register tests/general.test.ts", | ||
"test-speed": "mocha --exit --timeout 10000 -r ts-node/register tests/speed.test.ts", | ||
"test-speed": "set debug=tsredisorm* & mocha --exit --timeout 10000 -r ts-node/register tests/speed.test.ts", | ||
"test-query": "mocha --exit --timeout 10000 -r ts-node/register tests/query.test.ts", | ||
@@ -20,2 +20,3 @@ "test-unique": "mocha --exit --timeout 10000 -r ts-node/register tests/unique.test.ts", | ||
"test-rank": "mocha --exit --timeout 10000 -r ts-node/register tests/rank.test.ts", | ||
"patch": "npm version patch", | ||
"build": "tsc" | ||
@@ -26,3 +27,5 @@ }, | ||
"@types/ioredis": "^4.0.18", | ||
"reflect-metadata": "^0.1.13" | ||
"reflect-metadata": "^0.1.13", | ||
"debug": "^4.1.1", | ||
"@types/debug": "^4.1.5" | ||
}, | ||
@@ -29,0 +32,0 @@ "devDependencies": { |
@@ -211,2 +211,3 @@ # Redis ORM (Typescript) | ||
- Export env variable REDIS_ORM_CONFIG=custom.json for your own config file path | ||
- For debug, export debug=tsredisorm* | ||
@@ -213,0 +214,0 @@ ```json5 |
110
src/Query.ts
@@ -0,1 +1,2 @@ | ||
import Debug from "debug"; | ||
import {BaseEntity} from "./BaseEntity"; | ||
@@ -6,2 +7,4 @@ import {RedisOrmQueryError} from "./errors/RedisOrmQueryError"; | ||
const debug = Debug("tsredisorm/performance"); | ||
import { | ||
@@ -29,2 +32,4 @@ IAggregateObject, | ||
private _groupByDateFormat: string = ""; // this is experimental feature | ||
private _timer = new Date(); | ||
private _timerType = ""; | ||
@@ -37,2 +42,3 @@ constructor(private readonly _entityType: T) { | ||
public async find(idObject: IIdObject<InstanceType<T>>): Promise<InstanceType<T> | undefined> { | ||
this._timerStart("find"); | ||
const entityId = serviceInstance.convertAsEntityId(this._entityType, idObject); | ||
@@ -42,2 +48,3 @@ const primaryKeys = serviceInstance.getPrimaryKeys(this._entityType); | ||
// if we have a valid entity id | ||
let entity: InstanceType<T> | undefined; | ||
if (entityId) { | ||
@@ -55,9 +62,13 @@ const entityStorageKey = serviceInstance.getEntityStorageKey(this._entityType, entityId); | ||
if ((storageStrings.deletedAt !== "NaN") === this._onlyDeleted) { | ||
return this._entityType.newFromStorageStrings(storageStrings); | ||
entity = this._entityType.newFromStorageStrings(storageStrings); | ||
} | ||
} | ||
} | ||
this._timerEndCustom("find", `id: ${JSON.stringify(idObject)}`); | ||
return entity; | ||
} | ||
public async findMany(idObjects: Array<IIdObject<InstanceType<T>>>): Promise<Array<InstanceType<T>>> { | ||
this._timerStart("findMany"); | ||
const promises = []; | ||
@@ -69,6 +80,11 @@ for (const idObject of idObjects) { | ||
const result = await Promise.all(promises); | ||
return result.filter(x => x) as Array<InstanceType<T>>; | ||
const entities = result.filter(x => x) as Array<InstanceType<T>>; | ||
this._timerEndCustom("findMany", `Total Id: ${idObjects.length}`); | ||
return entities; | ||
} | ||
public async findUnique(column: IArgColumn<T>, value: IUniqueValueType): Promise<InstanceType<T> | undefined> { | ||
this._timerStart("findUnique"); | ||
if (!serviceInstance.isUniqueKey(this._entityType, column as string)) { | ||
@@ -84,5 +100,9 @@ throw new RedisOrmQueryError(`(${this._entityType.name}) Invalid unique column: ${column}`); | ||
let entity: InstanceType<T> | undefined; | ||
if (id) { | ||
return await this.find(id); | ||
entity = await this.find(id); | ||
} | ||
this._timerEndCustom("findUnique", `Column: ${column}, Value: ${value}`); | ||
return entity; | ||
} | ||
@@ -92,2 +112,4 @@ | ||
Promise<Array<InstanceType<T>>> { | ||
this._timerStart("findUniqueMany"); | ||
const promises = []; | ||
@@ -99,3 +121,6 @@ for (const value of values) { | ||
const result = await Promise.all(promises); | ||
return result.filter(x => x) as Array<InstanceType<T>>; | ||
const entities = result.filter(x => x) as Array<InstanceType<T>>; | ||
this._timerEndCustom("findUniqueMany", `Column: ${column}, Total values: ${values.length}`); | ||
return entities; | ||
} | ||
@@ -110,3 +135,2 @@ | ||
this.limit(1); | ||
const entities = await this.get(); | ||
@@ -117,3 +141,6 @@ return entities.length ? entities[0] : undefined; | ||
public async get(): Promise<Array<InstanceType<T>>> { | ||
return this._get(); | ||
this._timerStart("get"); | ||
const result = this._get(); | ||
this._timerEnd("get"); | ||
return result; | ||
} | ||
@@ -247,19 +274,34 @@ | ||
public async count(): Promise<number> { | ||
return await this._aggregate("count", "") as number; | ||
this._timerStart("count"); | ||
const result = await this._aggregate("count", "") as number; | ||
this._timerEnd("count"); | ||
return result; | ||
} | ||
public async min(column: IArgColumn<T>) { | ||
return await this._aggregate("min", column as string); | ||
this._timerStart("min"); | ||
const result = await this._aggregate("min", column as string); | ||
this._timerEnd("min"); | ||
return result; | ||
} | ||
public async max(column: IArgColumn<T>) { | ||
return await this._aggregate("max", column as string); | ||
this._timerStart("max"); | ||
const result = await this._aggregate("max", column as string); | ||
this._timerEnd("max"); | ||
return result; | ||
} | ||
public async sum(column: IArgColumn<T>) { | ||
return await this._aggregate("sum", column as string); | ||
this._timerStart("sum"); | ||
const result = await this._aggregate("sum", column as string); | ||
this._timerEnd("sum"); | ||
return result; | ||
} | ||
public async avg(column: IArgColumn<T>) { | ||
return await this._aggregate("avg", column as string); | ||
this._timerStart("avg"); | ||
const result = await this._aggregate("avg", column as string); | ||
this._timerEnd("avg"); | ||
return result; | ||
} | ||
@@ -269,6 +311,9 @@ | ||
// region: rank | ||
// region rank | ||
public async rank(column: IArgColumn<T>, idObject: IIdObject<InstanceType<T>>, isReverse: boolean = false): | ||
Promise<number> { | ||
this._timerStart("rank"); | ||
if (!serviceInstance.isIndexKey(this._entityType, column as string)) { | ||
@@ -281,17 +326,19 @@ throw new RedisOrmQueryError(`(${this._entityType.name}) Invalid index column: ${column}`); | ||
let offset = -1; | ||
if (entityId) { | ||
const redis = await this._getRedis(); | ||
let offset: number | null = null; | ||
let tempOffset: number | null = null; | ||
if (isReverse) { | ||
offset = await redis.zrevrank(indexStorageKey, entityId); | ||
tempOffset = await redis.zrevrank(indexStorageKey, entityId); | ||
} else { | ||
offset = await redis.zrank(indexStorageKey, entityId); | ||
tempOffset = await redis.zrank(indexStorageKey, entityId); | ||
} | ||
if (offset !== null) { | ||
return offset; | ||
if (tempOffset !== null) { | ||
offset = tempOffset; | ||
} | ||
} | ||
return -1; | ||
this._timerEnd("rank"); | ||
return offset; | ||
} | ||
@@ -462,3 +509,30 @@ | ||
private _timerStart(type: string) { | ||
if (debug.enabled && this._timerType === "") { | ||
this._timer = new Date(); | ||
this._timerType = type; | ||
} | ||
} | ||
private _timerEnd(type: string) { | ||
if (debug.enabled && this._timerType === type) { | ||
const diff = new Date().getTime() - this._timer.getTime(); | ||
const indexWhere = `Index: ${JSON.stringify(this._whereIndexes)}`; | ||
const searchWhere = `Search: ${JSON.stringify(this._whereSearches)}`; | ||
const sort = `Sort by: ${JSON.stringify(this._sortBy)}`; | ||
const groupBy = `Group by: ${JSON.stringify(this._groupByColumn)}`; | ||
const offset = `offset: ${this._offset}`; | ||
const limit = `limit: ${this._limit}`; | ||
debug(`(${this._entityType.name}) ${type} executed in ${diff}ms. ${indexWhere}. ${searchWhere}. ${sort}. ${groupBy}. ${offset}. ${limit}`); | ||
} | ||
} | ||
private _timerEndCustom(type: string, data: any) { | ||
if (debug.enabled && this._timerType === type) { | ||
const diff = new Date().getTime() - this._timer.getTime(); | ||
debug(`(${this._entityType.name}) ${type} executed in ${diff}ms. ${data}`); | ||
} | ||
} | ||
// endregion | ||
} |
@@ -99,2 +99,27 @@ import {assert, expect } from "chai"; | ||
}).timeout(1000 * 10); | ||
it(`query: count`, async () => { | ||
let count = await TestingSpeed.count(); | ||
count = await TestingSpeed.query().where("boolean", "=", true).count(); | ||
}); | ||
it(`query: find`, async () => { | ||
const limit = 100; | ||
const entities = await TestingSpeed.query().limit(limit).get(); | ||
assert.equal(entities.length, limit); | ||
const entity = await TestingSpeed.query().find(entities[0].id); | ||
assert.isDefined(entity); | ||
const newEntities = await TestingSpeed.query().findMany(entities.map(x => x.id)); | ||
assert.equal(entities.length, newEntities.length); | ||
if (entity) { | ||
const newEntity = await TestingSpeed.query().findUnique("number", entity.number); | ||
assert.isDefined(newEntity); | ||
} | ||
const newEntities2 = await TestingSpeed.query().findUniqueMany("number", entities.map(x => x.number)); | ||
assert.equal(entities.length, newEntities2.length); | ||
}); | ||
}); | ||
@@ -101,0 +126,0 @@ |
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
322497
6197
257
5
+ Added@types/debug@^4.1.5
+ Addeddebug@^4.1.1
+ Added@types/debug@4.1.12(transitive)
+ Added@types/ms@0.7.34(transitive)