ismx-nexo-node-app 0.4.114 → 0.4.115

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -233,6 +233,20 @@ class RepositoryDatabasePostgres extends RepositoryDatabase_1.default {
233
233
  return this.query(query, values).then((result) => { var _a, _b; return new Repository_1.Hash((_b = (_a = result[0]) === null || _a === void 0 ? void 0 : _a.list) !== null && _b !== void 0 ? _b : {}); });
234
234
  });
235
235
  }
236
+ run(name_1) {
237
+ return __awaiter(this, arguments, void 0, function* (name, params = [], filters) {
238
+ var _a;
239
+ let { where, values } = this.anyWhere(filters);
240
+ let placeholders = (_a = values === null || values === void 0 ? void 0 : values.map((_, index) => "$" + (index + 1 + where.length))) !== null && _a !== void 0 ? _a : [];
241
+ let query = `SELECT ${name}(${placeholders.join(',')}) WHERE ${where}`;
242
+ return this.query(query, [...values, ...params]);
243
+ });
244
+ }
245
+ runValue(name_1) {
246
+ return __awaiter(this, arguments, void 0, function* (name, params = [], filters) {
247
+ return this.run(name, params, filters).then((result) => { var _a; return Object.values((_a = result[0]) !== null && _a !== void 0 ? _a : {})[0]; });
248
+ });
249
+ }
236
250
  toWhere(tableName, filters = {}, alias = "", index = 1) {
237
251
  let table = this.tables[tableName];
238
252
  let columns = PostgresUtils_1.default.columns(table);
@@ -256,6 +270,28 @@ class RepositoryDatabasePostgres extends RepositoryDatabase_1.default {
256
270
  }
257
271
  return { where, values };
258
272
  }
273
+ anyWhere(filters, alias = "", index = 1) {
274
+ let where = "TRUE";
275
+ let iter = index;
276
+ let values = [];
277
+ for (let key of Object.keys(filters)) {
278
+ let value = filters[key];
279
+ let column = PostgresUtils_1.default.camelToSnake(key);
280
+ if (value === undefined)
281
+ continue;
282
+ if (alias !== "")
283
+ alias += ".";
284
+ if (value instanceof Array)
285
+ where += ` AND ${alias}${column} = ANY(\$${iter++})`;
286
+ else if (value === null)
287
+ where += ` AND ${alias}${column} IS NULL`;
288
+ else
289
+ where += ` AND ${alias}${column}=\$${iter++}`;
290
+ if (value !== null)
291
+ values.push(value);
292
+ }
293
+ return { where, values };
294
+ }
259
295
  introspect() {
260
296
  return __awaiter(this, void 0, void 0, function* () {
261
297
  let result = yield this.native(`
@@ -52,11 +52,17 @@ export default class RepositoryDatabasePostgres extends RepositoryDatabase {
52
52
  [key: string]: Valuable | Array<Valuable>;
53
53
  }): Promise<string[]>;
54
54
  selectHash<T>(tableName: string, select: string, filters: Partial<T>): Promise<Hash<Primitive>>;
55
+ run<T>(name: string, params?: Valuable[], filters?: Partial<T>): Promise<T[]>;
56
+ runValue<T>(name: string, params?: Valuable[], filters?: Partial<T>): Promise<Valuable | undefined>;
55
57
  toWhere(tableName: string, filters?: {
56
58
  [key: string]: Valuable | Array<Valuable>;
57
59
  }, alias?: string, index?: number): {
58
60
  where: string;
59
61
  values: (string | number | symbol | Date | Valuable[])[];
60
62
  };
63
+ anyWhere(filters: any, alias?: string, index?: number): {
64
+ where: string;
65
+ values: any[];
66
+ };
61
67
  private introspect;
62
68
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ismx-nexo-node-app",
3
- "version": "0.4.114",
3
+ "version": "0.4.115",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "build": "rm -rf ./dist && npx tsc",
@@ -204,6 +204,18 @@ export default class RepositoryDatabasePostgres extends RepositoryDatabase
204
204
  return this.query<{list:Record<Primitive, null>}>(query, values).then((result) => new Hash(result[0]?.list ?? {}));
205
205
  }
206
206
 
207
+ async run<T>(name: string, params: Valuable[] = [], filters?: Partial<T>): Promise<T[]>
208
+ {
209
+ let { where, values } = this.anyWhere(filters as any);
210
+ let placeholders = values?.map((_, index) => "$"+(index+1+where.length)) ?? [];
211
+ let query = `SELECT ${name}(${placeholders.join(',')}) WHERE ${where}`;
212
+ return this.query<any>(query, [ ...values, ...params ])
213
+ }
214
+
215
+ async runValue<T>(name: string, params: Valuable[] = [], filters?: Partial<T>): Promise<Valuable | undefined> {
216
+ return this.run<T>(name, params, filters).then((result) => Object.values(result[0] ?? {})[0] as Valuable)
217
+ }
218
+
207
219
  toWhere(tableName: string, filters: { [key: string]: Valuable | Array<Valuable> }={}, alias:string = "", index=1)
208
220
  {
209
221
  let table = this.tables[tableName];
@@ -223,6 +235,24 @@ export default class RepositoryDatabasePostgres extends RepositoryDatabase
223
235
  return { where, values };
224
236
  }
225
237
 
238
+ anyWhere(filters: any, alias: string = "", index=1)
239
+ {
240
+ let where = "TRUE"; let iter = index; let values = [];
241
+
242
+ for (let key of Object.keys(filters)) {
243
+ let value = filters[key]
244
+ let column = PostgresUtils.camelToSnake(key);
245
+ if (value === undefined) continue;
246
+ if (alias !== "") alias += "."
247
+ if (value instanceof Array) where += ` AND ${alias}${column} = ANY(\$${iter++})`
248
+ else if (value === null) where += ` AND ${alias}${column} IS NULL`
249
+ else where += ` AND ${alias}${column}=\$${iter++}`
250
+ if (value !== null) values.push(value);
251
+ }
252
+
253
+ return { where, values };
254
+ }
255
+
226
256
  private async introspect() {
227
257
  let result = await this.native(`
228
258
  SELECT table_name as name,