@heyhru/server-plugin-pg 0.1.9
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 +41 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +116 -0
- package/dist/index.mjs +79 -0
- package/package.json +38 -0
package/README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# @heyhru/server-util-pg
|
|
2
|
+
|
|
3
|
+
PostgreSQL connection pool with unified `DbPool` interface.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @heyhru/server-util-pg
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## API
|
|
12
|
+
|
|
13
|
+
### `createPool(config): DbPool`
|
|
14
|
+
|
|
15
|
+
Create a PostgreSQL connection pool.
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { createPool } from "@heyhru/server-util-pg";
|
|
19
|
+
|
|
20
|
+
const pool = createPool({
|
|
21
|
+
host: "localhost",
|
|
22
|
+
port: 5432,
|
|
23
|
+
database: "mydb",
|
|
24
|
+
username: "postgres",
|
|
25
|
+
password: "password",
|
|
26
|
+
poolMin: 2,
|
|
27
|
+
poolMax: 10,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const rows = await pool.execute("SELECT * FROM users");
|
|
31
|
+
await pool.end();
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### `DbPool` interface
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
interface DbPool {
|
|
38
|
+
execute(sql: string): Promise<unknown[]>;
|
|
39
|
+
end(): Promise<void>;
|
|
40
|
+
}
|
|
41
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export interface PgPoolConfig {
|
|
2
|
+
host: string;
|
|
3
|
+
port: number;
|
|
4
|
+
database: string;
|
|
5
|
+
username: string;
|
|
6
|
+
password: string;
|
|
7
|
+
poolMin: number;
|
|
8
|
+
poolMax: number;
|
|
9
|
+
ssl?: boolean;
|
|
10
|
+
}
|
|
11
|
+
export interface DbPool {
|
|
12
|
+
execute(sql: string): Promise<unknown[]>;
|
|
13
|
+
end(): Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
export declare function createPool(config: PgPoolConfig): DbPool;
|
|
16
|
+
export interface PgDb {
|
|
17
|
+
query<T = Record<string, unknown>>(sql: string, params?: unknown[]): Promise<T[]>;
|
|
18
|
+
queryOne<T = Record<string, unknown>>(sql: string, params?: unknown[]): Promise<T | undefined>;
|
|
19
|
+
run(sql: string, params?: unknown[]): Promise<{
|
|
20
|
+
changes: number;
|
|
21
|
+
}>;
|
|
22
|
+
exec(sql: string): Promise<void>;
|
|
23
|
+
close(): Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
export declare function createPgDb(url: string): PgDb;
|
|
26
|
+
export declare function getPgDb(): PgDb;
|
|
27
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,GAAG,CAAC,EAAE,OAAO,CAAC;CACf;AAED,MAAM,WAAW,MAAM;IACrB,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC;IACzC,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACtB;AAED,wBAAgB,UAAU,CAAC,MAAM,EAAE,YAAY,GAAG,MAAM,CAqBvD;AAID,MAAM,WAAW,IAAI;IACnB,KAAK,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC;IAClF,QAAQ,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;IAC/F,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC;QAAE,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IACnE,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACjC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB;AAoDD,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAI5C;AAED,wBAAgB,OAAO,IAAI,IAAI,CAG9B"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
createPgDb: () => createPgDb,
|
|
34
|
+
createPool: () => createPool,
|
|
35
|
+
getPgDb: () => getPgDb
|
|
36
|
+
});
|
|
37
|
+
module.exports = __toCommonJS(index_exports);
|
|
38
|
+
var import_pg = __toESM(require("pg"));
|
|
39
|
+
function createPool(config) {
|
|
40
|
+
const pool = new import_pg.default.Pool({
|
|
41
|
+
host: config.host,
|
|
42
|
+
port: config.port,
|
|
43
|
+
database: config.database,
|
|
44
|
+
user: config.username,
|
|
45
|
+
password: config.password,
|
|
46
|
+
min: config.poolMin,
|
|
47
|
+
max: config.poolMax,
|
|
48
|
+
ssl: config.ssl ? { rejectUnauthorized: false } : void 0
|
|
49
|
+
});
|
|
50
|
+
return {
|
|
51
|
+
async execute(sql) {
|
|
52
|
+
const result = await pool.query(sql);
|
|
53
|
+
return result.rows;
|
|
54
|
+
},
|
|
55
|
+
async end() {
|
|
56
|
+
await pool.end();
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function toNumberedParams(sql) {
|
|
61
|
+
let idx = 0;
|
|
62
|
+
let inString = false;
|
|
63
|
+
let result = "";
|
|
64
|
+
for (let i = 0; i < sql.length; i++) {
|
|
65
|
+
const ch = sql[i];
|
|
66
|
+
if (ch === "'" && sql[i - 1] !== "\\") {
|
|
67
|
+
inString = !inString;
|
|
68
|
+
result += ch;
|
|
69
|
+
} else if (ch === "?" && !inString) {
|
|
70
|
+
idx++;
|
|
71
|
+
result += `$${idx}`;
|
|
72
|
+
} else {
|
|
73
|
+
result += ch;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return result;
|
|
77
|
+
}
|
|
78
|
+
function createPgDbInstance(connectionString) {
|
|
79
|
+
const pool = new import_pg.default.Pool({ connectionString });
|
|
80
|
+
return {
|
|
81
|
+
async query(sql, params = []) {
|
|
82
|
+
const result = await pool.query(toNumberedParams(sql), params);
|
|
83
|
+
return result.rows;
|
|
84
|
+
},
|
|
85
|
+
async queryOne(sql, params = []) {
|
|
86
|
+
const result = await pool.query(toNumberedParams(sql), params);
|
|
87
|
+
return result.rows[0];
|
|
88
|
+
},
|
|
89
|
+
async run(sql, params = []) {
|
|
90
|
+
const result = await pool.query(toNumberedParams(sql), params);
|
|
91
|
+
return { changes: result.rowCount ?? 0 };
|
|
92
|
+
},
|
|
93
|
+
async exec(sql) {
|
|
94
|
+
await pool.query(sql);
|
|
95
|
+
},
|
|
96
|
+
async close() {
|
|
97
|
+
await pool.end();
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
var _db = null;
|
|
102
|
+
function createPgDb(url) {
|
|
103
|
+
if (_db) throw new Error("PgDb already initialized. Call close() first.");
|
|
104
|
+
_db = createPgDbInstance(url);
|
|
105
|
+
return _db;
|
|
106
|
+
}
|
|
107
|
+
function getPgDb() {
|
|
108
|
+
if (!_db) throw new Error("PgDb not initialized. Call createPgDb() first.");
|
|
109
|
+
return _db;
|
|
110
|
+
}
|
|
111
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
112
|
+
0 && (module.exports = {
|
|
113
|
+
createPgDb,
|
|
114
|
+
createPool,
|
|
115
|
+
getPgDb
|
|
116
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import pg from "pg";
|
|
3
|
+
function createPool(config) {
|
|
4
|
+
const pool = new pg.Pool({
|
|
5
|
+
host: config.host,
|
|
6
|
+
port: config.port,
|
|
7
|
+
database: config.database,
|
|
8
|
+
user: config.username,
|
|
9
|
+
password: config.password,
|
|
10
|
+
min: config.poolMin,
|
|
11
|
+
max: config.poolMax,
|
|
12
|
+
ssl: config.ssl ? { rejectUnauthorized: false } : void 0
|
|
13
|
+
});
|
|
14
|
+
return {
|
|
15
|
+
async execute(sql) {
|
|
16
|
+
const result = await pool.query(sql);
|
|
17
|
+
return result.rows;
|
|
18
|
+
},
|
|
19
|
+
async end() {
|
|
20
|
+
await pool.end();
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
function toNumberedParams(sql) {
|
|
25
|
+
let idx = 0;
|
|
26
|
+
let inString = false;
|
|
27
|
+
let result = "";
|
|
28
|
+
for (let i = 0; i < sql.length; i++) {
|
|
29
|
+
const ch = sql[i];
|
|
30
|
+
if (ch === "'" && sql[i - 1] !== "\\") {
|
|
31
|
+
inString = !inString;
|
|
32
|
+
result += ch;
|
|
33
|
+
} else if (ch === "?" && !inString) {
|
|
34
|
+
idx++;
|
|
35
|
+
result += `$${idx}`;
|
|
36
|
+
} else {
|
|
37
|
+
result += ch;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
42
|
+
function createPgDbInstance(connectionString) {
|
|
43
|
+
const pool = new pg.Pool({ connectionString });
|
|
44
|
+
return {
|
|
45
|
+
async query(sql, params = []) {
|
|
46
|
+
const result = await pool.query(toNumberedParams(sql), params);
|
|
47
|
+
return result.rows;
|
|
48
|
+
},
|
|
49
|
+
async queryOne(sql, params = []) {
|
|
50
|
+
const result = await pool.query(toNumberedParams(sql), params);
|
|
51
|
+
return result.rows[0];
|
|
52
|
+
},
|
|
53
|
+
async run(sql, params = []) {
|
|
54
|
+
const result = await pool.query(toNumberedParams(sql), params);
|
|
55
|
+
return { changes: result.rowCount ?? 0 };
|
|
56
|
+
},
|
|
57
|
+
async exec(sql) {
|
|
58
|
+
await pool.query(sql);
|
|
59
|
+
},
|
|
60
|
+
async close() {
|
|
61
|
+
await pool.end();
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
var _db = null;
|
|
66
|
+
function createPgDb(url) {
|
|
67
|
+
if (_db) throw new Error("PgDb already initialized. Call close() first.");
|
|
68
|
+
_db = createPgDbInstance(url);
|
|
69
|
+
return _db;
|
|
70
|
+
}
|
|
71
|
+
function getPgDb() {
|
|
72
|
+
if (!_db) throw new Error("PgDb not initialized. Call createPgDb() first.");
|
|
73
|
+
return _db;
|
|
74
|
+
}
|
|
75
|
+
export {
|
|
76
|
+
createPgDb,
|
|
77
|
+
createPool,
|
|
78
|
+
getPgDb
|
|
79
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@heyhru/server-plugin-pg",
|
|
3
|
+
"publishConfig": {
|
|
4
|
+
"access": "public"
|
|
5
|
+
},
|
|
6
|
+
"version": "0.1.9",
|
|
7
|
+
"description": "PostgreSQL connection pool with unified execute interface",
|
|
8
|
+
"main": "./dist/index.js",
|
|
9
|
+
"module": "./dist/index.mjs",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"import": "./dist/index.mjs",
|
|
15
|
+
"require": "./dist/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsup && tsc --emitDeclarationOnly",
|
|
23
|
+
"dev": "tsup --watch",
|
|
24
|
+
"lint": "eslint src",
|
|
25
|
+
"test": "vitest run",
|
|
26
|
+
"clean": "rm -rf dist"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"pg": "^8.20.0"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@types/pg": "^8.20.0",
|
|
33
|
+
"tsup": "^8.5.1",
|
|
34
|
+
"typescript": "^6.0.2",
|
|
35
|
+
"vitest": "^4.1.2"
|
|
36
|
+
},
|
|
37
|
+
"gitHead": "f25e81a1f0e845f5d6c63f5c8df1fbe069f11239"
|
|
38
|
+
}
|