@vibeorm/adapter-bun 6.2.3 → 6.2.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 +1 -1
- package/dist/index.js +114 -31
- package/dist/index.js.map +3 -3
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -131,7 +131,7 @@ What adapter-bun cannot match: `BEGIN` ahead of a write (one round trip more tha
|
|
|
131
131
|
|
|
132
132
|
Nested handles share the root budget and expire when their own callback finishes. Deadline-bearing statements are serialized on the connection, so each checks the remaining time after earlier work completes. Server-cancellation mode installs a fresh `statement_timeout` before each adapter statement and before COMMIT. This does not cancel JavaScript callbacks, network waits or pool acquisition. Trusted raw SQL can change server settings, and a multi-command script applies PostgreSQL's timeout separately to its commands; it is not a hard wall-clock limit for the whole script. Use individual adapter calls when the total deadline must be checked between commands.
|
|
133
133
|
|
|
134
|
-
If native savepoint recovery cannot be confirmed, the whole transaction is poisoned. Catching that error cannot permit further statements or a successful root commit. A lost COMMIT reply throws `VIBE_TRANSACTION` with `meta.outcome: "unknown"`. So does a COMMIT on a connection that was already dead before it (for example a backend terminated while the callback was still running): bun:sql gives no signal that tells "refused before sending" from "lost in flight", so this adapter cannot report that case as not committed, where adapter-pg can. When the callback fails and the cleanup ROLLBACK fails too (for example the backend was terminated), COMMIT was never sent: the callback's own error is thrown, like node-postgres, the transaction is not committed, and `transactionOutcomeOf({ error })` from `@vibeorm/runtime` reports `{ commit: "not-committed", commitSent: false, rollback: "failed", connection: "discarded" }`; a later use of the handle reports `meta.outcome: "not-committed"`. A deadline that refuses COMMIT stays the deadline error in that case too. No query or transaction callback is replayed. A reserved connection whose root rollback fails is closed rather than returned to the pool. A COMMIT the server answers with an error (a deferred unique or foreign-key constraint, a serialization failure) is a definite rollback and throws that error's own code, for example `VIBE_UNIQUE_VIOLATION`, exactly like node-postgres.
|
|
134
|
+
If native savepoint recovery cannot be confirmed, the whole transaction is poisoned. Catching that error cannot permit further statements or a successful root commit. A lost COMMIT reply throws `VIBE_TRANSACTION` with `meta.outcome: "unknown"`. So does a COMMIT on a connection that was already dead before it (for example a backend terminated while the callback was still running): bun:sql gives no signal that tells "refused before sending" from "lost in flight", so this adapter cannot report that case as not committed, where adapter-pg can. When the callback fails and the cleanup ROLLBACK fails too (for example the backend was terminated), COMMIT was never sent: the callback's own error is thrown, like node-postgres, the transaction is not committed, and `transactionOutcomeOf({ error })` from `@vibeorm/runtime` reports `{ commit: "not-committed", commitSent: false, rollback: "failed", connection: "discarded" }`; a later use of the handle reports `meta.outcome: "not-committed"`. A deadline that refuses COMMIT stays the deadline error in that case too. No query or transaction callback is replayed. A reserved connection whose root rollback fails is closed rather than returned to the pool. So is the connection of a transaction or a raw `BEGIN` session on which a statement failed with SQLSTATE class 08, 53, 57, 58 or XX (a `statement_timeout` included), whatever the transaction's outcome: such a backend can stay broken for every later statement (pg_search 0.25, paradedb/paradedb#5810), and `transactionOutcomeOf` reports `connection: "discarded"`. A statement outside a transaction runs on bun:sql's own pool, which never says which connection ran it. When such a statement fails with one of those classes, the adapter retires its bun:sql instance. Later work runs on a fresh instance, and the old one closes once the work already running on it has settled. So the next caller never inherits that backend, at the cost of reconnecting the pool after each such failure. A COMMIT the server answers with an error (a deferred unique or foreign-key constraint, a serialization failure) is a definite rollback and throws that error's own code, for example `VIBE_UNIQUE_VIOLATION`, exactly like node-postgres.
|
|
135
135
|
|
|
136
136
|
A statement with more than 65,535 parameters is refused by bun:sql before it reaches the server; node-postgres sends it and the server aborts the transaction. Inside a transaction the adapter treats that refusal as an abort too, so a callback that catches it cannot commit the transaction's other writes.
|
|
137
137
|
|
package/dist/index.js
CHANGED
|
@@ -715,6 +715,8 @@ function bunAdapter(options) {
|
|
|
715
715
|
let wireSettled = false;
|
|
716
716
|
let wireProbe = null;
|
|
717
717
|
let instance = null;
|
|
718
|
+
const retiredInstances = new Set;
|
|
719
|
+
const closingInstances = new Set;
|
|
718
720
|
const controlTemplates = createTemplateCache({ max: 64 });
|
|
719
721
|
function controlQuery(params) {
|
|
720
722
|
return params.connection(controlTemplates.getTemplate({ text: params.text }).template, ...params.values ?? []);
|
|
@@ -727,7 +729,7 @@ function bunAdapter(options) {
|
|
|
727
729
|
return connection(prepared.template, ...bindTemplateValues({ text: step.text, indices: prepared.indices, values: step.values }));
|
|
728
730
|
}
|
|
729
731
|
function createReservedControl(params) {
|
|
730
|
-
const { reserved, budget, scope } = params;
|
|
732
|
+
const { reserved, budget, scope, observeFailure } = params;
|
|
731
733
|
const deferred = createDeferredControl({ opening: params.opening === null ? [] : [params.opening], deferReleases: true });
|
|
732
734
|
let openingFailure;
|
|
733
735
|
let openingStep = null;
|
|
@@ -743,6 +745,7 @@ function bunAdapter(options) {
|
|
|
743
745
|
let settingsSent = false;
|
|
744
746
|
const step = (query) => new Promise((resolve) => resolve(query())).then(undefined, (error) => {
|
|
745
747
|
const mapped = mapBunDriverError({ error });
|
|
748
|
+
observeFailure(mapped);
|
|
746
749
|
if (mapped.meta.sqlstate === "25P02") {
|
|
747
750
|
throw transactionAbortedError({ cause: lastFailure ?? scope.state.aborted?.cause });
|
|
748
751
|
}
|
|
@@ -772,6 +775,7 @@ function bunAdapter(options) {
|
|
|
772
775
|
settingsSent = true;
|
|
773
776
|
return new Promise((resolve) => resolve(controlStep({ connection: reserved, step: deferredStep }))).then(undefined, (error) => {
|
|
774
777
|
const mapped = mapBunDriverError({ error });
|
|
778
|
+
observeFailure(mapped);
|
|
775
779
|
if (mapped.meta.sqlstate === "25P02")
|
|
776
780
|
throw transactionAbortedError({ cause: lastFailure ?? scope.state.aborted?.cause });
|
|
777
781
|
if (typeof mapped.meta.sqlstate !== "string")
|
|
@@ -1163,6 +1167,8 @@ function bunAdapter(options) {
|
|
|
1163
1167
|
return base;
|
|
1164
1168
|
}
|
|
1165
1169
|
let rawSession = null;
|
|
1170
|
+
let rawSessionInstance = null;
|
|
1171
|
+
const brokenRawSessions = new WeakSet;
|
|
1166
1172
|
function createSqlInstance(params) {
|
|
1167
1173
|
const sqlOptions = { max: params.max, prepare: usePrepared };
|
|
1168
1174
|
if (options?.connectionTimeout !== undefined) {
|
|
@@ -1182,10 +1188,41 @@ function bunAdapter(options) {
|
|
|
1182
1188
|
}
|
|
1183
1189
|
return createBunSql({ url, options: sqlOptions });
|
|
1184
1190
|
}
|
|
1185
|
-
function
|
|
1186
|
-
instance ??= createSqlInstance({ max: options?.max ?? 10 });
|
|
1191
|
+
function currentInstance() {
|
|
1192
|
+
instance ??= { sql: createSqlInstance({ max: options?.max ?? 10 }), active: 0, retired: false };
|
|
1187
1193
|
return instance;
|
|
1188
1194
|
}
|
|
1195
|
+
function retireInstance(target) {
|
|
1196
|
+
if (target.retired)
|
|
1197
|
+
return;
|
|
1198
|
+
target.retired = true;
|
|
1199
|
+
if (instance === target)
|
|
1200
|
+
instance = null;
|
|
1201
|
+
retiredInstances.add(target);
|
|
1202
|
+
}
|
|
1203
|
+
function leaveInstance(target) {
|
|
1204
|
+
target.active -= 1;
|
|
1205
|
+
if (target.active > 0 || !retiredInstances.delete(target))
|
|
1206
|
+
return;
|
|
1207
|
+
const closing = target.sql.close().then(ignore, ignore);
|
|
1208
|
+
closingInstances.add(closing);
|
|
1209
|
+
closing.then(() => {
|
|
1210
|
+
closingInstances.delete(closing);
|
|
1211
|
+
});
|
|
1212
|
+
}
|
|
1213
|
+
function onInstance(params) {
|
|
1214
|
+
const target = currentInstance();
|
|
1215
|
+
target.active += 1;
|
|
1216
|
+
return params.use(target.sql).then((result) => {
|
|
1217
|
+
leaveInstance(target);
|
|
1218
|
+
return result;
|
|
1219
|
+
}, (error) => {
|
|
1220
|
+
if (params.root === true && breaksSession({ error }))
|
|
1221
|
+
retireInstance(target);
|
|
1222
|
+
leaveInstance(target);
|
|
1223
|
+
throw error;
|
|
1224
|
+
});
|
|
1225
|
+
}
|
|
1189
1226
|
async function probeJsonWire(source, onReadControl) {
|
|
1190
1227
|
const probe = renderDiagnostic({ dialect: postgresDialect, query: "postgresJsonWire" });
|
|
1191
1228
|
const result = await runMapped(() => usePrepared ? source(templates.getTemplate({ text: probe.text }).template) : source.unsafe(probe.text, [])).then((result) => {
|
|
@@ -1466,6 +1503,16 @@ function bunAdapter(options) {
|
|
|
1466
1503
|
const eagerOpening = settingsStatement !== undefined && !settingsDeferred || timeout !== undefined && (!usePrepared || timeout > MAX_STATEMENT_TIMEOUT_MS);
|
|
1467
1504
|
const eagerOpen = eagerOpening && timeout !== undefined ? openTransactionStatement({ ...openingOptions, timeout }) : null;
|
|
1468
1505
|
const reserved = await runMapped(() => pool.reserve());
|
|
1506
|
+
let sessionBroken = false;
|
|
1507
|
+
const observeFailure = (error) => {
|
|
1508
|
+
if (breaksSession({ error }))
|
|
1509
|
+
sessionBroken = true;
|
|
1510
|
+
};
|
|
1511
|
+
const watched = (sending) => sending.then(undefined, (error) => {
|
|
1512
|
+
const mapped = mapBunDriverError({ error });
|
|
1513
|
+
observeFailure(mapped);
|
|
1514
|
+
throw mapped;
|
|
1515
|
+
});
|
|
1469
1516
|
const budget = startTransactionBudget({ options: transactionOptions, provider: PROVIDER });
|
|
1470
1517
|
const txScope = createSavepointScope();
|
|
1471
1518
|
const control = createReservedControl({
|
|
@@ -1474,6 +1521,7 @@ function bunAdapter(options) {
|
|
|
1474
1521
|
scope: txScope,
|
|
1475
1522
|
opening: eagerOpening ? null : beginStatement(openingOptions),
|
|
1476
1523
|
commits: usePrepared && budget.totalMs === null,
|
|
1524
|
+
observeFailure,
|
|
1477
1525
|
...settingsDeferred ? {
|
|
1478
1526
|
openingSettings: {
|
|
1479
1527
|
text: settingsStatement.text,
|
|
@@ -1491,10 +1539,8 @@ function bunAdapter(options) {
|
|
|
1491
1539
|
let discard = false;
|
|
1492
1540
|
let openingRollback;
|
|
1493
1541
|
const openWithSettings = async (statement) => {
|
|
1494
|
-
await (eagerOpen === null ? controlQuery({ connection: reserved, text: beginStatement(openingOptions) }) : reserved.unsafe(eagerOpen))
|
|
1495
|
-
|
|
1496
|
-
});
|
|
1497
|
-
const failed = await controlQuery({ connection: reserved, text: statement.text, values: statement.values }).then(() => {
|
|
1542
|
+
await watched(eagerOpen === null ? controlQuery({ connection: reserved, text: beginStatement(openingOptions) }) : reserved.unsafe(eagerOpen));
|
|
1543
|
+
const failed = await watched(controlQuery({ connection: reserved, text: statement.text, values: statement.values })).then(() => {
|
|
1498
1544
|
return;
|
|
1499
1545
|
}, (error) => ({ error }));
|
|
1500
1546
|
if (failed === undefined)
|
|
@@ -1512,7 +1558,7 @@ function bunAdapter(options) {
|
|
|
1512
1558
|
if (eagerOpening && settingsStatement !== undefined)
|
|
1513
1559
|
await openWithSettings(settingsStatement);
|
|
1514
1560
|
else if (eagerOpen !== null)
|
|
1515
|
-
await
|
|
1561
|
+
await watched(new Promise((resolve) => resolve(reserved.unsafe(eagerOpen))));
|
|
1516
1562
|
const txAdapter = createAdapter({
|
|
1517
1563
|
source: reserved,
|
|
1518
1564
|
parentMode: "tx",
|
|
@@ -1591,10 +1637,10 @@ function bunAdapter(options) {
|
|
|
1591
1637
|
scope: txScope,
|
|
1592
1638
|
commit: commit.progress,
|
|
1593
1639
|
rollback,
|
|
1594
|
-
connection: discard ? "discarded" : "returned"
|
|
1640
|
+
connection: discard || sessionBroken ? "discarded" : "returned"
|
|
1595
1641
|
});
|
|
1596
1642
|
} finally {
|
|
1597
|
-
if (discard)
|
|
1643
|
+
if (discard || sessionBroken)
|
|
1598
1644
|
await runMapped(() => reserved.close());
|
|
1599
1645
|
else
|
|
1600
1646
|
reserved.release();
|
|
@@ -1705,6 +1751,7 @@ function bunAdapter(options) {
|
|
|
1705
1751
|
}
|
|
1706
1752
|
const opened = await Promise.allSettled(Array.from({ length: Math.max(1, poolMin) }, () => runMapped(() => source.reserve())));
|
|
1707
1753
|
const held = opened.flatMap((outcome) => outcome.status === "fulfilled" ? [outcome.value] : []);
|
|
1754
|
+
let broken;
|
|
1708
1755
|
try {
|
|
1709
1756
|
const failed = opened.find((outcome) => outcome.status === "rejected");
|
|
1710
1757
|
if (failed !== undefined)
|
|
@@ -1714,11 +1761,19 @@ function bunAdapter(options) {
|
|
|
1714
1761
|
wireProbe = probeJsonWire(reserved).finally(() => {
|
|
1715
1762
|
wireProbe = null;
|
|
1716
1763
|
});
|
|
1717
|
-
await wireProbe
|
|
1764
|
+
await wireProbe.catch((error) => {
|
|
1765
|
+
if (breaksSession({ error }))
|
|
1766
|
+
broken = reserved;
|
|
1767
|
+
throw error;
|
|
1768
|
+
});
|
|
1718
1769
|
}
|
|
1719
1770
|
} finally {
|
|
1720
|
-
for (const reserved of held)
|
|
1721
|
-
reserved
|
|
1771
|
+
for (const reserved of held) {
|
|
1772
|
+
if (reserved === broken)
|
|
1773
|
+
await runMapped(() => reserved.close());
|
|
1774
|
+
else
|
|
1775
|
+
reserved.release();
|
|
1776
|
+
}
|
|
1722
1777
|
}
|
|
1723
1778
|
},
|
|
1724
1779
|
async disconnect() {
|
|
@@ -1727,7 +1782,8 @@ function bunAdapter(options) {
|
|
|
1727
1782
|
throw transactionLifecycleRefusal({ method: "disconnect" });
|
|
1728
1783
|
}
|
|
1729
1784
|
await runMapped(() => source.close());
|
|
1730
|
-
instance
|
|
1785
|
+
if (instance?.sql === source)
|
|
1786
|
+
instance = null;
|
|
1731
1787
|
},
|
|
1732
1788
|
formatArrayParam(values) {
|
|
1733
1789
|
return toPgArrayLiteral(values);
|
|
@@ -1735,9 +1791,9 @@ function bunAdapter(options) {
|
|
|
1735
1791
|
};
|
|
1736
1792
|
return handle;
|
|
1737
1793
|
}
|
|
1738
|
-
function poolAdapter() {
|
|
1794
|
+
function poolAdapter(sql) {
|
|
1739
1795
|
return createAdapter({
|
|
1740
|
-
source:
|
|
1796
|
+
source: sql,
|
|
1741
1797
|
parentMode: "pool",
|
|
1742
1798
|
scope: createSavepointScope()
|
|
1743
1799
|
});
|
|
@@ -1750,6 +1806,7 @@ function bunAdapter(options) {
|
|
|
1750
1806
|
return;
|
|
1751
1807
|
const session = rawSession;
|
|
1752
1808
|
rawSession = null;
|
|
1809
|
+
rawSessionInstance = null;
|
|
1753
1810
|
await runMapped(() => session.unsafe("ROLLBACK")).then(() => {
|
|
1754
1811
|
session.release();
|
|
1755
1812
|
}, (error) => {
|
|
@@ -1758,11 +1815,10 @@ function bunAdapter(options) {
|
|
|
1758
1815
|
});
|
|
1759
1816
|
},
|
|
1760
1817
|
async () => {
|
|
1761
|
-
|
|
1762
|
-
return;
|
|
1763
|
-
const closing = instance;
|
|
1818
|
+
const open = [...instance === null ? [] : [instance], ...retiredInstances];
|
|
1764
1819
|
instance = null;
|
|
1765
|
-
|
|
1820
|
+
retiredInstances.clear();
|
|
1821
|
+
await Promise.all([...closingInstances, ...open.map((closing) => runMapped(() => closing.sql.close()))]);
|
|
1766
1822
|
}
|
|
1767
1823
|
]
|
|
1768
1824
|
});
|
|
@@ -1788,40 +1844,62 @@ function bunAdapter(options) {
|
|
|
1788
1844
|
});
|
|
1789
1845
|
},
|
|
1790
1846
|
async execute(params) {
|
|
1791
|
-
return runExecute({ source:
|
|
1847
|
+
return onInstance({ root: true, use: (sql) => runExecute({ source: sql, ...params }) });
|
|
1792
1848
|
},
|
|
1793
1849
|
async executeUnsafe(params) {
|
|
1794
1850
|
const control = classifyRawTransactionControl({ text: params.text });
|
|
1795
1851
|
if (rawSession !== null) {
|
|
1852
|
+
const session = rawSession;
|
|
1853
|
+
const run = () => runExecuteUnsafe({ source: session, ...params }).catch((error) => {
|
|
1854
|
+
if (breaksSession({ error }))
|
|
1855
|
+
brokenRawSessions.add(session);
|
|
1856
|
+
throw error;
|
|
1857
|
+
});
|
|
1796
1858
|
if (control === "close") {
|
|
1797
|
-
const
|
|
1859
|
+
const held = rawSessionInstance;
|
|
1798
1860
|
rawSession = null;
|
|
1861
|
+
rawSessionInstance = null;
|
|
1799
1862
|
try {
|
|
1800
|
-
return await
|
|
1863
|
+
return await run();
|
|
1801
1864
|
} finally {
|
|
1802
|
-
|
|
1865
|
+
if (brokenRawSessions.has(session))
|
|
1866
|
+
await runMapped(() => session.close());
|
|
1867
|
+
else
|
|
1868
|
+
session.release();
|
|
1869
|
+
if (held !== null)
|
|
1870
|
+
leaveInstance(held);
|
|
1803
1871
|
}
|
|
1804
1872
|
}
|
|
1805
|
-
return
|
|
1873
|
+
return run();
|
|
1806
1874
|
}
|
|
1807
1875
|
if (control === "open") {
|
|
1808
|
-
const
|
|
1876
|
+
const held = currentInstance();
|
|
1877
|
+
held.active += 1;
|
|
1878
|
+
const reserved = await runMapped(() => held.sql.reserve()).catch((error) => {
|
|
1879
|
+
leaveInstance(held);
|
|
1880
|
+
throw error;
|
|
1881
|
+
});
|
|
1809
1882
|
try {
|
|
1810
1883
|
const result = await runExecuteUnsafe({ source: reserved, ...params });
|
|
1811
1884
|
rawSession = reserved;
|
|
1885
|
+
rawSessionInstance = held;
|
|
1812
1886
|
return result;
|
|
1813
1887
|
} catch (error) {
|
|
1814
|
-
|
|
1888
|
+
if (breaksSession({ error }))
|
|
1889
|
+
await runMapped(() => reserved.close());
|
|
1890
|
+
else
|
|
1891
|
+
reserved.release();
|
|
1892
|
+
leaveInstance(held);
|
|
1815
1893
|
throw error;
|
|
1816
1894
|
}
|
|
1817
1895
|
}
|
|
1818
|
-
return runExecuteUnsafe({ source:
|
|
1896
|
+
return onInstance({ root: true, use: (sql) => runExecuteUnsafe({ source: sql, ...params }) });
|
|
1819
1897
|
},
|
|
1820
1898
|
async transaction(fn, transactionOptions) {
|
|
1821
|
-
return poolAdapter().transaction(fn, transactionOptions);
|
|
1899
|
+
return onInstance({ use: (sql) => poolAdapter(sql).transaction(fn, transactionOptions) });
|
|
1822
1900
|
},
|
|
1823
1901
|
async connect() {
|
|
1824
|
-
return poolAdapter().connect();
|
|
1902
|
+
return onInstance({ use: (sql) => poolAdapter(sql).connect() });
|
|
1825
1903
|
}
|
|
1826
1904
|
};
|
|
1827
1905
|
return {
|
|
@@ -1890,6 +1968,11 @@ function isConnectionLoss(params) {
|
|
|
1890
1968
|
const sqlstate = error.meta.sqlstate;
|
|
1891
1969
|
return typeof sqlstate !== "string" || sqlstate.startsWith("08") || ["57P01", "57P02", "57P03"].includes(sqlstate);
|
|
1892
1970
|
}
|
|
1971
|
+
var NEVER_REUSE_SQLSTATE_CLASSES = new Set(["08", "53", "57", "58", "XX"]);
|
|
1972
|
+
function breaksSession(params) {
|
|
1973
|
+
const sqlstate = mapBunDriverError({ error: params.error }).meta.sqlstate;
|
|
1974
|
+
return typeof sqlstate === "string" && NEVER_REUSE_SQLSTATE_CLASSES.has(sqlstate.slice(0, 2));
|
|
1975
|
+
}
|
|
1893
1976
|
function abortTransactionOnStatementFailure(params) {
|
|
1894
1977
|
const { scope, error } = params;
|
|
1895
1978
|
if (error instanceof VibeError7 && error.meta.driverCode === "ERR_POSTGRES_TOO_MANY_PARAMETERS") {
|
|
@@ -1935,4 +2018,4 @@ export {
|
|
|
1935
2018
|
validateTransactionDeadline2 as validateTransactionDeadline
|
|
1936
2019
|
};
|
|
1937
2020
|
|
|
1938
|
-
//# debugId=
|
|
2021
|
+
//# debugId=0A139B5B4EDCE50364756E2164756E21
|