@dwtechs/antity-pgsql 0.11.1 → 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}',
@@ -655,12 +688,12 @@ class SQLEntity extends Entity {
655
688
  });
656
689
  this.update = (req, res, next) => __awaiter(this, void 0, void 0, function* () {
657
690
  const l = res.locals;
658
- const rows = req.body.rows;
691
+ const r = req.body.rows;
659
692
  const dbClient = l.dbClient || null;
660
693
  const cId = l.consumerId;
661
694
  const cName = l.consumerName;
662
- log.debug(`${LOGS_PREFIX}update(rows=${rows.length}, consumerId=${cId})`);
663
- const chunks = chunk(rows);
695
+ log.debug(`${LOGS_PREFIX}update(rows=${r.length}, consumerId=${cId})`);
696
+ const chunks = chunk(r);
664
697
  for (const c of chunks) {
665
698
  const { query, args } = this.upd.query(this._schema, this._table, c, cId, cName);
666
699
  try {
@@ -670,19 +703,19 @@ class SQLEntity extends Entity {
670
703
  return next(err);
671
704
  }
672
705
  }
706
+ l.rows = r;
673
707
  next();
674
708
  });
675
709
  this.archive = (req, res, next) => __awaiter(this, void 0, void 0, function* () {
676
710
  const l = res.locals;
677
- let rows = req.body.rows;
711
+ let r = req.body.rows;
678
712
  const dbClient = l.dbClient || null;
679
713
  const cId = l.consumerId;
680
714
  const cName = l.consumerName;
681
- log.debug(`${LOGS_PREFIX}archive(rows=${rows.length}, consumerId=${cId})`);
682
- rows = rows.map((id) => (Object.assign(Object.assign({}, id), { archived: true })));
683
- const chunks = chunk(rows);
715
+ log.debug(`${LOGS_PREFIX}archive(rows=${r.length}, consumerId=${cId})`);
716
+ const chunks = chunk(r);
684
717
  for (const c of chunks) {
685
- 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);
686
719
  try {
687
720
  yield execute(query, args, dbClient);
688
721
  }
@@ -765,6 +798,9 @@ class SQLEntity extends Entity {
765
798
  throw new Error(`${LOGS_PREFIX}schema must be a string of length > 0`);
766
799
  this._schema = schema;
767
800
  }
801
+ get properties() {
802
+ return super.properties;
803
+ }
768
804
  get addArraySubstack() {
769
805
  return [this.normalizeArray, this.validateArray, this.add];
770
806
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dwtechs/antity-pgsql",
3
- "version": "0.11.1",
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"