ts-redis-orm
Advanced tools
Comparing version 0.1.6 to 0.1.7
@@ -42,2 +42,12 @@ /// <reference types="ioredis" /> | ||
clone(): this; | ||
getSchemas(): { | ||
columns: string[]; | ||
indexKeys: string[]; | ||
uniqueKeys: string[]; | ||
primaryKeys: string[]; | ||
autoIncrementKey: string; | ||
table: string; | ||
connection: string; | ||
indexUpdatedAt: boolean; | ||
}; | ||
toJSON(): IInstanceValues<this>; | ||
@@ -44,0 +54,0 @@ protected assignStorageStrings(storageStrings: { |
@@ -72,10 +72,7 @@ "use strict"; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var redis, schemaErrors; | ||
var schemaErrors; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, serviceInstance_1.serviceInstance.getRedis(this)]; | ||
case 0: return [4 /*yield*/, serviceInstance_1.serviceInstance.compareSchemas(this)]; | ||
case 1: | ||
redis = _a.sent(); | ||
return [4 /*yield*/, serviceInstance_1.serviceInstance.compareSchemas(this)]; | ||
case 2: | ||
schemaErrors = _a.sent(); | ||
@@ -85,3 +82,4 @@ if (schemaErrors.length) { | ||
} | ||
return [2 /*return*/, redis]; | ||
return [4 /*yield*/, serviceInstance_1.serviceInstance.getRedis(this)]; | ||
case 2: return [2 /*return*/, _a.sent()]; | ||
} | ||
@@ -424,2 +422,20 @@ }); | ||
}; | ||
BaseEntity.prototype.getSchemas = function () { | ||
var columns = serviceInstance_1.serviceInstance.getColumns(this.constructor); | ||
var indexKeys = serviceInstance_1.serviceInstance.getIndexKeys(this.constructor); | ||
var uniqueKeys = serviceInstance_1.serviceInstance.getUniqueKeys(this.constructor); | ||
var primaryKeys = serviceInstance_1.serviceInstance.getPrimaryKeys(this.constructor); | ||
var autoIncrementKey = serviceInstance_1.serviceInstance.getAutoIncrementKey(this.constructor); | ||
var entityMeta = serviceInstance_1.serviceInstance.getEntityMeta(this.constructor); | ||
return { | ||
columns: columns, | ||
indexKeys: indexKeys, | ||
uniqueKeys: uniqueKeys, | ||
primaryKeys: primaryKeys, | ||
autoIncrementKey: autoIncrementKey, | ||
table: entityMeta.table, | ||
connection: entityMeta.connection, | ||
indexUpdatedAt: entityMeta.indexUpdatedAt, | ||
}; | ||
}; | ||
BaseEntity.prototype.toJSON = function () { | ||
@@ -426,0 +442,0 @@ return this.getValues(); |
@@ -13,2 +13,4 @@ import { BaseEntity } from "./BaseEntity"; | ||
private _groupByDateFormat; | ||
private _timer; | ||
private _timerType; | ||
constructor(_entityType: T); | ||
@@ -39,2 +41,5 @@ find(idObject: IIdObject<InstanceType<T>>): Promise<InstanceType<T> | undefined>; | ||
private _getRedis; | ||
private _timerStart; | ||
private _timerEnd; | ||
private _timerEndCustom; | ||
} |
@@ -45,6 +45,11 @@ "use strict"; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var debug_1 = __importDefault(require("debug")); | ||
var RedisOrmQueryError_1 = require("./errors/RedisOrmQueryError"); | ||
var parser_1 = require("./parser"); | ||
var serviceInstance_1 = require("./serviceInstance"); | ||
var debug = debug_1.default("tsredisorm/performance"); | ||
var Query = /** @class */ (function () { | ||
@@ -61,2 +66,4 @@ function Query(_entityType) { | ||
this._groupByDateFormat = ""; // this is experimental feature | ||
this._timer = [0, 0]; | ||
this._timerType = ""; | ||
} | ||
@@ -66,6 +73,7 @@ // region find | ||
return __awaiter(this, void 0, void 0, function () { | ||
var entityId, primaryKeys, entityStorageKey, redis, storageStrings_1; | ||
var entityId, primaryKeys, entity, entityStorageKey, redis, storageStrings_1; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
this._timerStart("find"); | ||
entityId = serviceInstance_1.serviceInstance.convertAsEntityId(this._entityType, idObject); | ||
@@ -86,7 +94,9 @@ primaryKeys = serviceInstance_1.serviceInstance.getPrimaryKeys(this._entityType); | ||
if ((storageStrings_1.deletedAt !== "NaN") === this._onlyDeleted) { | ||
return [2 /*return*/, this._entityType.newFromStorageStrings(storageStrings_1)]; | ||
entity = this._entityType.newFromStorageStrings(storageStrings_1); | ||
} | ||
} | ||
_a.label = 3; | ||
case 3: return [2 /*return*/]; | ||
case 3: | ||
this._timerEndCustom("find", "id: " + JSON.stringify(idObject)); | ||
return [2 /*return*/, entity]; | ||
} | ||
@@ -98,6 +108,7 @@ }); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var promises, _i, idObjects_1, idObject, result; | ||
var promises, _i, idObjects_1, idObject, result, entities; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
this._timerStart("findMany"); | ||
promises = []; | ||
@@ -111,3 +122,5 @@ for (_i = 0, idObjects_1 = idObjects; _i < idObjects_1.length; _i++) { | ||
result = _a.sent(); | ||
return [2 /*return*/, result.filter(function (x) { return x; })]; | ||
entities = result.filter(function (x) { return x; }); | ||
this._timerEndCustom("findMany", "Total Id: " + idObjects.length); | ||
return [2 /*return*/, entities]; | ||
} | ||
@@ -119,6 +132,7 @@ }); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var redis, id; | ||
var redis, id, entity; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
this._timerStart("findUnique"); | ||
if (!serviceInstance_1.serviceInstance.isUniqueKey(this._entityType, column)) { | ||
@@ -135,4 +149,8 @@ throw new RedisOrmQueryError_1.RedisOrmQueryError("(" + this._entityType.name + ") Invalid unique column: " + column); | ||
return [4 /*yield*/, this.find(id)]; | ||
case 3: return [2 /*return*/, _a.sent()]; | ||
case 4: return [2 /*return*/]; | ||
case 3: | ||
entity = _a.sent(); | ||
_a.label = 4; | ||
case 4: | ||
this._timerEndCustom("findUnique", "Column: " + column + ", Value: " + value); | ||
return [2 /*return*/, entity]; | ||
} | ||
@@ -144,6 +162,7 @@ }); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var promises, _i, values_1, value, result; | ||
var promises, _i, values_1, value, result, entities; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
this._timerStart("findUniqueMany"); | ||
promises = []; | ||
@@ -157,3 +176,5 @@ for (_i = 0, values_1 = values; _i < values_1.length; _i++) { | ||
result = _a.sent(); | ||
return [2 /*return*/, result.filter(function (x) { return x; })]; | ||
entities = result.filter(function (x) { return x; }); | ||
this._timerEndCustom("findUniqueMany", "Column: " + column + ", Total values: " + values.length); | ||
return [2 /*return*/, entities]; | ||
} | ||
@@ -183,4 +204,13 @@ }); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var result; | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, this._get()]; | ||
switch (_a.label) { | ||
case 0: | ||
this._timerStart("get"); | ||
return [4 /*yield*/, this._get()]; | ||
case 1: | ||
result = _a.sent(); | ||
this._timerEnd("get"); | ||
return [2 /*return*/, result]; | ||
} | ||
}); | ||
@@ -289,6 +319,12 @@ }); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var result; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this._aggregate("count", "")]; | ||
case 1: return [2 /*return*/, _a.sent()]; | ||
case 0: | ||
this._timerStart("count"); | ||
return [4 /*yield*/, this._aggregate("count", "")]; | ||
case 1: | ||
result = _a.sent(); | ||
this._timerEnd("count"); | ||
return [2 /*return*/, result]; | ||
} | ||
@@ -300,6 +336,12 @@ }); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var result; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this._aggregate("min", column)]; | ||
case 1: return [2 /*return*/, _a.sent()]; | ||
case 0: | ||
this._timerStart("min"); | ||
return [4 /*yield*/, this._aggregate("min", column)]; | ||
case 1: | ||
result = _a.sent(); | ||
this._timerEnd("min"); | ||
return [2 /*return*/, result]; | ||
} | ||
@@ -311,6 +353,12 @@ }); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var result; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this._aggregate("max", column)]; | ||
case 1: return [2 /*return*/, _a.sent()]; | ||
case 0: | ||
this._timerStart("max"); | ||
return [4 /*yield*/, this._aggregate("max", column)]; | ||
case 1: | ||
result = _a.sent(); | ||
this._timerEnd("max"); | ||
return [2 /*return*/, result]; | ||
} | ||
@@ -322,6 +370,12 @@ }); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var result; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this._aggregate("sum", column)]; | ||
case 1: return [2 /*return*/, _a.sent()]; | ||
case 0: | ||
this._timerStart("sum"); | ||
return [4 /*yield*/, this._aggregate("sum", column)]; | ||
case 1: | ||
result = _a.sent(); | ||
this._timerEnd("sum"); | ||
return [2 /*return*/, result]; | ||
} | ||
@@ -333,6 +387,12 @@ }); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var result; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this._aggregate("avg", column)]; | ||
case 1: return [2 /*return*/, _a.sent()]; | ||
case 0: | ||
this._timerStart("avg"); | ||
return [4 /*yield*/, this._aggregate("avg", column)]; | ||
case 1: | ||
result = _a.sent(); | ||
this._timerEnd("avg"); | ||
return [2 /*return*/, result]; | ||
} | ||
@@ -343,10 +403,11 @@ }); | ||
// endregion | ||
// region: rank | ||
// region rank | ||
Query.prototype.rank = function (column, idObject, isReverse) { | ||
if (isReverse === void 0) { isReverse = false; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var indexStorageKey, entityId, redis, offset; | ||
var indexStorageKey, entityId, offset, redis, tempOffset; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
this._timerStart("rank"); | ||
if (!serviceInstance_1.serviceInstance.isIndexKey(this._entityType, column)) { | ||
@@ -357,2 +418,3 @@ throw new RedisOrmQueryError_1.RedisOrmQueryError("(" + this._entityType.name + ") Invalid index column: " + column); | ||
entityId = serviceInstance_1.serviceInstance.convertAsEntityId(this._entityType, idObject); | ||
offset = -1; | ||
if (!entityId) return [3 /*break*/, 6]; | ||
@@ -362,18 +424,20 @@ return [4 /*yield*/, this._getRedis()]; | ||
redis = _a.sent(); | ||
offset = null; | ||
tempOffset = null; | ||
if (!isReverse) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, redis.zrevrank(indexStorageKey, entityId)]; | ||
case 2: | ||
offset = _a.sent(); | ||
tempOffset = _a.sent(); | ||
return [3 /*break*/, 5]; | ||
case 3: return [4 /*yield*/, redis.zrank(indexStorageKey, entityId)]; | ||
case 4: | ||
offset = _a.sent(); | ||
tempOffset = _a.sent(); | ||
_a.label = 5; | ||
case 5: | ||
if (offset !== null) { | ||
return [2 /*return*/, offset]; | ||
if (tempOffset !== null) { | ||
offset = tempOffset; | ||
} | ||
_a.label = 6; | ||
case 6: return [2 /*return*/, -1]; | ||
case 6: | ||
this._timerEnd("rank"); | ||
return [2 /*return*/, offset]; | ||
} | ||
@@ -395,3 +459,8 @@ }); | ||
if (whereIndexKeys.length === 0) { | ||
this.where("createdAt", "<=", "+inf"); | ||
if (this._sortBy && serviceInstance_1.serviceInstance.isIndexKey(this._entityType, this._sortBy.column)) { | ||
this.where(this._sortBy.column, "<=", "+inf"); | ||
} | ||
else { | ||
this.where("createdAt", "<=", "+inf"); | ||
} | ||
whereIndexKeys = Object.keys(this._whereIndexes); | ||
@@ -581,4 +650,30 @@ } | ||
}; | ||
Query.prototype._timerStart = function (type) { | ||
if (debug.enabled && this._timerType === "") { | ||
this._timer = process.hrtime(); | ||
this._timerType = type; | ||
} | ||
}; | ||
Query.prototype._timerEnd = function (type) { | ||
if (debug.enabled && this._timerType === type) { | ||
var diff = process.hrtime(this._timer); | ||
var executionTime = (diff[1] / 1000000).toFixed(2); | ||
var indexWhere = "Index: " + JSON.stringify(this._whereIndexes); | ||
var searchWhere = "Search: " + JSON.stringify(this._whereSearches); | ||
var sort = "Sort by: " + JSON.stringify(this._sortBy); | ||
var groupBy = "Group by: " + JSON.stringify(this._groupByColumn); | ||
var offset = "offset: " + this._offset; | ||
var limit = "limit: " + this._limit; | ||
debug("(" + this._entityType.name + ") " + type + " executed in " + executionTime + "ms. " + indexWhere + ". " + searchWhere + ". " + sort + ". " + groupBy + ". " + offset + ". " + limit); | ||
} | ||
}; | ||
Query.prototype._timerEndCustom = function (type, data) { | ||
if (debug.enabled && this._timerType === type) { | ||
var diff = process.hrtime(this._timer); | ||
var executionTime = (diff[1] / 1000000).toFixed(2); | ||
debug("(" + this._entityType.name + ") " + type + " executed in " + executionTime + "ms. " + data); | ||
} | ||
}; | ||
return Query; | ||
}()); | ||
exports.Query = Query; |
{ | ||
"name": "ts-redis-orm", | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"description": "A full functional Redis Orm library written in Typescript.", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
@@ -290,2 +290,21 @@ import {entityExporter} from "./entityExporter"; | ||
public getSchemas() { | ||
const columns = serviceInstance.getColumns(this.constructor); | ||
const indexKeys = serviceInstance.getIndexKeys(this.constructor); | ||
const uniqueKeys = serviceInstance.getUniqueKeys(this.constructor); | ||
const primaryKeys = serviceInstance.getPrimaryKeys(this.constructor); | ||
const autoIncrementKey = serviceInstance.getAutoIncrementKey(this.constructor); | ||
const entityMeta = serviceInstance.getEntityMeta(this.constructor); | ||
return { | ||
columns, | ||
indexKeys, | ||
uniqueKeys, | ||
primaryKeys, | ||
autoIncrementKey, | ||
table: entityMeta.table, | ||
connection: entityMeta.connection, | ||
indexUpdatedAt: entityMeta.indexUpdatedAt, | ||
}; | ||
} | ||
public toJSON() { | ||
@@ -292,0 +311,0 @@ return this.getValues(); |
@@ -31,3 +31,3 @@ import Debug from "debug"; | ||
private _groupByDateFormat: string = ""; // this is experimental feature | ||
private _timer = new Date(); | ||
private _timer: [number, number] = [0, 0]; | ||
private _timerType = ""; | ||
@@ -133,3 +133,3 @@ | ||
this._timerStart("get"); | ||
const result = this._get(); | ||
const result = await this._get(); | ||
this._timerEnd("get"); | ||
@@ -503,3 +503,3 @@ return result; | ||
if (debug.enabled && this._timerType === "") { | ||
this._timer = new Date(); | ||
this._timer = process.hrtime(); | ||
this._timerType = type; | ||
@@ -511,3 +511,4 @@ } | ||
if (debug.enabled && this._timerType === type) { | ||
const diff = new Date().getTime() - this._timer.getTime(); | ||
const diff = process.hrtime(this._timer); | ||
const executionTime = (diff[1] / 1000000).toFixed(2); | ||
const indexWhere = `Index: ${JSON.stringify(this._whereIndexes)}`; | ||
@@ -519,3 +520,3 @@ const searchWhere = `Search: ${JSON.stringify(this._whereSearches)}`; | ||
const limit = `limit: ${this._limit}`; | ||
debug(`(${this._entityType.name}) ${type} executed in ${diff}ms. ${indexWhere}. ${searchWhere}. ${sort}. ${groupBy}. ${offset}. ${limit}`); | ||
debug(`(${this._entityType.name}) ${type} executed in ${executionTime}ms. ${indexWhere}. ${searchWhere}. ${sort}. ${groupBy}. ${offset}. ${limit}`); | ||
} | ||
@@ -526,4 +527,5 @@ } | ||
if (debug.enabled && this._timerType === type) { | ||
const diff = new Date().getTime() - this._timer.getTime(); | ||
debug(`(${this._entityType.name}) ${type} executed in ${diff}ms. ${data}`); | ||
const diff = process.hrtime(this._timer); | ||
const executionTime = (diff[1] / 1000000).toFixed(2); | ||
debug(`(${this._entityType.name}) ${type} executed in ${executionTime}ms. ${data}`); | ||
} | ||
@@ -530,0 +532,0 @@ } |
@@ -98,3 +98,3 @@ import {assert, expect } from "chai"; | ||
assert.equal(total, batch * iterate); | ||
}).timeout(1000 * 10); | ||
}).timeout(1000 * 100); | ||
@@ -124,2 +124,11 @@ it(`query: count`, async () => { | ||
assert.equal(entities.length, newEntities2.length); | ||
const sortByEntities = await TestingSpeed.query() | ||
.where("createdAt", "<=", "+inf") | ||
.sortBy("number", "desc").limit(limit).get(); | ||
assert.equal(sortByEntities.length, limit); | ||
const sortByEntities2 = await TestingSpeed.query() | ||
.sortBy("number", "desc").limit(limit).get(); | ||
assert.equal(sortByEntities2.length, limit); | ||
}); | ||
@@ -126,0 +135,0 @@ }); |
Sorry, the diff of this file is not supported yet
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
7
304537
78
5845