@sqb/postgres 4.2.1 → 4.5.1

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/README.md ADDED
@@ -0,0 +1,67 @@
1
+
2
+ <p style="text-align:center">
3
+ <img src="https://user-images.githubusercontent.com/3836517/32965280-1a2b63ce-cbe7-11e7-8ee1-ba47313503c5.png" width="500px" alt="SQB Logo"/>
4
+ </p>
5
+
6
+ <br>
7
+
8
+ [![NPM Version][npm-image]][npm-url]
9
+ [![NPM Downloads][downloads-image]][downloads-url]
10
+ [![Build Status][travis-image]][travis-url]
11
+ [![Test Coverage][coveralls-image]][coveralls-url]
12
+ [![Dependencies][dependencies-image]][dependencies-url]
13
+ [![DevDependencies][devdependencies-image]][devdependencies-url]
14
+ [![Package Quality][quality-image]][quality-url]
15
+
16
+
17
+ ## About SQB
18
+
19
+ SQB is an extensible, multi-dialect SQL query builder and Database connection wrapper for NodeJS.
20
+
21
+ ## Main goals
22
+
23
+ - Single code base for any sql based database
24
+ - Powerful and simplified query coding scheme
25
+ - Fast applications with low memory requirements
26
+ - Let applications work with large data tables efficiently
27
+ - Support latest JavaScript language standards
28
+ - Lightweight and extensible framework.
29
+
30
+
31
+ You can report bugs and discuss features on the [GitHub issues](https://github.com/sqbjs/sqb/issues) page
32
+
33
+ Thanks to all of the great [contributions](https://github.com/sqbjs/sqb/graphs/contributors) to the project.
34
+
35
+ You may want to check detailed [DOCUMENTATION](https://sqbjs.github.io/sqb/)
36
+
37
+
38
+ ## Installation
39
+
40
+ ```bash
41
+ $ npm install @sqb/postgres --save
42
+ ```
43
+
44
+ ## Node Compatibility
45
+
46
+ - node >= 16.x
47
+
48
+
49
+ ### License
50
+ SQB is available under [MIT](LICENSE) license.
51
+
52
+ [npm-image]: https://img.shields.io/npm/v/@sqb/postgres.svg
53
+ [npm-url]: https://npmjs.org/package/@sqb/postgres
54
+ [travis-image]: https://img.shields.io/travis/sqbjs/@sqb/postgres/master.svg
55
+ [travis-url]: https://travis-ci.org/sqbjs/@sqb/postgres
56
+ [coveralls-image]: https://img.shields.io/coveralls/sqbjs/@sqb/postgres/master.svg
57
+ [coveralls-url]: https://coveralls.io/r/sqbjs/@sqb/postgres
58
+ [downloads-image]: https://img.shields.io/npm/dm/@sqb/postgres.svg
59
+ [downloads-url]: https://npmjs.org/package/@sqb/postgres
60
+ [gitter-image]: https://badges.gitter.im/sqbjs/@sqb/postgres.svg
61
+ [gitter-url]: https://gitter.im/sqbjs/@sqb/postgres?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge
62
+ [dependencies-image]: https://david-dm.org/sqbjs/@sqb/postgres/status.svg
63
+ [dependencies-url]:https://david-dm.org/sqbjs/@sqb/postgres
64
+ [devdependencies-image]: https://david-dm.org/sqbjs/@sqb/postgres/dev-status.svg
65
+ [devdependencies-url]:https://david-dm.org/sqbjs/@sqb/postgres?type=dev
66
+ [quality-image]: http://npm.packagequality.com/shield/@sqb/postgres.png
67
+ [quality-url]: http://packagequality.com/#?package=@sqb/postgres
File without changes
@@ -2,5 +2,5 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  require("@sqb/postgres-dialect");
4
4
  const connect_1 = require("@sqb/connect");
5
- const PgAdapter_1 = require("./PgAdapter");
6
- (0, connect_1.registerAdapter)(new PgAdapter_1.PgAdapter());
5
+ const pg_adapter_js_1 = require("./pg-adapter.js");
6
+ (0, connect_1.registerAdapter)(new pg_adapter_js_1.PgAdapter());
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -4,17 +4,15 @@ exports.PgAdapter = void 0;
4
4
  require("@sqb/postgres-dialect");
5
5
  const postgresql_client_1 = require("postgresql-client");
6
6
  const connect_1 = require("@sqb/connect");
7
- const PgConnection_1 = require("./PgConnection");
7
+ const pg_connection_js_1 = require("./pg-connection.js");
8
8
  class PgAdapter {
9
- constructor() {
10
- this.driver = 'postgresql-client';
11
- this.dialect = 'postgres';
12
- this.features = {
13
- cursor: true,
14
- schema: true,
15
- fetchAsString: [connect_1.DataType.DATE, connect_1.DataType.TIMESTAMP, connect_1.DataType.TIMESTAMPTZ]
16
- };
17
- }
9
+ driver = 'postgresql-client';
10
+ dialect = 'postgres';
11
+ features = {
12
+ cursor: true,
13
+ schema: true,
14
+ fetchAsString: [connect_1.DataType.DATE, connect_1.DataType.TIMESTAMP, connect_1.DataType.TIMESTAMPTZ]
15
+ };
18
16
  async connect(config) {
19
17
  const cfg = { ...config.driverOptions };
20
18
  if (config.user)
@@ -32,7 +30,7 @@ class PgAdapter {
32
30
  const connection = new postgresql_client_1.Connection(cfg);
33
31
  try {
34
32
  await connection.connect();
35
- return new PgConnection_1.PgConnection(connection);
33
+ return new pg_connection_js_1.PgConnection(connection);
36
34
  }
37
35
  catch (e) {
38
36
  await connection.close(0);
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PgConnection = void 0;
4
4
  const postgresql_client_1 = require("postgresql-client");
5
5
  const connect_1 = require("@sqb/connect");
6
- const datatype_map_1 = require("./datatype-map");
6
+ const datatype_map_js_1 = require("./datatype-map.js");
7
7
  const SqbDataTypToOIDMap = {
8
8
  [connect_1.DataType.BOOL]: postgresql_client_1.DataTypeOIDs.bool,
9
9
  [connect_1.DataType.CHAR]: postgresql_client_1.DataTypeOIDs.char,
@@ -23,6 +23,7 @@ const SqbDataTypToOIDMap = {
23
23
  [connect_1.DataType.GUID]: postgresql_client_1.DataTypeOIDs.uuid,
24
24
  };
25
25
  class PgConnection {
26
+ intlcon;
26
27
  constructor(conn) {
27
28
  this.intlcon = conn;
28
29
  }
@@ -101,7 +102,7 @@ class PgConnection {
101
102
  const params = query.params?.map((v, i) => {
102
103
  const paramOpts = Array.isArray(query.paramOptions) ? query.paramOptions[i] : undefined;
103
104
  if (v != null && paramOpts && paramOpts.dataType) {
104
- const oid = (0, datatype_map_1.dataTypeToOID)(paramOpts.dataType, paramOpts.isArray);
105
+ const oid = (0, datatype_map_js_1.dataTypeToOID)(paramOpts.dataType, paramOpts.isArray);
105
106
  if (oid)
106
107
  return new postgresql_client_1.BindParam(oid, v);
107
108
  }
File without changes
@@ -0,0 +1,28 @@
1
+ import { DataTypeOIDs } from 'postgresql-client';
2
+ import { DataType } from '@sqb/builder';
3
+ export function dataTypeToOID(dataType, isArray) {
4
+ if (dataType === DataType.DATE)
5
+ return isArray ? DataTypeOIDs._date : DataTypeOIDs.date;
6
+ if (dataType === DataType.BOOL)
7
+ return isArray ? DataTypeOIDs._bool : DataTypeOIDs.bool;
8
+ if (dataType === DataType.CHAR)
9
+ return isArray ? DataTypeOIDs._bpchar : DataTypeOIDs.bpchar;
10
+ if (dataType === DataType.SMALLINT)
11
+ return isArray ? DataTypeOIDs._int2 : DataTypeOIDs.int2;
12
+ if (dataType === DataType.INTEGER)
13
+ return isArray ? DataTypeOIDs._int4 : DataTypeOIDs.int4;
14
+ if (dataType === DataType.BIGINT)
15
+ return isArray ? DataTypeOIDs._int8 : DataTypeOIDs.int8;
16
+ if (dataType === DataType.BINARY)
17
+ return isArray ? DataTypeOIDs._bytea : DataTypeOIDs.bytea;
18
+ if (dataType === DataType.FLOAT)
19
+ return isArray ? DataTypeOIDs._float4 : DataTypeOIDs.float4;
20
+ if (dataType === DataType.DOUBLE)
21
+ return isArray ? DataTypeOIDs._float8 : DataTypeOIDs.float8;
22
+ if (dataType === DataType.GUID)
23
+ return isArray ? DataTypeOIDs._uuid : DataTypeOIDs.uuid;
24
+ if (dataType === DataType.TEXT)
25
+ return isArray ? DataTypeOIDs._text : DataTypeOIDs.text;
26
+ if (dataType === DataType.JSON)
27
+ return isArray ? DataTypeOIDs._json : DataTypeOIDs.json;
28
+ }
File without changes
package/esm/index.js ADDED
@@ -0,0 +1,4 @@
1
+ import '@sqb/postgres-dialect';
2
+ import { registerAdapter } from '@sqb/connect';
3
+ import { PgAdapter } from './pg-adapter.js';
4
+ registerAdapter(new PgAdapter());
File without changes
@@ -0,0 +1,37 @@
1
+ import '@sqb/postgres-dialect';
2
+ import { Connection } from 'postgresql-client';
3
+ import { DataType } from '@sqb/connect';
4
+ import { PgConnection } from './pg-connection.js';
5
+ export class PgAdapter {
6
+ driver = 'postgresql-client';
7
+ dialect = 'postgres';
8
+ features = {
9
+ cursor: true,
10
+ schema: true,
11
+ fetchAsString: [DataType.DATE, DataType.TIMESTAMP, DataType.TIMESTAMPTZ]
12
+ };
13
+ async connect(config) {
14
+ const cfg = { ...config.driverOptions };
15
+ if (config.user)
16
+ cfg.user = config.user;
17
+ if (config.password)
18
+ cfg.password = config.password;
19
+ if (config.host)
20
+ cfg.host = config.host;
21
+ if (config.port)
22
+ cfg.port = config.port;
23
+ if (config.database)
24
+ cfg.database = config.database;
25
+ if (config.schema)
26
+ cfg.schema = config.schema;
27
+ const connection = new Connection(cfg);
28
+ try {
29
+ await connection.connect();
30
+ return new PgConnection(connection);
31
+ }
32
+ catch (e) {
33
+ await connection.close(0);
34
+ throw e;
35
+ }
36
+ }
37
+ }
@@ -0,0 +1,155 @@
1
+ import { BindParam, DataTypeOIDs } from 'postgresql-client';
2
+ import { DataType } from '@sqb/connect';
3
+ import { dataTypeToOID } from './datatype-map.js';
4
+ const SqbDataTypToOIDMap = {
5
+ [DataType.BOOL]: DataTypeOIDs.bool,
6
+ [DataType.CHAR]: DataTypeOIDs.char,
7
+ [DataType.VARCHAR]: DataTypeOIDs.varchar,
8
+ [DataType.SMALLINT]: DataTypeOIDs.int2,
9
+ [DataType.INTEGER]: DataTypeOIDs.int4,
10
+ [DataType.BIGINT]: DataTypeOIDs.int8,
11
+ [DataType.FLOAT]: DataTypeOIDs.float4,
12
+ [DataType.DOUBLE]: DataTypeOIDs.float8,
13
+ [DataType.NUMBER]: DataTypeOIDs.float8,
14
+ [DataType.DATE]: DataTypeOIDs.date,
15
+ [DataType.TIMESTAMP]: DataTypeOIDs.timestamp,
16
+ [DataType.TIMESTAMPTZ]: DataTypeOIDs.timestamptz,
17
+ [DataType.TIME]: DataTypeOIDs.time,
18
+ [DataType.BINARY]: DataTypeOIDs.bytea,
19
+ [DataType.TEXT]: DataTypeOIDs.text,
20
+ [DataType.GUID]: DataTypeOIDs.uuid,
21
+ };
22
+ export class PgConnection {
23
+ intlcon;
24
+ constructor(conn) {
25
+ this.intlcon = conn;
26
+ }
27
+ get sessionId() {
28
+ return this.intlcon && this.intlcon.processID;
29
+ }
30
+ async close() {
31
+ if (!this.intlcon)
32
+ return;
33
+ await this.intlcon.close(0);
34
+ this.intlcon = undefined;
35
+ }
36
+ async reset() {
37
+ return this.rollback();
38
+ }
39
+ async startTransaction() {
40
+ if (!this.intlcon)
41
+ throw new Error('Can not start transaction for a closed db session');
42
+ await this.intlcon.startTransaction();
43
+ }
44
+ async commit() {
45
+ if (!this.intlcon)
46
+ throw new Error('Can not commit transaction for a closed db session');
47
+ await this.intlcon.commit();
48
+ }
49
+ async rollback() {
50
+ if (!this.intlcon)
51
+ return;
52
+ await this.intlcon.rollback();
53
+ }
54
+ async setSavepoint(savepoint) {
55
+ if (!this.intlcon)
56
+ throw new Error('Can not set savepoint for a closed db session');
57
+ return this.intlcon.savepoint(savepoint);
58
+ }
59
+ async releaseSavepoint(savepoint) {
60
+ if (!this.intlcon)
61
+ throw new Error('Can not release savepoint for a closed db session');
62
+ return this.intlcon.releaseSavepoint(savepoint);
63
+ }
64
+ async rollbackSavepoint(savepoint) {
65
+ if (!this.intlcon)
66
+ throw new Error('Can not rollback to a savepoint for a closed db session');
67
+ return this.intlcon.rollbackToSavepoint(savepoint);
68
+ }
69
+ getInTransaction() {
70
+ return !!(this.intlcon && this.intlcon.inTransaction);
71
+ }
72
+ async test() {
73
+ if (!this.intlcon)
74
+ throw new Error('DB session is closed');
75
+ await this.intlcon.query('select 1');
76
+ }
77
+ async getSchema() {
78
+ if (!this.intlcon)
79
+ throw new Error('DB session is closed');
80
+ const r = await this.intlcon.query('SHOW search_path');
81
+ if (r && r.rows && r.rows[0])
82
+ return r.rows[0][0];
83
+ return '';
84
+ }
85
+ async setSchema(schema) {
86
+ if (!this.intlcon)
87
+ throw new Error('Can not set schema of a closed db session');
88
+ await this.intlcon.execute('SET search_path TO ' + schema);
89
+ }
90
+ onGenerateQuery(request) {
91
+ if (this.intlcon) {
92
+ // eslint-disable-next-line dot-notation
93
+ request.dialectVersion = this.intlcon.sessionParameters['server_version'];
94
+ }
95
+ }
96
+ async execute(query) {
97
+ if (!this.intlcon)
98
+ throw new Error('Can not execute query with a closed db session');
99
+ const params = query.params?.map((v, i) => {
100
+ const paramOpts = Array.isArray(query.paramOptions) ? query.paramOptions[i] : undefined;
101
+ if (v != null && paramOpts && paramOpts.dataType) {
102
+ const oid = dataTypeToOID(paramOpts.dataType, paramOpts.isArray);
103
+ if (oid)
104
+ return new BindParam(oid, v);
105
+ }
106
+ return v;
107
+ });
108
+ const opts = {
109
+ autoCommit: query.autoCommit,
110
+ params,
111
+ cursor: query.cursor,
112
+ fetchCount: query.fetchRows,
113
+ objectRows: query.objectRows
114
+ };
115
+ if (query.fetchAsString) {
116
+ const items = query.fetchAsString.reduce((a, v) => {
117
+ const oid = SqbDataTypToOIDMap[v];
118
+ if (oid)
119
+ a.push(oid);
120
+ return a;
121
+ }, []);
122
+ if (items.length)
123
+ opts.fetchAsString = items;
124
+ }
125
+ const resp = await this.intlcon.query(query.sql, opts);
126
+ const out = {};
127
+ if (resp.fields)
128
+ out.fields = this._convertFields(resp.fields);
129
+ if (resp.rows)
130
+ out.rows = resp.rows;
131
+ if (resp.cursor)
132
+ out.cursor = resp.cursor;
133
+ if (resp.rowType)
134
+ out.rowType = resp.rowType;
135
+ if (resp.rowsAffected)
136
+ out.rowsAffected = resp.rowsAffected;
137
+ return out;
138
+ }
139
+ _convertFields(fields) {
140
+ const result = [];
141
+ for (let i = 0; i < fields.length; i++) {
142
+ const v = fields[i];
143
+ const o = {
144
+ fieldName: v.fieldName,
145
+ dataType: v.dataTypeName,
146
+ elementDataType: v.elementDataTypeName,
147
+ jsType: v.jsType,
148
+ isArray: v.isArray,
149
+ _inf: v
150
+ };
151
+ result.push(o);
152
+ }
153
+ return result;
154
+ }
155
+ }
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@sqb/postgres",
3
3
  "description": "SQB serialization extension for PostgreSQL database",
4
- "version": "4.2.1",
4
+ "version": "4.5.1",
5
5
  "author": "Panates",
6
6
  "contributors": [
7
- "Eray Hanoglu <e.hanoglu@panates.com>"
7
+ "Eray Hanoglu <e.hanoglu@panates.com>",
8
+ "Ilker Gurelli <i.gurelli@panates.com>"
8
9
  ],
9
10
  "license": "Apache-2.0",
10
11
  "repository": {
@@ -12,52 +13,66 @@
12
13
  "url": "https://github.com/sqbjs/sqb.git",
13
14
  "directory": "packages/postgres"
14
15
  },
15
- "keywords": [
16
- "javascript",
17
- "typescript",
18
- "sql",
19
- "query",
20
- "builder",
21
- "dialect",
22
- "postgres",
23
- "postgresql",
24
- "pg",
25
- "database"
26
- ],
16
+ "scripts": {
17
+ "compile": "tsc",
18
+ "prebuild": "npm run lint && npm run clean",
19
+ "build": "npm run build:cjs && npm run build:esm",
20
+ "build:cjs": "tsc -b tsconfig-build-cjs.json",
21
+ "build:esm": "tsc -b tsconfig-build-esm.json",
22
+ "postbuild": "cp README.md package.json ../../LICENSE ../../build/postgres && cp ../../package.cjs.json ../../build/postgres/cjs/package.json",
23
+ "lint": "eslint .",
24
+ "test": "jest",
25
+ "cover": "jest --collect-coverage",
26
+ "clean": "npm run clean:src | npm run clean:dist | npm run clean:cover",
27
+ "clean:src": "ts-cleanup -s src --all",
28
+ "clean:dist": "rimraf ../../build/postgres",
29
+ "clean:cover": "rimraf ../../coverage/postgres"
30
+ },
27
31
  "dependencies": {
28
32
  "putil-promisify": "^1.8.5"
29
33
  },
30
34
  "devDependencies": {
31
- "postgresql-client": "^2.1.4"
35
+ "postgresql-client": "^2.1.5"
32
36
  },
33
37
  "peerDependencies": {
34
- "@sqb/builder": "^4.2.0",
35
- "@sqb/connect": "^4.2.0",
36
- "@sqb/postgres-dialect": "^4.2.0",
37
- "postgresql-client": "^2.0.0"
38
+ "@sqb/builder": "^4.5.0",
39
+ "@sqb/connect": "^4.5.0",
40
+ "@sqb/postgres-dialect": "^4.5.0",
41
+ "postgresql-client": "^2.x.x"
38
42
  },
39
- "main": "dist/index.js",
40
- "types": "dist/index.d.ts",
41
- "engines": {
42
- "node": ">= 16.0"
43
+ "type": "module",
44
+ "main": "cjs/index.js",
45
+ "module": "esm/index.js",
46
+ "types": "esm/index.d.ts",
47
+ "exports": {
48
+ ".": {
49
+ "require": "./cjs/index.js",
50
+ "default": "./esm/index.js"
51
+ },
52
+ "./cjs": "./cjs/index.js",
53
+ "./esm": "./esm/index.js"
43
54
  },
44
- "directories": {
45
- "lib": "dist",
46
- "test": "test"
55
+ "engines": {
56
+ "node": ">=16.0",
57
+ "npm": ">=7.0.0"
47
58
  },
48
59
  "files": [
60
+ "bin/",
61
+ "cjs/",
62
+ "esm/",
49
63
  "LICENSE",
50
- "README",
51
- "dist/"
64
+ "README.md"
52
65
  ],
53
- "scripts": {
54
- "build": "tsc -b tsconfig-build.json",
55
- "prebuild": "npm run lint && npm run clean:dist",
56
- "postbuild": "copyfiles package.json README.md LICENSE ../../build/postgres",
57
- "clean": "ts-cleanup -s src --all & ts-cleanup -s test",
58
- "clean:dist": "rimraf ../../build/postgres",
59
- "compile": "tsc",
60
- "lint": "eslint src/** --no-error-on-unmatched-pattern",
61
- "test": "TS_NODE_PROJECT='./test/tsconfig.json' mocha -r ts-node/register -r tsconfig-paths/register --reporter spec test/**/*.spec.ts"
62
- }
66
+ "keywords": [
67
+ "sqb",
68
+ "postgressql",
69
+ "postgres",
70
+ "pg",
71
+ "sql",
72
+ "query",
73
+ "builder",
74
+ "dialect",
75
+ "serializer",
76
+ "database"
77
+ ]
63
78
  }