@sigil-dev/plugin-sql 0.9.13
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 +314 -0
- package/package.json +30 -0
- package/src/bun.ts +265 -0
- package/src/contract.ts +249 -0
- package/src/database.ts +750 -0
- package/src/fragment.ts +275 -0
- package/src/index.ts +65 -0
- package/src/testing.ts +148 -0
package/src/fragment.ts
ADDED
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A query that has been *described* but not run.
|
|
3
|
+
*
|
|
4
|
+
* A tagged template is usually a promise, and a promise is a result. This is
|
|
5
|
+
* the opposite: it holds the template and its values, and resolves the client
|
|
6
|
+
* only when you run it — `await`, or `run()`. That is what makes it safe to
|
|
7
|
+
* build at module scope, before a worker has opened a connection, and safe to
|
|
8
|
+
* reuse, since every run is a fresh execution rather than a shared settled
|
|
9
|
+
* promise.
|
|
10
|
+
*
|
|
11
|
+
* Fragments compose. Interpolating one splices its text and its values into
|
|
12
|
+
* the enclosing template, so a condition with a parameter inside it stays a
|
|
13
|
+
* parameter all the way down:
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* const active = db.sql`deleted_at is null`;
|
|
17
|
+
* const byOwner = (id: number) => db.sql`owner_id = ${id} and ${active}`;
|
|
18
|
+
* const rows = await db.sql<User>`select * from t where ${byOwner(7)}`;
|
|
19
|
+
* // select * from t where owner_id = $1 and deleted_at is null
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* A registered template, so two copies of this package still compose.
|
|
25
|
+
*
|
|
26
|
+
* A symbol from `Symbol.for` rather than a module-local one: `db.sql` from an
|
|
27
|
+
* older `@sigil-dev/plugin-sql` in a transitive dependency must be recognised
|
|
28
|
+
* as a fragment, and `in` also means a plain object literal carrying the
|
|
29
|
+
* marker works, which keeps tests from needing the real constructor.
|
|
30
|
+
*/
|
|
31
|
+
export const SQL_FRAGMENT = Symbol.for("sigil.sql.fragment");
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* `T` is the row type. It defaults to `any`, like `Bun.SQL`'s own results,
|
|
35
|
+
* so code that casts rows to its own interfaces — `(await q) as CardRow[]` —
|
|
36
|
+
* keeps compiling; `db.sql<CardRow>` is the typed spelling.
|
|
37
|
+
*/
|
|
38
|
+
// biome-ignore lint/suspicious/noExplicitAny: the default row, as in Bun.SQL
|
|
39
|
+
export interface Fragment<T = any> extends Promise<T[]> {
|
|
40
|
+
readonly [SQL_FRAGMENT]: true;
|
|
41
|
+
/**
|
|
42
|
+
* The static parts of the query with every nested fragment spliced in.
|
|
43
|
+
* Always one longer than `values`, like a `TemplateStringsArray`.
|
|
44
|
+
*
|
|
45
|
+
* This, not `text`, is what an adapter should execute: handing it to the
|
|
46
|
+
* driver's own tagged template lets the driver choose its placeholders,
|
|
47
|
+
* which is `$1` on postgres and `?` on MySQL. Choosing them here is the
|
|
48
|
+
* bug this package already had once.
|
|
49
|
+
*/
|
|
50
|
+
readonly strings: readonly string[];
|
|
51
|
+
/**
|
|
52
|
+
* The query with `$1`-style placeholders, for logs and error messages —
|
|
53
|
+
* and for an adapter over a postgres-only driver. Not portable: MySQL
|
|
54
|
+
* reads `$1` as a column name.
|
|
55
|
+
*/
|
|
56
|
+
readonly text: string;
|
|
57
|
+
/** Bound parameters, in order. Never interpolated into `strings`. */
|
|
58
|
+
readonly values: readonly unknown[];
|
|
59
|
+
|
|
60
|
+
/** The request's transaction if there is one, else the pool. */
|
|
61
|
+
run(): Promise<T[]>;
|
|
62
|
+
|
|
63
|
+
/** Inside a transaction, or throw. */
|
|
64
|
+
runTx(): Promise<T[]>;
|
|
65
|
+
|
|
66
|
+
/** Always the pool, deliberately outside any transaction. */
|
|
67
|
+
runPool(): Promise<T[]>;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* So `await db.sql`...`` works, and reads like every other query in the
|
|
71
|
+
* app. The same as `run()`.
|
|
72
|
+
*
|
|
73
|
+
* The cost is the one every thenable has: anything that awaits a
|
|
74
|
+
* fragment runs it — returning one from an `async` function included.
|
|
75
|
+
* Return it from a plain function if the caller is meant to compose it.
|
|
76
|
+
*/
|
|
77
|
+
then<A = T[], B = never>(
|
|
78
|
+
onFulfilled?: ((value: T[]) => A | PromiseLike<A>) | null,
|
|
79
|
+
// biome-ignore lint/suspicious/noExplicitAny: matches Promise's own signature
|
|
80
|
+
onRejected?: ((reason: any) => B | PromiseLike<B>) | null,
|
|
81
|
+
): Promise<A | B>;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function isFragment(v: unknown): v is Fragment<unknown> {
|
|
85
|
+
return typeof v === "object" && v !== null && SQL_FRAGMENT in v;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export const SQL_HELPER = Symbol.for("sigil.sql.helper");
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* A driver helper — an `IN` list, a postgres array, a row to insert — that
|
|
92
|
+
* has been described but not built.
|
|
93
|
+
*
|
|
94
|
+
* `Bun.SQL` builds these from a client: `sql(ids)`, `sql.array(xs, "text")`.
|
|
95
|
+
* A fragment at module scope has no client yet, so `db.sql(ids)` records the
|
|
96
|
+
* call and the adapter replays it on whichever client runs the query. It is a
|
|
97
|
+
* value, not a fragment: it sits in `values` and is bound, never spliced.
|
|
98
|
+
*/
|
|
99
|
+
export interface SqlHelper {
|
|
100
|
+
readonly [SQL_HELPER]: true;
|
|
101
|
+
/** The helper to call on the client; undefined for the client itself. */
|
|
102
|
+
readonly method: string | undefined;
|
|
103
|
+
readonly args: readonly unknown[];
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function isHelper(v: unknown): v is SqlHelper {
|
|
107
|
+
return typeof v === "object" && v !== null && SQL_HELPER in v;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function createHelper(
|
|
111
|
+
method: string | undefined,
|
|
112
|
+
args: readonly unknown[],
|
|
113
|
+
): SqlHelper {
|
|
114
|
+
return {
|
|
115
|
+
[SQL_HELPER]: true,
|
|
116
|
+
method,
|
|
117
|
+
args,
|
|
118
|
+
toString(): never {
|
|
119
|
+
throw new Error(
|
|
120
|
+
"This sql helper was used as a string. Interpolate it into a " +
|
|
121
|
+
"sql`` template instead.",
|
|
122
|
+
);
|
|
123
|
+
},
|
|
124
|
+
} as SqlHelper;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Splice a template and everything nested in it into one list of static
|
|
129
|
+
* parts and one list of values.
|
|
130
|
+
*
|
|
131
|
+
* A nested fragment's first part is joined onto the current last part, its
|
|
132
|
+
* middle parts are appended, and the enclosing template's next part is
|
|
133
|
+
* joined onto its last one. Keeping that as one accumulating array rather
|
|
134
|
+
* than a parallel string is what stops the trailing text being emitted twice,
|
|
135
|
+
* or lost — `ART_KIND` spliced into a `SELECT` list is the case that matters.
|
|
136
|
+
*/
|
|
137
|
+
export function splice(
|
|
138
|
+
strings: readonly string[],
|
|
139
|
+
values: readonly unknown[],
|
|
140
|
+
): { strings: string[]; values: unknown[] } {
|
|
141
|
+
const parts: string[] = [];
|
|
142
|
+
const out: unknown[] = [];
|
|
143
|
+
// The part still being built; pushed when a value closes it.
|
|
144
|
+
let tail = strings[0] ?? "";
|
|
145
|
+
for (let i = 0; i < values.length; i++) {
|
|
146
|
+
const v = values[i];
|
|
147
|
+
if (isFragment(v)) {
|
|
148
|
+
const f = v as Partial<Fragment<unknown>>;
|
|
149
|
+
if (!f.strings) {
|
|
150
|
+
throw new Error(
|
|
151
|
+
"This sql fragment has no `strings`, so it cannot be spliced " +
|
|
152
|
+
"into another query. It most likely comes from an older copy " +
|
|
153
|
+
"of @sigil-dev/plugin-sql; deduplicate the package.",
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
const inner = splice(f.strings, f.values ?? []);
|
|
157
|
+
const [first = "", ...rest] = inner.strings;
|
|
158
|
+
tail += first;
|
|
159
|
+
if (rest.length > 0) {
|
|
160
|
+
parts.push(tail, ...rest.slice(0, -1));
|
|
161
|
+
tail = rest[rest.length - 1] ?? "";
|
|
162
|
+
}
|
|
163
|
+
out.push(...inner.values);
|
|
164
|
+
} else {
|
|
165
|
+
parts.push(tail);
|
|
166
|
+
tail = "";
|
|
167
|
+
out.push(v);
|
|
168
|
+
}
|
|
169
|
+
tail += strings[i + 1] ?? "";
|
|
170
|
+
}
|
|
171
|
+
parts.push(tail);
|
|
172
|
+
return { strings: parts, values: out };
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* `splice`, rendered as one string with `$n` placeholders.
|
|
177
|
+
*
|
|
178
|
+
* Kept for display and for postgres-only drivers. The placeholder style is a
|
|
179
|
+
* parameter so a caller that knows its dialect can ask for another.
|
|
180
|
+
*/
|
|
181
|
+
export function flatten(
|
|
182
|
+
strings: readonly string[],
|
|
183
|
+
values: readonly unknown[],
|
|
184
|
+
placeholder: (index: number) => string = (i) => `$${i + 1}`,
|
|
185
|
+
): { text: string; values: unknown[] } {
|
|
186
|
+
const s = splice(strings, values);
|
|
187
|
+
let text = s.strings[0] ?? "";
|
|
188
|
+
for (let i = 1; i < s.strings.length; i++) {
|
|
189
|
+
text += placeholder(i - 1) + s.strings[i];
|
|
190
|
+
}
|
|
191
|
+
return { text, values: s.values };
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** What a fragment needs from its database to run. */
|
|
195
|
+
export interface FragmentRunner {
|
|
196
|
+
run: () => unknown;
|
|
197
|
+
runTx: () => unknown;
|
|
198
|
+
runPool: () => unknown;
|
|
199
|
+
query: (
|
|
200
|
+
target: unknown,
|
|
201
|
+
text: string,
|
|
202
|
+
values: readonly unknown[],
|
|
203
|
+
strings: readonly string[],
|
|
204
|
+
) => Promise<unknown[]>;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Build a fragment from a template.
|
|
209
|
+
*
|
|
210
|
+
* `resolve` is supplied by the database and picks the client, which is the
|
|
211
|
+
* only reason this can be called before a worker exists.
|
|
212
|
+
*/
|
|
213
|
+
// biome-ignore lint/suspicious/noExplicitAny: the default row, as in Bun.SQL
|
|
214
|
+
export function createFragment<T = any>(
|
|
215
|
+
strings: TemplateStringsArray | readonly string[],
|
|
216
|
+
values: readonly unknown[],
|
|
217
|
+
resolve: FragmentRunner,
|
|
218
|
+
): Fragment<T> {
|
|
219
|
+
// Spliced once, at construction. It is pure string work with no client
|
|
220
|
+
// involved, so a module-scope fragment costs nothing and cannot fail for
|
|
221
|
+
// want of a connection.
|
|
222
|
+
const s = splice(strings, values);
|
|
223
|
+
const { text } = flatten(s.strings, s.values);
|
|
224
|
+
|
|
225
|
+
// `pick` is called inside the promise, so a throwing accessor — tx()
|
|
226
|
+
// outside a transaction — is a rejection like any other query error.
|
|
227
|
+
// runTx() used to throw synchronously, which `await x.runTx()` hides but
|
|
228
|
+
// `x.runTx().catch()` does not.
|
|
229
|
+
const exec = (pick: () => unknown): Promise<T[]> =>
|
|
230
|
+
Promise.resolve().then(
|
|
231
|
+
() =>
|
|
232
|
+
resolve.query(pick(), text, s.values, s.strings) as Promise<T[]>,
|
|
233
|
+
);
|
|
234
|
+
|
|
235
|
+
const frag: Fragment<T> = {
|
|
236
|
+
[SQL_FRAGMENT]: true,
|
|
237
|
+
strings: s.strings,
|
|
238
|
+
text,
|
|
239
|
+
values: s.values,
|
|
240
|
+
|
|
241
|
+
run: () => exec(resolve.run),
|
|
242
|
+
runTx: () => exec(resolve.runTx),
|
|
243
|
+
runPool: () => exec(resolve.runPool),
|
|
244
|
+
|
|
245
|
+
then(onFulfilled, onRejected) {
|
|
246
|
+
return exec(resolve.run).then(onFulfilled, onRejected);
|
|
247
|
+
},
|
|
248
|
+
// The rest of Promise, so a fragment goes wherever a Promise does:
|
|
249
|
+
// Promise.all, a `() => Promise<T>` parameter, an `as Promise<Row[]>`.
|
|
250
|
+
catch(onRejected) {
|
|
251
|
+
return exec(resolve.run).catch(onRejected);
|
|
252
|
+
},
|
|
253
|
+
finally(onFinally) {
|
|
254
|
+
return exec(resolve.run).finally(onFinally);
|
|
255
|
+
},
|
|
256
|
+
[Symbol.toStringTag]: "SqlFragment",
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Refuses rather than returning text.
|
|
260
|
+
*
|
|
261
|
+
* The tempting alternative is `text`, and it is wrong: this object
|
|
262
|
+
* holds bound values that `text` does not contain, so stringifying it
|
|
263
|
+
* either drops the parameters or inlines them unescaped. Failing here
|
|
264
|
+
* is the difference between a wrong query and a broken build.
|
|
265
|
+
*/
|
|
266
|
+
toString(): never {
|
|
267
|
+
throw new Error(
|
|
268
|
+
"This sql fragment was used as a string. Interpolate it into " +
|
|
269
|
+
"another sql`` template instead, so its values stay bound.",
|
|
270
|
+
);
|
|
271
|
+
},
|
|
272
|
+
} as Fragment<T>;
|
|
273
|
+
|
|
274
|
+
return frag;
|
|
275
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @sigil-dev/plugin-sql
|
|
3
|
+
*
|
|
4
|
+
* A contract for SQL adapters, a conformance kit that proves one behaves,
|
|
5
|
+
* and an official adapter for `Bun.SQL`.
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* import { sql } from "@sigil-dev/plugin-sql";
|
|
9
|
+
* import { bunSqlAdapter } from "@sigil-dev/plugin-sql/bun";
|
|
10
|
+
*
|
|
11
|
+
* // sigil.config.ts
|
|
12
|
+
* export default { plugins: [sql(bunSqlAdapter(process.env.DATABASE_URL!))] };
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* // a route
|
|
17
|
+
* const rows = await locals.db`select id, name from users`;
|
|
18
|
+
*
|
|
19
|
+
* // any other server module: a websocket handler, a timer, hooks.server.ts
|
|
20
|
+
* import { database } from "@sigil-dev/plugin-sql";
|
|
21
|
+
* const rows = await database().sql`select id, name from users`;
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* This package previously exported `bunSqlite` / `bunPg` / `bunMysql` and a
|
|
25
|
+
* `SqlExecutor` interface with `query(sql, params)`. That layer existed to
|
|
26
|
+
* bridge `Bun.sql`'s positional-parameter API, and every bug in it came
|
|
27
|
+
* from guessing a dialect from a string: `sqlite://app.db` became the
|
|
28
|
+
* absolute path `//app.db`, postgres and mysql were assumed to share a
|
|
29
|
+
* placeholder style when they are opposites, and `toISOString()` timestamps
|
|
30
|
+
* were rejected by MySQL. `Bun.SQL` infers the dialect correctly and
|
|
31
|
+
* serialises timestamps itself, so the guessing has nothing left to do.
|
|
32
|
+
*
|
|
33
|
+
* @module
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
export {
|
|
37
|
+
defineSqlAdapter,
|
|
38
|
+
sql,
|
|
39
|
+
SQL_CONTRACT_VERSION,
|
|
40
|
+
workerContext,
|
|
41
|
+
type ClientOf,
|
|
42
|
+
type SqlAdapter,
|
|
43
|
+
type SqlCapabilities,
|
|
44
|
+
type SqlPluginOptions,
|
|
45
|
+
type TxOf,
|
|
46
|
+
type WorkerContext,
|
|
47
|
+
} from "./contract.ts";
|
|
48
|
+
|
|
49
|
+
export {
|
|
50
|
+
createDatabase,
|
|
51
|
+
database,
|
|
52
|
+
hasDatabase,
|
|
53
|
+
type CreateDatabaseOptions,
|
|
54
|
+
type Database,
|
|
55
|
+
type SqlHandle,
|
|
56
|
+
type SqlTag,
|
|
57
|
+
} from "./database.ts";
|
|
58
|
+
export {
|
|
59
|
+
isFragment,
|
|
60
|
+
isHelper,
|
|
61
|
+
SQL_FRAGMENT,
|
|
62
|
+
SQL_HELPER,
|
|
63
|
+
type Fragment,
|
|
64
|
+
type SqlHelper,
|
|
65
|
+
} from "./fragment.ts";
|
package/src/testing.ts
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A conformance kit for SQL adapters.
|
|
3
|
+
*
|
|
4
|
+
* The type catches shape mistakes. It cannot catch a transaction that
|
|
5
|
+
* silently does not roll back, which is the failure that loses data and
|
|
6
|
+
* passes every type check. So an adapter author runs this suite and can say
|
|
7
|
+
* their adapter behaves, rather than that it type-checks.
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* runAdapterConformance(bunSqlAdapter(url), {
|
|
11
|
+
* exec: (db, q) => db.unsafe(q),
|
|
12
|
+
* count: async (db, t) => Number((await db.unsafe(`select count(*) as n from ${t}`))[0].n),
|
|
13
|
+
* });
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* Point CI at a Postgres (and MySQL) service and the same file checks every
|
|
17
|
+
* dialect. It is `bun:test`-only, which suits a Bun-first framework.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { afterAll, beforeAll, beforeEach, describe, expect, test } from "bun:test";
|
|
21
|
+
import type { SqlAdapter, WorkerContext } from "./contract.ts";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Hooks the kit uses to drive an adapter.
|
|
25
|
+
*
|
|
26
|
+
* The client parameter is `C | Tx` because the kit genuinely calls them
|
|
27
|
+
* with both: `exec` runs inside `adapter.transaction` to prove rollback
|
|
28
|
+
* works, and outside it to set up. A hook typed for `C` alone has to
|
|
29
|
+
* narrow at every call site, and the natural way to do that — cast — is
|
|
30
|
+
* how a kit stops testing what it claims to.
|
|
31
|
+
*
|
|
32
|
+
* A library whose transaction handle is genuinely the same type sees no
|
|
33
|
+
* difference, because `Tx` defaults to `C` and the union collapses.
|
|
34
|
+
*/
|
|
35
|
+
export interface ConformanceHooks<C, Tx = C> {
|
|
36
|
+
exec(client: C | Tx, sql: string): Promise<void>;
|
|
37
|
+
count(client: C | Tx, table: string): Promise<number>;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface ConformanceOptions {
|
|
41
|
+
ctx?: WorkerContext;
|
|
42
|
+
/**
|
|
43
|
+
* Whether an in-transaction read is invisible to a read outside it.
|
|
44
|
+
*
|
|
45
|
+
* False for single-connection drivers. SQLite through `Bun.SQL` is one
|
|
46
|
+
* connection, so it genuinely does not isolate — measured, in tx: 2
|
|
47
|
+
* outside: 2, against postgres: in tx 2, outside 1. Asserting isolation
|
|
48
|
+
* for a driver that has none would be asserting a lie, so the test is
|
|
49
|
+
* skipped and the flag records why.
|
|
50
|
+
*/
|
|
51
|
+
isolation?: boolean;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Plain SQL, so the same statements run on postgres, mysql and sqlite. */
|
|
55
|
+
const T = "sigil_conformance";
|
|
56
|
+
|
|
57
|
+
export function runAdapterConformance<C, Tx = C>(
|
|
58
|
+
adapter: SqlAdapter<C, Tx>,
|
|
59
|
+
hooks: ConformanceHooks<C, Tx>,
|
|
60
|
+
{
|
|
61
|
+
ctx = { workerId: 0, workers: 1, dev: false },
|
|
62
|
+
isolation = true,
|
|
63
|
+
}: ConformanceOptions = {},
|
|
64
|
+
): void {
|
|
65
|
+
describe(`sql adapter: ${adapter.name}`, () => {
|
|
66
|
+
let db: C;
|
|
67
|
+
|
|
68
|
+
beforeAll(async () => {
|
|
69
|
+
db = await adapter.create(ctx);
|
|
70
|
+
await hooks.exec(db, `create table if not exists ${T} (id integer)`);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
beforeEach(async () => {
|
|
74
|
+
await hooks.exec(db, `delete from ${T}`);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
afterAll(async () => {
|
|
78
|
+
// Drop before close: a client that cannot take statements may still
|
|
79
|
+
// hold a table, and a leaked conformance table in a shared CI
|
|
80
|
+
// database makes the next run's counts wrong.
|
|
81
|
+
try {
|
|
82
|
+
await hooks.exec(db, `drop table if exists ${T}`);
|
|
83
|
+
} catch {
|
|
84
|
+
// the client may already be unusable; close regardless
|
|
85
|
+
}
|
|
86
|
+
await adapter.close(db);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test("declares the contract this build expects", () => {
|
|
90
|
+
expect(adapter.contract).toBe(1);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test("create received the worker context it was promised", () => {
|
|
94
|
+
// An adapter that ignores ctx.workers opens a full-size pool per
|
|
95
|
+
// worker, and nothing else here would notice.
|
|
96
|
+
expect(ctx.workers).toBeGreaterThanOrEqual(1);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test("ping resolves", async () => {
|
|
100
|
+
await adapter.ping(db);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test("transaction returns the callback's result", async () => {
|
|
104
|
+
expect(await adapter.transaction(db, async () => 42)).toBe(42);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
test("transaction commits on success", async () => {
|
|
108
|
+
await adapter.transaction(db, (tx) =>
|
|
109
|
+
hooks.exec(tx, `insert into ${T} (id) values (1)`),
|
|
110
|
+
);
|
|
111
|
+
expect(await hooks.count(db, T)).toBe(1);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test("transaction rolls back and rethrows the same error", async () => {
|
|
115
|
+
const boom = new Error("boom");
|
|
116
|
+
const thrown = await adapter
|
|
117
|
+
.transaction(db, async (tx) => {
|
|
118
|
+
await hooks.exec(tx, `insert into ${T} (id) values (1)`);
|
|
119
|
+
throw boom;
|
|
120
|
+
})
|
|
121
|
+
.catch((e: unknown) => e);
|
|
122
|
+
// Identity, not just "it threw": an adapter that wrapped the
|
|
123
|
+
// error would hide the real cause behind its own message.
|
|
124
|
+
expect(thrown).toBe(boom);
|
|
125
|
+
expect(await hooks.count(db, T)).toBe(0);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
test.if(isolation)(
|
|
129
|
+
"uncommitted writes are invisible outside the transaction",
|
|
130
|
+
async () => {
|
|
131
|
+
await adapter.transaction(db, async (tx) => {
|
|
132
|
+
await hooks.exec(tx, `insert into ${T} (id) values (1)`);
|
|
133
|
+
expect(await hooks.count(tx, T)).toBe(1);
|
|
134
|
+
// The line that fails for a single-connection driver, which
|
|
135
|
+
// is why the whole test is behind the flag.
|
|
136
|
+
expect(await hooks.count(db, T)).toBe(0);
|
|
137
|
+
});
|
|
138
|
+
},
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
test("close releases the client", async () => {
|
|
142
|
+
// A separate client so this cannot close the shared one the other
|
|
143
|
+
// tests use.
|
|
144
|
+
const spare = await adapter.create(ctx);
|
|
145
|
+
await adapter.close(spare);
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
}
|