sedentary-pg 0.0.49 → 0.0.50

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