@anabranch/db-postgres 0.1.0 → 0.1.5
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 +4 -0
- package/esm/db/db.d.ts +9 -9
- package/esm/db/db.d.ts.map +1 -1
- package/esm/db/db.js +57 -57
- package/esm/db-postgres/index.d.ts +1 -16
- package/esm/db-postgres/index.d.ts.map +1 -1
- package/esm/db-postgres/index.js +1 -64
- package/esm/db-postgres/postgres.d.ts +19 -0
- package/esm/db-postgres/postgres.d.ts.map +1 -0
- package/esm/db-postgres/postgres.js +64 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -111,3 +111,7 @@ npm install @anabranch/db-postgres @anabranch/db pg pg-cursor
|
|
|
111
111
|
|
|
112
112
|
See [@anabranch/db](https://jsr.io/@anabranch/db) for the core database
|
|
113
113
|
abstraction.
|
|
114
|
+
|
|
115
|
+
See
|
|
116
|
+
[generated documentation](https://frodi-karlsson.github.io/anabranch/db-postgres)
|
|
117
|
+
for full API details.
|
package/esm/db/db.d.ts
CHANGED
|
@@ -1,15 +1,6 @@
|
|
|
1
1
|
import { type Promisable, Source, Task } from "../anabranch/index.js";
|
|
2
2
|
import type { DBAdapter, DBConnector, DBTransactionAdapter } from "./adapter.js";
|
|
3
3
|
import { ConnectionFailed, ConstraintViolation, QueryFailed, TransactionFailed } from "./errors.js";
|
|
4
|
-
/** Database transaction with Task semantics. */
|
|
5
|
-
export declare class DBTransaction {
|
|
6
|
-
private readonly adapter;
|
|
7
|
-
constructor(adapter: DBTransactionAdapter);
|
|
8
|
-
query<T>(sql: string, params?: unknown[]): Task<T[], QueryFailed>;
|
|
9
|
-
execute(sql: string, params?: unknown[]): Task<number, QueryFailed>;
|
|
10
|
-
commit(): Task<void, TransactionFailed>;
|
|
11
|
-
rollback(): Task<void, TransactionFailed>;
|
|
12
|
-
}
|
|
13
4
|
/**
|
|
14
5
|
* Database wrapper with Task/Stream semantics.
|
|
15
6
|
*
|
|
@@ -70,4 +61,13 @@ export declare class DB {
|
|
|
70
61
|
withTransaction<R>(fn: (tx: DBTransaction) => Promisable<R>): Task<R, TransactionFailed | QueryFailed | ConstraintViolation>;
|
|
71
62
|
private transaction;
|
|
72
63
|
}
|
|
64
|
+
/** Database transaction with Task semantics. */
|
|
65
|
+
export declare class DBTransaction {
|
|
66
|
+
private readonly adapter;
|
|
67
|
+
constructor(adapter: DBTransactionAdapter);
|
|
68
|
+
query<T>(sql: string, params?: unknown[]): Task<T[], QueryFailed>;
|
|
69
|
+
execute(sql: string, params?: unknown[]): Task<number, QueryFailed>;
|
|
70
|
+
commit(): Task<void, TransactionFailed>;
|
|
71
|
+
rollback(): Task<void, TransactionFailed>;
|
|
72
|
+
}
|
|
73
73
|
//# sourceMappingURL=db.d.ts.map
|
package/esm/db/db.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"db.d.ts","sourceRoot":"","sources":["../../src/db/db.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAC;AACtE,OAAO,KAAK,EACV,SAAS,EACT,WAAW,EACX,oBAAoB,EACrB,MAAM,cAAc,CAAC;AACtB,OAAO,EACL,gBAAgB,EAChB,mBAAmB,EACnB,WAAW,EACX,iBAAiB,EAClB,MAAM,aAAa,CAAC;AAErB
|
|
1
|
+
{"version":3,"file":"db.d.ts","sourceRoot":"","sources":["../../src/db/db.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAC;AACtE,OAAO,KAAK,EACV,SAAS,EACT,WAAW,EACX,oBAAoB,EACrB,MAAM,cAAc,CAAC;AACtB,OAAO,EACL,gBAAgB,EAChB,mBAAmB,EACnB,WAAW,EACX,iBAAiB,EAClB,MAAM,aAAa,CAAC;AAErB;;;;;;;;;;;;;;GAcG;AACH,qB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AAE,MAAM,CAAC,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC,MAAM,EAAE,WAAW,CAAC;IAgBnE,MAAM,IAAI,IAAI,CAAC,IAAI,EAAE,iBAAiB,CAAC;IAYvC,QAAQ,IAAI,IAAI,CAAC,IAAI,EAAE,iBAAiB,CAAC;CAW1C"}
|
package/esm/db/db.js
CHANGED
|
@@ -1,62 +1,5 @@
|
|
|
1
1
|
import { Source, Task } from "../anabranch/index.js";
|
|
2
2
|
import { ConnectionFailed, ConstraintViolation, QueryFailed, TransactionFailed, } from "./errors.js";
|
|
3
|
-
/** Database transaction with Task semantics. */
|
|
4
|
-
export class DBTransaction {
|
|
5
|
-
constructor(adapter) {
|
|
6
|
-
Object.defineProperty(this, "adapter", {
|
|
7
|
-
enumerable: true,
|
|
8
|
-
configurable: true,
|
|
9
|
-
writable: true,
|
|
10
|
-
value: adapter
|
|
11
|
-
});
|
|
12
|
-
}
|
|
13
|
-
query(sql, params) {
|
|
14
|
-
return Task.of(async () => {
|
|
15
|
-
try {
|
|
16
|
-
return await this.adapter.query(sql, params);
|
|
17
|
-
}
|
|
18
|
-
catch (error) {
|
|
19
|
-
if (error instanceof Error && error.message.includes("constraint")) {
|
|
20
|
-
throw new ConstraintViolation(sql, error.message);
|
|
21
|
-
}
|
|
22
|
-
throw new QueryFailed(sql, error instanceof Error ? error.message : String(error));
|
|
23
|
-
}
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
execute(sql, params) {
|
|
27
|
-
return Task.of(async () => {
|
|
28
|
-
try {
|
|
29
|
-
return await this.adapter.execute(sql, params);
|
|
30
|
-
}
|
|
31
|
-
catch (error) {
|
|
32
|
-
if (error instanceof Error && error.message.includes("constraint")) {
|
|
33
|
-
throw new ConstraintViolation(sql, error.message);
|
|
34
|
-
}
|
|
35
|
-
throw new QueryFailed(sql, error instanceof Error ? error.message : String(error));
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
commit() {
|
|
40
|
-
return Task.of(async () => {
|
|
41
|
-
try {
|
|
42
|
-
await this.adapter.commit();
|
|
43
|
-
}
|
|
44
|
-
catch (error) {
|
|
45
|
-
throw new TransactionFailed(error instanceof Error ? error.message : String(error));
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
rollback() {
|
|
50
|
-
return Task.of(async () => {
|
|
51
|
-
try {
|
|
52
|
-
await this.adapter.rollback();
|
|
53
|
-
}
|
|
54
|
-
catch (error) {
|
|
55
|
-
throw new TransactionFailed(error instanceof Error ? error.message : String(error));
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
3
|
/**
|
|
61
4
|
* Database wrapper with Task/Stream semantics.
|
|
62
5
|
*
|
|
@@ -192,3 +135,60 @@ export class DB {
|
|
|
192
135
|
}).mapErr((error) => new TransactionFailed(error instanceof Error ? error.message : String(error)));
|
|
193
136
|
}
|
|
194
137
|
}
|
|
138
|
+
/** Database transaction with Task semantics. */
|
|
139
|
+
export class DBTransaction {
|
|
140
|
+
constructor(adapter) {
|
|
141
|
+
Object.defineProperty(this, "adapter", {
|
|
142
|
+
enumerable: true,
|
|
143
|
+
configurable: true,
|
|
144
|
+
writable: true,
|
|
145
|
+
value: adapter
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
query(sql, params) {
|
|
149
|
+
return Task.of(async () => {
|
|
150
|
+
try {
|
|
151
|
+
return await this.adapter.query(sql, params);
|
|
152
|
+
}
|
|
153
|
+
catch (error) {
|
|
154
|
+
if (error instanceof Error && error.message.includes("constraint")) {
|
|
155
|
+
throw new ConstraintViolation(sql, error.message);
|
|
156
|
+
}
|
|
157
|
+
throw new QueryFailed(sql, error instanceof Error ? error.message : String(error));
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
execute(sql, params) {
|
|
162
|
+
return Task.of(async () => {
|
|
163
|
+
try {
|
|
164
|
+
return await this.adapter.execute(sql, params);
|
|
165
|
+
}
|
|
166
|
+
catch (error) {
|
|
167
|
+
if (error instanceof Error && error.message.includes("constraint")) {
|
|
168
|
+
throw new ConstraintViolation(sql, error.message);
|
|
169
|
+
}
|
|
170
|
+
throw new QueryFailed(sql, error instanceof Error ? error.message : String(error));
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
commit() {
|
|
175
|
+
return Task.of(async () => {
|
|
176
|
+
try {
|
|
177
|
+
await this.adapter.commit();
|
|
178
|
+
}
|
|
179
|
+
catch (error) {
|
|
180
|
+
throw new TransactionFailed(error instanceof Error ? error.message : String(error));
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
rollback() {
|
|
185
|
+
return Task.of(async () => {
|
|
186
|
+
try {
|
|
187
|
+
await this.adapter.rollback();
|
|
188
|
+
}
|
|
189
|
+
catch (error) {
|
|
190
|
+
throw new TransactionFailed(error instanceof Error ? error.message : String(error));
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
}
|
|
@@ -1,17 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
export interface PostgresOptions {
|
|
3
|
-
host?: string;
|
|
4
|
-
port?: number;
|
|
5
|
-
user?: string;
|
|
6
|
-
password?: string;
|
|
7
|
-
database?: string;
|
|
8
|
-
connectionString?: string;
|
|
9
|
-
max?: number;
|
|
10
|
-
idleTimeoutMillis?: number;
|
|
11
|
-
connectionTimeoutMillis?: number;
|
|
12
|
-
}
|
|
13
|
-
export interface PostgresConnector extends DBConnector {
|
|
14
|
-
end(): Promise<void>;
|
|
15
|
-
}
|
|
16
|
-
export declare function createPostgres(options?: PostgresOptions): PostgresConnector;
|
|
1
|
+
export { createPostgres, type PostgresConnector, type PostgresOptions, } from "./postgres.js";
|
|
17
2
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/db-postgres/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/db-postgres/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,KAAK,iBAAiB,EACtB,KAAK,eAAe,GACrB,MAAM,eAAe,CAAC"}
|
package/esm/db-postgres/index.js
CHANGED
|
@@ -1,64 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import Cursor from "pg-cursor";
|
|
3
|
-
import process from "node:process";
|
|
4
|
-
const { Pool } = pg;
|
|
5
|
-
function toPoolConfig(options) {
|
|
6
|
-
if (options.connectionString) {
|
|
7
|
-
return {
|
|
8
|
-
connectionString: options.connectionString,
|
|
9
|
-
max: options.max,
|
|
10
|
-
idleTimeoutMillis: options.idleTimeoutMillis,
|
|
11
|
-
connectionTimeoutMillis: options.connectionTimeoutMillis,
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
return {
|
|
15
|
-
host: options.host ?? process.env.PGHOST ?? "localhost",
|
|
16
|
-
port: options.port ?? parseInt(process.env.PGPORT ?? "5432"),
|
|
17
|
-
user: options.user ?? process.env.PGUSER ?? "postgres",
|
|
18
|
-
password: options.password ?? process.env.PGPASSWORD ?? "",
|
|
19
|
-
database: options.database ?? process.env.PGDATABASE ?? "postgres",
|
|
20
|
-
max: options.max,
|
|
21
|
-
idleTimeoutMillis: options.idleTimeoutMillis,
|
|
22
|
-
connectionTimeoutMillis: options.connectionTimeoutMillis,
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
export function createPostgres(options = {}) {
|
|
26
|
-
const pool = new Pool(toPoolConfig(options));
|
|
27
|
-
return {
|
|
28
|
-
async connect(signal) {
|
|
29
|
-
const client = await pool.connect();
|
|
30
|
-
const onAbort = () => client.release(true);
|
|
31
|
-
signal?.addEventListener("abort", onAbort, { once: true });
|
|
32
|
-
return {
|
|
33
|
-
query: (sql, params) => client
|
|
34
|
-
.query(sql, params)
|
|
35
|
-
.then((r) => r.rows),
|
|
36
|
-
execute: (sql, params) => client
|
|
37
|
-
.query(sql, params)
|
|
38
|
-
.then((r) => Number(r.rowCount ?? 0)),
|
|
39
|
-
close: () => {
|
|
40
|
-
signal?.removeEventListener("abort", onAbort);
|
|
41
|
-
client.release();
|
|
42
|
-
return Promise.resolve();
|
|
43
|
-
},
|
|
44
|
-
stream: async function* (sql, params) {
|
|
45
|
-
const cursor = client.query(new Cursor(sql, params));
|
|
46
|
-
try {
|
|
47
|
-
while (true) {
|
|
48
|
-
const rows = await cursor.read(100);
|
|
49
|
-
if (rows.length === 0)
|
|
50
|
-
break;
|
|
51
|
-
yield* rows;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
finally {
|
|
55
|
-
await cursor.close();
|
|
56
|
-
}
|
|
57
|
-
},
|
|
58
|
-
};
|
|
59
|
-
},
|
|
60
|
-
end() {
|
|
61
|
-
return pool.end();
|
|
62
|
-
},
|
|
63
|
-
};
|
|
64
|
-
}
|
|
1
|
+
export { createPostgres, } from "./postgres.js";
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { DBAdapter } from "../db/index.js";
|
|
2
|
+
export declare function createPostgres(options?: PostgresOptions): PostgresConnector;
|
|
3
|
+
type PostgresOptions = {
|
|
4
|
+
host?: string;
|
|
5
|
+
port?: number;
|
|
6
|
+
user?: string;
|
|
7
|
+
password?: string;
|
|
8
|
+
database?: string;
|
|
9
|
+
connectionString?: string;
|
|
10
|
+
max?: number;
|
|
11
|
+
idleTimeoutMillis?: number;
|
|
12
|
+
connectionTimeoutMillis?: number;
|
|
13
|
+
};
|
|
14
|
+
export interface PostgresConnector {
|
|
15
|
+
connect(signal?: AbortSignal): Promise<DBAdapter>;
|
|
16
|
+
end(): Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
export type { PostgresOptions };
|
|
19
|
+
//# sourceMappingURL=postgres.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"postgres.d.ts","sourceRoot":"","sources":["../../src/db-postgres/postgres.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAKhD,wBAAgB,cAAc,CAC5B,OAAO,GAAE,eAAoB,GAC5B,iBAAiB,CA4CnB;AAED,KAAK,eAAe,GAAG;IACrB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,uBAAuB,CAAC,EAAE,MAAM,CAAC;CAClC,CAAC;AAuBF,MAAM,WAAW,iBAAiB;IAChC,OAAO,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;IAClD,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACtB;AAED,YAAY,EAAE,eAAe,EAAE,CAAC"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import pg from "pg";
|
|
2
|
+
import Cursor from "pg-cursor";
|
|
3
|
+
import process from "node:process";
|
|
4
|
+
const { Pool } = pg;
|
|
5
|
+
export function createPostgres(options = {}) {
|
|
6
|
+
const pool = new Pool(toPoolConfig(options));
|
|
7
|
+
return {
|
|
8
|
+
async connect(signal) {
|
|
9
|
+
const client = await pool.connect();
|
|
10
|
+
const onAbort = () => client.release(true);
|
|
11
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
12
|
+
return {
|
|
13
|
+
query: (sql, params) => client
|
|
14
|
+
.query(sql, params)
|
|
15
|
+
.then((r) => r.rows),
|
|
16
|
+
execute: (sql, params) => client
|
|
17
|
+
.query(sql, params)
|
|
18
|
+
.then((r) => Number(r.rowCount ?? 0)),
|
|
19
|
+
close: () => {
|
|
20
|
+
signal?.removeEventListener("abort", onAbort);
|
|
21
|
+
client.release();
|
|
22
|
+
return Promise.resolve();
|
|
23
|
+
},
|
|
24
|
+
stream: async function* (sql, params) {
|
|
25
|
+
const cursor = client.query(new Cursor(sql, params));
|
|
26
|
+
try {
|
|
27
|
+
while (true) {
|
|
28
|
+
const rows = await cursor.read(100);
|
|
29
|
+
if (rows.length === 0)
|
|
30
|
+
break;
|
|
31
|
+
yield* rows;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
finally {
|
|
35
|
+
await cursor.close();
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
},
|
|
40
|
+
end() {
|
|
41
|
+
return pool.end();
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function toPoolConfig(options) {
|
|
46
|
+
if (options.connectionString) {
|
|
47
|
+
return {
|
|
48
|
+
connectionString: options.connectionString,
|
|
49
|
+
max: options.max,
|
|
50
|
+
idleTimeoutMillis: options.idleTimeoutMillis,
|
|
51
|
+
connectionTimeoutMillis: options.connectionTimeoutMillis,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
host: options.host ?? process.env.PGHOST ?? "localhost",
|
|
56
|
+
port: options.port ?? parseInt(process.env.PGPORT ?? "5432"),
|
|
57
|
+
user: options.user ?? process.env.PGUSER ?? "postgres",
|
|
58
|
+
password: options.password ?? process.env.PGPASSWORD ?? "",
|
|
59
|
+
database: options.database ?? process.env.PGDATABASE ?? "postgres",
|
|
60
|
+
max: options.max,
|
|
61
|
+
idleTimeoutMillis: options.idleTimeoutMillis,
|
|
62
|
+
connectionTimeoutMillis: options.connectionTimeoutMillis,
|
|
63
|
+
};
|
|
64
|
+
}
|