ts-redis-orm
Advanced tools
Comparing version 0.1.14 to 0.1.15
@@ -26,2 +26,3 @@ /// <reference types="ioredis" /> | ||
table: string; | ||
tablePrefix: string; | ||
connection: string; | ||
@@ -34,2 +35,3 @@ indexUpdatedAt: boolean; | ||
private _table; | ||
private _tableName; | ||
private _isNew; | ||
@@ -36,0 +38,0 @@ private _values; |
@@ -30,2 +30,3 @@ "use strict"; | ||
this._table = ""; | ||
this._tableName = ""; | ||
// flags | ||
@@ -42,3 +43,3 @@ this._isNew = true; | ||
this.updatedAt = now; | ||
this._table = serviceInstance_1.serviceInstance.getDefaultTable(this.constructor); | ||
this.setTable(serviceInstance_1.serviceInstance.getDefaultTable(this.constructor)); | ||
} | ||
@@ -50,5 +51,6 @@ // region static methods | ||
table = table || serviceInstance_1.serviceInstance.getDefaultTable(this); | ||
const schemaErrors = yield serviceInstance_1.serviceInstance.compareSchemas(this, table); | ||
const tableName = serviceInstance_1.serviceInstance.getTablePrefix(this) + table; | ||
const schemaErrors = yield serviceInstance_1.serviceInstance.compareSchemas(this, tableName); | ||
if (schemaErrors.length) { | ||
throw new RedisOrmSchemaError_1.RedisOrmSchemaError(`(${this.name}, ${table}) Mismatch with remote Schemas`, schemaErrors); | ||
throw new RedisOrmSchemaError_1.RedisOrmSchemaError(`(${this.name}, ${tableName}) Mismatch with remote Schemas`, schemaErrors); | ||
} | ||
@@ -99,4 +101,5 @@ return yield serviceInstance_1.serviceInstance.getRedis(this); | ||
table = table || serviceInstance_1.serviceInstance.getDefaultTable(this); | ||
const tableName = serviceInstance_1.serviceInstance.getTablePrefix(this) + table; | ||
const redis = yield serviceInstance_1.serviceInstance.getRedis(this); | ||
const remoteSchemas = yield serviceInstance_1.serviceInstance.getRemoteSchemas(this, table); | ||
const remoteSchemas = yield serviceInstance_1.serviceInstance.getRemoteSchemas(this, tableName); | ||
// we resync only if we found any schema exist | ||
@@ -108,3 +111,3 @@ if (remoteSchemas) { | ||
serviceInstance_1.serviceInstance.getSchemasJson(this), | ||
table, | ||
tableName, | ||
]; | ||
@@ -115,8 +118,8 @@ // remove everything | ||
if (saveResult.error) { | ||
throw new RedisOrmEntityError_1.RedisOrmEntityError(`(${this.name}, ${table}) ${saveResult.error}`); | ||
throw new RedisOrmEntityError_1.RedisOrmEntityError(`(${this.name}, ${tableName}) ${saveResult.error}`); | ||
} | ||
debug(`(${this.name}, ${table}) resync db complete`); | ||
debug(`(${this.name}, ${tableName}) resync db complete`); | ||
} | ||
else { | ||
debug(`(${this.name}, ${table}) no schemas exist for resync db`); | ||
debug(`(${this.name}, ${tableName}) no schemas exist for resync db`); | ||
} | ||
@@ -132,4 +135,5 @@ }); | ||
table = table || serviceInstance_1.serviceInstance.getDefaultTable(this); | ||
const tableName = serviceInstance_1.serviceInstance.getTablePrefix(this) + table; | ||
const redis = yield serviceInstance_1.serviceInstance.getRedis(this); | ||
const remoteSchemas = yield serviceInstance_1.serviceInstance.getRemoteSchemas(this, table); | ||
const remoteSchemas = yield serviceInstance_1.serviceInstance.getRemoteSchemas(this, tableName); | ||
// we truncate only if we found any schema exist | ||
@@ -139,9 +143,9 @@ if (remoteSchemas) { | ||
const keys = []; | ||
const params = [table]; | ||
const params = [tableName]; | ||
// remove everything | ||
yield redis.commandAtomicTruncate(keys, params); | ||
debug(`(${this.name}, ${table}) truncate complete`); | ||
debug(`(${this.name}, ${tableName}) truncate complete`); | ||
} | ||
else { | ||
debug(`(${this.name}, ${table}) no schemas exist for truncate`); | ||
debug(`(${this.name}, ${tableName}) no schemas exist for truncate`); | ||
} | ||
@@ -170,2 +174,3 @@ }); | ||
table: entityMeta.table, | ||
tablePrefix: entityMeta.tablePrefix, | ||
connection: entityMeta.connection, | ||
@@ -228,2 +233,3 @@ indexUpdatedAt: entityMeta.indexUpdatedAt, | ||
this._table = table; | ||
this._tableName = serviceInstance_1.serviceInstance.getTablePrefix(this.constructor) + table; | ||
} | ||
@@ -243,3 +249,3 @@ getTable() { | ||
else { | ||
throw new RedisOrmEntityError_1.RedisOrmEntityError(`(${this.constructor.name}, ${this._table}) Invalid number value: ${value} for primary key: ${column}`); | ||
throw new RedisOrmEntityError_1.RedisOrmEntityError(`(${this.constructor.name}, ${this._tableName}) Invalid number value: ${value} for primary key: ${column}`); | ||
} | ||
@@ -252,7 +258,7 @@ } | ||
else { | ||
throw new RedisOrmEntityError_1.RedisOrmEntityError(`(${this.constructor.name}, ${this._table}) Invalid string value: '${value}' for primary key: ${column}`); | ||
throw new RedisOrmEntityError_1.RedisOrmEntityError(`(${this.constructor.name}, ${this._tableName}) Invalid string value: '${value}' for primary key: ${column}`); | ||
} | ||
} | ||
else { | ||
throw new RedisOrmEntityError_1.RedisOrmEntityError(`(${this.constructor.name}, ${this._table}) Invalid value: ${value} for primary key: ${column}`); | ||
throw new RedisOrmEntityError_1.RedisOrmEntityError(`(${this.constructor.name}, ${this._tableName}) Invalid value: ${value} for primary key: ${column}`); | ||
} | ||
@@ -272,15 +278,15 @@ } | ||
if (this.isNew) { | ||
throw new RedisOrmEntityError_1.RedisOrmEntityError(`(${this.constructor.name}, ${this._table}) You cannot increment a new entity`); | ||
throw new RedisOrmEntityError_1.RedisOrmEntityError(`(${this.constructor.name}, ${this._tableName}) You cannot increment a new entity`); | ||
} | ||
if (serviceInstance_1.serviceInstance.isPrimaryKey(this.constructor, column)) { | ||
throw new RedisOrmEntityError_1.RedisOrmEntityError(`(${this.constructor.name}, ${this._table}) You cannot increment primary key`); | ||
throw new RedisOrmEntityError_1.RedisOrmEntityError(`(${this.constructor.name}, ${this._tableName}) You cannot increment primary key`); | ||
} | ||
if (serviceInstance_1.serviceInstance.isUniqueKey(this.constructor, column)) { | ||
throw new RedisOrmEntityError_1.RedisOrmEntityError(`(${this.constructor.name}, ${this._table}) You cannot increment unique key`); | ||
throw new RedisOrmEntityError_1.RedisOrmEntityError(`(${this.constructor.name}, ${this._tableName}) You cannot increment unique key`); | ||
} | ||
if (!serviceInstance_1.serviceInstance.isNumberColumn(this.constructor, column)) { | ||
throw new RedisOrmEntityError_1.RedisOrmEntityError(`(${this.constructor.name}, ${this._table}) Column need to be in the type of Number`); | ||
throw new RedisOrmEntityError_1.RedisOrmEntityError(`(${this.constructor.name}, ${this._tableName}) Column need to be in the type of Number`); | ||
} | ||
if (!Number.isInteger(value)) { | ||
throw new RedisOrmEntityError_1.RedisOrmEntityError(`(${this.constructor.name}, ${this._table}) Increment value need to be an integer`); | ||
throw new RedisOrmEntityError_1.RedisOrmEntityError(`(${this.constructor.name}, ${this._tableName}) Increment value need to be an integer`); | ||
} | ||
@@ -354,3 +360,3 @@ this._increments[column] = value; | ||
if (this.isDeleted && !isRestore) { | ||
throw new RedisOrmEntityError_1.RedisOrmEntityError(`(${this.constructor.name}, ${this._table}) You cannot update a deleted entity`); | ||
throw new RedisOrmEntityError_1.RedisOrmEntityError(`(${this.constructor.name}, ${this._tableName}) You cannot update a deleted entity`); | ||
} | ||
@@ -387,3 +393,3 @@ const changes = this._getChanges(); | ||
this.isNew, | ||
this._table, | ||
this._tableName, | ||
autoIncrementKey, | ||
@@ -401,7 +407,7 @@ JSON.stringify(indexKeys), | ||
if (saveResult.error === "Mismatch with remote Schemas") { | ||
const schemaErrors = yield serviceInstance_1.serviceInstance.compareSchemas(this.constructor, this._table); | ||
throw new RedisOrmSchemaError_1.RedisOrmSchemaError(`(${this.constructor.name}, ${this._table}) ${saveResult.error}`, schemaErrors); | ||
const schemaErrors = yield serviceInstance_1.serviceInstance.compareSchemas(this.constructor, this._tableName); | ||
throw new RedisOrmSchemaError_1.RedisOrmSchemaError(`(${this.constructor.name}, ${this._tableName}) ${saveResult.error}`, schemaErrors); | ||
} | ||
else { | ||
throw new RedisOrmEntityError_1.RedisOrmEntityError(`(${this.constructor.name}, ${this._table}) ${saveResult.error}`); | ||
throw new RedisOrmEntityError_1.RedisOrmEntityError(`(${this.constructor.name}, ${this._tableName}) ${saveResult.error}`); | ||
} | ||
@@ -446,3 +452,3 @@ } | ||
if (this.isNew) { | ||
throw new RedisOrmEntityError_1.RedisOrmEntityError(`(${this.constructor.name}, ${this._table}) You cannot delete a new entity`); | ||
throw new RedisOrmEntityError_1.RedisOrmEntityError(`(${this.constructor.name}, ${this._tableName}) You cannot delete a new entity`); | ||
} | ||
@@ -452,3 +458,3 @@ // if it's soft delete | ||
if (!forceDelete && this.isDeleted) { | ||
throw new RedisOrmEntityError_1.RedisOrmEntityError(`(${this.constructor.name}, ${this._table}) You cannot delete a deleted entity`); | ||
throw new RedisOrmEntityError_1.RedisOrmEntityError(`(${this.constructor.name}, ${this._tableName}) You cannot delete a deleted entity`); | ||
} | ||
@@ -469,3 +475,3 @@ // if we didn't set deletedAt, set a new one | ||
!forceDelete, | ||
this._table, | ||
this._tableName, | ||
deletedAt.getTime(), | ||
@@ -480,3 +486,3 @@ JSON.stringify(indexKeys), | ||
if (saveResult.error) { | ||
throw new RedisOrmEntityError_1.RedisOrmEntityError(`(${this.constructor.name}, ${this._table}) ${saveResult.error}`); | ||
throw new RedisOrmEntityError_1.RedisOrmEntityError(`(${this.constructor.name}, ${this._tableName}) ${saveResult.error}`); | ||
} | ||
@@ -483,0 +489,0 @@ // update deleted At |
@@ -10,2 +10,3 @@ "use strict"; | ||
table: "", | ||
tablePrefix: "", | ||
connection: "default", | ||
@@ -12,0 +13,0 @@ indexUpdatedAt: true, |
@@ -33,2 +33,3 @@ "use strict"; | ||
class: entityType.name, | ||
tablePrefix: serviceInstance_1.serviceInstance.getTablePrefix(entityType), | ||
table: serviceInstance_1.serviceInstance.getDefaultTable(entityType), | ||
@@ -35,0 +36,0 @@ schemas: serviceInstance_1.serviceInstance.getSchemasJson(entityType), |
@@ -6,2 +6,3 @@ import { BaseEntity } from "./BaseEntity"; | ||
private _table; | ||
private _tableName; | ||
private _onlyDeleted; | ||
@@ -8,0 +9,0 @@ private _offset; |
@@ -25,2 +25,3 @@ "use strict"; | ||
this._table = ""; | ||
this._tableName = ""; // prefix + _table | ||
this._onlyDeleted = false; | ||
@@ -36,3 +37,3 @@ this._offset = 0; | ||
this._timerType = ""; | ||
this._table = serviceInstance_1.serviceInstance.getDefaultTable(_entityType); | ||
this.setTable(serviceInstance_1.serviceInstance.getDefaultTable(_entityType)); | ||
} | ||
@@ -42,2 +43,3 @@ // region operation | ||
this._table = table; | ||
this._tableName = serviceInstance_1.serviceInstance.getTablePrefix(this._entityType) + this._table; | ||
return this; | ||
@@ -55,3 +57,3 @@ } | ||
if (entityId) { | ||
const entityStorageKey = serviceInstance_1.serviceInstance.getEntityStorageKey(this._table, entityId); | ||
const entityStorageKey = serviceInstance_1.serviceInstance.getEntityStorageKey(this._tableName, entityId); | ||
if (!entityStorageKey) { | ||
@@ -95,3 +97,3 @@ throw new RedisOrmQueryError_1.RedisOrmQueryError(`(${this._entityType.name}) Invalid id ${JSON.stringify(idObject)}`); | ||
const redis = yield this._getRedis(); | ||
const id = yield redis.hget(serviceInstance_1.serviceInstance.getUniqueStorageKey(this._table, column), value.toString()); | ||
const id = yield redis.hget(serviceInstance_1.serviceInstance.getUniqueStorageKey(this._tableName, column), value.toString()); | ||
let entity; | ||
@@ -282,3 +284,3 @@ if (id) { | ||
} | ||
const indexStorageKey = serviceInstance_1.serviceInstance.getIndexStorageKey(this._table, column); | ||
const indexStorageKey = serviceInstance_1.serviceInstance.getIndexStorageKey(this._tableName, column); | ||
const entityId = serviceInstance_1.serviceInstance.convertAsEntityId(this._entityType, idObject); | ||
@@ -330,3 +332,3 @@ let offset = -1; | ||
this._limit, | ||
this._table, | ||
this._tableName, | ||
"", | ||
@@ -366,3 +368,3 @@ "", | ||
// redis params | ||
const indexStorageKey = serviceInstance_1.serviceInstance.getIndexStorageKey(this._table, column); | ||
const indexStorageKey = serviceInstance_1.serviceInstance.getIndexStorageKey(this._tableName, column); | ||
const extraParams = ["LIMIT", this._offset.toString(), this._limit.toString()]; | ||
@@ -405,3 +407,3 @@ // collect result ids | ||
this._offset, | ||
this._table, | ||
this._tableName, | ||
aggregate, | ||
@@ -444,6 +446,6 @@ aggregateColumn, | ||
if (max === "+inf" && min === "-inf") { | ||
count = yield redis.zcard(serviceInstance_1.serviceInstance.getIndexStorageKey(this._table, column)); | ||
count = yield redis.zcard(serviceInstance_1.serviceInstance.getIndexStorageKey(this._tableName, column)); | ||
} | ||
else { | ||
count = yield redis.zcount(serviceInstance_1.serviceInstance.getIndexStorageKey(this._table, column), min, max); | ||
count = yield redis.zcount(serviceInstance_1.serviceInstance.getIndexStorageKey(this._tableName, column), min, max); | ||
} | ||
@@ -474,3 +476,3 @@ return count; | ||
const limit = `limit: ${this._limit}`; | ||
debugPerformance(`(${this._entityType.name}, ${this._table}) ${type} executed in ${executionTime}ms. ${indexWhere}. ${searchWhere}. ${sort}. ${groupBy}. ${offset}. ${limit}`); | ||
debugPerformance(`(${this._entityType.name}, ${this._tableName}) ${type} executed in ${executionTime}ms. ${indexWhere}. ${searchWhere}. ${sort}. ${groupBy}. ${offset}. ${limit}`); | ||
} | ||
@@ -482,3 +484,3 @@ } | ||
const executionTime = (diff[1] / 1000000).toFixed(2); | ||
debugPerformance(`(${this._entityType.name}, ${this._table}) ${type} executed in ${executionTime}ms. ${data}`); | ||
debugPerformance(`(${this._entityType.name}, ${this._tableName}) ${type} executed in ${executionTime}ms. ${data}`); | ||
} | ||
@@ -485,0 +487,0 @@ } |
@@ -13,2 +13,3 @@ import IORedis from "ioredis"; | ||
getDefaultTable(target: object): string; | ||
getTablePrefix(target: object): string; | ||
getConnection(target: object): string; | ||
@@ -37,9 +38,9 @@ getPrimaryKeys(target: object): string[]; | ||
getRedis(target: object, registerRedis?: boolean): Promise<IORedis.Redis>; | ||
compareSchemas(target: object, table: string): Promise<string[]>; | ||
getRemoteSchemas(target: object, table: string): Promise<{ | ||
compareSchemas(target: object, tableName: string): Promise<string[]>; | ||
getRemoteSchemas(target: object, tableName: string): Promise<{ | ||
[key: string]: ISchema; | ||
} | null>; | ||
getEntityStorageKey(table: string, entityId: string): string; | ||
getIndexStorageKey(table: string, column: string): string; | ||
getUniqueStorageKey(table: string, column: string): string; | ||
getEntityStorageKey(tableName: string, entityId: string): string; | ||
getIndexStorageKey(tableName: string, column: string): string; | ||
getUniqueStorageKey(tableName: string, column: string): string; | ||
getSchemasStorageKey(): string; | ||
@@ -46,0 +47,0 @@ getEntityTypes(): object[]; |
@@ -87,2 +87,5 @@ "use strict"; | ||
} | ||
getTablePrefix(target) { | ||
return this.getEntityMeta(target).tablePrefix; | ||
} | ||
getConnection(target) { | ||
@@ -206,7 +209,7 @@ return this.getEntityMeta(target).connection; | ||
} | ||
compareSchemas(target, table) { | ||
compareSchemas(target, tableName) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let errors = []; | ||
try { | ||
const remoteSchemas = yield this.getRemoteSchemas(target, table); | ||
const remoteSchemas = yield this.getRemoteSchemas(target, tableName); | ||
if (remoteSchemas) { | ||
@@ -226,7 +229,7 @@ // we do such indirect case is to convert primitive types to strings | ||
} | ||
getRemoteSchemas(target, table) { | ||
getRemoteSchemas(target, tableName) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const redis = yield exports.serviceInstance.getRedis(target); | ||
const storageKey = this.getSchemasStorageKey(); | ||
const remoteSchemasString = yield redis.hget(storageKey, table); | ||
const remoteSchemasString = yield redis.hget(storageKey, tableName); | ||
if (remoteSchemasString) { | ||
@@ -240,10 +243,10 @@ return JSON.parse(remoteSchemasString); | ||
// region public methods: storage key | ||
getEntityStorageKey(table, entityId) { | ||
return `entity:${table}:${entityId}`; | ||
getEntityStorageKey(tableName, entityId) { | ||
return `entity:${tableName}:${entityId}`; | ||
} | ||
getIndexStorageKey(table, column) { | ||
return `index:${table}:${column}`; | ||
getIndexStorageKey(tableName, column) { | ||
return `index:${tableName}:${column}`; | ||
} | ||
getUniqueStorageKey(table, column) { | ||
return `unique:${table}:${column}`; | ||
getUniqueStorageKey(tableName, column) { | ||
return `unique:${tableName}:${column}`; | ||
} | ||
@@ -250,0 +253,0 @@ getSchemasStorageKey() { |
@@ -17,2 +17,3 @@ import * as IORedis from "ioredis"; | ||
table: string; | ||
tablePrefix: string; | ||
connection: string; | ||
@@ -19,0 +20,0 @@ indexUpdatedAt: boolean; |
{ | ||
"name": "ts-redis-orm", | ||
"version": "0.1.14", | ||
"version": "0.1.15", | ||
"description": "A full functional Redis Orm library written in Typescript.", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
@@ -41,3 +41,3 @@ # Redis ORM (Typescript) | ||
@Entity({connection: "default", table: "entity", indexUpdatedAt: true}) | ||
@Entity({connection: "default", table: "my_table", tablePrefix: "prefix_", indexUpdatedAt: true}) | ||
class MyEntity extends BaseEntity { | ||
@@ -44,0 +44,0 @@ @Column({primary: true, autoIncrement: true}) |
@@ -20,5 +20,6 @@ import Debug from "debug"; | ||
table = table || serviceInstance.getDefaultTable(this); | ||
const schemaErrors = await serviceInstance.compareSchemas(this, table); | ||
const tableName = serviceInstance.getTablePrefix(this) + table; | ||
const schemaErrors = await serviceInstance.compareSchemas(this, tableName); | ||
if (schemaErrors.length) { | ||
throw new RedisOrmSchemaError(`(${this.name}, ${table}) Mismatch with remote Schemas`, schemaErrors); | ||
throw new RedisOrmSchemaError(`(${this.name}, ${tableName}) Mismatch with remote Schemas`, schemaErrors); | ||
} | ||
@@ -70,4 +71,5 @@ | ||
table = table || serviceInstance.getDefaultTable(this); | ||
const tableName = serviceInstance.getTablePrefix(this) + table; | ||
const redis = await serviceInstance.getRedis(this); | ||
const remoteSchemas = await serviceInstance.getRemoteSchemas(this, table); | ||
const remoteSchemas = await serviceInstance.getRemoteSchemas(this, tableName); | ||
@@ -80,3 +82,3 @@ // we resync only if we found any schema exist | ||
serviceInstance.getSchemasJson(this), | ||
table, | ||
tableName, | ||
]; | ||
@@ -89,8 +91,8 @@ | ||
if (saveResult.error) { | ||
throw new RedisOrmEntityError(`(${this.name}, ${table}) ${saveResult.error}`); | ||
throw new RedisOrmEntityError(`(${this.name}, ${tableName}) ${saveResult.error}`); | ||
} | ||
debug(`(${this.name}, ${table}) resync db complete`); | ||
debug(`(${this.name}, ${tableName}) resync db complete`); | ||
} else { | ||
debug(`(${this.name}, ${table}) no schemas exist for resync db`); | ||
debug(`(${this.name}, ${tableName}) no schemas exist for resync db`); | ||
} | ||
@@ -106,4 +108,5 @@ } | ||
table = table || serviceInstance.getDefaultTable(this); | ||
const tableName = serviceInstance.getTablePrefix(this) + table; | ||
const redis = await serviceInstance.getRedis(this); | ||
const remoteSchemas = await serviceInstance.getRemoteSchemas(this, table); | ||
const remoteSchemas = await serviceInstance.getRemoteSchemas(this, tableName); | ||
@@ -114,3 +117,3 @@ // we truncate only if we found any schema exist | ||
const keys: [] = []; | ||
const params = [table]; | ||
const params = [tableName]; | ||
@@ -120,5 +123,5 @@ // remove everything | ||
debug(`(${this.name}, ${table}) truncate complete`); | ||
debug(`(${this.name}, ${tableName}) truncate complete`); | ||
} else { | ||
debug(`(${this.name}, ${table}) no schemas exist for truncate`); | ||
debug(`(${this.name}, ${tableName}) no schemas exist for truncate`); | ||
} | ||
@@ -150,2 +153,3 @@ } | ||
table: entityMeta.table, | ||
tablePrefix: entityMeta.tablePrefix, | ||
connection: entityMeta.connection, | ||
@@ -180,2 +184,3 @@ indexUpdatedAt: entityMeta.indexUpdatedAt, | ||
private _table: string = ""; | ||
private _tableName: string = ""; | ||
@@ -198,3 +203,3 @@ // flags | ||
this.updatedAt = now; | ||
this._table = serviceInstance.getDefaultTable(this.constructor); | ||
this.setTable(serviceInstance.getDefaultTable(this.constructor)); | ||
} | ||
@@ -248,2 +253,3 @@ | ||
this._table = table; | ||
this._tableName = serviceInstance.getTablePrefix(this.constructor) + table; | ||
} | ||
@@ -265,3 +271,3 @@ | ||
} else { | ||
throw new RedisOrmEntityError(`(${this.constructor.name}, ${this._table}) Invalid number value: ${value} for primary key: ${column}`); | ||
throw new RedisOrmEntityError(`(${this.constructor.name}, ${this._tableName}) Invalid number value: ${value} for primary key: ${column}`); | ||
} | ||
@@ -273,6 +279,6 @@ | ||
} else { | ||
throw new RedisOrmEntityError(`(${this.constructor.name}, ${this._table}) Invalid string value: '${value}' for primary key: ${column}`); | ||
throw new RedisOrmEntityError(`(${this.constructor.name}, ${this._tableName}) Invalid string value: '${value}' for primary key: ${column}`); | ||
} | ||
} else { | ||
throw new RedisOrmEntityError(`(${this.constructor.name}, ${this._table}) Invalid value: ${value} for primary key: ${column}`); | ||
throw new RedisOrmEntityError(`(${this.constructor.name}, ${this._tableName}) Invalid value: ${value} for primary key: ${column}`); | ||
} | ||
@@ -296,19 +302,19 @@ } | ||
if (this.isNew) { | ||
throw new RedisOrmEntityError(`(${this.constructor.name}, ${this._table}) You cannot increment a new entity`); | ||
throw new RedisOrmEntityError(`(${this.constructor.name}, ${this._tableName}) You cannot increment a new entity`); | ||
} | ||
if (serviceInstance.isPrimaryKey(this.constructor, column as string)) { | ||
throw new RedisOrmEntityError(`(${this.constructor.name}, ${this._table}) You cannot increment primary key`); | ||
throw new RedisOrmEntityError(`(${this.constructor.name}, ${this._tableName}) You cannot increment primary key`); | ||
} | ||
if (serviceInstance.isUniqueKey(this.constructor, column as string)) { | ||
throw new RedisOrmEntityError(`(${this.constructor.name}, ${this._table}) You cannot increment unique key`); | ||
throw new RedisOrmEntityError(`(${this.constructor.name}, ${this._tableName}) You cannot increment unique key`); | ||
} | ||
if (!serviceInstance.isNumberColumn(this.constructor, column as string)) { | ||
throw new RedisOrmEntityError(`(${this.constructor.name}, ${this._table}) Column need to be in the type of Number`); | ||
throw new RedisOrmEntityError(`(${this.constructor.name}, ${this._tableName}) Column need to be in the type of Number`); | ||
} | ||
if (!Number.isInteger(value)) { | ||
throw new RedisOrmEntityError(`(${this.constructor.name}, ${this._table}) Increment value need to be an integer`); | ||
throw new RedisOrmEntityError(`(${this.constructor.name}, ${this._tableName}) Increment value need to be an integer`); | ||
} | ||
@@ -394,3 +400,3 @@ | ||
if (this.isDeleted && !isRestore) { | ||
throw new RedisOrmEntityError(`(${this.constructor.name}, ${this._table}) You cannot update a deleted entity`); | ||
throw new RedisOrmEntityError(`(${this.constructor.name}, ${this._tableName}) You cannot update a deleted entity`); | ||
} | ||
@@ -433,3 +439,3 @@ | ||
this.isNew, | ||
this._table, | ||
this._tableName, | ||
autoIncrementKey, | ||
@@ -449,6 +455,6 @@ JSON.stringify(indexKeys), | ||
if (saveResult.error === "Mismatch with remote Schemas") { | ||
const schemaErrors = await serviceInstance.compareSchemas(this.constructor, this._table); | ||
throw new RedisOrmSchemaError(`(${this.constructor.name}, ${this._table}) ${saveResult.error}`, schemaErrors); | ||
const schemaErrors = await serviceInstance.compareSchemas(this.constructor, this._tableName); | ||
throw new RedisOrmSchemaError(`(${this.constructor.name}, ${this._tableName}) ${saveResult.error}`, schemaErrors); | ||
} else { | ||
throw new RedisOrmEntityError(`(${this.constructor.name}, ${this._table}) ${saveResult.error}`); | ||
throw new RedisOrmEntityError(`(${this.constructor.name}, ${this._tableName}) ${saveResult.error}`); | ||
} | ||
@@ -498,3 +504,3 @@ } | ||
if (this.isNew) { | ||
throw new RedisOrmEntityError(`(${this.constructor.name}, ${this._table}) You cannot delete a new entity`); | ||
throw new RedisOrmEntityError(`(${this.constructor.name}, ${this._tableName}) You cannot delete a new entity`); | ||
} | ||
@@ -505,3 +511,3 @@ | ||
if (!forceDelete && this.isDeleted) { | ||
throw new RedisOrmEntityError(`(${this.constructor.name}, ${this._table}) You cannot delete a deleted entity`); | ||
throw new RedisOrmEntityError(`(${this.constructor.name}, ${this._tableName}) You cannot delete a deleted entity`); | ||
} | ||
@@ -525,3 +531,3 @@ | ||
!forceDelete, | ||
this._table, | ||
this._tableName, | ||
deletedAt.getTime(), | ||
@@ -538,3 +544,3 @@ JSON.stringify(indexKeys), | ||
if (saveResult.error) { | ||
throw new RedisOrmEntityError(`(${this.constructor.name}, ${this._table}) ${saveResult.error}`); | ||
throw new RedisOrmEntityError(`(${this.constructor.name}, ${this._tableName}) ${saveResult.error}`); | ||
} | ||
@@ -541,0 +547,0 @@ |
@@ -10,2 +10,3 @@ import {RedisOrmDecoratorError} from ".."; | ||
table: "", | ||
tablePrefix: "", | ||
connection: "default", | ||
@@ -12,0 +13,0 @@ indexUpdatedAt: true, |
@@ -15,2 +15,3 @@ import fs from "fs"; | ||
class: (entityType as any).name, | ||
tablePrefix: serviceInstance.getTablePrefix(entityType), | ||
table: serviceInstance.getDefaultTable(entityType), | ||
@@ -17,0 +18,0 @@ schemas: serviceInstance.getSchemasJson(entityType), |
@@ -24,2 +24,3 @@ import Debug from "debug"; | ||
private _table: string = ""; | ||
private _tableName: string = ""; // prefix + _table | ||
private _onlyDeleted = false; | ||
@@ -37,3 +38,3 @@ private _offset = 0; | ||
constructor(private readonly _entityType: T) { | ||
this._table = serviceInstance.getDefaultTable(_entityType); | ||
this.setTable(serviceInstance.getDefaultTable(_entityType)); | ||
} | ||
@@ -45,2 +46,3 @@ | ||
this._table = table; | ||
this._tableName = serviceInstance.getTablePrefix(this._entityType) + this._table; | ||
return this; | ||
@@ -61,3 +63,3 @@ } | ||
if (entityId) { | ||
const entityStorageKey = serviceInstance.getEntityStorageKey(this._table, entityId); | ||
const entityStorageKey = serviceInstance.getEntityStorageKey(this._tableName, entityId); | ||
@@ -107,3 +109,3 @@ if (!entityStorageKey) { | ||
const id = await redis.hget( | ||
serviceInstance.getUniqueStorageKey(this._table, column as string), | ||
serviceInstance.getUniqueStorageKey(this._tableName, column as string), | ||
value.toString(), | ||
@@ -328,3 +330,3 @@ ); | ||
const indexStorageKey = serviceInstance.getIndexStorageKey(this._table, column as string); | ||
const indexStorageKey = serviceInstance.getIndexStorageKey(this._tableName, column as string); | ||
const entityId = serviceInstance.convertAsEntityId(this._entityType, idObject); | ||
@@ -381,3 +383,3 @@ | ||
this._limit, | ||
this._table, | ||
this._tableName, | ||
"", // aggregate | ||
@@ -420,3 +422,3 @@ "", // aggregate column | ||
// redis params | ||
const indexStorageKey = serviceInstance.getIndexStorageKey(this._table, column); | ||
const indexStorageKey = serviceInstance.getIndexStorageKey(this._tableName, column); | ||
const extraParams = ["LIMIT", this._offset.toString(), this._limit.toString()]; | ||
@@ -464,3 +466,3 @@ | ||
this._offset, // not used | ||
this._table, | ||
this._tableName, | ||
aggregate, | ||
@@ -509,5 +511,5 @@ aggregateColumn, | ||
if (max === "+inf" && min === "-inf") { | ||
count = await redis.zcard(serviceInstance.getIndexStorageKey(this._table, column)); | ||
count = await redis.zcard(serviceInstance.getIndexStorageKey(this._tableName, column)); | ||
} else { | ||
count = await redis.zcount(serviceInstance.getIndexStorageKey(this._table, column), min, max); | ||
count = await redis.zcount(serviceInstance.getIndexStorageKey(this._tableName, column), min, max); | ||
} | ||
@@ -539,3 +541,3 @@ | ||
const limit = `limit: ${this._limit}`; | ||
debugPerformance(`(${this._entityType.name}, ${this._table}) ${type} executed in ${executionTime}ms. ${indexWhere}. ${searchWhere}. ${sort}. ${groupBy}. ${offset}. ${limit}`); | ||
debugPerformance(`(${this._entityType.name}, ${this._tableName}) ${type} executed in ${executionTime}ms. ${indexWhere}. ${searchWhere}. ${sort}. ${groupBy}. ${offset}. ${limit}`); | ||
} | ||
@@ -548,3 +550,3 @@ } | ||
const executionTime = (diff[1] / 1000000).toFixed(2); | ||
debugPerformance(`(${this._entityType.name}, ${this._table}) ${type} executed in ${executionTime}ms. ${data}`); | ||
debugPerformance(`(${this._entityType.name}, ${this._tableName}) ${type} executed in ${executionTime}ms. ${data}`); | ||
} | ||
@@ -551,0 +553,0 @@ } |
@@ -81,2 +81,6 @@ import Debug from "debug"; | ||
public getTablePrefix(target: object): string { | ||
return this.getEntityMeta(target).tablePrefix; | ||
} | ||
public getConnection(target: object): string { | ||
@@ -226,7 +230,7 @@ return this.getEntityMeta(target).connection; | ||
public async compareSchemas(target: object, table: string): Promise<string[]> { | ||
public async compareSchemas(target: object, tableName: string): Promise<string[]> { | ||
let errors: string[] = []; | ||
try { | ||
const remoteSchemas = await this.getRemoteSchemas(target, table); | ||
const remoteSchemas = await this.getRemoteSchemas(target, tableName); | ||
if (remoteSchemas) { | ||
@@ -246,6 +250,6 @@ // we do such indirect case is to convert primitive types to strings | ||
public async getRemoteSchemas(target: object, table: string): Promise<{[key: string]: ISchema} | null> { | ||
public async getRemoteSchemas(target: object, tableName: string): Promise<{[key: string]: ISchema} | null> { | ||
const redis = await serviceInstance.getRedis(target); | ||
const storageKey = this.getSchemasStorageKey(); | ||
const remoteSchemasString = await redis.hget(storageKey, table); | ||
const remoteSchemasString = await redis.hget(storageKey, tableName); | ||
if (remoteSchemasString) { | ||
@@ -262,12 +266,12 @@ return JSON.parse(remoteSchemasString); | ||
public getEntityStorageKey(table: string, entityId: string) { | ||
return `entity:${table}:${entityId}`; | ||
public getEntityStorageKey(tableName: string, entityId: string) { | ||
return `entity:${tableName}:${entityId}`; | ||
} | ||
public getIndexStorageKey(table: string, column: string) { | ||
return `index:${table}:${column}`; | ||
public getIndexStorageKey(tableName: string, column: string) { | ||
return `index:${tableName}:${column}`; | ||
} | ||
public getUniqueStorageKey(table: string, column: string) { | ||
return `unique:${table}:${column}`; | ||
public getUniqueStorageKey(tableName: string, column: string) { | ||
return `unique:${tableName}:${column}`; | ||
} | ||
@@ -274,0 +278,0 @@ |
@@ -19,2 +19,3 @@ import * as IORedis from "ioredis"; | ||
table: string; | ||
tablePrefix: string; | ||
connection: string; | ||
@@ -21,0 +22,0 @@ indexUpdatedAt: boolean; |
@@ -196,3 +196,3 @@ import { assert, expect } from "chai"; | ||
} catch (err) { | ||
assert.equal(err.message, `(${TestingSchema1.name}, testing_schema) Unique key: unique1 with value: 0 already exist on entity id: 1. Current entity id: ${duplicatedEntity.id}`); | ||
assert.equal(err.message, `(${TestingSchema1.name}, testing_schema) Unique key (unique1) with value (0) already exist on entity id (1). Current entity id (${duplicatedEntity.id})`); | ||
} | ||
@@ -199,0 +199,0 @@ |
import { assert, expect } from "chai"; | ||
import {BaseEntity, Column, Entity, RedisOrmEntityError, RedisOrmSchemaError} from "../src/"; | ||
@Entity({table: "testing_tables"}) | ||
@Entity({table: "testing_tables", tablePrefix: "prefix_"}) | ||
class TestingTables extends BaseEntity { | ||
@@ -19,3 +19,3 @@ @Column({primary: true}) | ||
@Entity({table: "testing_tables_invalid"}) | ||
@Entity({table: "testing_tables_invalid", tablePrefix: "prefix_"}) | ||
class TestingTablesInvalid extends BaseEntity { | ||
@@ -22,0 +22,0 @@ @Column({primary: true}) |
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
285511
5667