@dwtechs/antity-pgsql 0.6.0 → 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 +29 -19
- package/dist/antity-pgsql.d.ts +7 -2
- package/dist/antity-pgsql.js +46 -24
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
[](https://opensource.org/licenses/MIT)
|
|
3
3
|
[](https://www.npmjs.com/package/@dwtechs/antity-pgsql)
|
|
4
4
|
[](https://www.npmjs.com/package/@dwtechs/antity-pgsql)
|
|
5
|
-

|
|
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
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
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
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
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
|
|
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
|
|
package/dist/antity-pgsql.d.ts
CHANGED
|
@@ -96,14 +96,19 @@ declare class SQLEntity extends Entity {
|
|
|
96
96
|
query: string;
|
|
97
97
|
args: unknown[];
|
|
98
98
|
};
|
|
99
|
-
delete: () =>
|
|
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(
|
package/dist/antity-pgsql.js
CHANGED
|
@@ -58,7 +58,7 @@ var perf = {
|
|
|
58
58
|
end,
|
|
59
59
|
};
|
|
60
60
|
|
|
61
|
-
function execute
|
|
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
|
|
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
|
|
296
|
+
return `RETURNING ${quoteIfUppercase(prop)}`;
|
|
297
297
|
}
|
|
298
298
|
execute(query, args, client) {
|
|
299
|
-
return execute
|
|
299
|
+
return execute(query, args, client);
|
|
300
300
|
}
|
|
301
301
|
}
|
|
302
302
|
|
|
@@ -314,16 +314,15 @@ class Update {
|
|
|
314
314
|
this._props = [];
|
|
315
315
|
}
|
|
316
316
|
addProp(prop) {
|
|
317
|
-
this._props.push(
|
|
317
|
+
this._props.push(prop);
|
|
318
318
|
}
|
|
319
319
|
query(table, rows, consumerId, consumerName) {
|
|
320
|
-
if (consumerId !== undefined && consumerName !== undefined)
|
|
320
|
+
if (consumerId !== undefined && consumerName !== undefined)
|
|
321
321
|
rows = this.addConsumer(rows, consumerId, consumerName);
|
|
322
|
-
}
|
|
323
322
|
const propsToUse = [...this._props];
|
|
324
|
-
if (consumerId !== undefined && consumerName !== undefined)
|
|
323
|
+
if (consumerId !== undefined && consumerName !== undefined)
|
|
325
324
|
propsToUse.push("consumerId", "consumerName");
|
|
326
|
-
}
|
|
325
|
+
log.debug(`${LOGS_PREFIX}Update query input rows: ${JSON.stringify(rows, null, 2)}`);
|
|
327
326
|
const l = rows.length;
|
|
328
327
|
const args = rows.map(row => row.id);
|
|
329
328
|
let query = `UPDATE "${quoteIfUppercase(table)}" SET `;
|
|
@@ -331,7 +330,7 @@ class Update {
|
|
|
331
330
|
for (const p of propsToUse) {
|
|
332
331
|
if (rows[0][p] === undefined)
|
|
333
332
|
continue;
|
|
334
|
-
query += `${p} = CASE `;
|
|
333
|
+
query += `${quoteIfUppercase(p)} = CASE `;
|
|
335
334
|
for (let j = 0; j < l; j++) {
|
|
336
335
|
const row = rows[j];
|
|
337
336
|
query += `WHEN id = $${j + 1} THEN $${i++} `;
|
|
@@ -344,7 +343,7 @@ class Update {
|
|
|
344
343
|
}
|
|
345
344
|
execute(query, args, client) {
|
|
346
345
|
return __awaiter$2(this, void 0, void 0, function* () {
|
|
347
|
-
return execute
|
|
346
|
+
return execute(query, args, client);
|
|
348
347
|
});
|
|
349
348
|
}
|
|
350
349
|
addConsumer(rows, consumerId, consumerName) {
|
|
@@ -362,14 +361,20 @@ var __awaiter$1 = (undefined && undefined.__awaiter) || function (thisArg, _argu
|
|
|
362
361
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
363
362
|
});
|
|
364
363
|
};
|
|
365
|
-
function
|
|
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) {
|
|
366
371
|
return `DELETE FROM ${quoteIfUppercase(table)} WHERE "archivedAt" < $1`;
|
|
367
372
|
}
|
|
368
|
-
function
|
|
373
|
+
function executeArchived(date, query, client) {
|
|
369
374
|
return __awaiter$1(this, void 0, void 0, function* () {
|
|
370
375
|
let db;
|
|
371
376
|
try {
|
|
372
|
-
db = yield execute
|
|
377
|
+
db = yield execute(query, [date], client);
|
|
373
378
|
}
|
|
374
379
|
catch (err) {
|
|
375
380
|
throw err;
|
|
@@ -562,8 +567,11 @@ class SQLEntity extends Entity {
|
|
|
562
567
|
insert: (rows, consumerId, consumerName, rtn = "") => {
|
|
563
568
|
return this.ins.query(this.table, rows, consumerId, consumerName, rtn);
|
|
564
569
|
},
|
|
565
|
-
delete: () => {
|
|
566
|
-
return
|
|
570
|
+
delete: (ids) => {
|
|
571
|
+
return queryById(this.table, ids);
|
|
572
|
+
},
|
|
573
|
+
deleteArchive: () => {
|
|
574
|
+
return queryArchived(this.table);
|
|
567
575
|
},
|
|
568
576
|
return: (prop) => {
|
|
569
577
|
return this.ins.rtn(prop);
|
|
@@ -605,7 +613,7 @@ class SQLEntity extends Entity {
|
|
|
605
613
|
const { query, args } = this.ins.query(this._table, c, cId, cName, rtn);
|
|
606
614
|
let db;
|
|
607
615
|
try {
|
|
608
|
-
db = yield execute
|
|
616
|
+
db = yield execute(query, args, dbClient);
|
|
609
617
|
}
|
|
610
618
|
catch (err) {
|
|
611
619
|
return next(err);
|
|
@@ -629,7 +637,7 @@ class SQLEntity extends Entity {
|
|
|
629
637
|
for (const c of chunks) {
|
|
630
638
|
const { query, args } = this.upd.query(this._table, c, cId, cName);
|
|
631
639
|
try {
|
|
632
|
-
yield execute
|
|
640
|
+
yield execute(query, args, dbClient);
|
|
633
641
|
}
|
|
634
642
|
catch (err) {
|
|
635
643
|
return next(err);
|
|
@@ -649,7 +657,7 @@ class SQLEntity extends Entity {
|
|
|
649
657
|
for (const c of chunks) {
|
|
650
658
|
const { query, args } = this.upd.query(this._table, c, cId, cName);
|
|
651
659
|
try {
|
|
652
|
-
yield execute
|
|
660
|
+
yield execute(query, args, dbClient);
|
|
653
661
|
}
|
|
654
662
|
catch (err) {
|
|
655
663
|
return next(err);
|
|
@@ -657,12 +665,26 @@ class SQLEntity extends Entity {
|
|
|
657
665
|
}
|
|
658
666
|
next();
|
|
659
667
|
});
|
|
660
|
-
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) => {
|
|
661
683
|
const date = req.body.date;
|
|
662
684
|
const dbClient = res.locals.dbClient || null;
|
|
663
|
-
log.debug(`${LOGS_PREFIX}
|
|
664
|
-
const q =
|
|
665
|
-
|
|
685
|
+
log.debug(`${LOGS_PREFIX}deleteArchive(date=${date})`);
|
|
686
|
+
const q = queryArchived(this._table);
|
|
687
|
+
executeArchived(date, q, dbClient)
|
|
666
688
|
.then(() => next())
|
|
667
689
|
.catch((err) => next(err));
|
|
668
690
|
};
|
|
@@ -698,4 +720,4 @@ class SQLEntity extends Entity {
|
|
|
698
720
|
}
|
|
699
721
|
}
|
|
700
722
|
|
|
701
|
-
export { SQLEntity, execute
|
|
723
|
+
export { SQLEntity, execute, filter };
|