@iamkirbki/database-handler-pg 2.0.0
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/PostgresAdapter.d.ts +12 -0
- package/dist/PostgresAdapter.d.ts.map +1 -0
- package/dist/PostgresAdapter.js +92 -0
- package/dist/PostgresStatement.d.ts +16 -0
- package/dist/PostgresStatement.d.ts.map +1 -0
- package/dist/PostgresStatement.js +77 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +42 -0
- package/package.json +38 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { IDatabaseAdapter, IStatementAdapter, TableColumnInfo } from "@iamkirbki/database-handler-core";
|
|
2
|
+
import { PoolConfig } from "pg";
|
|
3
|
+
export default class PostgresAdapter implements IDatabaseAdapter {
|
|
4
|
+
private pool;
|
|
5
|
+
connect(config: PoolConfig): Promise<void>;
|
|
6
|
+
prepare(query: string): Promise<IStatementAdapter>;
|
|
7
|
+
exec(query: string): Promise<void>;
|
|
8
|
+
transaction(fn: (items: unknown[]) => void): Promise<Function>;
|
|
9
|
+
tableColumnInformation(tableName: string): Promise<TableColumnInfo[]>;
|
|
10
|
+
close(): Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=PostgresAdapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PostgresAdapter.d.ts","sourceRoot":"","sources":["../src/PostgresAdapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,kCAAkC,CAAC;AACxG,OAAO,EAAQ,UAAU,EAAE,MAAM,IAAI,CAAC;AAGtC,MAAM,CAAC,OAAO,OAAO,eAAgB,YAAW,gBAAgB;IAC5D,OAAO,CAAC,IAAI,CAAqB;IAE3B,OAAO,CAAC,MAAM,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC;IAI1C,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,iBAAiB,CAAC;IAKlD,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAOlC,WAAW,CAAC,EAAE,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,KAAK,IAAI,GAAG,OAAO,CAAC,QAAQ,CAAC;IA6B9D,sBAAsB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,EAAE,CAAC;IAyBrE,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAO/B"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { Pool } from "pg";
|
|
11
|
+
import PostgresStatement from "./PostgresStatement";
|
|
12
|
+
export default class PostgresAdapter {
|
|
13
|
+
constructor() {
|
|
14
|
+
this.pool = null;
|
|
15
|
+
}
|
|
16
|
+
connect(config) {
|
|
17
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
18
|
+
this.pool = new Pool(config);
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
prepare(query) {
|
|
22
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
23
|
+
const client = this.pool ? yield this.pool.connect() : undefined;
|
|
24
|
+
return new PostgresStatement(query, client);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
exec(query) {
|
|
28
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
29
|
+
const client = this.pool ? yield this.pool.connect() : undefined;
|
|
30
|
+
const statement = new PostgresStatement(query, client);
|
|
31
|
+
yield statement.run();
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
// eslint-disable-next-line no-unused-vars
|
|
35
|
+
transaction(fn) {
|
|
36
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
37
|
+
const client = this.pool ? yield this.pool.connect() : undefined;
|
|
38
|
+
if (!client) {
|
|
39
|
+
throw new Error("Database client is not available for transaction.");
|
|
40
|
+
}
|
|
41
|
+
yield client.query('BEGIN');
|
|
42
|
+
const rollback = () => __awaiter(this, void 0, void 0, function* () {
|
|
43
|
+
yield client.query('ROLLBACK');
|
|
44
|
+
client.release();
|
|
45
|
+
});
|
|
46
|
+
const commit = () => __awaiter(this, void 0, void 0, function* () {
|
|
47
|
+
yield client.query('COMMIT');
|
|
48
|
+
client.release();
|
|
49
|
+
});
|
|
50
|
+
try {
|
|
51
|
+
fn([]);
|
|
52
|
+
yield commit();
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
yield rollback();
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
return commit;
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
tableColumnInformation(tableName) {
|
|
62
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
63
|
+
const client = this.pool ? yield this.pool.connect() : undefined;
|
|
64
|
+
if (!client) {
|
|
65
|
+
throw new Error("Database client is not available.");
|
|
66
|
+
}
|
|
67
|
+
const query = `
|
|
68
|
+
SELECT *
|
|
69
|
+
FROM information_schema.columns
|
|
70
|
+
WHERE table_name = $1
|
|
71
|
+
`;
|
|
72
|
+
const res = yield client.query(query, [tableName]);
|
|
73
|
+
client.release();
|
|
74
|
+
return res.rows.map((row, index) => ({
|
|
75
|
+
cid: index,
|
|
76
|
+
name: row.column_name,
|
|
77
|
+
type: row.data_type,
|
|
78
|
+
notnull: row.is_nullable === 'NO' ? 1 : 0,
|
|
79
|
+
dflt_value: row.column_default,
|
|
80
|
+
pk: 0 // PostgreSQL does not provide primary key info in this query
|
|
81
|
+
}));
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
close() {
|
|
85
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
86
|
+
if (this.pool) {
|
|
87
|
+
yield this.pool.end();
|
|
88
|
+
this.pool = null;
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { IStatementAdapter } from "@iamkirbki/database-handler-core";
|
|
2
|
+
import { PoolClient } from "pg";
|
|
3
|
+
export default class PostgresStatement implements IStatementAdapter {
|
|
4
|
+
private query;
|
|
5
|
+
private client;
|
|
6
|
+
constructor(query: string, _client: PoolClient | undefined);
|
|
7
|
+
/**
|
|
8
|
+
* Transform SQLite-style named parameters (@name) to PostgreSQL positional parameters ($1, $2)
|
|
9
|
+
* and convert the parameters object to an array
|
|
10
|
+
*/
|
|
11
|
+
private transformQuery;
|
|
12
|
+
run(parameters?: object): Promise<unknown>;
|
|
13
|
+
all(parameters?: object): Promise<unknown[]>;
|
|
14
|
+
get(parameters?: object): Promise<unknown | undefined>;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=PostgresStatement.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PostgresStatement.d.ts","sourceRoot":"","sources":["../src/PostgresStatement.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kCAAkC,CAAC;AACrE,OAAO,EAAE,UAAU,EAAE,MAAM,IAAI,CAAC;AAEhC,MAAM,CAAC,OAAO,OAAO,iBAAkB,YAAW,iBAAiB;IAC/D,OAAO,CAAC,KAAK,CAAS;IACtB,OAAO,CAAC,MAAM,CAAyB;gBAE3B,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,UAAU,GAAG,SAAS;IAK1D;;;OAGG;IACH,OAAO,CAAC,cAAc;IAoBhB,GAAG,CAAC,UAAU,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAW1C,GAAG,CAAC,UAAU,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;IAW5C,GAAG,CAAC,UAAU,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,GAAG,SAAS,CAAC;CAU/D"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
export default class PostgresStatement {
|
|
11
|
+
constructor(query, _client) {
|
|
12
|
+
this.query = query;
|
|
13
|
+
this.client = _client;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Transform SQLite-style named parameters (@name) to PostgreSQL positional parameters ($1, $2)
|
|
17
|
+
* and convert the parameters object to an array
|
|
18
|
+
*/
|
|
19
|
+
transformQuery(parameters) {
|
|
20
|
+
if (!parameters) {
|
|
21
|
+
return { query: this.query, values: [] };
|
|
22
|
+
}
|
|
23
|
+
const paramMap = new Map();
|
|
24
|
+
let paramIndex = 1;
|
|
25
|
+
const values = [];
|
|
26
|
+
const transformedQuery = this.query.replace(/@(\w+)/g, (_match, paramName) => {
|
|
27
|
+
if (!paramMap.has(paramName)) {
|
|
28
|
+
paramMap.set(paramName, paramIndex++);
|
|
29
|
+
values.push(parameters[paramName]);
|
|
30
|
+
}
|
|
31
|
+
return `$${paramMap.get(paramName)}`;
|
|
32
|
+
});
|
|
33
|
+
return { query: transformedQuery, values };
|
|
34
|
+
}
|
|
35
|
+
run(parameters) {
|
|
36
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
37
|
+
var _a, _b;
|
|
38
|
+
const { query, values } = this.transformQuery(parameters);
|
|
39
|
+
try {
|
|
40
|
+
const res = yield ((_a = this.client) === null || _a === void 0 ? void 0 : _a.query(query, values));
|
|
41
|
+
return res;
|
|
42
|
+
}
|
|
43
|
+
finally {
|
|
44
|
+
(_b = this.client) === null || _b === void 0 ? void 0 : _b.release();
|
|
45
|
+
this.client = undefined; // Prevent double release
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
all(parameters) {
|
|
50
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
51
|
+
var _a, _b;
|
|
52
|
+
const { query, values } = this.transformQuery(parameters);
|
|
53
|
+
try {
|
|
54
|
+
const res = yield ((_a = this.client) === null || _a === void 0 ? void 0 : _a.query(query, values));
|
|
55
|
+
return (res === null || res === void 0 ? void 0 : res.rows) || [];
|
|
56
|
+
}
|
|
57
|
+
finally {
|
|
58
|
+
(_b = this.client) === null || _b === void 0 ? void 0 : _b.release();
|
|
59
|
+
this.client = undefined; // Prevent double release
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
get(parameters) {
|
|
64
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
65
|
+
var _a, _b;
|
|
66
|
+
const { query, values } = this.transformQuery(parameters);
|
|
67
|
+
try {
|
|
68
|
+
const res = yield ((_a = this.client) === null || _a === void 0 ? void 0 : _a.query(query, values));
|
|
69
|
+
return res === null || res === void 0 ? void 0 : res.rows[0];
|
|
70
|
+
}
|
|
71
|
+
finally {
|
|
72
|
+
(_b = this.client) === null || _b === void 0 ? void 0 : _b.release();
|
|
73
|
+
this.client = undefined; // Prevent double release
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Database } from "@iamkirbki/database-handler-core";
|
|
2
|
+
import { PoolConfig } from "pg";
|
|
3
|
+
export declare class PostgresDatabase extends Database {
|
|
4
|
+
private postgresAdapter;
|
|
5
|
+
private constructor();
|
|
6
|
+
static create(config: PoolConfig): Promise<PostgresDatabase>;
|
|
7
|
+
close(): Promise<void>;
|
|
8
|
+
cleanDatabase(): Promise<void>;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kCAAkC,CAAC;AAE5D,OAAO,EAAE,UAAU,EAAE,MAAM,IAAI,CAAC;AAEhC,qBAAa,gBAAiB,SAAQ,QAAQ;IAC1C,OAAO,CAAC,eAAe,CAAkB;IAEzC,OAAO;WAKM,MAAM,CAAC,MAAM,EAAE,UAAU,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAM5D,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAItB,aAAa,IAAI,OAAO,CAAC,IAAI,CAAC;CAYvC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { Database } from "@iamkirbki/database-handler-core";
|
|
11
|
+
import PostgresAdapter from "./PostgresAdapter";
|
|
12
|
+
export class PostgresDatabase extends Database {
|
|
13
|
+
constructor(adapter) {
|
|
14
|
+
super(adapter);
|
|
15
|
+
this.postgresAdapter = adapter;
|
|
16
|
+
}
|
|
17
|
+
static create(config) {
|
|
18
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
19
|
+
const adapter = new PostgresAdapter();
|
|
20
|
+
yield adapter.connect(config);
|
|
21
|
+
return new PostgresDatabase(adapter);
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
close() {
|
|
25
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
26
|
+
yield this.postgresAdapter.close();
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
cleanDatabase() {
|
|
30
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
31
|
+
// Get all tables and drop them
|
|
32
|
+
const tables = yield this.postgresAdapter.prepare(`
|
|
33
|
+
SELECT tablename FROM pg_tables
|
|
34
|
+
WHERE schemaname = 'public'
|
|
35
|
+
`);
|
|
36
|
+
const result = yield tables.all();
|
|
37
|
+
for (const row of result) {
|
|
38
|
+
yield this.postgresAdapter.exec(`DROP TABLE IF EXISTS "${row.tablename}" CASCADE`);
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@iamkirbki/database-handler-pg",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"author": "iamkirbki",
|
|
5
|
+
"description": "PostgreSQL adapter for @kirbkis-database-handler/core",
|
|
6
|
+
"license": "ISC",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "dist/index.js",
|
|
9
|
+
"module": "dist/index.js",
|
|
10
|
+
"types": "dist/index.d.ts",
|
|
11
|
+
"files": [
|
|
12
|
+
"dist"
|
|
13
|
+
],
|
|
14
|
+
"keywords": [
|
|
15
|
+
"database",
|
|
16
|
+
"postgres",
|
|
17
|
+
"postgresql",
|
|
18
|
+
"pg",
|
|
19
|
+
"orm",
|
|
20
|
+
"typescript"
|
|
21
|
+
],
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "https://github.com/IamKirbki/bettersqlite3-handler.git",
|
|
25
|
+
"directory": "packages/pg"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "tsc"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@iamkirbki/database-handler-core": "^2.0.0",
|
|
32
|
+
"pg": "^8.16.3"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@types/pg": "^8.15.6",
|
|
36
|
+
"typescript": "^5.9.3"
|
|
37
|
+
}
|
|
38
|
+
}
|