@sqb/postgres 4.0.8 → 4.0.12
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.d.ts +12 -0
- package/dist/PgAdapter.js +43 -0
- package/dist/PgConnection.d.ts +22 -0
- package/dist/PgConnection.js +158 -0
- package/dist/datatype-map.d.ts +3 -0
- package/dist/datatype-map.js +32 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +6 -0
- package/package.json +5 -6
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Adapter, ClientConfiguration, DataType } from '@sqb/connect';
|
|
2
|
+
import '@sqb/postgres-dialect';
|
|
3
|
+
export declare class PgAdapter implements Adapter {
|
|
4
|
+
driver: string;
|
|
5
|
+
dialect: string;
|
|
6
|
+
features: {
|
|
7
|
+
cursor: boolean;
|
|
8
|
+
schema: boolean;
|
|
9
|
+
fetchAsString: DataType[];
|
|
10
|
+
};
|
|
11
|
+
connect(config: ClientConfiguration): Promise<Adapter.Connection>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PgAdapter = void 0;
|
|
4
|
+
const connect_1 = require("@sqb/connect");
|
|
5
|
+
require("@sqb/postgres-dialect");
|
|
6
|
+
const postgresql_client_1 = require("postgresql-client");
|
|
7
|
+
const PgConnection_1 = require("./PgConnection");
|
|
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
|
+
}
|
|
18
|
+
async connect(config) {
|
|
19
|
+
const cfg = { ...config.driverOptions };
|
|
20
|
+
if (config.user)
|
|
21
|
+
cfg.user = config.user;
|
|
22
|
+
if (config.password)
|
|
23
|
+
cfg.password = config.password;
|
|
24
|
+
if (config.host)
|
|
25
|
+
cfg.host = config.host;
|
|
26
|
+
if (config.port)
|
|
27
|
+
cfg.port = config.port;
|
|
28
|
+
if (config.database)
|
|
29
|
+
cfg.database = config.database;
|
|
30
|
+
if (config.schema)
|
|
31
|
+
cfg.schema = config.schema;
|
|
32
|
+
const connection = new postgresql_client_1.Connection(cfg);
|
|
33
|
+
try {
|
|
34
|
+
await connection.connect();
|
|
35
|
+
return new PgConnection_1.PgConnection(connection);
|
|
36
|
+
}
|
|
37
|
+
catch (e) {
|
|
38
|
+
await connection.close(0);
|
|
39
|
+
throw e;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
exports.PgAdapter = PgAdapter;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Adapter, QueryRequest } from '@sqb/connect';
|
|
2
|
+
import { Connection, FieldInfo } from 'postgresql-client';
|
|
3
|
+
export declare class PgConnection implements Adapter.Connection {
|
|
4
|
+
private intlcon?;
|
|
5
|
+
constructor(conn: Connection);
|
|
6
|
+
get sessionId(): any;
|
|
7
|
+
close(): Promise<void>;
|
|
8
|
+
reset(): Promise<void>;
|
|
9
|
+
startTransaction(): Promise<void>;
|
|
10
|
+
commit(): Promise<void>;
|
|
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;
|
|
16
|
+
test(): Promise<void>;
|
|
17
|
+
getSchema(): Promise<string>;
|
|
18
|
+
setSchema(schema: string): Promise<void>;
|
|
19
|
+
onGenerateQuery(request: QueryRequest): void;
|
|
20
|
+
execute(query: QueryRequest): Promise<Adapter.Response>;
|
|
21
|
+
_convertFields(fields: FieldInfo[]): any[];
|
|
22
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PgConnection = void 0;
|
|
4
|
+
const connect_1 = require("@sqb/connect");
|
|
5
|
+
const postgresql_client_1 = require("postgresql-client");
|
|
6
|
+
const datatype_map_1 = require("./datatype-map");
|
|
7
|
+
const SqbDataTypToOIDMap = {
|
|
8
|
+
[connect_1.DataType.BOOL]: postgresql_client_1.DataTypeOIDs.bool,
|
|
9
|
+
[connect_1.DataType.CHAR]: postgresql_client_1.DataTypeOIDs.char,
|
|
10
|
+
[connect_1.DataType.VARCHAR]: postgresql_client_1.DataTypeOIDs.varchar,
|
|
11
|
+
[connect_1.DataType.SMALLINT]: postgresql_client_1.DataTypeOIDs.int2,
|
|
12
|
+
[connect_1.DataType.INTEGER]: postgresql_client_1.DataTypeOIDs.int4,
|
|
13
|
+
[connect_1.DataType.BIGINT]: postgresql_client_1.DataTypeOIDs.int8,
|
|
14
|
+
[connect_1.DataType.FLOAT]: postgresql_client_1.DataTypeOIDs.float4,
|
|
15
|
+
[connect_1.DataType.DOUBLE]: postgresql_client_1.DataTypeOIDs.float8,
|
|
16
|
+
[connect_1.DataType.NUMBER]: postgresql_client_1.DataTypeOIDs.float8,
|
|
17
|
+
[connect_1.DataType.DATE]: postgresql_client_1.DataTypeOIDs.date,
|
|
18
|
+
[connect_1.DataType.TIMESTAMP]: postgresql_client_1.DataTypeOIDs.timestamp,
|
|
19
|
+
[connect_1.DataType.TIMESTAMPTZ]: postgresql_client_1.DataTypeOIDs.timestamptz,
|
|
20
|
+
[connect_1.DataType.TIME]: postgresql_client_1.DataTypeOIDs.time,
|
|
21
|
+
[connect_1.DataType.BINARY]: postgresql_client_1.DataTypeOIDs.bytea,
|
|
22
|
+
[connect_1.DataType.TEXT]: postgresql_client_1.DataTypeOIDs.text,
|
|
23
|
+
[connect_1.DataType.GUID]: postgresql_client_1.DataTypeOIDs.uuid,
|
|
24
|
+
};
|
|
25
|
+
class PgConnection {
|
|
26
|
+
constructor(conn) {
|
|
27
|
+
this.intlcon = conn;
|
|
28
|
+
}
|
|
29
|
+
get sessionId() {
|
|
30
|
+
return this.intlcon && this.intlcon.processID;
|
|
31
|
+
}
|
|
32
|
+
async close() {
|
|
33
|
+
if (!this.intlcon)
|
|
34
|
+
return;
|
|
35
|
+
await this.intlcon.close(0);
|
|
36
|
+
this.intlcon = undefined;
|
|
37
|
+
}
|
|
38
|
+
async reset() {
|
|
39
|
+
return this.rollback();
|
|
40
|
+
}
|
|
41
|
+
async startTransaction() {
|
|
42
|
+
if (!this.intlcon)
|
|
43
|
+
throw new Error('Can not start transaction for a closed db session');
|
|
44
|
+
await this.intlcon.startTransaction();
|
|
45
|
+
}
|
|
46
|
+
async commit() {
|
|
47
|
+
if (!this.intlcon)
|
|
48
|
+
throw new Error('Can not commit transaction for a closed db session');
|
|
49
|
+
await this.intlcon.commit();
|
|
50
|
+
}
|
|
51
|
+
async rollback() {
|
|
52
|
+
if (!this.intlcon)
|
|
53
|
+
return;
|
|
54
|
+
await this.intlcon.rollback();
|
|
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
|
+
}
|
|
74
|
+
async test() {
|
|
75
|
+
if (!this.intlcon)
|
|
76
|
+
throw new Error('DB session is closed');
|
|
77
|
+
await this.intlcon.query('select 1');
|
|
78
|
+
}
|
|
79
|
+
async getSchema() {
|
|
80
|
+
if (!this.intlcon)
|
|
81
|
+
throw new Error('DB session is closed');
|
|
82
|
+
const r = await this.intlcon.query('SHOW search_path');
|
|
83
|
+
if (r && r.rows && r.rows[0])
|
|
84
|
+
return r.rows[0][0];
|
|
85
|
+
return '';
|
|
86
|
+
}
|
|
87
|
+
async setSchema(schema) {
|
|
88
|
+
if (!this.intlcon)
|
|
89
|
+
throw new Error('Can not set schema of a closed db session');
|
|
90
|
+
await this.intlcon.execute('SET search_path TO ' + schema);
|
|
91
|
+
}
|
|
92
|
+
onGenerateQuery(request) {
|
|
93
|
+
if (this.intlcon) {
|
|
94
|
+
// eslint-disable-next-line dot-notation
|
|
95
|
+
request.dialectVersion = this.intlcon.sessionParameters['server_version'];
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
async execute(query) {
|
|
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
|
+
});
|
|
110
|
+
const opts = {
|
|
111
|
+
autoCommit: query.autoCommit,
|
|
112
|
+
params,
|
|
113
|
+
cursor: query.cursor,
|
|
114
|
+
fetchCount: query.fetchRows,
|
|
115
|
+
objectRows: query.objectRows
|
|
116
|
+
};
|
|
117
|
+
if (query.fetchAsString) {
|
|
118
|
+
const items = query.fetchAsString.reduce((a, v) => {
|
|
119
|
+
const oid = SqbDataTypToOIDMap[v];
|
|
120
|
+
if (oid)
|
|
121
|
+
a.push(oid);
|
|
122
|
+
return a;
|
|
123
|
+
}, []);
|
|
124
|
+
if (items.length)
|
|
125
|
+
opts.fetchAsString = items;
|
|
126
|
+
}
|
|
127
|
+
const resp = await this.intlcon.query(query.sql, opts);
|
|
128
|
+
const out = {};
|
|
129
|
+
if (resp.fields)
|
|
130
|
+
out.fields = this._convertFields(resp.fields);
|
|
131
|
+
if (resp.rows)
|
|
132
|
+
out.rows = resp.rows;
|
|
133
|
+
if (resp.cursor)
|
|
134
|
+
out.cursor = resp.cursor;
|
|
135
|
+
if (resp.rowType)
|
|
136
|
+
out.rowType = resp.rowType;
|
|
137
|
+
if (resp.rowsAffected)
|
|
138
|
+
out.rowsAffected = resp.rowsAffected;
|
|
139
|
+
return out;
|
|
140
|
+
}
|
|
141
|
+
_convertFields(fields) {
|
|
142
|
+
const result = [];
|
|
143
|
+
for (let i = 0; i < fields.length; i++) {
|
|
144
|
+
const v = fields[i];
|
|
145
|
+
const o = {
|
|
146
|
+
fieldName: v.fieldName,
|
|
147
|
+
dataType: v.dataTypeName,
|
|
148
|
+
elementDataType: v.elementDataTypeName,
|
|
149
|
+
jsType: v.jsType,
|
|
150
|
+
isArray: v.isArray,
|
|
151
|
+
_inf: v
|
|
152
|
+
};
|
|
153
|
+
result.push(o);
|
|
154
|
+
}
|
|
155
|
+
return result;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
exports.PgConnection = PgConnection;
|
|
@@ -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.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import '@sqb/postgres-dialect';
|
package/dist/index.js
ADDED
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sqb/postgres",
|
|
3
3
|
"description": "SQB serialization extension for PostgreSQL database",
|
|
4
|
-
"version": "4.0.
|
|
5
|
-
"author": "Panates
|
|
4
|
+
"version": "4.0.12",
|
|
5
|
+
"author": "Panates",
|
|
6
6
|
"contributors": [
|
|
7
7
|
"Eray Hanoglu <e.hanoglu@panates.com>"
|
|
8
8
|
],
|
|
@@ -27,10 +27,10 @@
|
|
|
27
27
|
"putil-promisify": "^1.8.5"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
|
-
"postgresql-client": "^
|
|
30
|
+
"postgresql-client": "^2.0.0"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
|
-
"postgresql-client": "^
|
|
33
|
+
"postgresql-client": "^2.0.0",
|
|
34
34
|
"@sqb/postgres-dialect": "^4.x.x",
|
|
35
35
|
"@sqb/builder": "^4.x.x",
|
|
36
36
|
"@sqb/connect": "^4.x.x"
|
|
@@ -57,7 +57,6 @@
|
|
|
57
57
|
"clean:dist": "rimraf ../../build/postgres",
|
|
58
58
|
"compile": "tsc",
|
|
59
59
|
"lint": "eslint src/** --no-error-on-unmatched-pattern",
|
|
60
|
-
"test": "TS_NODE_PROJECT='./test/tsconfig.json' mocha -r ts-node/register -r tsconfig-paths/register --reporter spec test/**/*.spec.ts"
|
|
61
|
-
"version": "#version"
|
|
60
|
+
"test": "TS_NODE_PROJECT='./test/tsconfig.json' mocha -r ts-node/register -r tsconfig-paths/register --reporter spec test/**/*.spec.ts"
|
|
62
61
|
}
|
|
63
62
|
}
|