kern-orm 0.1.0 → 0.2.0
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.
|
@@ -1679,12 +1679,12 @@ declare class TransactionCoordinator {
|
|
|
1679
1679
|
/** Executes a script of statements without parameters where `execute` executes a statement. */
|
|
1680
1680
|
executeScript(text: string): Promise<void>;
|
|
1681
1681
|
/**
|
|
1682
|
-
* Runs `callback` in a transaction that commits when it resolves and rolls back when it rejects
|
|
1682
|
+
* Runs `callback` in a transaction that commits when it resolves and rolls back when it rejects, with the settings of `options`.
|
|
1683
1683
|
*
|
|
1684
|
-
* Inside a transaction, it runs `callback` in a savepoint nested in it.
|
|
1684
|
+
* Inside a transaction, it runs `callback` in a savepoint nested in it, which restores the settings it changed when it ends.
|
|
1685
1685
|
* A failed cleanup after an error rejects with a `SuppressedError` that keeps the original error in its `suppressed` chain.
|
|
1686
1686
|
*/
|
|
1687
|
-
transaction<T>(callback: () => Promise<T
|
|
1687
|
+
transaction<T>(callback: () => Promise<T>, options?: TransactionOptions): Promise<T>;
|
|
1688
1688
|
/**
|
|
1689
1689
|
* Runs `callback` in a transaction that always rolls back, on a coordinator created for tests.
|
|
1690
1690
|
*
|
|
@@ -1754,10 +1754,19 @@ interface Database extends AsyncDisposable {
|
|
|
1754
1754
|
*
|
|
1755
1755
|
* Inside an existing transaction, it starts a nested transaction.
|
|
1756
1756
|
*/
|
|
1757
|
-
transaction<T>(callback: () => Promise<T
|
|
1757
|
+
transaction<T>(callback: () => Promise<T>, options?: TransactionOptions): Promise<T>;
|
|
1758
1758
|
/** Rolls back the open test fixtures and closes the connections of the driver once the running transactions end. */
|
|
1759
1759
|
close(): Promise<void>;
|
|
1760
1760
|
}
|
|
1761
|
+
/** Options of `transaction` of a `Database` and `$transaction` of a `Db`. */
|
|
1762
|
+
interface TransactionOptions {
|
|
1763
|
+
/**
|
|
1764
|
+
* PostgreSQL schemas where the unqualified names of the statements of the transaction resolve, in their order, such as the schema of a tenant followed by the schema of extensions.
|
|
1765
|
+
*
|
|
1766
|
+
* It applies to the statements of the callback, including those of its nested transactions, until the transaction ends; a nested transaction then restores the search path of the enclosing transaction.
|
|
1767
|
+
*/
|
|
1768
|
+
searchPath?: readonly string[];
|
|
1769
|
+
}
|
|
1761
1770
|
/** Options of `createDatabase`. */
|
|
1762
1771
|
interface DatabaseOptions {
|
|
1763
1772
|
driver: Driver;
|
|
@@ -1792,6 +1801,14 @@ interface DbMutationState<T extends AnyTable, U extends UsedRelation> extends Mu
|
|
|
1792
1801
|
}
|
|
1793
1802
|
interface DbMethods<T extends Tables, R extends SchemaRelations> {
|
|
1794
1803
|
readonly $schema: Schema<T, R>;
|
|
1804
|
+
/** Database that the `Db` is bound to. */
|
|
1805
|
+
readonly $database: Database;
|
|
1806
|
+
/**
|
|
1807
|
+
* PostgreSQL schema of the tables, or `undefined` for a `Db` bound without `schemaName`.
|
|
1808
|
+
*
|
|
1809
|
+
* Reading it calls the function of `schemaName`, as compiling a query does, such as to resolve the schema of the current tenant.
|
|
1810
|
+
*/
|
|
1811
|
+
readonly $schemaName: string | undefined;
|
|
1795
1812
|
/**
|
|
1796
1813
|
* Compiles a query over the tables of the schema into SQL text and parameters.
|
|
1797
1814
|
*
|
|
@@ -1833,7 +1850,7 @@ interface DbMethods<T extends Tables, R extends SchemaRelations> {
|
|
|
1833
1850
|
*
|
|
1834
1851
|
* Inside an existing transaction, it starts a nested transaction.
|
|
1835
1852
|
*/
|
|
1836
|
-
$transaction<Result>(callback: () => Promise<Result
|
|
1853
|
+
$transaction<Result>(callback: () => Promise<Result>, options?: TransactionOptions): Promise<Result>;
|
|
1837
1854
|
}
|
|
1838
1855
|
/**
|
|
1839
1856
|
* `Db` of schema `S`, such as the type of a parameter of code shared by several schemas.
|
|
@@ -2088,4 +2105,4 @@ type TypedExpression<T = unknown> = AnyExpression<T> & {
|
|
|
2088
2105
|
interface BaseOperators {
|
|
2089
2106
|
isNull(): Expression<boolean>;
|
|
2090
2107
|
}
|
|
2091
|
-
export {
|
|
2108
|
+
export { many as $, BaseType as $n, Predicate as $t, SqlQuery as A, ColumnDefinitions as An, UpdateSet as At, Relation as B, defineTable as Bn, desc as Bt, createTestHelpers as C, ComparisonSignature as Cn, FilteredStateRows as Ct, DecodedFields as D, CompileOptions as Dn, from as Dt, AnySqlQuery as E, compare as En, TableOrQuery as Et, MutationQueryState as F, Selectable as Fn, OrderTerm as Ft, RelationKind as G, ResultRows as Gn, EqValue as Gt, RelationDeclarations as H, RequiredRelationError as Hn, KeyCheck as Ht, DeclaredRelations as I, Table as In, OrderTerms as It, SchemaRelations as J, DriverResult as Jn, WhereValues as Jt, RequiredMember as K, Connection as Kn, WhereCondition as Kt, MergeRelations as L, TableKeys as Ln, Ordering as Lt, AnyMutationQuery as M, ColumnShape as Mn, OrderColumns as Mt, InitialMutationState as N, ColumnValues as Nn, OrderDirection as Nt, FieldTypes as O, AnyTable as On, ColumnValue as Ot, MutationQuery as P, KeyColumns as Pn, OrderKey as Pt, UsedRelation as Q, AnyType as Qn, ConditionOperand as Qt, NoRelations as R, TableOptions as Rn, SelectedRefs as Rt, TestTransactionOptions as S, BooleanResult as Sn, ChangeState as St, SelectQueryKind as T, OperandValue as Tn, SelectQuery as Tt, RelationKey as U, ResultDecodeError as Un, NotAKey as Ut, RelationDeclaration as V, NotFoundError as Vn, orderBy as Vt, RelationKeys as W, ResultMode as Wn, PrimaryKeyValues as Wt, TableRelations as X, TransactionFailure as Xn, CombinedCondition as Xt, TableContext as Y, SqlStatement as Yn, defineFilter as Yt, UsableRelation as Z, createStatement as Zn, Condition as Zt, OpenedTestFixture as _, EqualityOperator as _n, GenericOperator as _r, Selection as _t, UntypedExpression as a, RelationRefs as an, TypeOutput as ar, SchemaTables as at, TestHelpers as b, InSignature as bn, defineGenericOperator as br, AnySelectQuery as bt, Db as c, OneValueRows as cn, AnyColumn as cr, AnyRecordQuery as ct, DbQueryState as d, ValueRows as dn, KeyKind as dr, SelectionState as dt, and as en, TextValue as er, one as et, Database as f, EmptyRecord as fn, ToColumn as fr, ExpressionInput as ft, TransactionCoordinator as g, SelectQueryState as gn, OperatorDefinitions as gr, NestedQuery as gt, createDatabase as h, QueryStateOf as hn, NoOperators as hr, MergeSelection as ht, TypedExpression as i, QueryContext as in, TypeOptions as ir, SchemaPartRelations as it, SqlQueryState as j, ColumnOperators as jn, UpdateValues as jt, InitialSqlQueryState as k, AnyTableKeys as kn, InsertValues as kt, DbMutationState as l, QueryResult as ln, Column as lr, RecordQuery as lt, TransactionOptions as m, QueryState as mn, MergeOperators as mr, HelperFields as mt, BaseOperators as n, or as nn, TypeInput as nr, Schema as nt, AnyDb as o, FilteredRows as on, TypeTransform as or, Tables as ot, DatabaseOptions as p, InitialQueryState as pn, BoundOperators as pr, ExpressionValue as pt, SchemaContext as q, Driver as qn, WhereFilter as qt, Expression as r, ColumnRefs as rn, TypeOperators as rr, SchemaPart as rt, BindOptions as s, OneRow as sn, defineType as sr, defineSchema as st, AnyExpression as t, not as tn, Type as tr, AnySchema as tt, DbOf as u, ResultByRows as un, ColumnAttributes as ur, RecordQueryState as ut, PendingTestFixture as v, EqualityOperators as vn, GenericOperatorImplementation as vr, SelectionResult as vt, AnyQuery as w, Operand as wn, ResultOf as wt, TestHooks as x, equalityOperators as xn, BoundSelectQuery as xt, TestFixture as y, HasEquality as yn, OperatorSignature as yr, SelectionUses as yt, OneRelationKeys as z, ToColumns as zn, asc as zt };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Cn as
|
|
1
|
+
import { Cn as ComparisonSignature, Ct as FilteredStateRows, Et as TableOrQuery, Gn as ResultRows, On as AnyTable, Q as UsedRelation, R as NoRelations, Sn as BooleanResult, St as ChangeState, T as SelectQueryKind, Tn as OperandValue, Tt as SelectQuery, Z as UsableRelation, _n as EqualityOperator, _r as GenericOperator, a as UntypedExpression, bn as InSignature, gn as SelectQueryState, gr as OperatorDefinitions, hn as QueryStateOf, ht as MergeSelection, mt as HelperFields, q as SchemaContext, r as Expression, sn as OneRow, t as AnyExpression, tr as Type, wn as Operand, yr as OperatorSignature } from "./expression-tYmWRuKR.mjs";
|
|
2
2
|
/** Operators of ordered types: those of `equalityOperators`, and `lt`, `lte`, `gt`, and `gte` comparing with operands of the expression type. */
|
|
3
3
|
declare const orderingOperators: {
|
|
4
4
|
eq: EqualityOperator<ComparisonSignature>;
|
package/dist/index.d.mts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { $ as
|
|
2
|
-
import { a as HelperEffect, c as textOperators, i as Helper, l as orderingOperators, n as param, o as defineHelper, r as sql, s as PatternSignature, t as SqlTemplate } from "./index-
|
|
3
|
-
export { type AnyColumn, type AnyDb, type AnyExpression, type AnyMutationQuery, type AnyQuery, type AnyRecordQuery, type AnySchema, type AnySelectQuery, type AnySqlQuery, type AnyTable, type AnyTableKeys, type AnyType, type BaseOperators, type BaseType, type BindOptions, type BooleanResult, type BoundOperators, type BoundSelectQuery, type Column, type ColumnAttributes, type ColumnDefinitions, type ColumnOperators, type ColumnRefs, type ColumnShape, type ColumnValue, type ColumnValues, type CombinedCondition, type ComparisonSignature, type CompileOptions, type Condition, type ConditionOperand, type Connection, type Database, type DatabaseOptions, type Db, type DbMutationState, type DbOf, type DbQueryState, type DeclaredRelations, type DecodedFields, type Driver, type DriverResult, type EmptyRecord, type EqValue, type EqualityOperator, type EqualityOperators, type Expression, type ExpressionInput, type ExpressionValue, type FieldTypes, type FilteredRows, type FilteredStateRows, type GenericOperator, type GenericOperatorImplementation, type HasEquality, type Helper, type HelperEffect, type InSignature, type InitialMutationState, type InitialQueryState, type InitialSqlQueryState, type InsertValues, type KeyCheck, type KeyColumns, type KeyKind, type MergeOperators, type MergeRelations, type MergeSelection, type MutationQuery, type MutationQueryState, type NestedQuery, type NoOperators, type NoRelations, type NotAKey, NotFoundError, type OneRelationKeys, type OneRow, type OneValueRows, type Operand, type OperandValue, type OperatorDefinitions, type OperatorSignature, type OrderColumns, type OrderDirection, type OrderKey, type OrderTerm, type OrderTerms, type Ordering, type PatternSignature, type Predicate, type PrimaryKeyValues, type QueryContext, type QueryResult, type QueryState, type QueryStateOf, type RecordQuery, type RecordQueryState, type Relation, type RelationDeclaration, type RelationDeclarations, type RelationKey, type RelationKeys, type RelationKind, type RelationRefs, type RequiredMember, RequiredRelationError, type ResultByRows, ResultDecodeError, type ResultMode, type ResultOf, type ResultRows, type Schema, type SchemaContext, type SchemaPart, type SchemaPartRelations, type SchemaRelations, type SchemaTables, type SelectQuery, type SelectQueryKind, type SelectQueryState, type Selectable, type SelectedRefs, type Selection, type SelectionResult, type SelectionState, type SelectionUses, type SqlQuery, type SqlQueryState, type SqlStatement, type SqlTemplate, type Table, type TableContext, type TableKeys, type TableOptions, type TableRelations, type Tables, type TextValue, type ToColumn, type ToColumns, type TransactionCoordinator, type TransactionFailure, type Type, type TypeInput, type TypeOperators, type TypeOptions, type TypeOutput, type TypeTransform, type TypedExpression, type UntypedExpression, type UpdateSet, type UpdateValues, type UsableRelation, type UsedRelation, type ValueRows, type WhereCondition, type WhereFilter, type WhereValues, and, asc, compare, createDatabase, createStatement, defineFilter, defineGenericOperator, defineHelper, defineSchema, defineTable, defineType, desc, equalityOperators, from, many, not, one, or, orderBy, orderingOperators, param, sql, textOperators };
|
|
1
|
+
import { $ as many, $n as BaseType, $t as Predicate, A as SqlQuery, An as ColumnDefinitions, At as UpdateSet, B as Relation, Bn as defineTable, Bt as desc, Cn as ComparisonSignature, Ct as FilteredStateRows, D as DecodedFields, Dn as CompileOptions, Dt as from, E as AnySqlQuery, En as compare, F as MutationQueryState, Fn as Selectable, Ft as OrderTerm, G as RelationKind, Gn as ResultRows, Gt as EqValue, H as RelationDeclarations, Hn as RequiredRelationError, Ht as KeyCheck, I as DeclaredRelations, In as Table, It as OrderTerms, J as SchemaRelations, Jn as DriverResult, Jt as WhereValues, K as RequiredMember, Kn as Connection, Kt as WhereCondition, L as MergeRelations, Ln as TableKeys, Lt as Ordering, M as AnyMutationQuery, Mn as ColumnShape, Mt as OrderColumns, N as InitialMutationState, Nn as ColumnValues, Nt as OrderDirection, O as FieldTypes, On as AnyTable, Ot as ColumnValue, P as MutationQuery, Pn as KeyColumns, Pt as OrderKey, Q as UsedRelation, Qn as AnyType, Qt as ConditionOperand, R as NoRelations, Rn as TableOptions, Rt as SelectedRefs, Sn as BooleanResult, T as SelectQueryKind, Tn as OperandValue, Tt as SelectQuery, U as RelationKey, Un as ResultDecodeError, Ut as NotAKey, V as RelationDeclaration, Vn as NotFoundError, Vt as orderBy, W as RelationKeys, Wn as ResultMode, Wt as PrimaryKeyValues, X as TableRelations, Xn as TransactionFailure, Xt as CombinedCondition, Y as TableContext, Yn as SqlStatement, Yt as defineFilter, Z as UsableRelation, Zn as createStatement, Zt as Condition, _n as EqualityOperator, _r as GenericOperator, _t as Selection, a as UntypedExpression, an as RelationRefs, ar as TypeOutput, at as SchemaTables, bn as InSignature, br as defineGenericOperator, bt as AnySelectQuery, c as Db, cn as OneValueRows, cr as AnyColumn, ct as AnyRecordQuery, d as DbQueryState, dn as ValueRows, dr as KeyKind, dt as SelectionState, en as and, er as TextValue, et as one, f as Database, fn as EmptyRecord, fr as ToColumn, ft as ExpressionInput, g as TransactionCoordinator, gn as SelectQueryState, gr as OperatorDefinitions, gt as NestedQuery, h as createDatabase, hn as QueryStateOf, hr as NoOperators, ht as MergeSelection, i as TypedExpression, in as QueryContext, ir as TypeOptions, it as SchemaPartRelations, j as SqlQueryState, jn as ColumnOperators, jt as UpdateValues, k as InitialSqlQueryState, kn as AnyTableKeys, kt as InsertValues, l as DbMutationState, ln as QueryResult, lr as Column, lt as RecordQuery, m as TransactionOptions, mn as QueryState, mr as MergeOperators, n as BaseOperators, nn as or, nr as TypeInput, nt as Schema, o as AnyDb, on as FilteredRows, or as TypeTransform, ot as Tables, p as DatabaseOptions, pn as InitialQueryState, pr as BoundOperators, pt as ExpressionValue, q as SchemaContext, qn as Driver, qt as WhereFilter, r as Expression, rn as ColumnRefs, rr as TypeOperators, rt as SchemaPart, s as BindOptions, sn as OneRow, sr as defineType, st as defineSchema, t as AnyExpression, tn as not, tr as Type, tt as AnySchema, u as DbOf, un as ResultByRows, ur as ColumnAttributes, ut as RecordQueryState, vn as EqualityOperators, vr as GenericOperatorImplementation, vt as SelectionResult, w as AnyQuery, wn as Operand, wt as ResultOf, xn as equalityOperators, xt as BoundSelectQuery, yn as HasEquality, yr as OperatorSignature, yt as SelectionUses, z as OneRelationKeys, zn as ToColumns, zt as asc } from "./expression-tYmWRuKR.mjs";
|
|
2
|
+
import { a as HelperEffect, c as textOperators, i as Helper, l as orderingOperators, n as param, o as defineHelper, r as sql, s as PatternSignature, t as SqlTemplate } from "./index-pj2AhTRY.mjs";
|
|
3
|
+
export { type AnyColumn, type AnyDb, type AnyExpression, type AnyMutationQuery, type AnyQuery, type AnyRecordQuery, type AnySchema, type AnySelectQuery, type AnySqlQuery, type AnyTable, type AnyTableKeys, type AnyType, type BaseOperators, type BaseType, type BindOptions, type BooleanResult, type BoundOperators, type BoundSelectQuery, type Column, type ColumnAttributes, type ColumnDefinitions, type ColumnOperators, type ColumnRefs, type ColumnShape, type ColumnValue, type ColumnValues, type CombinedCondition, type ComparisonSignature, type CompileOptions, type Condition, type ConditionOperand, type Connection, type Database, type DatabaseOptions, type Db, type DbMutationState, type DbOf, type DbQueryState, type DeclaredRelations, type DecodedFields, type Driver, type DriverResult, type EmptyRecord, type EqValue, type EqualityOperator, type EqualityOperators, type Expression, type ExpressionInput, type ExpressionValue, type FieldTypes, type FilteredRows, type FilteredStateRows, type GenericOperator, type GenericOperatorImplementation, type HasEquality, type Helper, type HelperEffect, type InSignature, type InitialMutationState, type InitialQueryState, type InitialSqlQueryState, type InsertValues, type KeyCheck, type KeyColumns, type KeyKind, type MergeOperators, type MergeRelations, type MergeSelection, type MutationQuery, type MutationQueryState, type NestedQuery, type NoOperators, type NoRelations, type NotAKey, NotFoundError, type OneRelationKeys, type OneRow, type OneValueRows, type Operand, type OperandValue, type OperatorDefinitions, type OperatorSignature, type OrderColumns, type OrderDirection, type OrderKey, type OrderTerm, type OrderTerms, type Ordering, type PatternSignature, type Predicate, type PrimaryKeyValues, type QueryContext, type QueryResult, type QueryState, type QueryStateOf, type RecordQuery, type RecordQueryState, type Relation, type RelationDeclaration, type RelationDeclarations, type RelationKey, type RelationKeys, type RelationKind, type RelationRefs, type RequiredMember, RequiredRelationError, type ResultByRows, ResultDecodeError, type ResultMode, type ResultOf, type ResultRows, type Schema, type SchemaContext, type SchemaPart, type SchemaPartRelations, type SchemaRelations, type SchemaTables, type SelectQuery, type SelectQueryKind, type SelectQueryState, type Selectable, type SelectedRefs, type Selection, type SelectionResult, type SelectionState, type SelectionUses, type SqlQuery, type SqlQueryState, type SqlStatement, type SqlTemplate, type Table, type TableContext, type TableKeys, type TableOptions, type TableRelations, type Tables, type TextValue, type ToColumn, type ToColumns, type TransactionCoordinator, type TransactionFailure, type TransactionOptions, type Type, type TypeInput, type TypeOperators, type TypeOptions, type TypeOutput, type TypeTransform, type TypedExpression, type UntypedExpression, type UpdateSet, type UpdateValues, type UsableRelation, type UsedRelation, type ValueRows, type WhereCondition, type WhereFilter, type WhereValues, and, asc, compare, createDatabase, createStatement, defineFilter, defineGenericOperator, defineHelper, defineSchema, defineTable, defineType, desc, equalityOperators, from, many, not, one, or, orderBy, orderingOperators, param, sql, textOperators };
|
package/dist/index.mjs
CHANGED
|
@@ -2753,12 +2753,17 @@ function bindSchema(schema, database, options = {}) {
|
|
|
2753
2753
|
const relations = schema[schemaRelations];
|
|
2754
2754
|
const definitions = collectDefinitions(relations);
|
|
2755
2755
|
const { schemaName } = options;
|
|
2756
|
+
const resolveSchemaName = () => typeof schemaName === "function" ? schemaName() : schemaName;
|
|
2756
2757
|
function compile(query) {
|
|
2757
2758
|
if (query.db !== void 0 && query.db !== db) throw new Error("Cannot run a query bound to another Db");
|
|
2758
|
-
return compileStatement(lowerStatement(query.node, definitions, db), { schemaName:
|
|
2759
|
+
return compileStatement(lowerStatement(query.node, definitions, db), { schemaName: resolveSchemaName() }, tables, db);
|
|
2759
2760
|
}
|
|
2760
2761
|
const methods = {
|
|
2761
2762
|
$schema: schema,
|
|
2763
|
+
$database: database,
|
|
2764
|
+
get $schemaName() {
|
|
2765
|
+
return resolveSchemaName();
|
|
2766
|
+
},
|
|
2762
2767
|
$compile(query) {
|
|
2763
2768
|
const { text, parameters } = compile(query);
|
|
2764
2769
|
return createStatement(text, parameters);
|
|
@@ -2775,7 +2780,7 @@ function bindSchema(schema, database, options = {}) {
|
|
|
2775
2780
|
if (parameters.length > 0) throw new Error("A script takes no values, since it has no parameters; interpolate only tables and sql fragments without values");
|
|
2776
2781
|
return database.script(text);
|
|
2777
2782
|
},
|
|
2778
|
-
$transaction: (callback) => database.transaction(callback)
|
|
2783
|
+
$transaction: (callback, options) => database.transaction(callback, options)
|
|
2779
2784
|
};
|
|
2780
2785
|
const contexts = createQueryContexts(Object.keys(schema.$tables), relations);
|
|
2781
2786
|
const db = defineEntries(methods, schema.$tables, (key) => bindQuery(startQuery(schema.$tables[key], contexts[key]), db));
|
|
@@ -2833,16 +2838,17 @@ var TransactionCoordinator = class {
|
|
|
2833
2838
|
return createUnavailableError(level, operation) ?? level.connection;
|
|
2834
2839
|
}
|
|
2835
2840
|
/**
|
|
2836
|
-
* Runs `callback` in a transaction that commits when it resolves and rolls back when it rejects
|
|
2841
|
+
* Runs `callback` in a transaction that commits when it resolves and rolls back when it rejects, with the settings of `options`.
|
|
2837
2842
|
*
|
|
2838
|
-
* Inside a transaction, it runs `callback` in a savepoint nested in it.
|
|
2843
|
+
* Inside a transaction, it runs `callback` in a savepoint nested in it, which restores the settings it changed when it ends.
|
|
2839
2844
|
* A failed cleanup after an error rejects with a `SuppressedError` that keeps the original error in its `suppressed` chain.
|
|
2840
2845
|
*/
|
|
2841
|
-
transaction(callback) {
|
|
2846
|
+
transaction(callback, options) {
|
|
2847
|
+
const settings = getLevelSettings(options);
|
|
2842
2848
|
const parent = this.#storage.getStore();
|
|
2843
|
-
if (parent) return this.#runNested(parent, callback, true);
|
|
2849
|
+
if (parent) return this.#runNested(parent, callback, true, void 0, settings);
|
|
2844
2850
|
if (this.#closed) return Promise.reject(createDatabaseClosedError("start a transaction"));
|
|
2845
|
-
return this.test ? Promise.reject(new Error(describeOutsideTest("start a transaction"))) : this.#runRoot(callback, true);
|
|
2851
|
+
return this.test ? Promise.reject(new Error(describeOutsideTest("start a transaction"))) : this.#runRoot(callback, true, void 0, settings);
|
|
2846
2852
|
}
|
|
2847
2853
|
/**
|
|
2848
2854
|
* Runs `callback` in a transaction that always rolls back, on a coordinator created for tests.
|
|
@@ -3081,7 +3087,7 @@ var TransactionCoordinator = class {
|
|
|
3081
3087
|
signal?.throwIfAborted();
|
|
3082
3088
|
return this.#startNested(fixture, signal);
|
|
3083
3089
|
}
|
|
3084
|
-
async #runRoot(callback, commits, signal) {
|
|
3090
|
+
async #runRoot(callback, commits, signal, settings) {
|
|
3085
3091
|
const connection = await (signal ? acquireUnlessAborted(this.driver, signal) : this.driver.acquire());
|
|
3086
3092
|
if (signal?.aborted) throw (await releaseConnection(connection, void 0, { error: signal.reason })).error;
|
|
3087
3093
|
const level = createLevel(connection, void 0);
|
|
@@ -3095,22 +3101,28 @@ var TransactionCoordinator = class {
|
|
|
3095
3101
|
failure: { error }
|
|
3096
3102
|
};
|
|
3097
3103
|
}
|
|
3104
|
+
if (!outcome && settings) try {
|
|
3105
|
+
await applySettings(level, settings);
|
|
3106
|
+
} catch (error) {
|
|
3107
|
+
outcome = await getEndedOutcome(this.#end(level, false), error);
|
|
3108
|
+
}
|
|
3098
3109
|
outcome ??= await this.#run(level, callback, commits, signal);
|
|
3099
3110
|
const rejection = await releaseConnection(connection, outcome.failure, outcome.rejection);
|
|
3100
3111
|
if (rejection) throw rejection.error;
|
|
3101
3112
|
return outcome.result;
|
|
3102
3113
|
}
|
|
3103
|
-
async #runNested(parent, callback, commits, signal) {
|
|
3114
|
+
async #runNested(parent, callback, commits, signal, settings) {
|
|
3104
3115
|
const unavailable = createUnavailableError(parent, "start a nested transaction");
|
|
3105
3116
|
if (unavailable) throw unavailable;
|
|
3106
|
-
return this.#runStarted(await this.#startNested(parent, signal), callback, commits, signal);
|
|
3117
|
+
return this.#runStarted(await this.#startNested(parent, signal, settings), callback, commits, signal);
|
|
3107
3118
|
}
|
|
3108
3119
|
/**
|
|
3109
|
-
* Reserves a nested level of `parent` and
|
|
3120
|
+
* Reserves a nested level of `parent`, sends its `SAVEPOINT`, and applies its `settings`, rejecting when one of them fails.
|
|
3110
3121
|
*
|
|
3122
|
+
* A level whose settings fail rolls back to its savepoint first.
|
|
3111
3123
|
* The failure suppresses the reason of `signal` when the signal aborted before it.
|
|
3112
3124
|
*/
|
|
3113
|
-
async #startNested(parent, signal) {
|
|
3125
|
+
async #startNested(parent, signal, settings) {
|
|
3114
3126
|
const level = createLevel(parent.connection, parent);
|
|
3115
3127
|
parent.child = level;
|
|
3116
3128
|
try {
|
|
@@ -3122,6 +3134,11 @@ var TransactionCoordinator = class {
|
|
|
3122
3134
|
}
|
|
3123
3135
|
throw suppressAbortReason(combineRejections({ error }, await level.cleanup).error, signal);
|
|
3124
3136
|
}
|
|
3137
|
+
if (settings) try {
|
|
3138
|
+
await applySettings(level, settings);
|
|
3139
|
+
} catch (error) {
|
|
3140
|
+
throw suppressAbortReason(combineRejections({ error }, await this.#end(level, false)).error, signal);
|
|
3141
|
+
}
|
|
3125
3142
|
return level;
|
|
3126
3143
|
}
|
|
3127
3144
|
/** Runs `callback` in a started level, returning its result or throwing its rejection. */
|
|
@@ -3202,9 +3219,50 @@ function createLevel(connection, parent) {
|
|
|
3202
3219
|
child: void 0,
|
|
3203
3220
|
rollbackOnly: void 0,
|
|
3204
3221
|
cleanup: void 0,
|
|
3205
|
-
endedBy: void 0
|
|
3222
|
+
endedBy: void 0,
|
|
3223
|
+
restore: void 0
|
|
3206
3224
|
};
|
|
3207
3225
|
}
|
|
3226
|
+
/** Settings of a level of `transaction` with `options`, or `undefined` when it has none. */
|
|
3227
|
+
function getLevelSettings(options) {
|
|
3228
|
+
return options?.searchPath ? [{
|
|
3229
|
+
name: "search_path",
|
|
3230
|
+
value: options.searchPath.map(quoteIdentifier).join(", ")
|
|
3231
|
+
}] : void 0;
|
|
3232
|
+
}
|
|
3233
|
+
/**
|
|
3234
|
+
* Applies `settings` to a level whose `BEGIN` or `SAVEPOINT` completed, unless its cleanup has started.
|
|
3235
|
+
*
|
|
3236
|
+
* A nested level first reads the values they replace, which it restores before its `RELEASE`: PostgreSQL keeps a setting of a released savepoint until the transaction ends, while `ROLLBACK TO` restores it itself.
|
|
3237
|
+
*/
|
|
3238
|
+
async function applySettings(level, settings) {
|
|
3239
|
+
if (level.cleanup) return;
|
|
3240
|
+
if (level.parent) {
|
|
3241
|
+
let text = "SELECT";
|
|
3242
|
+
const names = [];
|
|
3243
|
+
for (const { name } of settings) {
|
|
3244
|
+
names.push(name);
|
|
3245
|
+
text += `${names.length > 1 ? "," : ""} current_setting($${names.length})`;
|
|
3246
|
+
}
|
|
3247
|
+
const values = (await level.connection.execute(createStatement(text, names))).rows[0];
|
|
3248
|
+
level.restore = createSettingStatement(settings.map(({ name }, index) => ({
|
|
3249
|
+
name,
|
|
3250
|
+
value: values[index]
|
|
3251
|
+
})));
|
|
3252
|
+
if (level.cleanup) return;
|
|
3253
|
+
}
|
|
3254
|
+
await level.connection.execute(createSettingStatement(settings));
|
|
3255
|
+
}
|
|
3256
|
+
/** Creates the statement that sets `settings` until the transaction ends or rolls back to an earlier savepoint, as `SET LOCAL` does. */
|
|
3257
|
+
function createSettingStatement(settings) {
|
|
3258
|
+
let text = "SELECT";
|
|
3259
|
+
const parameters = [];
|
|
3260
|
+
for (const { name, value } of settings) {
|
|
3261
|
+
parameters.push(name, value);
|
|
3262
|
+
text += `${parameters.length > 2 ? "," : ""} set_config($${parameters.length - 1}, $${parameters.length}, true)`;
|
|
3263
|
+
}
|
|
3264
|
+
return createStatement(text, parameters);
|
|
3265
|
+
}
|
|
3208
3266
|
/** Name of the savepoint of a nested level; a level has at most one nested level, so the depth makes it unique. */
|
|
3209
3267
|
function getSavepointName(level) {
|
|
3210
3268
|
return `kern_${level.depth}`;
|
|
@@ -3315,11 +3373,16 @@ async function endTransaction(connection, commit) {
|
|
|
3315
3373
|
};
|
|
3316
3374
|
}
|
|
3317
3375
|
}
|
|
3318
|
-
/**
|
|
3376
|
+
/**
|
|
3377
|
+
* Ends the savepoint of a nested level, releasing it on commit and rolling back to it otherwise.
|
|
3378
|
+
*
|
|
3379
|
+
* Before a release, it restores the settings that the level changed; a rollback restores them itself.
|
|
3380
|
+
*/
|
|
3319
3381
|
async function endSavepoint(level, commit) {
|
|
3320
3382
|
const name = getSavepointName(level);
|
|
3321
3383
|
try {
|
|
3322
3384
|
if (!commit) await level.connection.execute(createStatement(`ROLLBACK TO SAVEPOINT ${name}`));
|
|
3385
|
+
else if (level.restore) await level.connection.execute(level.restore);
|
|
3323
3386
|
await level.connection.execute(createStatement(`RELEASE SAVEPOINT ${name}`));
|
|
3324
3387
|
return {};
|
|
3325
3388
|
} catch (error) {
|
|
@@ -3346,7 +3409,7 @@ function createDatabase({ driver, test }) {
|
|
|
3346
3409
|
query: (strings, ...values) => getTablelessDb().$query(strings, ...values),
|
|
3347
3410
|
script: (script, ...values) => typeof script === "string" ? coordinator.executeScript(script) : getTablelessDb().$script(script, ...values),
|
|
3348
3411
|
execute: (statement) => coordinator.execute(statement),
|
|
3349
|
-
transaction: (callback) => coordinator.transaction(callback),
|
|
3412
|
+
transaction: (callback, options) => coordinator.transaction(callback, options),
|
|
3350
3413
|
close: () => coordinator.close(),
|
|
3351
3414
|
[Symbol.asyncDispose]: () => coordinator.close()
|
|
3352
3415
|
};
|
package/dist/testing.d.mts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { C as createTestHelpers, S as TestTransactionOptions, _ as OpenedTestFixture, b as TestHelpers, v as PendingTestFixture, x as TestHooks, y as TestFixture } from "./expression-tYmWRuKR.mjs";
|
|
2
2
|
export { type OpenedTestFixture, type PendingTestFixture, type TestFixture, type TestHelpers, type TestHooks, type TestTransactionOptions, createTestHelpers };
|
package/dist/types.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { s as PatternSignature } from "./index-
|
|
1
|
+
import { $n as BaseType, Cn as ComparisonSignature, _n as EqualityOperator, _r as GenericOperator, bn as InSignature, gr as OperatorDefinitions, hr as NoOperators, mr as MergeOperators, t as AnyExpression, tr as Type, vn as EqualityOperators, yn as HasEquality } from "./expression-tYmWRuKR.mjs";
|
|
2
|
+
import { s as PatternSignature } from "./index-pj2AhTRY.mjs";
|
|
3
3
|
/**
|
|
4
4
|
* Creates a one-dimensional array type of `element` values.
|
|
5
5
|
*
|