sedentary-pg 0.0.31 → 0.0.34

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
@@ -18,10 +18,15 @@ const needDrop = [
18
18
  const needUsing = [
19
19
  ["DATETIME", "varchar"],
20
20
  ["INT", "varchar"],
21
- ["INT8", "varchar"]
21
+ ["INT8", "varchar"],
22
+ ["NUMBER", "varchar"]
22
23
  ];
23
- const types = { int2: "SMALLINT", int4: "INTEGER", int8: "BIGINT", timestamptz: "DATETIME", varchar: "VARCHAR" };
24
+ const types = { int2: "SMALLINT", int4: "INTEGER", int8: "BIGINT", numeric: "NUMERIC", timestamptz: "DATETIME", varchar: "VARCHAR" };
24
25
  const actions = { cascade: "c", "no action": "a", restrict: "r", "set default": "d", "set null": "n" };
26
+ function parseInt8(value) {
27
+ return BigInt(value);
28
+ }
29
+ pg_1.types.setTypeParser(20, parseInt8);
25
30
  class PGDB extends sedentary_1.DB {
26
31
  constructor(connection, log) {
27
32
  super(log);
@@ -112,14 +117,34 @@ class PGDB extends sedentary_1.DB {
112
117
  return ret;
113
118
  };
114
119
  }
120
+ remove(tableName, pk) {
121
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
122
+ const self = this;
123
+ const pkAttrnName = pk.attributeName;
124
+ const pkFldName = pk.fieldName;
125
+ return async function () {
126
+ const client = this.tx ? this.tx.client : await self.pool.connect();
127
+ let removed = false;
128
+ try {
129
+ const query = `DELETE FROM ${tableName} WHERE ${pkFldName} = ${self.escape(this[pkAttrnName])}`;
130
+ self.log(query);
131
+ removed = (await client.query(query)).rowCount === 1;
132
+ }
133
+ finally {
134
+ if (!this.tx)
135
+ client.release();
136
+ }
137
+ return removed;
138
+ };
139
+ }
115
140
  save(tableName, attributes, pk) {
116
141
  // eslint-disable-next-line @typescript-eslint/no-this-alias
117
142
  const self = this;
118
143
  const pkAttrnName = pk.attributeName;
119
144
  const pkFldName = pk.fieldName;
120
145
  return async function () {
121
- let changed = false;
122
146
  const client = this.tx ? this.tx.client : await self.pool.connect();
147
+ let changed = false;
123
148
  try {
124
149
  const { loaded } = this;
125
150
  if (loaded) {
@@ -258,6 +283,8 @@ class PGDB extends sedentary_1.DB {
258
283
  switch (type) {
259
284
  case "DATETIME":
260
285
  return ["DATETIME", "TIMESTAMP (3) WITH TIME ZONE"];
286
+ case "NUMBER":
287
+ return ["NUMERIC", "NUMERIC"];
261
288
  case "INT":
262
289
  ret = size === 2 ? "SMALLINT" : "INTEGER";
263
290
  return [ret, ret];
package/dist/es/pgdb.js CHANGED
@@ -1,4 +1,4 @@
1
- import { DatabaseError, Pool } from "pg";
1
+ import { DatabaseError, Pool, types as PGtypes } from "pg";
2
2
  import format from "pg-format";
3
3
  import { DB, Transaction } from "sedentary";
4
4
  import { adsrc } from "./adsrc";
@@ -12,10 +12,15 @@ const needDrop = [
12
12
  const needUsing = [
13
13
  ["DATETIME", "varchar"],
14
14
  ["INT", "varchar"],
15
- ["INT8", "varchar"]
15
+ ["INT8", "varchar"],
16
+ ["NUMBER", "varchar"]
16
17
  ];
17
- const types = { int2: "SMALLINT", int4: "INTEGER", int8: "BIGINT", timestamptz: "DATETIME", varchar: "VARCHAR" };
18
+ const types = { int2: "SMALLINT", int4: "INTEGER", int8: "BIGINT", numeric: "NUMERIC", timestamptz: "DATETIME", varchar: "VARCHAR" };
18
19
  const actions = { cascade: "c", "no action": "a", restrict: "r", "set default": "d", "set null": "n" };
20
+ function parseInt8(value) {
21
+ return BigInt(value);
22
+ }
23
+ PGtypes.setTypeParser(20, parseInt8);
19
24
  export class PGDB extends DB {
20
25
  client;
21
26
  indexes;
@@ -110,14 +115,34 @@ export class PGDB extends DB {
110
115
  return ret;
111
116
  };
112
117
  }
118
+ remove(tableName, pk) {
119
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
120
+ const self = this;
121
+ const pkAttrnName = pk.attributeName;
122
+ const pkFldName = pk.fieldName;
123
+ return async function () {
124
+ const client = this.tx ? this.tx.client : await self.pool.connect();
125
+ let removed = false;
126
+ try {
127
+ const query = `DELETE FROM ${tableName} WHERE ${pkFldName} = ${self.escape(this[pkAttrnName])}`;
128
+ self.log(query);
129
+ removed = (await client.query(query)).rowCount === 1;
130
+ }
131
+ finally {
132
+ if (!this.tx)
133
+ client.release();
134
+ }
135
+ return removed;
136
+ };
137
+ }
113
138
  save(tableName, attributes, pk) {
114
139
  // eslint-disable-next-line @typescript-eslint/no-this-alias
115
140
  const self = this;
116
141
  const pkAttrnName = pk.attributeName;
117
142
  const pkFldName = pk.fieldName;
118
143
  return async function () {
119
- let changed = false;
120
144
  const client = this.tx ? this.tx.client : await self.pool.connect();
145
+ let changed = false;
121
146
  try {
122
147
  const { loaded } = this;
123
148
  if (loaded) {
@@ -256,6 +281,8 @@ export class PGDB extends DB {
256
281
  switch (type) {
257
282
  case "DATETIME":
258
283
  return ["DATETIME", "TIMESTAMP (3) WITH TIME ZONE"];
284
+ case "NUMBER":
285
+ return ["NUMERIC", "NUMERIC"];
259
286
  case "INT":
260
287
  ret = size === 2 ? "SMALLINT" : "INTEGER";
261
288
  return [ret, ret];
@@ -14,6 +14,9 @@ export declare class PGDB extends DB<TransactionPG> {
14
14
  escape(value: Natural): string;
15
15
  fill(attributes: Record<string, string>, row: Record<string, Natural>, entry: Record<string, Natural>): void;
16
16
  load(tableName: string, attributes: Record<string, string>, pk: Attribute<Natural, unknown>, model: new (from: "load") => EntryBase, table: Table): (where: string, order?: string[], tx?: Transaction) => Promise<EntryBase[]>;
17
+ remove(tableName: string, pk: Attribute<Natural, unknown>): (this: Record<string, Natural> & {
18
+ tx?: TransactionPG;
19
+ }) => Promise<boolean>;
17
20
  save(tableName: string, attributes: Record<string, string>, pk: Attribute<Natural, unknown>): (this: Record<string, Natural> & {
18
21
  loaded?: Record<string, unknown>;
19
22
  tx?: TransactionPG;
package/package.json CHANGED
@@ -9,20 +9,20 @@
9
9
  "@types/pg": "8.6.5",
10
10
  "pg": "8.7.3",
11
11
  "pg-format": "1.0.4",
12
- "sedentary": "0.0.31"
12
+ "sedentary": "0.0.34"
13
13
  },
14
14
  "description": "The ORM which never needs to migrate - PostgreSQL",
15
15
  "devDependencies": {
16
16
  "@types/mocha": "9.1.0",
17
- "@types/node": "17.0.23",
17
+ "@types/node": "17.0.24",
18
18
  "@types/pg-format": "1.0.2",
19
19
  "@types/yamljs": "0.2.31",
20
- "@typescript-eslint/eslint-plugin": "5.17.0",
21
- "@typescript-eslint/parser": "5.17.0",
22
- "eslint": "8.12.0",
20
+ "@typescript-eslint/eslint-plugin": "5.19.0",
21
+ "@typescript-eslint/parser": "5.19.0",
22
+ "eslint": "8.13.0",
23
23
  "mocha": "9.2.2",
24
24
  "nyc": "15.1.0",
25
- "prettier": "2.6.1",
25
+ "prettier": "2.6.2",
26
26
  "ts-node": "10.7.0",
27
27
  "typescript": "4.6.3",
28
28
  "yamljs": "0.3.0"
@@ -84,5 +84,5 @@
84
84
  }
85
85
  },
86
86
  "types": "./dist/types/index.d.ts",
87
- "version": "0.0.31"
87
+ "version": "0.0.34"
88
88
  }