@bezoar/core 0.0.7 → 0.0.8
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/.dist/index.d.mts +54 -7
- package/.dist/index.mjs +47 -3
- package/README.md +35 -5
- package/package.json +2 -2
package/.dist/index.d.mts
CHANGED
|
@@ -3,7 +3,7 @@ import { Compilable, Kysely } from "kysely";
|
|
|
3
3
|
/**
|
|
4
4
|
* The members of a service. A query group cannot take one of these names.
|
|
5
5
|
*/
|
|
6
|
-
export declare const RESERVED: readonly ["config", "schema", "query", "first", "execute", "build", "transaction", "migrate", "seed", "roles"];
|
|
6
|
+
export declare const RESERVED: readonly ["config", "schema", "query", "first", "one", "execute", "build", "transaction", "migrate", "seed", "roles"];
|
|
7
7
|
/**
|
|
8
8
|
* The query name a report carries for a built query: one that ran through
|
|
9
9
|
* the builder rather than the contract. The name of the builder member of
|
|
@@ -112,11 +112,31 @@ export declare class AmbiguousQueryError extends Error {
|
|
|
112
112
|
/**
|
|
113
113
|
* Raised when the driver fails to run a query. Carries the query name, which
|
|
114
114
|
* is `BUILT` for a built query, and the driver's error as `cause`.
|
|
115
|
+
*
|
|
116
|
+
* When Postgres refused the statement, the error also says why, read from
|
|
117
|
+
* the fields of the driver's error: `code` is the SQLSTATE, as `23505` is
|
|
118
|
+
* for a unique violation, and `constraint`, `table`, and `column` name what
|
|
119
|
+
* refused it, each when Postgres reports one. Every one is `undefined` for
|
|
120
|
+
* a failure that is not of Postgres, such as a lost connection.
|
|
115
121
|
*/
|
|
116
122
|
export declare class QueryError extends Error {
|
|
117
123
|
readonly query: string;
|
|
124
|
+
readonly code: string | undefined;
|
|
125
|
+
readonly constraint: string | undefined;
|
|
126
|
+
readonly table: string | undefined;
|
|
127
|
+
readonly column: string | undefined;
|
|
118
128
|
constructor(query: string, cause: unknown);
|
|
119
129
|
}
|
|
130
|
+
/**
|
|
131
|
+
* Raised when `one` runs a query that does not return exactly one row.
|
|
132
|
+
* Carries the query name, which is `BUILT` for a built query, and the
|
|
133
|
+
* number of rows it returned.
|
|
134
|
+
*/
|
|
135
|
+
export declare class RowCountError extends Error {
|
|
136
|
+
readonly query: string;
|
|
137
|
+
readonly count: number;
|
|
138
|
+
constructor(query: string, count: number);
|
|
139
|
+
}
|
|
120
140
|
/**
|
|
121
141
|
* Raised when a migration fails to apply. Carries the version and the
|
|
122
142
|
* driver's error as `cause`. The run rolls back. No migration of the run is
|
|
@@ -198,6 +218,12 @@ export interface Outcome {
|
|
|
198
218
|
* `transaction` runs a function with an executor bound to one transaction.
|
|
199
219
|
* The transaction commits when the function returns and rolls back when it
|
|
200
220
|
* throws.
|
|
221
|
+
*
|
|
222
|
+
* When Postgres refuses a statement, `query` rejects with an error that
|
|
223
|
+
* carries the fields of the error of Postgres as text, where it reports
|
|
224
|
+
* them: `code`, the SQLSTATE, and `constraint`, `table`, and `column`. A
|
|
225
|
+
* `QueryError` reads them by these names. The errors of node-postgres and
|
|
226
|
+
* of PGlite have them already.
|
|
201
227
|
*/
|
|
202
228
|
export interface Executor {
|
|
203
229
|
query(sql: string, params: readonly unknown[]): Promise<Outcome>;
|
|
@@ -294,10 +320,13 @@ export type Args<D extends Definition, Q extends Query<D>> = Record<never, never
|
|
|
294
320
|
/**
|
|
295
321
|
* The function of one query. The function takes the arguments of the query
|
|
296
322
|
* and returns its rows, or its count when it has no column. A query with a
|
|
297
|
-
* column also has `first`, which returns the first row or `null
|
|
323
|
+
* column also has `first`, which returns the first row or `null`, and
|
|
324
|
+
* `one`, which returns the one row and throws when there is not exactly
|
|
325
|
+
* one.
|
|
298
326
|
*/
|
|
299
327
|
export type Method<D extends Definition, Q extends Query<D>> = ((...args: Args<D, Q>) => Promise<Returns<D, Q>>) & (Q extends RowQuery<D> ? {
|
|
300
328
|
readonly first: (...args: Args<D, Q>) => Promise<Row<D, Q> | null>;
|
|
329
|
+
readonly one: (...args: Args<D, Q>) => Promise<Row<D, Q>>;
|
|
301
330
|
} : unknown);
|
|
302
331
|
/**
|
|
303
332
|
* One level of the {@link Groups}: a function for every name that completes
|
|
@@ -308,7 +337,8 @@ type Tree<D extends Definition, K extends string, P extends string = ""> = { rea
|
|
|
308
337
|
/**
|
|
309
338
|
* The queries of a contract as functions: `db.events.list({ day })`. A dot
|
|
310
339
|
* in a name is a level of the object. The query `events.list` is
|
|
311
|
-
* `db.events.list()`,
|
|
340
|
+
* `db.events.list()`, its first row is `db.events.list.first()`, and its
|
|
341
|
+
* one row is `db.events.list.one()`. The
|
|
312
342
|
* directory of a query file is its group and the file is its name. `Q` is
|
|
313
343
|
* the queries to offer: every query of the contract, or those of a role.
|
|
314
344
|
*/
|
|
@@ -337,6 +367,8 @@ export interface Validator<R> {
|
|
|
337
367
|
* an `InvalidRowError`. The output of the validator must fit the row the
|
|
338
368
|
* builder infers, so a validator that disagrees with the query fails to
|
|
339
369
|
* compile. `first` runs the query and takes its first row, or `null`.
|
|
370
|
+
* `one` takes its one row, and throws `RowCountError` when there is not
|
|
371
|
+
* exactly one.
|
|
340
372
|
*/
|
|
341
373
|
export interface Execute {
|
|
342
374
|
<O>(query: Compilable<O>): Promise<O[]>;
|
|
@@ -345,6 +377,10 @@ export interface Execute {
|
|
|
345
377
|
<O>(query: Compilable<O>): Promise<O | null>;
|
|
346
378
|
<O, R extends O>(query: Compilable<O>, row: Validator<R>): Promise<R | null>;
|
|
347
379
|
};
|
|
380
|
+
readonly one: {
|
|
381
|
+
<O>(query: Compilable<O>): Promise<O>;
|
|
382
|
+
<O, R extends O>(query: Compilable<O>, row: Validator<R>): Promise<R>;
|
|
383
|
+
};
|
|
348
384
|
}
|
|
349
385
|
/**
|
|
350
386
|
* Applies the grants of a build, and `declare` makes their roles alone.
|
|
@@ -390,11 +426,21 @@ export interface Service<D extends Definition, R extends Scoping<D> = undefined>
|
|
|
390
426
|
* group. Proves the row it returns and no other. Throws as `query` does.
|
|
391
427
|
*/
|
|
392
428
|
first: <Q extends Scope<D, R> & RowQuery<D>>(query: Q, ...args: Args<D, Q>) => Promise<Row<D, Q> | null>;
|
|
429
|
+
/**
|
|
430
|
+
* Runs a query with a column by name and returns its one row. The same
|
|
431
|
+
* call as `one` on the function under its group. For a query that always
|
|
432
|
+
* returns a row, such as an upsert with `RETURNING`. Throws
|
|
433
|
+
* `RowCountError` when the query does not return exactly one row, and
|
|
434
|
+
* otherwise as `query` does.
|
|
435
|
+
*/
|
|
436
|
+
one: <Q extends Scope<D, R> & RowQuery<D>>(query: Q, ...args: Args<D, Q>) => Promise<Row<D, Q>>;
|
|
393
437
|
/**
|
|
394
438
|
* Runs a built query and returns its rows, proven against a validator
|
|
395
|
-
* when one is given. `execute.first` returns the first row or `null
|
|
396
|
-
* {@link Execute}. Throws `QueryError`
|
|
397
|
-
* `InvalidRowError` when a row does not fit the
|
|
439
|
+
* when one is given. `execute.first` returns the first row or `null`, and
|
|
440
|
+
* `execute.one` the one row. See {@link Execute}. Throws `QueryError`
|
|
441
|
+
* when the driver fails, `InvalidRowError` when a row does not fit the
|
|
442
|
+
* validator, and, from `execute.one`, `RowCountError` when there is not
|
|
443
|
+
* exactly one row.
|
|
398
444
|
*/
|
|
399
445
|
execute: Execute;
|
|
400
446
|
/**
|
|
@@ -444,7 +490,8 @@ export type Bezoar<D extends Definition, R extends Scoping<D> = undefined> = Ser
|
|
|
444
490
|
* container holds. A call proves its arguments against the slots of the
|
|
445
491
|
* query, binds them by position, runs the SQL of the bundle, proves each row
|
|
446
492
|
* against the columns, and returns the rows, or the count when the query
|
|
447
|
-
* has no column. `first` returns the first row instead
|
|
493
|
+
* has no column. `first` returns the first row instead, and `one` the one
|
|
494
|
+
* row of a query that always has one. The service never
|
|
448
495
|
* connects: the caller brings an executor from a driver
|
|
449
496
|
* package, and a transaction is the same service over the executor of the
|
|
450
497
|
* transaction. A container with a role makes a service over the queries of
|
package/.dist/index.mjs
CHANGED
|
@@ -6,6 +6,7 @@ const RESERVED = [
|
|
|
6
6
|
"schema",
|
|
7
7
|
"query",
|
|
8
8
|
"first",
|
|
9
|
+
"one",
|
|
9
10
|
"execute",
|
|
10
11
|
"build",
|
|
11
12
|
"transaction",
|
|
@@ -85,12 +86,35 @@ var AmbiguousQueryError = class extends Error {
|
|
|
85
86
|
this.query = query;
|
|
86
87
|
}
|
|
87
88
|
};
|
|
89
|
+
const field = (cause, name) => {
|
|
90
|
+
if (typeof cause !== "object" || cause === null) return;
|
|
91
|
+
const value = cause[name];
|
|
92
|
+
return typeof value === "string" ? value : void 0;
|
|
93
|
+
};
|
|
88
94
|
var QueryError = class extends Error {
|
|
89
95
|
query;
|
|
96
|
+
code;
|
|
97
|
+
constraint;
|
|
98
|
+
table;
|
|
99
|
+
column;
|
|
90
100
|
constructor(query, cause) {
|
|
91
101
|
super(`@bezoar/core: ${JSON.stringify(query)} failed`, { cause });
|
|
92
102
|
this.name = "QueryError";
|
|
93
103
|
this.query = query;
|
|
104
|
+
this.code = field(cause, "code");
|
|
105
|
+
this.constraint = field(cause, "constraint");
|
|
106
|
+
this.table = field(cause, "table");
|
|
107
|
+
this.column = field(cause, "column");
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
var RowCountError = class extends Error {
|
|
111
|
+
query;
|
|
112
|
+
count;
|
|
113
|
+
constructor(query, count) {
|
|
114
|
+
super(`@bezoar/core: ${JSON.stringify(query)} returned ${count} rows where one was expected`);
|
|
115
|
+
this.name = "RowCountError";
|
|
116
|
+
this.query = query;
|
|
117
|
+
this.count = count;
|
|
94
118
|
}
|
|
95
119
|
};
|
|
96
120
|
var MigrationError = class extends Error {
|
|
@@ -323,6 +347,13 @@ const defineService = (definition, schema, config, options) => {
|
|
|
323
347
|
prove(query, 0, row);
|
|
324
348
|
return row;
|
|
325
349
|
}
|
|
350
|
+
async function only(query, args = {}) {
|
|
351
|
+
const outcome = await perform(query, args);
|
|
352
|
+
const row = outcome.rows[0];
|
|
353
|
+
if (row === void 0 || outcome.rows.length !== 1) throw new RowCountError(query, outcome.rows.length);
|
|
354
|
+
prove(query, 0, row);
|
|
355
|
+
return row;
|
|
356
|
+
}
|
|
326
357
|
const builder = new Kysely({ dialect: defineDialect(() => config.executor) });
|
|
327
358
|
const built = async (query) => {
|
|
328
359
|
const compiled = query.compile();
|
|
@@ -357,6 +388,12 @@ const defineService = (definition, schema, config, options) => {
|
|
|
357
388
|
if (row === void 0) return null;
|
|
358
389
|
return validator === void 0 ? row : proveWith(validator, 0, row);
|
|
359
390
|
}
|
|
391
|
+
async function executeOne(query, validator) {
|
|
392
|
+
const { rows } = await built(query);
|
|
393
|
+
const row = rows[0];
|
|
394
|
+
if (row === void 0 || rows.length !== 1) throw new RowCountError(BUILT, rows.length);
|
|
395
|
+
return validator === void 0 ? row : proveWith(validator, 0, row);
|
|
396
|
+
}
|
|
360
397
|
const transaction = (fn) => config.executor.transaction((executor) => fn(defineService(definition, schema, {
|
|
361
398
|
...config,
|
|
362
399
|
executor
|
|
@@ -366,7 +403,11 @@ const defineService = (definition, schema, config, options) => {
|
|
|
366
403
|
schema,
|
|
367
404
|
query: run,
|
|
368
405
|
first: take,
|
|
369
|
-
|
|
406
|
+
one: only,
|
|
407
|
+
execute: Object.assign(executeAll, {
|
|
408
|
+
first: executeFirst,
|
|
409
|
+
one: executeOne
|
|
410
|
+
}),
|
|
370
411
|
build: builder,
|
|
371
412
|
transaction,
|
|
372
413
|
migrate: (migrations) => migrate(config.executor, migrations),
|
|
@@ -375,7 +416,10 @@ const defineService = (definition, schema, config, options) => {
|
|
|
375
416
|
};
|
|
376
417
|
const groups = defineGroups(names, new Set(RESERVED), (query) => {
|
|
377
418
|
const method = (args) => run(query, args);
|
|
378
|
-
return returnsRows(query) ? Object.assign(method, {
|
|
419
|
+
return returnsRows(query) ? Object.assign(method, {
|
|
420
|
+
first: (args) => take(query, args),
|
|
421
|
+
one: (args) => only(query, args)
|
|
422
|
+
}) : method;
|
|
379
423
|
});
|
|
380
424
|
return Object.assign(service, groups);
|
|
381
425
|
};
|
|
@@ -384,4 +428,4 @@ const makeBezoar = (definition, config, options = {}) => {
|
|
|
384
428
|
reframe((issues) => new InvalidBundleError(issues), () => schema.assert.bundle(config.queries));
|
|
385
429
|
return defineService(definition, schema, config, options);
|
|
386
430
|
};
|
|
387
|
-
export { ACQUIRE, APPLIED, AmbiguousQueryError, BUILT, CREATE, DECLARE, EXISTS, ForbiddenQueryError, InvalidArgumentsError, InvalidBundleError, InvalidRowError, LOCK, MigrationDriftError, MigrationError, QueryError, RECORD, RESERVED, REVOKE, ROLES_LOCK, ReservedQueryError, RoleError, SCHEMAS, SeedError, TABLE, UnknownRoleError, declareRoles, makeBezoar, quote, reframe, roles };
|
|
431
|
+
export { ACQUIRE, APPLIED, AmbiguousQueryError, BUILT, CREATE, DECLARE, EXISTS, ForbiddenQueryError, InvalidArgumentsError, InvalidBundleError, InvalidRowError, LOCK, MigrationDriftError, MigrationError, QueryError, RECORD, RESERVED, REVOKE, ROLES_LOCK, ReservedQueryError, RoleError, RowCountError, SCHEMAS, SeedError, TABLE, UnknownRoleError, declareRoles, makeBezoar, quote, reframe, roles };
|
package/README.md
CHANGED
|
@@ -45,6 +45,11 @@ A query with a column also has `first`, which returns the first row or
|
|
|
45
45
|
`db.first("events.bySlug", { slug })` by name. A query with no column has
|
|
46
46
|
no `first`.
|
|
47
47
|
|
|
48
|
+
`one` is for a query that always returns a row, such as an upsert with
|
|
49
|
+
`RETURNING`: `db.events.save.one(args)`, or `db.one("events.save", args)`
|
|
50
|
+
by name. It returns the row, never `null`, and throws a `RowCountError`
|
|
51
|
+
with the count when the query does not return exactly one row.
|
|
52
|
+
|
|
48
53
|
## The container
|
|
49
54
|
|
|
50
55
|
The container holds the executor and the SQL bundle. The service reads the
|
|
@@ -72,8 +77,30 @@ A call proves its arguments against the slots of the query before anything
|
|
|
72
77
|
runs, and throws `InvalidArgumentsError` with every issue. The service binds
|
|
73
78
|
the arguments by slot position and runs the SQL of the bundle. A driver
|
|
74
79
|
failure is a `QueryError` that names the query and carries the driver's
|
|
75
|
-
error as `cause`.
|
|
76
|
-
|
|
80
|
+
error as `cause`. Each row is proven against the columns, and a row that
|
|
81
|
+
does not fit is an `InvalidRowError` with its index.
|
|
82
|
+
|
|
83
|
+
When Postgres refused the statement, the `QueryError` says why. `code` is
|
|
84
|
+
the SQLSTATE, and `constraint`, `table`, and `column` name what refused it,
|
|
85
|
+
each when Postgres reports one:
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
try {
|
|
89
|
+
await db.users.rename({ id, email });
|
|
90
|
+
} catch (error) {
|
|
91
|
+
if (error instanceof QueryError && error.constraint === "users_email_key") {
|
|
92
|
+
// another user has this email
|
|
93
|
+
}
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Every one is `undefined` for a failure that is not of Postgres, such as a
|
|
99
|
+
lost connection. The detail of the error stays on `cause`: it holds the
|
|
100
|
+
values of the row, which do not belong in a response. A statement that
|
|
101
|
+
fails inside a transaction aborts the transaction, so a call whose failure
|
|
102
|
+
the caller means to handle and go on from needs a transaction of its own
|
|
103
|
+
inside: `tx.transaction((inner) => ...)` is a savepoint.
|
|
77
104
|
|
|
78
105
|
The `onQuery` option receives a report after each call: the query name, the
|
|
79
106
|
SQL, the arguments, the time in milliseconds, and the count of the outcome.
|
|
@@ -114,7 +141,8 @@ them, and no row is proven. `execute` runs a built query through the service
|
|
|
114
141
|
instead: it reports the query through `onQuery`, and with a validator it
|
|
115
142
|
proves each row and returns the output of the validator. Any zod schema is a
|
|
116
143
|
validator. A validator whose output does not fit the row the builder infers
|
|
117
|
-
fails to compile. `execute.first` takes the first row, or `null
|
|
144
|
+
fails to compile. `execute.first` takes the first row, or `null`, and
|
|
145
|
+
`execute.one` takes the one row, or throws a `RowCountError`.
|
|
118
146
|
|
|
119
147
|
```ts
|
|
120
148
|
const rows = await db.execute(
|
|
@@ -235,7 +263,7 @@ tracked. Write a seed so that it can run again. A seed that fails is a
|
|
|
235
263
|
|
|
236
264
|
## Names
|
|
237
265
|
|
|
238
|
-
The members of the service are `config`, `schema`, `query`, `first`,
|
|
266
|
+
The members of the service are `config`, `schema`, `query`, `first`, `one`,
|
|
239
267
|
`execute`, `build`, `transaction`, `migrate`, `seed`, and `roles`. A query
|
|
240
268
|
group cannot take one of these names,
|
|
241
269
|
and a name cannot be both a query and a group. Either is an error at
|
|
@@ -248,7 +276,9 @@ exports the list.
|
|
|
248
276
|
- `InvalidArgumentsError`: the arguments of a call do not fit the slots.
|
|
249
277
|
- `InvalidRowError`: a row does not fit the columns, or the validator of a
|
|
250
278
|
built query.
|
|
251
|
-
- `QueryError`: the driver failed. `cause` is the driver's error
|
|
279
|
+
- `QueryError`: the driver failed. `cause` is the driver's error, and
|
|
280
|
+
`code`, `constraint`, `table`, and `column` are what Postgres reported.
|
|
281
|
+
- `RowCountError`: `one` ran a query that did not return exactly one row.
|
|
252
282
|
- `UnknownRoleError`: the container names a role the contract lacks.
|
|
253
283
|
- `ForbiddenQueryError`: a service scoped to a role was asked, by name, for
|
|
254
284
|
a query the role may not run.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bezoar/core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.8",
|
|
4
4
|
"description": "The bezoar runtime service: run queries by name, migrate, seed",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"dependencies": {
|
|
24
24
|
"kysely": "^0.29.7",
|
|
25
25
|
"objectively": "^0.3.0",
|
|
26
|
-
"@bezoar/schema": "0.0.
|
|
26
|
+
"@bezoar/schema": "0.0.8"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"zod": "^4.6.5"
|