@dwtechs/antity-pgsql 0.1.1 → 0.2.1

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
@@ -189,13 +189,19 @@ class SQLEntity {
189
189
 
190
190
  }
191
191
 
192
- filter(
192
+ function filter(
193
193
  first: number,
194
- rows: number | null,
194
+ rows: number,
195
195
  sortField: string | null,
196
- sortOrder: Sort,
196
+ sortOrder: Sort = "ASC",
197
197
  filters: Filters | null,
198
- ): { filterClause: string, args: (Filter["value"])[] } {
198
+ ): { filterClause: string, args: (Filter["value"])[] };
199
+
200
+ function execute(
201
+ query: string,
202
+ args: (string | number | boolean | Date | number[])[],
203
+ client: any,
204
+ ): Promise<PGResponse>;
199
205
 
200
206
 
201
207
  ```
@@ -25,17 +25,17 @@ https://github.com/DWTechs/Antity-pgsql.js
25
25
  */
26
26
 
27
27
  import { Entity, Property } from "@dwtechs/antity";
28
- import type { Request, NextFunction } from 'express';
28
+ import type { Request, Response, NextFunction } from 'express';
29
29
 
30
30
  export type Operation = "SELECT" | "INSERT" | "UPDATE" | "DELETE";
31
31
  export type Sort = "ASC" | "DESC";
32
32
  export type Filters = {
33
- [key: string]: Filter;
33
+ [key: string]: Filter;
34
34
  };
35
35
  export type Filter = {
36
- value: string | number | boolean | Date | number[];
37
- subProps?: string[];
38
- matchMode?: MatchMode;
36
+ value: string | number | boolean | Date | number[];
37
+ subProps?: string[];
38
+ matchMode?: MatchMode;
39
39
  };
40
40
  export type LogicalOperator = "AND" | "OR";
41
41
  export type Comparator = "=" | "<" | ">" | "<=" | ">=" | "<>" | "IS" | "IS NOT" | "IN" | "LIKE" | "NOT LIKE";
@@ -43,75 +43,77 @@ export type MatchMode = "startsWith" | "endsWith" | "contains" | "notContains" |
43
43
  export type MappedType = "string" | "number" | "date";
44
44
  export type Type = "boolean" | "string" | "number" | "integer" | "float" | "even" | "odd" | "positive" | "negative" | "powerOfTwo" | "ascii" | "array" | "jwt" | "symbol" | "email" | "regex" | "json" | "ipAddress" | "slug" | "hexadecimal" | "date" | "timestamp" | "function" | "htmlElement" | "htmlEventAttribute" | "node" | "object" | "geometry";
45
45
  export type Geometry = {
46
- lng: number;
47
- lat: number;
48
- radius: number;
49
- bounds: {
50
- minLng: number;
51
- minLat: number;
52
- maxLng: number;
53
- maxLat: number;
54
- };
46
+ lng: number;
47
+ lat: number;
48
+ radius: number;
49
+ bounds: {
50
+ minLng: number;
51
+ minLat: number;
52
+ maxLng: number;
53
+ maxLat: number;
54
+ };
55
55
  };
56
56
  export type PGResponse = {
57
- rows: Record<string, unknown>[];
58
- rowCount: number;
59
- total?: number;
60
- command?: string;
61
- oid?: number;
62
- fields?: unknown[];
63
- _parsers?: unknown[];
64
- _types?: unknown;
65
- RowCtor?: unknown;
66
- rowAsArray?: boolean;
67
- };
68
- export type Response = {
69
- rows: Record<string, unknown>[];
70
- total?: number;
57
+ rows: Record<string, unknown>[];
58
+ rowCount: number;
59
+ total?: number;
60
+ command?: string;
61
+ oid?: number;
62
+ fields?: unknown[];
63
+ _parsers?: unknown[];
64
+ _types?: unknown;
65
+ RowCtor?: unknown;
66
+ rowAsArray?: boolean;
71
67
  };
72
68
 
73
69
  declare class SQLEntity extends Entity {
74
- private _table;
75
- private sel;
76
- private ins;
77
- private upd;
78
- constructor(name: string, properties: Property[]);
79
- get table(): string;
80
- set table(table: string);
81
- query: {
82
- select: (paginate: boolean) => string;
83
- update: (rows: Record<string, unknown>[], consumerId: number | string, consumerName: string) => {
84
- query: string;
85
- args: unknown[];
86
- };
87
- insert: (rows: Record<string, unknown>[], consumerId: number | string, consumerName: string, rtn?: string) => {
88
- query: string;
89
- args: unknown[];
90
- };
91
- delete: () => string;
92
- return: (prop: string) => string;
70
+ private _table;
71
+ private sel;
72
+ private ins;
73
+ private upd;
74
+ constructor(name: string, properties: Property[]);
75
+ get table(): string;
76
+ set table(table: string);
77
+ query: {
78
+ select: (paginate: boolean) => string;
79
+ update: (rows: Record<string, unknown>[], consumerId: number | string, consumerName: string) => {
80
+ query: string;
81
+ args: unknown[];
82
+ };
83
+ insert: (rows: Record<string, unknown>[], consumerId: number | string, consumerName: string, rtn?: string) => {
84
+ query: string;
85
+ args: unknown[];
93
86
  };
94
- get(req: Request, res: Response, next: NextFunction): void;
95
- add(req: Request, res: Response, next: NextFunction): Promise<void>;
96
- update(req: Request, res: Response, next: NextFunction): Promise<void>;
97
- archive(req: Request, res: Response, next: NextFunction): Promise<void>;
98
- delete(req: Request, res: Response, next: NextFunction): void;
99
- private cleanFilters;
100
- private mapProps;
87
+ delete: () => string;
88
+ return: (prop: string) => string;
89
+ };
90
+ get(req: Request, res: Response, next: NextFunction): void;
91
+ add(req: Request, res: Response, next: NextFunction): Promise<void>;
92
+ update(req: Request, res: Response, next: NextFunction): Promise<void>;
93
+ archive(req: Request, res: Response, next: NextFunction): Promise<void>;
94
+ delete(req: Request, res: Response, next: NextFunction): void;
95
+ private cleanFilters;
96
+ private mapProps;
101
97
  }
102
98
 
103
- declare filter(
104
- first: number,
105
- rows: number | null,
106
- sortField: string | null,
107
- sortOrder: Sort,
108
- filters: Filters | null,
109
- ): { filterClause: string, args: (Filter["value"])[] } {]
99
+ declare function filter(
100
+ first: number,
101
+ rows: number | null,
102
+ sortField: string | null,
103
+ sortOrder: Sort | null,
104
+ filters: Filters | null,
105
+ ): { filterClause: string, args: (Filter["value"])[] };
110
106
 
107
+ declare function execute(
108
+ query: string,
109
+ args: (string | number | boolean | Date | number[])[],
110
+ client: any
111
+ ): Promise<PGResponse>;
111
112
 
112
113
  export {
113
114
  SQLEntity,
114
115
  Property,
115
116
  filter,
117
+ execute,
116
118
  };
117
119
 
@@ -127,8 +127,8 @@ function execute$1(query, args, clt) {
127
127
  return client
128
128
  .query(query, args)
129
129
  .then((res) => {
130
- perf.end(res, time);
131
130
  deleteIdleProperties(res);
131
+ perf.end(res, time);
132
132
  return res;
133
133
  })
134
134
  .catch((err) => {
@@ -144,6 +144,13 @@ function deleteIdleProperties(res) {
144
144
  res._types = undefined;
145
145
  res.RowCtor = undefined;
146
146
  res.rowAsArray = undefined;
147
+ res._prebuiltEmptyResultObject = undefined;
148
+ }
149
+
150
+ function quoteIfUppercase(word) {
151
+ if (/[A-Z]/.test(word))
152
+ return `"${word}"`;
153
+ return word;
147
154
  }
148
155
 
149
156
  class Select {
@@ -153,7 +160,7 @@ class Select {
153
160
  this._count = ", COUNT(*) OVER () AS total";
154
161
  }
155
162
  addProp(prop) {
156
- this._props.push(prop);
163
+ this._props.push(quoteIfUppercase(prop));
157
164
  this._cols = this._props.join(", ");
158
165
  }
159
166
  get props() {
@@ -162,7 +169,7 @@ class Select {
162
169
  query(table, paginate) {
163
170
  const p = paginate ? this._count : '';
164
171
  const c = this._cols ? this._cols : '*';
165
- return `SELECT ${c}${p} FROM "${table}"`;
172
+ return `SELECT ${c}${p} FROM ${quoteIfUppercase(table)}`;
166
173
  }
167
174
  execute(query, args, client) {
168
175
  return execute$1(query, args, client)
@@ -190,12 +197,12 @@ class Insert {
190
197
  this._nbProps = 2;
191
198
  }
192
199
  addProp(prop) {
193
- this._props = add$1(this._props, prop, this._props.length - 2);
200
+ this._props = add$1(this._props, quoteIfUppercase(prop), this._props.length - 2);
194
201
  this._cols = this._props.join(", ");
195
202
  this._nbProps++;
196
203
  }
197
204
  query(table, rows, consumerId, consumerName, rtn = "") {
198
- let query = `INSERT INTO "${table}" (${this._cols}) VALUES `;
205
+ let query = `INSERT INTO ${quoteIfUppercase(table)} (${this._cols}) VALUES `;
199
206
  const args = [];
200
207
  let i = 0;
201
208
  for (const row of rows) {
@@ -234,13 +241,13 @@ class Update {
234
241
  this._props = ["consumerId", "consumerName"];
235
242
  }
236
243
  addProp(prop) {
237
- this._props = add$1(this._props, prop, this._props.length - 2);
244
+ this._props = add$1(this._props, quoteIfUppercase(prop), this._props.length - 2);
238
245
  }
239
246
  query(table, rows, consumerId, consumerName) {
240
247
  rows = this.addConsumer(rows, consumerId, consumerName);
241
248
  const l = rows.length;
242
249
  const args = rows.map(row => row.id);
243
- let query = `UPDATE "${table}" SET `;
250
+ let query = `UPDATE "${quoteIfUppercase(table)}" SET `;
244
251
  let i = args.length + 1;
245
252
  for (const p of this._props) {
246
253
  if (rows[0][p] === undefined)
@@ -277,7 +284,7 @@ var __awaiter$1 = (undefined && undefined.__awaiter) || function (thisArg, _argu
277
284
  });
278
285
  };
279
286
  function query(table) {
280
- return `DELETE FROM "${table}" WHERE "archivedAt" < $1`;
287
+ return `DELETE FROM ${quoteIfUppercase(table)} WHERE "archivedAt" < $1`;
281
288
  }
282
289
  function execute(date, query, client) {
283
290
  return __awaiter$1(this, void 0, void 0, function* () {
@@ -368,7 +375,7 @@ function add(filters) {
368
375
  return { conditions, args };
369
376
  }
370
377
  function addOne(key, indexes, matchMode) {
371
- const sqlKey = `\"${key}\"`;
378
+ const sqlKey = `${quoteIfUppercase(key)}`;
372
379
  const comparator$1 = comparator(matchMode);
373
380
  const index$1 = index(indexes, matchMode);
374
381
  return comparator$1 ? `${sqlKey} ${comparator$1} ${index$1}` : "";
@@ -390,7 +397,8 @@ function where(conditions, operator = "AND") {
390
397
  function orderBy(sortField, sortOrder) {
391
398
  if (!sortField)
392
399
  return "";
393
- return ` ORDER BY "${sortField}" ${sortOrder}`;
400
+ const o = sortOrder || "ASC";
401
+ return ` ORDER BY ${quoteIfUppercase(sortField)} ${o}`;
394
402
  }
395
403
  function limit(rows, first) {
396
404
  return rows ? ` LIMIT ${rows} OFFSET ${first}` : "";
@@ -585,4 +593,4 @@ class SQLEntity extends Entity {
585
593
  }
586
594
  }
587
595
 
588
- export { SQLEntity, filter };
596
+ export { SQLEntity, execute$1 as execute, filter };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dwtechs/antity-pgsql",
3
- "version": "0.1.1",
3
+ "version": "0.2.1",
4
4
  "description": "Open source library for easy entity management",
5
5
  "keywords": [
6
6
  "entities"