@runlot/pg 0.1.0-rc.100
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 +60 -0
- package/dist/bundler.d.ts +33 -0
- package/dist/bundler.d.ts.map +1 -0
- package/dist/bundler.js +276 -0
- package/dist/bundler.js.map +1 -0
- package/dist/client.d.ts +138 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +505 -0
- package/dist/client.js.map +1 -0
- package/dist/emitter.d.ts +14 -0
- package/dist/emitter.d.ts.map +1 -0
- package/dist/emitter.js +63 -0
- package/dist/emitter.js.map +1 -0
- package/dist/errors.d.ts +34 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +46 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +94 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +27 -0
- package/dist/index.js.map +1 -0
- package/dist/orm/drizzle.d.ts +9 -0
- package/dist/orm/drizzle.d.ts.map +1 -0
- package/dist/orm/drizzle.js +13 -0
- package/dist/orm/drizzle.js.map +1 -0
- package/dist/orm/knex.d.ts +6 -0
- package/dist/orm/knex.d.ts.map +1 -0
- package/dist/orm/knex.js +28 -0
- package/dist/orm/knex.js.map +1 -0
- package/dist/orm/kysely.d.ts +5 -0
- package/dist/orm/kysely.d.ts.map +1 -0
- package/dist/orm/kysely.js +14 -0
- package/dist/orm/kysely.js.map +1 -0
- package/dist/orm/prisma.d.ts +49 -0
- package/dist/orm/prisma.d.ts.map +1 -0
- package/dist/orm/prisma.js +344 -0
- package/dist/orm/prisma.js.map +1 -0
- package/dist/orm/sequelize.d.ts +4 -0
- package/dist/orm/sequelize.d.ts.map +1 -0
- package/dist/orm/sequelize.js +24 -0
- package/dist/orm/sequelize.js.map +1 -0
- package/dist/orm/typeorm.d.ts +11 -0
- package/dist/orm/typeorm.d.ts.map +1 -0
- package/dist/orm/typeorm.js +22 -0
- package/dist/orm/typeorm.js.map +1 -0
- package/dist/type-parsers.d.ts +159 -0
- package/dist/type-parsers.d.ts.map +1 -0
- package/dist/type-parsers.js +329 -0
- package/dist/type-parsers.js.map +1 -0
- package/dist/types.d.ts +93 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +9 -0
- package/dist/types.js.map +1 -0
- package/package.json +83 -0
package/dist/client.js
ADDED
|
@@ -0,0 +1,505 @@
|
|
|
1
|
+
// `Client`, `Pool` and `PoolClient` in the node-postgres shape
|
|
2
|
+
// (docs/pg-driver-support.md §3.2).
|
|
3
|
+
//
|
|
4
|
+
// There is no connection. `Pool.query` goes to `env.db.tryExec` (a session per
|
|
5
|
+
// call), while `Client` and the `PoolClient` that `Pool.connect()` gives you
|
|
6
|
+
// hold one `env.db.session()` — `BEGIN…COMMIT`, prepared statements and `SET`
|
|
7
|
+
// are kept inside that. `release()` is `close()`.
|
|
8
|
+
import { Emitter } from "./emitter.js";
|
|
9
|
+
import { DatabaseError } from "./errors.js";
|
|
10
|
+
import { convertCell, types } from "./type-parsers.js";
|
|
11
|
+
// ---- The shape of a result ----
|
|
12
|
+
/** Moves a CommandComplete tag into pg's `command`, `rowCount` and `oid`. */
|
|
13
|
+
export function parseCommandTag(tag) {
|
|
14
|
+
if (!tag)
|
|
15
|
+
return { command: "", rowCount: null, oid: 0 };
|
|
16
|
+
const parts = tag.split(" ");
|
|
17
|
+
const command = parts[0] ?? "";
|
|
18
|
+
let rowCount = null;
|
|
19
|
+
let oid = 0;
|
|
20
|
+
switch (command) {
|
|
21
|
+
case "INSERT":
|
|
22
|
+
oid = Number(parts[1] ?? 0);
|
|
23
|
+
rowCount = Number(parts[2] ?? 0);
|
|
24
|
+
break;
|
|
25
|
+
case "DELETE":
|
|
26
|
+
case "UPDATE":
|
|
27
|
+
case "MERGE":
|
|
28
|
+
case "SELECT":
|
|
29
|
+
case "MOVE":
|
|
30
|
+
case "FETCH":
|
|
31
|
+
case "COPY":
|
|
32
|
+
rowCount = Number(parts[1] ?? 0);
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
if (rowCount !== null && Number.isNaN(rowCount))
|
|
36
|
+
rowCount = null;
|
|
37
|
+
return { command, rowCount, oid };
|
|
38
|
+
}
|
|
39
|
+
function shapeResult(r, cfg) {
|
|
40
|
+
const columns = r.columns ?? [];
|
|
41
|
+
const lookup = cfg.types?.getTypeParser ? cfg.types.getTypeParser.bind(cfg.types) : undefined;
|
|
42
|
+
const fields = columns.map((c) => ({
|
|
43
|
+
name: c.name,
|
|
44
|
+
tableID: 0,
|
|
45
|
+
columnID: 0,
|
|
46
|
+
dataTypeID: c.typeOid,
|
|
47
|
+
dataTypeSize: -1,
|
|
48
|
+
dataTypeModifier: -1,
|
|
49
|
+
format: "text",
|
|
50
|
+
}));
|
|
51
|
+
const values = r.values ?? [];
|
|
52
|
+
const converted = values.map((row) => row.map((cell, i) => convertCell(cell, columns[i]?.typeOid ?? 0, lookup)));
|
|
53
|
+
let rows;
|
|
54
|
+
if (cfg.rowMode === "array") {
|
|
55
|
+
rows = converted;
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
rows = converted.map((v) => {
|
|
59
|
+
const o = {};
|
|
60
|
+
for (let i = 0; i < columns.length; i++) {
|
|
61
|
+
const name = columns[i].name;
|
|
62
|
+
// When a column is named "__proto__", assignment calls the prototype
|
|
63
|
+
// setter (the same reason as in the facade). That one name is placed
|
|
64
|
+
// with defineProperty.
|
|
65
|
+
if (name === "__proto__") {
|
|
66
|
+
Object.defineProperty(o, name, { value: v[i], enumerable: true, writable: true, configurable: true });
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
o[name] = v[i];
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return o;
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
const tag = parseCommandTag(r.commandTag);
|
|
76
|
+
// An Execute in the extended protocol may have no tag. If there are
|
|
77
|
+
// columns, treat it as a SELECT.
|
|
78
|
+
if (tag.command === "" && columns.length > 0) {
|
|
79
|
+
tag.command = "SELECT";
|
|
80
|
+
tag.rowCount = rows.length;
|
|
81
|
+
}
|
|
82
|
+
return { command: tag.command, rowCount: tag.rowCount, oid: tag.oid, rows, fields };
|
|
83
|
+
}
|
|
84
|
+
function toConfig(a, b) {
|
|
85
|
+
const cfg = typeof a === "string" ? { text: a } : { ...a };
|
|
86
|
+
if (Array.isArray(b))
|
|
87
|
+
cfg.values = b;
|
|
88
|
+
return cfg;
|
|
89
|
+
}
|
|
90
|
+
function normalizeParams(values) {
|
|
91
|
+
if (!values)
|
|
92
|
+
return [];
|
|
93
|
+
return values.map((v) => {
|
|
94
|
+
if (v === undefined)
|
|
95
|
+
return null;
|
|
96
|
+
// pg serialises an array parameter as a PostgreSQL array literal. The
|
|
97
|
+
// facade sends an array as JSON, so we match pg's shape here.
|
|
98
|
+
if (Array.isArray(v))
|
|
99
|
+
return arrayLiteral(v);
|
|
100
|
+
if (v instanceof Uint8Array && !(v instanceof ArrayBuffer)) {
|
|
101
|
+
return v.buffer.slice(v.byteOffset, v.byteOffset + v.byteLength);
|
|
102
|
+
}
|
|
103
|
+
return v;
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
function arrayLiteral(arr) {
|
|
107
|
+
const items = arr.map((v) => {
|
|
108
|
+
if (v === null || v === undefined)
|
|
109
|
+
return "NULL";
|
|
110
|
+
if (Array.isArray(v))
|
|
111
|
+
return arrayLiteral(v);
|
|
112
|
+
let s;
|
|
113
|
+
if (v instanceof Date)
|
|
114
|
+
s = v.toISOString();
|
|
115
|
+
else if (typeof v === "object")
|
|
116
|
+
s = JSON.stringify(v);
|
|
117
|
+
else
|
|
118
|
+
s = String(v);
|
|
119
|
+
return '"' + s.replace(/\\/g, "\\\\").replace(/"/g, '\\"') + '"';
|
|
120
|
+
});
|
|
121
|
+
return "{" + items.join(",") + "}";
|
|
122
|
+
}
|
|
123
|
+
async function run(target, cfg) {
|
|
124
|
+
const r = await target.tryExec(cfg.text, normalizeParams(cfg.values));
|
|
125
|
+
if (!r.ok)
|
|
126
|
+
throw new DatabaseError(r.error);
|
|
127
|
+
return shapeResult(r, cfg);
|
|
128
|
+
}
|
|
129
|
+
function withCallback(p, cb) {
|
|
130
|
+
if (!cb)
|
|
131
|
+
return p;
|
|
132
|
+
p.then((res) => cb(null, res), (err) => cb(err));
|
|
133
|
+
return undefined;
|
|
134
|
+
}
|
|
135
|
+
function pickCallback(args) {
|
|
136
|
+
for (const a of args)
|
|
137
|
+
if (typeof a === "function")
|
|
138
|
+
return a;
|
|
139
|
+
return undefined;
|
|
140
|
+
}
|
|
141
|
+
// Read off Symbol rather than written as `Symbol.dispose`: this package compiles
|
|
142
|
+
// against lib ES2022, which does not declare it, and a runtime without it simply
|
|
143
|
+
// has no disposable handles.
|
|
144
|
+
const DISPOSE = Symbol.dispose;
|
|
145
|
+
/**
|
|
146
|
+
* Also dispose the handle, when the facade gives us a disposable one.
|
|
147
|
+
*
|
|
148
|
+
* `close()` alone was not enough in production: on `js/eatkk` the actor's
|
|
149
|
+
* `/owner/counts` read `sessionHandles {opened:3, closed:3, reaped:3, open:0}`,
|
|
150
|
+
* and `reaped ⊆ closed` — every handle the app opened came back by the 30 s TTL
|
|
151
|
+
* and none by a client close, because the close was still in flight when the
|
|
152
|
+
* request's context ended and was cancelled with it. The facade's
|
|
153
|
+
* `[Symbol.dispose]` closes through `waitUntil`, which outlives the request, so
|
|
154
|
+
* a handle that has one gets it too. It is synchronous and best-effort: a throw
|
|
155
|
+
* here must not become the caller's error, since the session is gone either way.
|
|
156
|
+
*/
|
|
157
|
+
function disposeSession(s) {
|
|
158
|
+
if (DISPOSE === undefined)
|
|
159
|
+
return;
|
|
160
|
+
const d = s[DISPOSE];
|
|
161
|
+
if (typeof d !== "function")
|
|
162
|
+
return;
|
|
163
|
+
try {
|
|
164
|
+
d.call(s);
|
|
165
|
+
}
|
|
166
|
+
catch {
|
|
167
|
+
// Nowhere to report it. The actor reclaims the session by TTL (§3.1).
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
// ---- Client ----
|
|
171
|
+
/**
|
|
172
|
+
* A client that holds one session. `connect()` opens the session and `end()`
|
|
173
|
+
* closes it. A `query` before `connect()` opens the session on its own — pg
|
|
174
|
+
* throws there, but opening is easier for an ORM to deal with and loses
|
|
175
|
+
* nothing.
|
|
176
|
+
*/
|
|
177
|
+
export class Client extends Emitter {
|
|
178
|
+
db;
|
|
179
|
+
/** Sequelize hooks `client.connection.on('parameterStatus')`. */
|
|
180
|
+
connection = new Emitter();
|
|
181
|
+
processID = null;
|
|
182
|
+
secretKey = null;
|
|
183
|
+
_ending = false;
|
|
184
|
+
_connected = false;
|
|
185
|
+
_connecting = false;
|
|
186
|
+
#session = null;
|
|
187
|
+
#opening = null;
|
|
188
|
+
constructor(config) {
|
|
189
|
+
super();
|
|
190
|
+
const db = config.db ?? boundDb(config);
|
|
191
|
+
if (!db)
|
|
192
|
+
throw new Error("@runlot/pg: `db` is required. Create a client with new Client({ db: env.db }).");
|
|
193
|
+
this.db = db;
|
|
194
|
+
}
|
|
195
|
+
get session() {
|
|
196
|
+
return this.#session;
|
|
197
|
+
}
|
|
198
|
+
connect(cb) {
|
|
199
|
+
const p = this.#open().then(() => undefined);
|
|
200
|
+
if (!cb)
|
|
201
|
+
return p;
|
|
202
|
+
p.then(() => cb(null), (e) => cb(e));
|
|
203
|
+
}
|
|
204
|
+
#open() {
|
|
205
|
+
if (this.#session)
|
|
206
|
+
return Promise.resolve(this.#session);
|
|
207
|
+
if (this.#opening)
|
|
208
|
+
return this.#opening;
|
|
209
|
+
this._connecting = true;
|
|
210
|
+
this.#opening = this.db.session().then((s) => {
|
|
211
|
+
this.#session = s;
|
|
212
|
+
this.#opening = null;
|
|
213
|
+
this._connecting = false;
|
|
214
|
+
this._connected = true;
|
|
215
|
+
this.emit("connect");
|
|
216
|
+
return s;
|
|
217
|
+
}, (e) => {
|
|
218
|
+
this.#opening = null;
|
|
219
|
+
this._connecting = false;
|
|
220
|
+
throw e;
|
|
221
|
+
});
|
|
222
|
+
return this.#opening;
|
|
223
|
+
}
|
|
224
|
+
query(a, b, c) {
|
|
225
|
+
const cb = pickCallback([b, c]);
|
|
226
|
+
const cfg = toConfig(a, b);
|
|
227
|
+
const p = this.#open().then((s) => run(s, cfg));
|
|
228
|
+
return withCallback(p, cb);
|
|
229
|
+
}
|
|
230
|
+
end(cb) {
|
|
231
|
+
this._ending = true;
|
|
232
|
+
const p = this.#close();
|
|
233
|
+
if (!cb)
|
|
234
|
+
return p;
|
|
235
|
+
p.then(() => cb(), (e) => cb(e));
|
|
236
|
+
}
|
|
237
|
+
async #close() {
|
|
238
|
+
const pending = this.#opening;
|
|
239
|
+
if (pending)
|
|
240
|
+
await pending.catch(() => undefined);
|
|
241
|
+
const s = this.#session;
|
|
242
|
+
this.#session = null;
|
|
243
|
+
this._connected = false;
|
|
244
|
+
if (s) {
|
|
245
|
+
try {
|
|
246
|
+
await s.close();
|
|
247
|
+
}
|
|
248
|
+
finally {
|
|
249
|
+
disposeSession(s);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
this.emit("end");
|
|
253
|
+
}
|
|
254
|
+
escapeIdentifier(s) {
|
|
255
|
+
return '"' + s.replace(/"/g, '""') + '"';
|
|
256
|
+
}
|
|
257
|
+
escapeLiteral(s) {
|
|
258
|
+
let hasBackslash = false;
|
|
259
|
+
let out = "'";
|
|
260
|
+
for (const ch of s) {
|
|
261
|
+
if (ch === "'")
|
|
262
|
+
out += "''";
|
|
263
|
+
else if (ch === "\\") {
|
|
264
|
+
out += "\\\\";
|
|
265
|
+
hasBackslash = true;
|
|
266
|
+
}
|
|
267
|
+
else
|
|
268
|
+
out += ch;
|
|
269
|
+
}
|
|
270
|
+
out += "'";
|
|
271
|
+
return hasBackslash ? " E" + out : out;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
// ---- Pool ----
|
|
275
|
+
export class PoolClient extends Client {
|
|
276
|
+
#pool;
|
|
277
|
+
#released = false;
|
|
278
|
+
constructor(pool, db) {
|
|
279
|
+
super({ db });
|
|
280
|
+
this.#pool = pool;
|
|
281
|
+
}
|
|
282
|
+
/** pg's `release(err?)`. Passing an error throws the session away — we
|
|
283
|
+
* always throw it away, so it is the same. */
|
|
284
|
+
release(_err) {
|
|
285
|
+
if (this.#released)
|
|
286
|
+
return;
|
|
287
|
+
this.#released = true;
|
|
288
|
+
this.#pool._forget(this);
|
|
289
|
+
// `release()` is synchronous in pg's API, so the close cannot be awaited
|
|
290
|
+
// here. It used to be fire-and-forget, which let the next `connect()`
|
|
291
|
+
// overtake it and ask the actor for a session while the released one was
|
|
292
|
+
// still counted — the engine's session cap refuses that. The promise goes
|
|
293
|
+
// to the pool instead: `connect()` and `end()` wait for outstanding closes
|
|
294
|
+
// first. There is still nowhere to report a close failure; the actor
|
|
295
|
+
// reclaims the session by TTL (§3.1).
|
|
296
|
+
this.#pool._closing(this.end());
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
/** The bound on how long a pool waits for `release()`'s closes. */
|
|
300
|
+
export const DEFAULT_CLOSE_WAIT_MS = 5_000;
|
|
301
|
+
export class Pool extends Emitter {
|
|
302
|
+
db;
|
|
303
|
+
options;
|
|
304
|
+
#clients = new Set();
|
|
305
|
+
#closing = new Set();
|
|
306
|
+
#ended = false;
|
|
307
|
+
constructor(config) {
|
|
308
|
+
super();
|
|
309
|
+
const db = config.db ?? boundDb(config);
|
|
310
|
+
if (!db)
|
|
311
|
+
throw new Error("@runlot/pg: `db` is required. Create a pool with new Pool({ db: env.db }).");
|
|
312
|
+
this.db = db;
|
|
313
|
+
this.options = { ...config };
|
|
314
|
+
}
|
|
315
|
+
get totalCount() {
|
|
316
|
+
return this.#clients.size;
|
|
317
|
+
}
|
|
318
|
+
get idleCount() {
|
|
319
|
+
return 0;
|
|
320
|
+
}
|
|
321
|
+
get waitingCount() {
|
|
322
|
+
return 0;
|
|
323
|
+
}
|
|
324
|
+
get ended() {
|
|
325
|
+
return this.#ended;
|
|
326
|
+
}
|
|
327
|
+
connect(cb) {
|
|
328
|
+
const p = this.#acquire();
|
|
329
|
+
if (!cb)
|
|
330
|
+
return p;
|
|
331
|
+
p.then((c) => cb(null, c, (err) => c.release(err)), (e) => cb(e));
|
|
332
|
+
}
|
|
333
|
+
async #acquire() {
|
|
334
|
+
if (this.#ended)
|
|
335
|
+
throw new Error("Cannot use a pool after calling end on the pool");
|
|
336
|
+
// Before asking for a new session, let the sessions that `release()` is
|
|
337
|
+
// closing finish. Otherwise the two cross at the actor and the engine's
|
|
338
|
+
// session cap refuses the new one.
|
|
339
|
+
await this.#settleClosing();
|
|
340
|
+
if (this.#ended)
|
|
341
|
+
throw new Error("Cannot use a pool after calling end on the pool");
|
|
342
|
+
const c = new PoolClient(this, this.db);
|
|
343
|
+
// The session is opened here. In pg too, once connect() returns the
|
|
344
|
+
// connection is alive.
|
|
345
|
+
await c.connect();
|
|
346
|
+
this.#clients.add(c);
|
|
347
|
+
this.emit("connect", c);
|
|
348
|
+
return c;
|
|
349
|
+
}
|
|
350
|
+
/** Called by `PoolClient.release()`, which cannot await its own close. */
|
|
351
|
+
_closing(close) {
|
|
352
|
+
const tracked = close.catch(() => undefined).then(() => undefined);
|
|
353
|
+
this.#closing.add(tracked);
|
|
354
|
+
void tracked.then(() => this.#closing.delete(tracked));
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Waits for the closes outstanding **now**, for at most `closeWaitMs`.
|
|
358
|
+
*
|
|
359
|
+
* One round only: a close that starts during the wait is not waited for, so
|
|
360
|
+
* two clients releasing in a loop cannot keep a caller here for ever.
|
|
361
|
+
*/
|
|
362
|
+
async #settleClosing() {
|
|
363
|
+
if (this.#closing.size === 0)
|
|
364
|
+
return;
|
|
365
|
+
const outstanding = Promise.all([...this.#closing]).then(() => undefined);
|
|
366
|
+
const raw = this.options.closeWaitMs;
|
|
367
|
+
const bound = typeof raw === "number" && Number.isFinite(raw) && raw >= 0 ? raw : DEFAULT_CLOSE_WAIT_MS;
|
|
368
|
+
let timer;
|
|
369
|
+
const expiry = new Promise((resolve) => {
|
|
370
|
+
timer = setTimeout(resolve, bound);
|
|
371
|
+
});
|
|
372
|
+
try {
|
|
373
|
+
await Promise.race([outstanding, expiry]);
|
|
374
|
+
}
|
|
375
|
+
finally {
|
|
376
|
+
if (timer !== undefined)
|
|
377
|
+
clearTimeout(timer);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
_forget(c) {
|
|
381
|
+
this.#clients.delete(c);
|
|
382
|
+
this.emit("remove", c);
|
|
383
|
+
}
|
|
384
|
+
query(a, b, c) {
|
|
385
|
+
const cb = pickCallback([b, c]);
|
|
386
|
+
const cfg = toConfig(a, b);
|
|
387
|
+
const p = (this.#ended
|
|
388
|
+
? Promise.reject(new Error("Cannot use a pool after calling end on the pool"))
|
|
389
|
+
: run(this.db, cfg));
|
|
390
|
+
return withCallback(p, cb);
|
|
391
|
+
}
|
|
392
|
+
end(cb) {
|
|
393
|
+
this.#ended = true;
|
|
394
|
+
const all = [...this.#clients];
|
|
395
|
+
this.#clients.clear();
|
|
396
|
+
// The closes `release()` started count as work this pool still owes, so
|
|
397
|
+
// end() is not done until they are — bounded the same way.
|
|
398
|
+
const p = Promise.all(all.map((c) => c.end().catch(() => undefined)))
|
|
399
|
+
.then(() => this.#settleClosing())
|
|
400
|
+
.then(() => undefined);
|
|
401
|
+
if (!cb)
|
|
402
|
+
return p;
|
|
403
|
+
p.then(() => cb(), (e) => cb(e));
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
// ---- Constructor names are part of the driver contract ----
|
|
407
|
+
//
|
|
408
|
+
// An ORM may decide what it was handed by reading the **class name**. Drizzle's
|
|
409
|
+
// node-postgres session does exactly that before it opens a transaction:
|
|
410
|
+
//
|
|
411
|
+
// const isPool = this.client instanceof Pool
|
|
412
|
+
// || Object.getPrototypeOf(this.client).constructor.name.includes("Pool");
|
|
413
|
+
//
|
|
414
|
+
// and when that is false it sends `BEGIN` through `Pool.query()` — a session per
|
|
415
|
+
// call, so the BEGIN is rolled back the moment it lands and every statement
|
|
416
|
+
// after it runs outside any transaction, with no error anywhere. A silently
|
|
417
|
+
// unwrapped transaction is the worst shape a defect can take.
|
|
418
|
+
//
|
|
419
|
+
// Two reasons the name is the only signal that reaches Drizzle:
|
|
420
|
+
//
|
|
421
|
+
// - `instanceof Pool` cannot hold. `Pool` there is the `pg` module Drizzle
|
|
422
|
+
// itself imported. `bundler.ts` aliases `pg` → `@runlot/pg` for the workers
|
|
423
|
+
// it builds, but on the `framework: next` path OpenNext bundles the app
|
|
424
|
+
// first (`web/packages/next/src/build.ts`), and our alias never sees that
|
|
425
|
+
// pass: inside the OpenNext bundle `pg` resolves to whatever the project
|
|
426
|
+
// has. Even when it resolves back to this package, it is a second copy at a
|
|
427
|
+
// second path, so its `Pool` is a different class object than the one that
|
|
428
|
+
// built the pool. `instanceof` is false either way.
|
|
429
|
+
// - OpenNext builds with `minify: true`, and `Function.prototype.name` follows
|
|
430
|
+
// the minified binding. `class Pool` becomes `class e`, and
|
|
431
|
+
// `"e".includes("Pool")` is false.
|
|
432
|
+
//
|
|
433
|
+
// So the names are pinned here rather than asked of a bundler flag. A
|
|
434
|
+
// `keepNames` setting would only cover the bundles we run ourselves; these
|
|
435
|
+
// classes travel inside the user's build, through minifiers we never configure.
|
|
436
|
+
function pinName(cls, name) {
|
|
437
|
+
Object.defineProperty(cls, "name", { value: name, configurable: true });
|
|
438
|
+
}
|
|
439
|
+
pinName(Client, "Client");
|
|
440
|
+
pinName(Pool, "Pool");
|
|
441
|
+
// **"Client", not "PoolClient".** In pg, `pool.connect()` answers with a
|
|
442
|
+
// `Client` that has had `release` attached, so "PoolClient" is a name no ORM
|
|
443
|
+
// sniffing pg has ever seen. It also contains "Pool", which would make
|
|
444
|
+
// Drizzle's check above read a borrowed client as a pool: it would then call
|
|
445
|
+
// `connect()` on it (answering `undefined`) and `release()` on the result.
|
|
446
|
+
pinName(PoolClient, "Client");
|
|
447
|
+
// ---- Binding the module ----
|
|
448
|
+
const BOUND = Symbol.for("@runlot/pg.bound");
|
|
449
|
+
function boundDb(config) {
|
|
450
|
+
if (config && typeof config === "object" && typeof config.tryExec === "function")
|
|
451
|
+
return config;
|
|
452
|
+
const b = globalThis[BOUND];
|
|
453
|
+
return b;
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* A pg-shaped module with `db` bound in advance.
|
|
457
|
+
*
|
|
458
|
+
* It is handed to the places where an ORM takes a whole driver module
|
|
459
|
+
* (TypeORM's `driver`, Sequelize's `dialectModule`, Knex's `_driver()`). Those
|
|
460
|
+
* ORMs call `new Pool(options)` with their own options, so there is no way to
|
|
461
|
+
* put `db` into the options — it is bound here.
|
|
462
|
+
*/
|
|
463
|
+
export function pgModule(db) {
|
|
464
|
+
// Because an ORM passes its own connection options straight through, a key
|
|
465
|
+
// called `db` can arrive by accident (Knex's `connection` becomes the Client
|
|
466
|
+
// options wholesale). We use it only when it has the shape of a Db.
|
|
467
|
+
const pick = (c) => (isDb(c?.db) ? c.db : db);
|
|
468
|
+
class BoundClient extends Client {
|
|
469
|
+
constructor(config) {
|
|
470
|
+
super({ ...(config ?? {}), db: pick(config) });
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
class BoundPool extends Pool {
|
|
474
|
+
constructor(config) {
|
|
475
|
+
super({ ...(config ?? {}), db: pick(config) });
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
// Same contract as the base classes above, and the same reason: these are the
|
|
479
|
+
// classes TypeORM, Sequelize and Knex construct, so they are the ones an ORM
|
|
480
|
+
// reads a name off. "BoundPool" keeps "Pool" in it and keeps the stack
|
|
481
|
+
// readable.
|
|
482
|
+
pinName(BoundClient, "Client");
|
|
483
|
+
pinName(BoundPool, "BoundPool");
|
|
484
|
+
return {
|
|
485
|
+
Client: BoundClient,
|
|
486
|
+
Pool: BoundPool,
|
|
487
|
+
DatabaseError,
|
|
488
|
+
defaults,
|
|
489
|
+
types,
|
|
490
|
+
native: null,
|
|
491
|
+
escapeIdentifier: Client.prototype.escapeIdentifier,
|
|
492
|
+
escapeLiteral: Client.prototype.escapeLiteral,
|
|
493
|
+
};
|
|
494
|
+
}
|
|
495
|
+
function isDb(v) {
|
|
496
|
+
return !!v && typeof v === "object" && typeof v.tryExec === "function";
|
|
497
|
+
}
|
|
498
|
+
/** pg's `defaults`. TypeORM touches `parseInt8` — we always keep int8 as a
|
|
499
|
+
* string. */
|
|
500
|
+
export const defaults = {
|
|
501
|
+
parseInt8: false,
|
|
502
|
+
ssl: false,
|
|
503
|
+
poolSize: 10,
|
|
504
|
+
};
|
|
505
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,+DAA+D;AAC/D,oCAAoC;AACpC,EAAE;AACF,+EAA+E;AAC/E,6EAA6E;AAC7E,8EAA8E;AAC9E,kDAAkD;AAClD,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,WAAW,EAAE,KAAK,EAAmB,MAAM,mBAAmB,CAAC;AAsCxE,kCAAkC;AAElC,6EAA6E;AAC7E,MAAM,UAAU,eAAe,CAAC,GAAkB;IAChD,IAAI,CAAC,GAAG;QAAE,OAAO,EAAE,OAAO,EAAE,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC,EAAE,CAAC;IACzD,MAAM,KAAK,GAAG,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAC7B,MAAM,OAAO,GAAG,KAAK,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;IAC/B,IAAI,QAAQ,GAAkB,IAAI,CAAC;IACnC,IAAI,GAAG,GAAG,CAAC,CAAC;IACZ,QAAQ,OAAO,EAAE,CAAC;QAChB,KAAK,QAAQ;YACX,GAAG,GAAG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AM,SAAU,SAAQ,IAAI;QAC1B,YAAY,MAAmB;YAC7B,KAAK,CAAC,EAAE,GAAG,CAAC,MAAM,IAAI,EAAE,CAAC,EAAE,EAAE,EAAE,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;QACjD,CAAC;KACF;IACD,8EAA8E;IAC9E,6EAA6E;IAC7E,uEAAuE;IACvE,YAAY;IACZ,OAAO,CAAC,WAAW,EAAE,QAAQ,CAAC,CAAC;IAC/B,OAAO,CAAC,SAAS,EAAE,WAAW,CAAC,CAAC;IAChC,OAAO;QACL,MAAM,EAAE,WAAuC;QAC/C,IAAI,EAAE,SAAmC;QACzC,aAAa;QACb,QAAQ;QACR,KAAK;QACL,MAAM,EAAE,IAAI;QACZ,gBAAgB,EAAE,MAAM,CAAC,SAAS,CAAC,gBAAgB;QACnD,aAAa,EAAE,MAAM,CAAC,SAAS,CAAC,aAAa;KAC9C,CAAC;AACJ,CAAC;AAED,SAAS,IAAI,CAAC,CAAU;IACtB,OAAO,CAAC,CAAC,CAAC,IAAI,OAAO,CAAC,KAAK,QAAQ,IAAI,OAAQ,CAAQ,CAAC,OAAO,KAAK,UAAU,CAAC;AACjF,CAAC;AAcD;cACc;AACd,MAAM,CAAC,MAAM,QAAQ,GAA4B;IAC/C,SAAS,EAAE,KAAK;IAChB,GAAG,EAAE,KAAK;IACV,QAAQ,EAAE,EAAE;CACb,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
type Listener = (...args: unknown[]) => void;
|
|
2
|
+
export declare class Emitter {
|
|
3
|
+
#private;
|
|
4
|
+
on(event: string, fn: Listener): this;
|
|
5
|
+
addListener(event: string, fn: Listener): this;
|
|
6
|
+
once(event: string, fn: Listener): this;
|
|
7
|
+
off(event: string, fn: Listener): this;
|
|
8
|
+
removeListener(event: string, fn: Listener): this;
|
|
9
|
+
removeAllListeners(event?: string): this;
|
|
10
|
+
listenerCount(event: string): number;
|
|
11
|
+
emit(event: string, ...args: unknown[]): boolean;
|
|
12
|
+
}
|
|
13
|
+
export {};
|
|
14
|
+
//# sourceMappingURL=emitter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"emitter.d.ts","sourceRoot":"","sources":["../src/emitter.ts"],"names":[],"mappings":"AAQA,KAAK,QAAQ,GAAG,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,IAAI,CAAC;AAE7C,qBAAa,OAAO;;IAGlB,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,QAAQ,GAAG,IAAI;IAKrC,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,QAAQ,GAAG,IAAI;IAI9C,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,QAAQ,GAAG,IAAI;IAKvC,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,QAAQ,GAAG,IAAI;IAQtC,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,QAAQ,GAAG,IAAI;IAIjD,kBAAkB,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI;IAMxC,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IAIpC,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,OAAO;CAkBjD"}
|
package/dist/emitter.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// A minimal emitter used without node:events.
|
|
2
|
+
//
|
|
3
|
+
// A user worker comes up without `nodejs_compat` (config.go — `experimental`
|
|
4
|
+
// alone). What an ORM hooks onto a Client or Pool is about
|
|
5
|
+
// `on('error')`, `once('end')` and `removeListener`, so that is all it needs.
|
|
6
|
+
// Pulling in the whole EventEmitter would, at that moment, make this package
|
|
7
|
+
// require the node runtime.
|
|
8
|
+
export class Emitter {
|
|
9
|
+
#listeners = new Map();
|
|
10
|
+
on(event, fn) {
|
|
11
|
+
this.#add(event, fn, false);
|
|
12
|
+
return this;
|
|
13
|
+
}
|
|
14
|
+
addListener(event, fn) {
|
|
15
|
+
return this.on(event, fn);
|
|
16
|
+
}
|
|
17
|
+
once(event, fn) {
|
|
18
|
+
this.#add(event, fn, true);
|
|
19
|
+
return this;
|
|
20
|
+
}
|
|
21
|
+
off(event, fn) {
|
|
22
|
+
const list = this.#listeners.get(event);
|
|
23
|
+
if (!list)
|
|
24
|
+
return this;
|
|
25
|
+
const i = list.findIndex((l) => l.fn === fn);
|
|
26
|
+
if (i >= 0)
|
|
27
|
+
list.splice(i, 1);
|
|
28
|
+
return this;
|
|
29
|
+
}
|
|
30
|
+
removeListener(event, fn) {
|
|
31
|
+
return this.off(event, fn);
|
|
32
|
+
}
|
|
33
|
+
removeAllListeners(event) {
|
|
34
|
+
if (event === undefined)
|
|
35
|
+
this.#listeners.clear();
|
|
36
|
+
else
|
|
37
|
+
this.#listeners.delete(event);
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
listenerCount(event) {
|
|
41
|
+
return this.#listeners.get(event)?.length ?? 0;
|
|
42
|
+
}
|
|
43
|
+
emit(event, ...args) {
|
|
44
|
+
const list = this.#listeners.get(event);
|
|
45
|
+
if (!list || list.length === 0)
|
|
46
|
+
return false;
|
|
47
|
+
for (const l of [...list]) {
|
|
48
|
+
if (l.once)
|
|
49
|
+
this.off(event, l.fn);
|
|
50
|
+
l.fn(...args);
|
|
51
|
+
}
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
#add(event, fn, once) {
|
|
55
|
+
let list = this.#listeners.get(event);
|
|
56
|
+
if (!list) {
|
|
57
|
+
list = [];
|
|
58
|
+
this.#listeners.set(event, list);
|
|
59
|
+
}
|
|
60
|
+
list.push({ fn, once });
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=emitter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"emitter.js","sourceRoot":"","sources":["../src/emitter.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E out of `err.code` (Prisma's 23505 → unique violation
|
|
6
|
+
* mapping, Knex's retry decision). The Error that `env.db.exec` throws loses
|
|
7
|
+
* its custom fields at the RPC boundary, but this adapter is JS inside the
|
|
8
|
+
* worker and can make its own Error — so it takes the value from `tryExec` and
|
|
9
|
+
* throws here.
|
|
10
|
+
*/
|
|
11
|
+
export declare class DatabaseError extends Error {
|
|
12
|
+
readonly code: string;
|
|
13
|
+
readonly severity: string;
|
|
14
|
+
readonly detail: string | undefined;
|
|
15
|
+
readonly hint: string | undefined;
|
|
16
|
+
readonly position: string | undefined;
|
|
17
|
+
/** pg always keeps this field. We have no value for it, so it stays
|
|
18
|
+
* undefined. */
|
|
19
|
+
readonly schema: string | undefined;
|
|
20
|
+
readonly table: string | undefined;
|
|
21
|
+
readonly column: string | undefined;
|
|
22
|
+
readonly constraint: string | undefined;
|
|
23
|
+
readonly dataType: string | undefined;
|
|
24
|
+
readonly file: string | undefined;
|
|
25
|
+
readonly line: string | undefined;
|
|
26
|
+
readonly routine: string | undefined;
|
|
27
|
+
readonly internalPosition: string | undefined;
|
|
28
|
+
readonly internalQuery: string | undefined;
|
|
29
|
+
readonly where: string | undefined;
|
|
30
|
+
readonly length: number;
|
|
31
|
+
constructor(e: DbErrorData);
|
|
32
|
+
}
|
|
33
|
+
export declare function isDatabaseError(e: unknown): e is DatabaseError;
|
|
34
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE9C;;;;;;;;GAQG;AACH,qBAAa,aAAc,SAAQ,KAAK;IACtC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;IACpC,Q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