sedentary-pg 0.0.45 → 0.0.46

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/dist/cjs/index.js CHANGED
@@ -1,13 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SedentaryPG = exports.TransactionPG = exports.Type = exports.EntryBase = void 0;
3
+ exports.SedentaryPG = exports.Type = exports.EntryBase = exports.TransactionPG = void 0;
4
4
  const sedentary_1 = require("sedentary");
5
5
  const pgdb_1 = require("./pgdb");
6
+ var pgdb_2 = require("./pgdb");
7
+ Object.defineProperty(exports, "TransactionPG", { enumerable: true, get: function () { return pgdb_2.TransactionPG; } });
6
8
  var sedentary_2 = require("sedentary");
7
9
  Object.defineProperty(exports, "EntryBase", { enumerable: true, get: function () { return sedentary_2.EntryBase; } });
8
10
  Object.defineProperty(exports, "Type", { enumerable: true, get: function () { return sedentary_2.Type; } });
9
- var pgdb_2 = require("./pgdb");
10
- Object.defineProperty(exports, "TransactionPG", { enumerable: true, get: function () { return pgdb_2.TransactionPG; } });
11
11
  class SedentaryPG extends sedentary_1.Sedentary {
12
12
  constructor(connection, options) {
13
13
  super(options);
package/dist/cjs/pgdb.js CHANGED
@@ -97,11 +97,9 @@ class PGDB extends sedentary_1.DB {
97
97
  throw new Error("SedentaryPG: Can't escape null nor undefined values; use the 'IS NULL' operator instead");
98
98
  if (typeof value === "boolean" || typeof value === "number")
99
99
  return value.toString();
100
- if (typeof value === "string")
101
- return (0, pg_format_1.default)("%L", value);
102
- //if(value instanceof Date)
103
- return (0, pg_format_1.default)("%L", value).replace(/\.\d\d\d\+/, "+");
104
- //return format("%L", JSON.stringify(value));
100
+ if (value instanceof Date)
101
+ return (0, pg_format_1.default)("%L", value).replace(/\.\d\d\d\+/, "+");
102
+ return (0, pg_format_1.default)("%L", value);
105
103
  }
106
104
  fill(attributes, row, entry) {
107
105
  const loaded = {};
@@ -161,11 +159,11 @@ class PGDB extends sedentary_1.DB {
161
159
  const pkFldName = pk.fieldName;
162
160
  return async function () {
163
161
  const client = this.tx ? this.tx._client : await self.pool.connect();
164
- let removed = false;
162
+ let removed;
165
163
  try {
166
164
  const query = `DELETE FROM ${tableName} WHERE ${pkFldName} = ${self.escape(this[pkAttrName])}`;
167
165
  self.log(query);
168
- removed = (await client.query(query)).rowCount === 1;
166
+ removed = (await client.query(query)).rowCount;
169
167
  }
170
168
  finally {
171
169
  if (!this.tx)
@@ -182,6 +180,14 @@ class PGDB extends sedentary_1.DB {
182
180
  return async function () {
183
181
  const client = this.tx ? this.tx._client : await self.pool.connect();
184
182
  let changed = false;
183
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
184
+ let result = null;
185
+ const save = async (query) => {
186
+ self.log(query);
187
+ changed = true;
188
+ result = await client.query(query + " RETURNING *");
189
+ self.fill(attributes, result.rows[0], this);
190
+ };
185
191
  try {
186
192
  const { loaded } = this;
187
193
  if (loaded) {
@@ -191,12 +197,8 @@ class PGDB extends sedentary_1.DB {
191
197
  if (value !== loaded[attribute])
192
198
  actions.push(`${attributes[attribute]} = ${self.escape(value)}`);
193
199
  }
194
- if (actions.length) {
195
- const query = `UPDATE ${tableName} SET ${actions.join(", ")} WHERE ${pkFldName} = ${self.escape(this[pkAttrName])}`;
196
- self.log(query);
197
- self.fill(attributes, (await client.query(query + " RETURNING *")).rows[0], this);
198
- changed = true;
199
- }
200
+ if (actions.length)
201
+ await save(`UPDATE ${tableName} SET ${actions.join(", ")} WHERE ${pkFldName} = ${self.escape(this[pkAttrName])}`);
200
202
  }
201
203
  else {
202
204
  const fields = [];
@@ -208,17 +210,14 @@ class PGDB extends sedentary_1.DB {
208
210
  values.push(self.escape(value));
209
211
  }
210
212
  }
211
- const query = fields.length ? `INSERT INTO ${tableName} (${fields.join(", ")}) VALUES (${values.join(", ")})` : `INSERT INTO ${tableName} DEFAULT VALUES`;
212
- self.log(query);
213
- self.fill(attributes, (await client.query(query + " RETURNING *")).rows[0], this);
214
- changed = true;
213
+ await save(fields.length ? `INSERT INTO ${tableName} (${fields.join(", ")}) VALUES (${values.join(", ")})` : `INSERT INTO ${tableName} DEFAULT VALUES`);
215
214
  }
216
215
  }
217
216
  finally {
218
217
  if (!this.tx)
219
218
  client.release();
220
219
  }
221
- return changed;
220
+ return changed && result.rowCount;
222
221
  };
223
222
  }
224
223
  async dropConstraints(table) {
@@ -514,6 +513,7 @@ class TransactionPG extends sedentary_1.Transaction {
514
513
  }
515
514
  async commit() {
516
515
  if (!this.released) {
516
+ this.preCommit();
517
517
  this.log("COMMIT");
518
518
  await this._client.query("COMMIT");
519
519
  this.release();
package/dist/es/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Sedentary } from "sedentary";
2
2
  import { PGDB } from "./pgdb";
3
- export { EntryBase, Type } from "sedentary";
4
3
  export { TransactionPG } from "./pgdb";
4
+ export { EntryBase, Type } from "sedentary";
5
5
  export class SedentaryPG extends Sedentary {
6
6
  constructor(connection, options) {
7
7
  super(options);
package/dist/es/pgdb.js CHANGED
@@ -92,11 +92,9 @@ export class PGDB extends DB {
92
92
  throw new Error("SedentaryPG: Can't escape null nor undefined values; use the 'IS NULL' operator instead");
93
93
  if (typeof value === "boolean" || typeof value === "number")
94
94
  return value.toString();
95
- if (typeof value === "string")
96
- return format("%L", value);
97
- //if(value instanceof Date)
98
- return format("%L", value).replace(/\.\d\d\d\+/, "+");
99
- //return format("%L", JSON.stringify(value));
95
+ if (value instanceof Date)
96
+ return format("%L", value).replace(/\.\d\d\d\+/, "+");
97
+ return format("%L", value);
100
98
  }
101
99
  fill(attributes, row, entry) {
102
100
  const loaded = {};
@@ -156,11 +154,11 @@ export class PGDB extends DB {
156
154
  const pkFldName = pk.fieldName;
157
155
  return async function () {
158
156
  const client = this.tx ? this.tx._client : await self.pool.connect();
159
- let removed = false;
157
+ let removed;
160
158
  try {
161
159
  const query = `DELETE FROM ${tableName} WHERE ${pkFldName} = ${self.escape(this[pkAttrName])}`;
162
160
  self.log(query);
163
- removed = (await client.query(query)).rowCount === 1;
161
+ removed = (await client.query(query)).rowCount;
164
162
  }
165
163
  finally {
166
164
  if (!this.tx)
@@ -177,6 +175,14 @@ export class PGDB extends DB {
177
175
  return async function () {
178
176
  const client = this.tx ? this.tx._client : await self.pool.connect();
179
177
  let changed = false;
178
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
179
+ let result = null;
180
+ const save = async (query) => {
181
+ self.log(query);
182
+ changed = true;
183
+ result = await client.query(query + " RETURNING *");
184
+ self.fill(attributes, result.rows[0], this);
185
+ };
180
186
  try {
181
187
  const { loaded } = this;
182
188
  if (loaded) {
@@ -186,12 +192,8 @@ export class PGDB extends DB {
186
192
  if (value !== loaded[attribute])
187
193
  actions.push(`${attributes[attribute]} = ${self.escape(value)}`);
188
194
  }
189
- if (actions.length) {
190
- const query = `UPDATE ${tableName} SET ${actions.join(", ")} WHERE ${pkFldName} = ${self.escape(this[pkAttrName])}`;
191
- self.log(query);
192
- self.fill(attributes, (await client.query(query + " RETURNING *")).rows[0], this);
193
- changed = true;
194
- }
195
+ if (actions.length)
196
+ await save(`UPDATE ${tableName} SET ${actions.join(", ")} WHERE ${pkFldName} = ${self.escape(this[pkAttrName])}`);
195
197
  }
196
198
  else {
197
199
  const fields = [];
@@ -203,17 +205,14 @@ export class PGDB extends DB {
203
205
  values.push(self.escape(value));
204
206
  }
205
207
  }
206
- const query = fields.length ? `INSERT INTO ${tableName} (${fields.join(", ")}) VALUES (${values.join(", ")})` : `INSERT INTO ${tableName} DEFAULT VALUES`;
207
- self.log(query);
208
- self.fill(attributes, (await client.query(query + " RETURNING *")).rows[0], this);
209
- changed = true;
208
+ await save(fields.length ? `INSERT INTO ${tableName} (${fields.join(", ")}) VALUES (${values.join(", ")})` : `INSERT INTO ${tableName} DEFAULT VALUES`);
210
209
  }
211
210
  }
212
211
  finally {
213
212
  if (!this.tx)
214
213
  client.release();
215
214
  }
216
- return changed;
215
+ return changed && result.rowCount;
217
216
  };
218
217
  }
219
218
  async dropConstraints(table) {
@@ -509,6 +508,7 @@ export class TransactionPG extends Transaction {
509
508
  }
510
509
  async commit() {
511
510
  if (!this.released) {
511
+ this.preCommit();
512
512
  this.log("COMMIT");
513
513
  await this._client.query("COMMIT");
514
514
  this.release();
@@ -1,8 +1,8 @@
1
- import { Attribute, EntryBase, ForeignKeyOptions, Sedentary, SedentaryOptions, Type } from "sedentary";
2
1
  import { PoolConfig } from "pg";
2
+ import { Attribute, EntryBase, ForeignKeyOptions, Sedentary, SedentaryOptions, Type } from "sedentary";
3
3
  import { PGDB, TransactionPG } from "./pgdb";
4
- export { Entry, EntryBase, SedentaryOptions, Type } from "sedentary";
5
4
  export { TransactionPG } from "./pgdb";
5
+ export { Action, Entry, EntryBase, SedentaryOptions, Type } from "sedentary";
6
6
  export declare class SedentaryPG extends Sedentary<PGDB, TransactionPG> {
7
7
  constructor(connection: PoolConfig, options?: SedentaryOptions);
8
8
  FKey<T, E extends EntryBase>(attribute: Attribute<T, E>, options?: ForeignKeyOptions): Type<T, E>;
@@ -19,11 +19,11 @@ export declare class PGDB extends DB<TransactionPG> {
19
19
  load(tableName: string, attributes: Record<string, string>, pk: Attribute<unknown, unknown>, model: new (from: "load") => EntryBase, table: Table): (where: string, order?: string | string[], limit?: number, tx?: Transaction) => Promise<EntryBase[]>;
20
20
  remove(tableName: string, pk: Attribute<unknown, unknown>): (this: Record<string, unknown> & {
21
21
  tx?: TransactionPG;
22
- }) => Promise<boolean>;
22
+ }) => Promise<number>;
23
23
  save(tableName: string, attributes: Record<string, string>, pk: Attribute<unknown, unknown>): (this: Record<string, unknown> & {
24
24
  loaded?: Record<string, unknown>;
25
25
  tx?: TransactionPG;
26
- }) => Promise<boolean>;
26
+ }) => Promise<number | false>;
27
27
  dropConstraints(table: Table): Promise<number[]>;
28
28
  dropField(tableName: string, fieldName: string): Promise<void>;
29
29
  dropFields(table: Table): Promise<void>;
package/package.json CHANGED
@@ -7,9 +7,9 @@
7
7
  ],
8
8
  "dependencies": {
9
9
  "@types/pg": "8.6.5",
10
- "pg": "8.7.3",
10
+ "pg": "8.8.0",
11
11
  "pg-format": "1.0.4",
12
- "sedentary": "0.0.45"
12
+ "sedentary": "0.0.46"
13
13
  },
14
14
  "description": "The ORM which never needs to migrate - PostgreSQL",
15
15
  "engines": {
@@ -47,5 +47,5 @@
47
47
  "test": "jest --no-cache --runInBand"
48
48
  },
49
49
  "types": "./dist/types/index.d.ts",
50
- "version": "0.0.45"
50
+ "version": "0.0.46"
51
51
  }