@dwtechs/antity-pgsql 0.12.0 → 0.13.0

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.
package/README.md CHANGED
@@ -208,6 +208,13 @@ class SQLEntity {
208
208
  query: string;
209
209
  args: unknown[];
210
210
  };
211
+ archive: (
212
+ rows: Record<string, unknown>[],
213
+ consumerId?: number | string,
214
+ consumerName?: string) => {
215
+ query: string;
216
+ args: unknown[];
217
+ };
211
218
  insert: (
212
219
  rows: Record<string, unknown>[],
213
220
  consumerId?: number | string,
@@ -278,6 +285,7 @@ Using substacks simplifies your route definitions and ensures consistent data pr
278
285
  ### Query Methods
279
286
 
280
287
  - **query.select()**: Generates a SELECT query. When the `rows` parameter is provided (not null), pagination is automatically enabled and the query includes `COUNT(*) OVER () AS total` to return the total number of rows. The total count is extracted from results and returned separately from the row data.
288
+ - **query.archive()**: Generates a simplified `UPDATE ... SET archived = true WHERE id IN (...)` query. Accepts an array of objects with `id` property. Optionally appends `consumerId` and `consumerName` for history tracking. Does not require an `archived` field in the rows — it is set directly in the SQL.
281
289
  - **delete()**: Deletes rows by their IDs. Expects `req.body.rows` to be an array of objects with `id` property: `[{id: 1}, {id: 2}]`
282
290
  - **deleteArchive()**: Deletes archived rows that were archived before a specific date using a PostgreSQL SECURITY DEFINER function. Expects `req.body.date` to be a Date object.
283
291
  - **getHistory()**: Retrieves modification history for rows from the `log.history` table. Expects `req.body.rows` to be an array of objects with `id` property. Returns all historical records for the specified entity IDs.
@@ -98,6 +98,7 @@ export declare class SQLEntity extends Entity {
98
98
  private sel: unknown;
99
99
  private ins: unknown;
100
100
  private upd: unknown;
101
+ private arc: unknown;
101
102
 
102
103
  constructor(name: string, properties: Property[], schema?: string);
103
104
 
@@ -106,7 +107,9 @@ export declare class SQLEntity extends Entity {
106
107
 
107
108
  get schema(): string;
108
109
  set schema(schema: string);
109
-
110
+
111
+ get properties(): Property[];
112
+
110
113
  get addArraySubstack(): SubstackTuple;
111
114
  get addOneSubstack(): SubstackTuple;
112
115
  get updateArraySubstack(): SubstackTuple;
@@ -133,6 +136,15 @@ export declare class SQLEntity extends Entity {
133
136
  args: unknown[];
134
137
  };
135
138
 
139
+ archive: (
140
+ rows: Record<string, unknown>[],
141
+ consumerId?: number | string,
142
+ consumerName?: string
143
+ ) => {
144
+ query: string;
145
+ args: unknown[];
146
+ };
147
+
136
148
  insert: (
137
149
  rows: Record<string, unknown>[],
138
150
  consumerId?: number | string,
@@ -320,7 +320,7 @@ class Insert {
320
320
  }
321
321
  }
322
322
 
323
- var __awaiter$2 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
323
+ var __awaiter$3 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
324
324
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
325
325
  return new (P || (P = Promise))(function (resolve, reject) {
326
326
  function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
@@ -362,7 +362,7 @@ class Update {
362
362
  return { query, args };
363
363
  }
364
364
  execute(query, args, client) {
365
- return __awaiter$2(this, void 0, void 0, function* () {
365
+ return __awaiter$3(this, void 0, void 0, function* () {
366
366
  return execute(query, args, client);
367
367
  });
368
368
  }
@@ -372,6 +372,35 @@ class Update {
372
372
  }
373
373
  }
374
374
 
375
+ var __awaiter$2 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
376
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
377
+ return new (P || (P = Promise))(function (resolve, reject) {
378
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
379
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
380
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
381
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
382
+ });
383
+ };
384
+ class Archive {
385
+ query(schema, table, rows, consumerId, consumerName) {
386
+ log.debug(`${LOGS_PREFIX}Archive query input rows: ${JSON.stringify(rows, null, 2)}`);
387
+ const l = rows.length;
388
+ const args = rows.map(row => row.id);
389
+ let query = `UPDATE ${quoteIfUppercase(schema)}.${quoteIfUppercase(table)} SET archived = true`;
390
+ if (consumerId !== undefined && consumerName !== undefined) {
391
+ query += `, ${quoteIfUppercase("consumerId")} = $${l + 1}, ${quoteIfUppercase("consumerName")} = $${l + 2}`;
392
+ args.push(consumerId, consumerName);
393
+ }
394
+ query += ` WHERE id IN ${$i(l, 0)}`;
395
+ return { query, args };
396
+ }
397
+ execute(query, args, client) {
398
+ return __awaiter$2(this, void 0, void 0, function* () {
399
+ return execute(query, args, client);
400
+ });
401
+ }
402
+ }
403
+
375
404
  var __awaiter$1 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
376
405
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
377
406
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -584,6 +613,7 @@ class SQLEntity extends Entity {
584
613
  this.sel = new Select();
585
614
  this.ins = new Insert();
586
615
  this.upd = new Update();
616
+ this.arc = new Archive();
587
617
  this.query = {
588
618
  select: (first = 0, rows = null, sortField = null, sortOrder = null, filters = null) => {
589
619
  return this.sel.query(this.schema, this.table, first, rows, sortField, sortOrder, filters);
@@ -597,6 +627,9 @@ class SQLEntity extends Entity {
597
627
  delete: (ids) => {
598
628
  return queryById(this.schema, this.table, ids);
599
629
  },
630
+ archive: (rows, consumerId, consumerName) => {
631
+ return this.arc.query(this.schema, this.table, rows, consumerId, consumerName);
632
+ },
600
633
  deleteArchive: () => {
601
634
  return queryByDate();
602
635
  },
@@ -612,7 +645,7 @@ class SQLEntity extends Entity {
612
645
  const rows = b.rows || null;
613
646
  const sortField = b.sortField || null;
614
647
  const sortOrder = b.sortOrder === -1 || b.sortOrder === "DESC" ? "DESC" : "ASC";
615
- const filters = cleanFilters(b.filters, this.properties || []) || null;
648
+ const filters = cleanFilters(b.filters, this.properties) || null;
616
649
  const pagination = b.pagination || false;
617
650
  const dbClient = l.dbClient || null;
618
651
  log.debug(`get(first='${first}', rows='${rows}',
@@ -675,15 +708,14 @@ class SQLEntity extends Entity {
675
708
  });
676
709
  this.archive = (req, res, next) => __awaiter(this, void 0, void 0, function* () {
677
710
  const l = res.locals;
678
- let rows = req.body.rows;
711
+ let r = req.body.rows;
679
712
  const dbClient = l.dbClient || null;
680
713
  const cId = l.consumerId;
681
714
  const cName = l.consumerName;
682
- log.debug(`${LOGS_PREFIX}archive(rows=${rows.length}, consumerId=${cId})`);
683
- rows = rows.map((id) => (Object.assign(Object.assign({}, id), { archived: true })));
684
- const chunks = chunk(rows);
715
+ log.debug(`${LOGS_PREFIX}archive(rows=${r.length}, consumerId=${cId})`);
716
+ const chunks = chunk(r);
685
717
  for (const c of chunks) {
686
- const { query, args } = this.upd.query(this._schema, this._table, c, cId, cName);
718
+ const { query, args } = this.arc.query(this._schema, this._table, c, cId, cName);
687
719
  try {
688
720
  yield execute(query, args, dbClient);
689
721
  }
@@ -766,6 +798,9 @@ class SQLEntity extends Entity {
766
798
  throw new Error(`${LOGS_PREFIX}schema must be a string of length > 0`);
767
799
  this._schema = schema;
768
800
  }
801
+ get properties() {
802
+ return super.properties;
803
+ }
769
804
  get addArraySubstack() {
770
805
  return [this.normalizeArray, this.validateArray, this.add];
771
806
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dwtechs/antity-pgsql",
3
- "version": "0.12.0",
3
+ "version": "0.13.0",
4
4
  "description": "Open source library to add PostgreSQL support to @dwtechs/Antity entities.",
5
5
  "keywords": [
6
6
  "entities"