@sqb/postgres 4.0.1-beta.8 → 4.0.3

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/PgAdapter.js CHANGED
@@ -16,7 +16,7 @@ class PgAdapter {
16
16
  };
17
17
  }
18
18
  async connect(config) {
19
- const cfg = Object.assign({}, config.driverOptions);
19
+ const cfg = { ...config.driverOptions };
20
20
  if (config.user)
21
21
  cfg.user = config.user;
22
22
  if (config.password)
@@ -9,6 +9,10 @@ export declare class PgConnection implements Adapter.Connection {
9
9
  startTransaction(): Promise<void>;
10
10
  commit(): Promise<void>;
11
11
  rollback(): Promise<void>;
12
+ setSavepoint(savepoint: string): Promise<void>;
13
+ releaseSavepoint(savepoint: string): Promise<void>;
14
+ rollbackSavepoint(savepoint: string): Promise<void>;
15
+ getInTransaction(): boolean;
12
16
  test(): Promise<void>;
13
17
  getSchema(): Promise<string>;
14
18
  setSchema(schema: string): Promise<void>;
@@ -1,12 +1,9 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.PgConnection = void 0;
7
- const assert_1 = __importDefault(require("assert"));
8
4
  const connect_1 = require("@sqb/connect");
9
5
  const postgresql_client_1 = require("postgresql-client");
6
+ const datatype_map_1 = require("./datatype-map");
10
7
  const SqbDataTypToOIDMap = {
11
8
  [connect_1.DataType.BOOL]: postgresql_client_1.DataTypeOIDs.bool,
12
9
  [connect_1.DataType.CHAR]: postgresql_client_1.DataTypeOIDs.char,
@@ -42,11 +39,13 @@ class PgConnection {
42
39
  return this.rollback();
43
40
  }
44
41
  async startTransaction() {
45
- assert_1.default.ok(this.intlcon, 'Can not start transaction for a closed db session');
42
+ if (!this.intlcon)
43
+ throw new Error('Can not start transaction for a closed db session');
46
44
  await this.intlcon.startTransaction();
47
45
  }
48
46
  async commit() {
49
- assert_1.default.ok(this.intlcon, 'Can not commit transaction for a closed db session');
47
+ if (!this.intlcon)
48
+ throw new Error('Can not commit transaction for a closed db session');
50
49
  await this.intlcon.commit();
51
50
  }
52
51
  async rollback() {
@@ -54,19 +53,40 @@ class PgConnection {
54
53
  return;
55
54
  await this.intlcon.rollback();
56
55
  }
56
+ async setSavepoint(savepoint) {
57
+ if (!this.intlcon)
58
+ throw new Error('Can not set savepoint for a closed db session');
59
+ return this.intlcon.savepoint(savepoint);
60
+ }
61
+ async releaseSavepoint(savepoint) {
62
+ if (!this.intlcon)
63
+ throw new Error('Can not release savepoint for a closed db session');
64
+ return this.intlcon.releaseSavepoint(savepoint);
65
+ }
66
+ async rollbackSavepoint(savepoint) {
67
+ if (!this.intlcon)
68
+ throw new Error('Can not rollback to a savepoint for a closed db session');
69
+ return this.intlcon.rollbackToSavepoint(savepoint);
70
+ }
71
+ getInTransaction() {
72
+ return !!(this.intlcon && this.intlcon.inTransaction);
73
+ }
57
74
  async test() {
58
- assert_1.default.ok(this.intlcon, 'DB session is closed');
75
+ if (!this.intlcon)
76
+ throw new Error('DB session is closed');
59
77
  await this.intlcon.query('select 1');
60
78
  }
61
79
  async getSchema() {
62
- assert_1.default.ok(this.intlcon, 'DB session is closed');
80
+ if (!this.intlcon)
81
+ throw new Error('DB session is closed');
63
82
  const r = await this.intlcon.query('SHOW search_path');
64
83
  if (r && r.rows && r.rows[0])
65
84
  return r.rows[0][0];
66
85
  return '';
67
86
  }
68
87
  async setSchema(schema) {
69
- assert_1.default.ok(this.intlcon, 'Can not set schema of a closed db session');
88
+ if (!this.intlcon)
89
+ throw new Error('Can not set schema of a closed db session');
70
90
  await this.intlcon.execute('SET search_path TO ' + schema);
71
91
  }
72
92
  onGenerateQuery(request) {
@@ -76,10 +96,20 @@ class PgConnection {
76
96
  }
77
97
  }
78
98
  async execute(query) {
79
- assert_1.default.ok(this.intlcon, 'Can not execute query with a closed db session');
99
+ if (!this.intlcon)
100
+ throw new Error('Can not execute query with a closed db session');
101
+ const params = query.params?.map((v, i) => {
102
+ const paramOpts = Array.isArray(query.paramOptions) ? query.paramOptions[i] : undefined;
103
+ if (v != null && paramOpts && paramOpts.dataType) {
104
+ const oid = (0, datatype_map_1.dataTypeToOID)(paramOpts.dataType, paramOpts.isArray);
105
+ if (oid)
106
+ return new postgresql_client_1.BindParam(oid, v);
107
+ }
108
+ return v;
109
+ });
80
110
  const opts = {
81
111
  autoCommit: query.autoCommit,
82
- params: query.params,
112
+ params,
83
113
  cursor: query.cursor,
84
114
  fetchCount: query.fetchRows,
85
115
  objectRows: query.objectRows
@@ -0,0 +1,3 @@
1
+ import { DataType } from '@sqb/builder';
2
+ import { OID } from 'postgresql-client';
3
+ export declare function dataTypeToOID(dataType: DataType, isArray?: boolean): OID | undefined;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.dataTypeToOID = void 0;
4
+ const builder_1 = require("@sqb/builder");
5
+ const postgresql_client_1 = require("postgresql-client");
6
+ function dataTypeToOID(dataType, isArray) {
7
+ if (dataType === builder_1.DataType.DATE)
8
+ return isArray ? postgresql_client_1.DataTypeOIDs._date : postgresql_client_1.DataTypeOIDs.date;
9
+ if (dataType === builder_1.DataType.BOOL)
10
+ return isArray ? postgresql_client_1.DataTypeOIDs._bool : postgresql_client_1.DataTypeOIDs.bool;
11
+ if (dataType === builder_1.DataType.CHAR)
12
+ return isArray ? postgresql_client_1.DataTypeOIDs._bpchar : postgresql_client_1.DataTypeOIDs.bpchar;
13
+ if (dataType === builder_1.DataType.SMALLINT)
14
+ return isArray ? postgresql_client_1.DataTypeOIDs._int2 : postgresql_client_1.DataTypeOIDs.int2;
15
+ if (dataType === builder_1.DataType.INTEGER)
16
+ return isArray ? postgresql_client_1.DataTypeOIDs._int4 : postgresql_client_1.DataTypeOIDs.int4;
17
+ if (dataType === builder_1.DataType.BIGINT)
18
+ return isArray ? postgresql_client_1.DataTypeOIDs._int8 : postgresql_client_1.DataTypeOIDs.int8;
19
+ if (dataType === builder_1.DataType.BINARY)
20
+ return isArray ? postgresql_client_1.DataTypeOIDs._bytea : postgresql_client_1.DataTypeOIDs.bytea;
21
+ if (dataType === builder_1.DataType.FLOAT)
22
+ return isArray ? postgresql_client_1.DataTypeOIDs._float4 : postgresql_client_1.DataTypeOIDs.float4;
23
+ if (dataType === builder_1.DataType.DOUBLE)
24
+ return isArray ? postgresql_client_1.DataTypeOIDs._float8 : postgresql_client_1.DataTypeOIDs.float8;
25
+ if (dataType === builder_1.DataType.GUID)
26
+ return isArray ? postgresql_client_1.DataTypeOIDs._uuid : postgresql_client_1.DataTypeOIDs.uuid;
27
+ if (dataType === builder_1.DataType.TEXT)
28
+ return isArray ? postgresql_client_1.DataTypeOIDs._text : postgresql_client_1.DataTypeOIDs.text;
29
+ if (dataType === builder_1.DataType.JSON)
30
+ return isArray ? postgresql_client_1.DataTypeOIDs._json : postgresql_client_1.DataTypeOIDs.json;
31
+ }
32
+ exports.dataTypeToOID = dataTypeToOID;
package/dist/index.js CHANGED
@@ -3,4 +3,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const connect_1 = require("@sqb/connect");
4
4
  const PgAdapter_1 = require("./PgAdapter");
5
5
  require("@sqb/postgres-dialect");
6
- connect_1.registerAdapter(new PgAdapter_1.PgAdapter());
6
+ (0, connect_1.registerAdapter)(new PgAdapter_1.PgAdapter());
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sqb/postgres",
3
3
  "description": "SQB serialization extension for PostgreSQL database",
4
- "version": "4.0.1-beta.8",
4
+ "version": "4.0.3",
5
5
  "author": "Panates Ltd.",
6
6
  "contributors": [
7
7
  "Eray Hanoglu <e.hanoglu@panates.com>"
@@ -24,19 +24,19 @@
24
24
  "database"
25
25
  ],
26
26
  "dependencies": {
27
- "putil-promisify": "^1.8.2"
27
+ "putil-promisify": "^1.8.5"
28
28
  },
29
29
  "devDependencies": {
30
- "postgresql-client": "^1.13.2"
30
+ "postgresql-client": "^1.21.0"
31
31
  },
32
32
  "peerDependencies": {
33
- "postgresql-client": "^1.13.0",
34
- "@sqb/postgres-dialect": "^4.0.1-beta.8"
33
+ "postgresql-client": "^1.21.0",
34
+ "@sqb/postgres-dialect": "^4.0.3"
35
35
  },
36
36
  "main": "dist/index.js",
37
37
  "types": "dist/index.d.ts",
38
38
  "engines": {
39
- "node": ">= 10.0"
39
+ "node": ">= 14.0"
40
40
  },
41
41
  "directories": {
42
42
  "lib": "dist",
@@ -51,7 +51,7 @@
51
51
  "temp-dir": "./coverage/.nyc_output"
52
52
  },
53
53
  "scripts": {
54
- "test": "ts-mocha -p test/tsconfig.json --paths --reporter spec test/**/*.spec.ts",
54
+ "test": "TS_NODE_PROJECT='./test/tsconfig.json' mocha -r ts-node/register -r tsconfig-paths/register --reporter spec test/**/*.spec.ts",
55
55
  "cover": "nyc --reporter=cobertura --reporter html --reporter text npm run test",
56
56
  "build": "tsc -b tsconfig-build.json",
57
57
  "compile": "tsc -b tsconfig.json",