sedentary-pg 0.0.48 → 0.0.51

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/pgdb.js CHANGED
@@ -7,7 +7,6 @@ exports.TransactionPG = exports.PGDB = void 0;
7
7
  const pg_1 = require("pg");
8
8
  const pg_format_1 = __importDefault(require("pg-format"));
9
9
  const sedentary_1 = require("sedentary");
10
- const db_1 = require("sedentary/db");
11
10
  const adsrc_1 = require("./adsrc");
12
11
  const needDrop = [
13
12
  ["DATETIME", "int2"],
@@ -102,11 +101,11 @@ class PGDB extends sedentary_1.DB {
102
101
  return (0, pg_format_1.default)("%L", value).replace(/\.\d\d\d\+/, "+");
103
102
  return (0, pg_format_1.default)("%L", value);
104
103
  }
105
- fill(attributes, row, entry) {
104
+ fill(attr2field, row, entry) {
106
105
  const value = {};
107
- for (const attribute in attributes)
108
- entry[attribute] = value[attribute] = row[attributes[attribute]];
109
- Object.defineProperty(entry, db_1.loaded, { configurable: true, value });
106
+ for (const attribute in attr2field)
107
+ value[attribute] = (0, sedentary_1.deepCopy)((entry[attribute] = row[attr2field[attribute]]));
108
+ Object.defineProperty(entry, sedentary_1.loaded, { configurable: true, value });
110
109
  }
111
110
  load(tableName, attributes, pk, model, table) {
112
111
  const pkFldName = pk.fieldName;
@@ -159,7 +158,7 @@ class PGDB extends sedentary_1.DB {
159
158
  const pkAttrName = pk.attributeName;
160
159
  const pkFldName = pk.fieldName;
161
160
  return async function () {
162
- const client = this[db_1.transaction] ? this[db_1.transaction]._client : await self.pool.connect();
161
+ const client = this[sedentary_1.transaction] ? this[sedentary_1.transaction]._client : await self.pool.connect();
163
162
  let removed;
164
163
  try {
165
164
  const query = `DELETE FROM ${tableName} WHERE ${pkFldName} = ${self.escape(this[pkAttrName])}`;
@@ -167,19 +166,19 @@ class PGDB extends sedentary_1.DB {
167
166
  removed = (await client.query(query)).rowCount;
168
167
  }
169
168
  finally {
170
- if (!this[db_1.transaction])
169
+ if (!this[sedentary_1.transaction])
171
170
  client.release();
172
171
  }
173
172
  return removed;
174
173
  };
175
174
  }
176
- save(tableName, attributes, pk) {
175
+ save(tableName, attr2field, pk) {
177
176
  // eslint-disable-next-line @typescript-eslint/no-this-alias
178
177
  const self = this;
179
178
  const pkAttrName = pk.attributeName;
180
179
  const pkFldName = pk.fieldName;
181
180
  return async function () {
182
- const client = this[db_1.transaction] ? this[db_1.transaction]._client : await self.pool.connect();
181
+ const client = this[sedentary_1.transaction] ? this[sedentary_1.transaction]._client : await self.pool.connect();
183
182
  let changed = false;
184
183
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
185
184
  let result = null;
@@ -187,16 +186,16 @@ class PGDB extends sedentary_1.DB {
187
186
  self.log(query);
188
187
  changed = true;
189
188
  result = await client.query(query + " RETURNING *");
190
- self.fill(attributes, result.rows[0], this);
189
+ self.fill(attr2field, result.rows[0], this);
191
190
  };
192
191
  try {
193
- const loadedRecord = this[db_1.loaded];
192
+ const loadedRecord = this[sedentary_1.loaded];
194
193
  if (loadedRecord) {
195
194
  const actions = [];
196
- for (const attribute in attributes) {
195
+ for (const attribute in attr2field) {
197
196
  const value = this[attribute];
198
- if ((0, sedentary_1.differ)(value, loadedRecord[attribute]))
199
- actions.push(`${attributes[attribute]} = ${self.escape(value)}`);
197
+ if ((0, sedentary_1.deepDiff)(value, loadedRecord[attribute]))
198
+ actions.push(`${attr2field[attribute]} = ${self.escape(value)}`);
200
199
  }
201
200
  if (actions.length)
202
201
  await save(`UPDATE ${tableName} SET ${actions.join(", ")} WHERE ${pkFldName} = ${self.escape(this[pkAttrName])}`);
@@ -204,10 +203,10 @@ class PGDB extends sedentary_1.DB {
204
203
  else {
205
204
  const fields = [];
206
205
  const values = [];
207
- for (const attribute in attributes) {
206
+ for (const attribute in attr2field) {
208
207
  const value = this[attribute];
209
208
  if (value !== null && value !== undefined) {
210
- fields.push(attributes[attribute]);
209
+ fields.push(attr2field[attribute]);
211
210
  values.push(self.escape(value));
212
211
  }
213
212
  }
@@ -215,7 +214,7 @@ class PGDB extends sedentary_1.DB {
215
214
  }
216
215
  }
217
216
  finally {
218
- if (!this[db_1.transaction])
217
+ if (!this[sedentary_1.transaction])
219
218
  client.release();
220
219
  }
221
220
  return changed && result.rowCount;
@@ -255,7 +254,7 @@ class PGDB extends sedentary_1.DB {
255
254
  const res = await this._client.query("SELECT attname FROM pg_attribute WHERE attrelid = $1 AND attnum > 0 AND attisdropped = false AND attinhcount = 0", [table.oid]);
256
255
  for (const i in res.rows) {
257
256
  const field = table.findField(res.rows[i].attname);
258
- if (!field || !field.base)
257
+ if (!field || !field[sedentary_1.base])
259
258
  await this.dropField(table.tableName, res.rows[i].attname);
260
259
  }
261
260
  }
@@ -326,7 +325,7 @@ class PGDB extends sedentary_1.DB {
326
325
  }
327
326
  }
328
327
  fieldType(attribute) {
329
- const { size, type } = attribute;
328
+ const { [sedentary_1.size]: _size, type } = attribute;
330
329
  let ret;
331
330
  switch (type) {
332
331
  case "BOOLEAN":
@@ -334,7 +333,7 @@ class PGDB extends sedentary_1.DB {
334
333
  case "DATETIME":
335
334
  return ["DATETIME", "TIMESTAMP (3) WITH TIME ZONE"];
336
335
  case "INT":
337
- ret = size === 2 ? "SMALLINT" : "INTEGER";
336
+ ret = _size === 2 ? "SMALLINT" : "INTEGER";
338
337
  return [ret, ret];
339
338
  case "INT8":
340
339
  return ["BIGINT", "BIGINT"];
@@ -345,14 +344,14 @@ class PGDB extends sedentary_1.DB {
345
344
  case "NUMBER":
346
345
  return ["NUMERIC", "NUMERIC"];
347
346
  case "VARCHAR":
348
- return ["VARCHAR", "VARCHAR" + (size ? `(${size})` : "")];
347
+ return ["VARCHAR", "VARCHAR" + (_size ? `(${_size})` : "")];
349
348
  }
350
- throw new Error(`Unknown type: '${type}', '${size}'`);
349
+ throw new Error(`Unknown type: '${type}', '${_size}'`);
351
350
  }
352
351
  async syncFields(table) {
353
352
  const { attributes, autoIncrement, oid, tableName } = table;
354
353
  for (const attribute of attributes) {
355
- const { fieldName, notNull, size } = attribute;
354
+ const { fieldName, notNull, [sedentary_1.size]: _size } = attribute;
356
355
  const defaultValue = attribute.defaultValue === undefined ? (autoIncrement && fieldName === "id" ? `nextval('${tableName}_id_seq'::regclass)` : undefined) : this.escape(attribute.defaultValue);
357
356
  const [base, type] = this.fieldType(attribute);
358
357
  const where = "attrelid = $1 AND attnum > 0 AND atttypid = pg_type.oid AND attislocal = 't' AND attname = $2";
@@ -400,7 +399,7 @@ class PGDB extends sedentary_1.DB {
400
399
  }
401
400
  else {
402
401
  const { adsrc, attnotnull, atttypmod, typname } = res.rows[0];
403
- if (types[typname] !== base || (base === "VARCHAR" && (size ? size + 4 !== atttypmod : atttypmod !== -1))) {
402
+ if (types[typname] !== base || (base === "VARCHAR" && (_size ? _size + 4 !== atttypmod : atttypmod !== -1))) {
404
403
  if (needDrop.some(([type, name]) => attribute.type === type && typname === name)) {
405
404
  await this.dropField(tableName, fieldName);
406
405
  await addField();
package/dist/es/pgdb.js CHANGED
@@ -1,7 +1,6 @@
1
1
  import { DatabaseError, Pool, types as PGtypes } from "pg";
2
2
  import format from "pg-format";
3
- import { DB, differ, Transaction } from "sedentary";
4
- import { loaded, transaction } from "sedentary/db";
3
+ import { base, DB, deepCopy, deepDiff, loaded, size, Transaction, transaction } from "sedentary";
5
4
  import { adsrc } from "./adsrc";
6
5
  const needDrop = [
7
6
  ["DATETIME", "int2"],
@@ -97,10 +96,10 @@ export class PGDB extends DB {
97
96
  return format("%L", value).replace(/\.\d\d\d\+/, "+");
98
97
  return format("%L", value);
99
98
  }
100
- fill(attributes, row, entry) {
99
+ fill(attr2field, row, entry) {
101
100
  const value = {};
102
- for (const attribute in attributes)
103
- entry[attribute] = value[attribute] = row[attributes[attribute]];
101
+ for (const attribute in attr2field)
102
+ value[attribute] = deepCopy((entry[attribute] = row[attr2field[attribute]]));
104
103
  Object.defineProperty(entry, loaded, { configurable: true, value });
105
104
  }
106
105
  load(tableName, attributes, pk, model, table) {
@@ -168,7 +167,7 @@ export class PGDB extends DB {
168
167
  return removed;
169
168
  };
170
169
  }
171
- save(tableName, attributes, pk) {
170
+ save(tableName, attr2field, pk) {
172
171
  // eslint-disable-next-line @typescript-eslint/no-this-alias
173
172
  const self = this;
174
173
  const pkAttrName = pk.attributeName;
@@ -182,16 +181,16 @@ export class PGDB extends DB {
182
181
  self.log(query);
183
182
  changed = true;
184
183
  result = await client.query(query + " RETURNING *");
185
- self.fill(attributes, result.rows[0], this);
184
+ self.fill(attr2field, result.rows[0], this);
186
185
  };
187
186
  try {
188
187
  const loadedRecord = this[loaded];
189
188
  if (loadedRecord) {
190
189
  const actions = [];
191
- for (const attribute in attributes) {
190
+ for (const attribute in attr2field) {
192
191
  const value = this[attribute];
193
- if (differ(value, loadedRecord[attribute]))
194
- actions.push(`${attributes[attribute]} = ${self.escape(value)}`);
192
+ if (deepDiff(value, loadedRecord[attribute]))
193
+ actions.push(`${attr2field[attribute]} = ${self.escape(value)}`);
195
194
  }
196
195
  if (actions.length)
197
196
  await save(`UPDATE ${tableName} SET ${actions.join(", ")} WHERE ${pkFldName} = ${self.escape(this[pkAttrName])}`);
@@ -199,10 +198,10 @@ export class PGDB extends DB {
199
198
  else {
200
199
  const fields = [];
201
200
  const values = [];
202
- for (const attribute in attributes) {
201
+ for (const attribute in attr2field) {
203
202
  const value = this[attribute];
204
203
  if (value !== null && value !== undefined) {
205
- fields.push(attributes[attribute]);
204
+ fields.push(attr2field[attribute]);
206
205
  values.push(self.escape(value));
207
206
  }
208
207
  }
@@ -250,7 +249,7 @@ export class PGDB extends DB {
250
249
  const res = await this._client.query("SELECT attname FROM pg_attribute WHERE attrelid = $1 AND attnum > 0 AND attisdropped = false AND attinhcount = 0", [table.oid]);
251
250
  for (const i in res.rows) {
252
251
  const field = table.findField(res.rows[i].attname);
253
- if (!field || !field.base)
252
+ if (!field || !field[base])
254
253
  await this.dropField(table.tableName, res.rows[i].attname);
255
254
  }
256
255
  }
@@ -321,7 +320,7 @@ export class PGDB extends DB {
321
320
  }
322
321
  }
323
322
  fieldType(attribute) {
324
- const { size, type } = attribute;
323
+ const { [size]: _size, type } = attribute;
325
324
  let ret;
326
325
  switch (type) {
327
326
  case "BOOLEAN":
@@ -329,7 +328,7 @@ export class PGDB extends DB {
329
328
  case "DATETIME":
330
329
  return ["DATETIME", "TIMESTAMP (3) WITH TIME ZONE"];
331
330
  case "INT":
332
- ret = size === 2 ? "SMALLINT" : "INTEGER";
331
+ ret = _size === 2 ? "SMALLINT" : "INTEGER";
333
332
  return [ret, ret];
334
333
  case "INT8":
335
334
  return ["BIGINT", "BIGINT"];
@@ -340,14 +339,14 @@ export class PGDB extends DB {
340
339
  case "NUMBER":
341
340
  return ["NUMERIC", "NUMERIC"];
342
341
  case "VARCHAR":
343
- return ["VARCHAR", "VARCHAR" + (size ? `(${size})` : "")];
342
+ return ["VARCHAR", "VARCHAR" + (_size ? `(${_size})` : "")];
344
343
  }
345
- throw new Error(`Unknown type: '${type}', '${size}'`);
344
+ throw new Error(`Unknown type: '${type}', '${_size}'`);
346
345
  }
347
346
  async syncFields(table) {
348
347
  const { attributes, autoIncrement, oid, tableName } = table;
349
348
  for (const attribute of attributes) {
350
- const { fieldName, notNull, size } = attribute;
349
+ const { fieldName, notNull, [size]: _size } = attribute;
351
350
  const defaultValue = attribute.defaultValue === undefined ? (autoIncrement && fieldName === "id" ? `nextval('${tableName}_id_seq'::regclass)` : undefined) : this.escape(attribute.defaultValue);
352
351
  const [base, type] = this.fieldType(attribute);
353
352
  const where = "attrelid = $1 AND attnum > 0 AND atttypid = pg_type.oid AND attislocal = 't' AND attname = $2";
@@ -395,7 +394,7 @@ export class PGDB extends DB {
395
394
  }
396
395
  else {
397
396
  const { adsrc, attnotnull, atttypmod, typname } = res.rows[0];
398
- if (types[typname] !== base || (base === "VARCHAR" && (size ? size + 4 !== atttypmod : atttypmod !== -1))) {
397
+ if (types[typname] !== base || (base === "VARCHAR" && (_size ? _size + 4 !== atttypmod : atttypmod !== -1))) {
399
398
  if (needDrop.some(([type, name]) => attribute.type === type && typname === name)) {
400
399
  await this.dropField(tableName, fieldName);
401
400
  await addField();
@@ -1,6 +1,5 @@
1
1
  import { PoolClient, PoolConfig } from "pg";
2
- import { Attribute, DB, EntryBase, Table, Transaction } from "sedentary";
3
- import { loaded, transaction } from "sedentary/db";
2
+ import { Attribute, DB, EntryBase, loaded, Table, Transaction, transaction } from "sedentary";
4
3
  export declare class PGDB extends DB<TransactionPG> {
5
4
  private _client;
6
5
  private indexes;
@@ -16,12 +15,12 @@ export declare class PGDB extends DB<TransactionPG> {
16
15
  cancel(tableName: string): (where: string, tx?: Transaction) => Promise<number>;
17
16
  client(): Promise<PoolClient>;
18
17
  escape(value: unknown): string;
19
- fill(attributes: Record<string, string>, row: Record<string, unknown>, entry: Record<string, unknown>): void;
18
+ fill(attr2field: Record<string, string>, row: Record<string, unknown>, entry: Record<string, unknown>): void;
20
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[]>;
21
20
  remove(tableName: string, pk: Attribute<unknown, unknown>): (this: Record<string, unknown> & {
22
21
  [transaction]?: TransactionPG;
23
22
  }) => Promise<number>;
24
- save(tableName: string, attributes: Record<string, string>, pk: Attribute<unknown, unknown>): (this: Record<string, unknown> & {
23
+ save(tableName: string, attr2field: Record<string, string>, pk: Attribute<unknown, unknown>): (this: Record<string, unknown> & {
25
24
  [loaded]?: Record<string, unknown>;
26
25
  [transaction]?: TransactionPG;
27
26
  }) => Promise<number | false>;
package/package.json CHANGED
@@ -9,7 +9,7 @@
9
9
  "@types/pg": "8.6.5",
10
10
  "pg": "8.8.0",
11
11
  "pg-format": "1.0.4",
12
- "sedentary": "0.0.48"
12
+ "sedentary": "0.0.51"
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.48"
50
+ "version": "0.0.51"
51
51
  }