@dwtechs/antity-pgsql 0.6.1 → 0.7.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
@@ -2,7 +2,7 @@
2
2
  [![License: MIT](https://img.shields.io/npm/l/@dwtechs/antity-pgsql.svg?color=brightgreen)](https://opensource.org/licenses/MIT)
3
3
  [![npm version](https://badge.fury.io/js/%40dwtechs%2Fantity-pgsql.svg)](https://www.npmjs.com/package/@dwtechs/antity-pgsql)
4
4
  [![last version release date](https://img.shields.io/github/release-date/DWTechs/Antity-pgsql.js)](https://www.npmjs.com/package/@dwtechs/antity-pgsql)
5
- ![Jest:coverage](https://img.shields.io/badge/Jest:coverage-100%25-brightgreen.svg)
5
+ ![Jest:coverage](https://img.shields.io/badge/Jest:coverage-54%25-brightgreen.svg)
6
6
 
7
7
  - [Synopsis](#synopsis)
8
8
  - [Support](#support)
@@ -123,7 +123,9 @@ const entity = new Entity("consumers", [
123
123
  router.get("/", ..., entity.get);
124
124
  router.post("/", entity.normalizeArray, entity.validateArray, ..., entity.add);
125
125
  router.put("/", entity.normalizeArray, entity.validateArray, ..., entity.update);
126
- router.put("/", ..., entity.archive);
126
+ router.put("/archive", ..., entity.archive);
127
+ router.delete("/", ..., entity.delete);
128
+ router.delete("/archived", ..., entity.deleteArchive);
127
129
 
128
130
  ```
129
131
 
@@ -181,29 +183,34 @@ class SQLEntity {
181
183
  query: string;
182
184
  args: (Filter["value"])[];
183
185
  };
184
- update: (
185
- rows: Record<string, unknown>[],
186
- consumerId?: number | string,
187
- consumerName?: string) => {
188
- query: string;
186
+ update: (
187
+ rows: Record<string, unknown>[],
188
+ consumerId?: number | string,
189
+ consumerName?: string) => {
190
+ query: string;
191
+ args: unknown[];
192
+ };
193
+ insert: (
194
+ rows: Record<string, unknown>[],
195
+ consumerId?: number | string,
196
+ consumerName?: string,
197
+ rtn?: string) => {
198
+ query: string;
189
199
  args: unknown[];
190
200
  };
191
- insert: (
192
- rows: Record<string, unknown>[],
193
- consumerId?: number | string,
194
- consumerName?: string,
195
- rtn?: string) => {
196
- query: string;
197
- args: unknown[];
198
- };
199
- delete: () => string;
200
- return: (prop: string) => string;
201
+ delete: (ids: number[]) => {
202
+ query: string;
203
+ args: number[];
204
+ };
205
+ deleteArchive: () => string;
206
+ return: (prop: string) => string;
201
207
  };
202
208
  get: (req: Request, res: Response, next: NextFunction) => void;
203
209
  add: (req: Request, res: Response, next: NextFunction) => Promise<void>;
204
210
  update: (req: Request, res: Response, next: NextFunction) => Promise<void>;
205
211
  archive: (req: Request, res: Response, next: NextFunction) => Promise<void>;
206
- delete: (req: Request, res: Response, next: NextFunction) => void;
212
+ delete: (req: Request, res: Response, next: NextFunction) => Promise<void>;
213
+ deleteArchive: (req: Request, res: Response, next: NextFunction) => void;
207
214
 
208
215
  }
209
216
 
@@ -223,9 +230,12 @@ function execute(
223
230
 
224
231
 
225
232
  ```
226
- get(), add(), update(), archive() and delete() methods are made to be used as Express.js middlewares.
233
+ get(), add(), update(), archive(), delete() and deleteArchive() methods are made to be used as Express.js middlewares.
227
234
  Each method will look for data to work on in the **req.body.rows** parameter.
228
235
 
236
+ - **delete()**: Deletes rows by their IDs. Expects `req.body.rows` to be an array of objects with `id` property: `[{id: 1}, {id: 2}]`
237
+ - **deleteArchive()**: Deletes archived rows that were archived before a specific date. Expects `req.body.date` to be a Date object.
238
+
229
239
 
230
240
  ## Match modes
231
241
 
@@ -96,14 +96,19 @@ declare class SQLEntity extends Entity {
96
96
  query: string;
97
97
  args: unknown[];
98
98
  };
99
- delete: () => string;
99
+ delete: (ids: number[]) => {
100
+ query: string;
101
+ args: number[];
102
+ };
103
+ deleteArchive: () => string;
100
104
  return: (prop: string) => string;
101
105
  };
102
106
  get: (req: Request, res: Response, next: NextFunction) => void;
103
107
  add: (req: Request, res: Response, next: NextFunction) => Promise<void>;
104
108
  update: (req: Request, res: Response, next: NextFunction) => Promise<void>;
105
109
  archive: (req: Request, res: Response, next: NextFunction) => Promise<void>;
106
- delete: (req: Request, res: Response, next: NextFunction) => void;
110
+ delete: (req: Request, res: Response, next: NextFunction) => Promise<void>;
111
+ deleteArchive: (req: Request, res: Response, next: NextFunction) => void;
107
112
  }
108
113
 
109
114
  declare function filter(
@@ -58,7 +58,7 @@ var perf = {
58
58
  end,
59
59
  };
60
60
 
61
- function execute$1(query, args, clt) {
61
+ function execute(query, args, clt) {
62
62
  const time = perf.start(query, args);
63
63
  const client = clt || pool;
64
64
  return client
@@ -233,7 +233,7 @@ class Select {
233
233
  };
234
234
  }
235
235
  execute(query, args, client) {
236
- return execute$1(query, args, client)
236
+ return execute(query, args, client)
237
237
  .then((r) => {
238
238
  if (!r.rowCount)
239
239
  throw { status: 404, msg: "Resource not found" };
@@ -293,10 +293,10 @@ class Insert {
293
293
  return { query, args };
294
294
  }
295
295
  rtn(prop) {
296
- return `RETURNING "${prop}"`;
296
+ return `RETURNING ${quoteIfUppercase(prop)}`;
297
297
  }
298
298
  execute(query, args, client) {
299
- return execute$1(query, args, client);
299
+ return execute(query, args, client);
300
300
  }
301
301
  }
302
302
 
@@ -322,6 +322,7 @@ class Update {
322
322
  const propsToUse = [...this._props];
323
323
  if (consumerId !== undefined && consumerName !== undefined)
324
324
  propsToUse.push("consumerId", "consumerName");
325
+ log.debug(`${LOGS_PREFIX}Update query input rows: ${JSON.stringify(rows, null, 2)}`);
325
326
  const l = rows.length;
326
327
  const args = rows.map(row => row.id);
327
328
  let query = `UPDATE "${quoteIfUppercase(table)}" SET `;
@@ -342,7 +343,7 @@ class Update {
342
343
  }
343
344
  execute(query, args, client) {
344
345
  return __awaiter$2(this, void 0, void 0, function* () {
345
- return execute$1(query, args, client);
346
+ return execute(query, args, client);
346
347
  });
347
348
  }
348
349
  addConsumer(rows, consumerId, consumerName) {
@@ -360,14 +361,20 @@ var __awaiter$1 = (undefined && undefined.__awaiter) || function (thisArg, _argu
360
361
  step((generator = generator.apply(thisArg, _arguments || [])).next());
361
362
  });
362
363
  };
363
- function query(table) {
364
+ function queryById(table, ids) {
365
+ return {
366
+ query: `DELETE FROM ${quoteIfUppercase(table)} WHERE id = ANY($1)`,
367
+ args: [ids]
368
+ };
369
+ }
370
+ function queryArchived(table) {
364
371
  return `DELETE FROM ${quoteIfUppercase(table)} WHERE "archivedAt" < $1`;
365
372
  }
366
- function execute(date, query, client) {
373
+ function executeArchived(date, query, client) {
367
374
  return __awaiter$1(this, void 0, void 0, function* () {
368
375
  let db;
369
376
  try {
370
- db = yield execute$1(query, [date], client);
377
+ db = yield execute(query, [date], client);
371
378
  }
372
379
  catch (err) {
373
380
  throw err;
@@ -560,8 +567,11 @@ class SQLEntity extends Entity {
560
567
  insert: (rows, consumerId, consumerName, rtn = "") => {
561
568
  return this.ins.query(this.table, rows, consumerId, consumerName, rtn);
562
569
  },
563
- delete: () => {
564
- return query(this.table);
570
+ delete: (ids) => {
571
+ return queryById(this.table, ids);
572
+ },
573
+ deleteArchive: () => {
574
+ return queryArchived(this.table);
565
575
  },
566
576
  return: (prop) => {
567
577
  return this.ins.rtn(prop);
@@ -603,7 +613,7 @@ class SQLEntity extends Entity {
603
613
  const { query, args } = this.ins.query(this._table, c, cId, cName, rtn);
604
614
  let db;
605
615
  try {
606
- db = yield execute$1(query, args, dbClient);
616
+ db = yield execute(query, args, dbClient);
607
617
  }
608
618
  catch (err) {
609
619
  return next(err);
@@ -627,7 +637,7 @@ class SQLEntity extends Entity {
627
637
  for (const c of chunks) {
628
638
  const { query, args } = this.upd.query(this._table, c, cId, cName);
629
639
  try {
630
- yield execute$1(query, args, dbClient);
640
+ yield execute(query, args, dbClient);
631
641
  }
632
642
  catch (err) {
633
643
  return next(err);
@@ -647,7 +657,7 @@ class SQLEntity extends Entity {
647
657
  for (const c of chunks) {
648
658
  const { query, args } = this.upd.query(this._table, c, cId, cName);
649
659
  try {
650
- yield execute$1(query, args, dbClient);
660
+ yield execute(query, args, dbClient);
651
661
  }
652
662
  catch (err) {
653
663
  return next(err);
@@ -655,12 +665,26 @@ class SQLEntity extends Entity {
655
665
  }
656
666
  next();
657
667
  });
658
- this.delete = (req, res, next) => {
668
+ this.delete = (req, res, next) => __awaiter(this, void 0, void 0, function* () {
669
+ const rows = req.body.rows;
670
+ const dbClient = res.locals.dbClient || null;
671
+ const ids = rows.map((row) => row.id);
672
+ log.debug(`${LOGS_PREFIX}delete ${rows.length} rows : (${ids.join(", ")})`);
673
+ const { query, args } = queryById(this._table, ids);
674
+ try {
675
+ yield execute(query, args, dbClient);
676
+ }
677
+ catch (err) {
678
+ return next(err);
679
+ }
680
+ next();
681
+ });
682
+ this.deleteArchive = (req, res, next) => {
659
683
  const date = req.body.date;
660
684
  const dbClient = res.locals.dbClient || null;
661
- log.debug(`${LOGS_PREFIX}delete archived`);
662
- const q = query(this._table);
663
- execute(date, q, dbClient)
685
+ log.debug(`${LOGS_PREFIX}deleteArchive(date=${date})`);
686
+ const q = queryArchived(this._table);
687
+ executeArchived(date, q, dbClient)
664
688
  .then(() => next())
665
689
  .catch((err) => next(err));
666
690
  };
@@ -696,4 +720,4 @@ class SQLEntity extends Entity {
696
720
  }
697
721
  }
698
722
 
699
- export { SQLEntity, execute$1 as execute, filter };
723
+ export { SQLEntity, execute, filter };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dwtechs/antity-pgsql",
3
- "version": "0.6.1",
3
+ "version": "0.7.0",
4
4
  "description": "Open source library to add PostgreSQL support to @dwtechs/Antity entities.",
5
5
  "keywords": [
6
6
  "entities"