@type-bridge/node 1.5.6 → 1.5.7
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 +63 -6
- package/dist/attribute.d.ts +22 -16
- package/dist/attribute.js +6 -0
- package/dist/flags.d.ts +46 -6
- package/dist/flags.js +33 -2
- package/dist/index.d.ts +33 -1
- package/dist/index.js +7 -2
- package/dist/model.d.ts +91 -10
- package/dist/model.js +165 -16
- package/dist/native.d.ts +106 -2
- package/dist/typed/index.d.ts +4 -0
- package/dist/typed/index.js +11 -0
- package/dist/typed/page.d.ts +13 -0
- package/dist/typed/page.js +35 -0
- package/dist/typed/query.d.ts +96 -0
- package/dist/typed/query.js +211 -0
- package/dist/typed/references.d.ts +192 -0
- package/dist/typed/references.js +363 -0
- package/dist/typed/results.d.ts +18 -0
- package/dist/typed/results.js +566 -0
- package/dist/typed/runtime-handles.d.ts +11 -0
- package/dist/typed/runtime-handles.js +33 -0
- package/dist/typed/session.d.ts +33 -0
- package/dist/typed/session.js +171 -0
- package/package.json +11 -3
- package/type_bridge_node.darwin-arm64.node +0 -0
- package/type_bridge_node.darwin-x64.node +0 -0
- package/type_bridge_node.linux-arm64-gnu.node +0 -0
- package/type_bridge_node.linux-x64-gnu.node +0 -0
- package/type_bridge_node.win32-arm64-msvc.node +0 -0
- package/type_bridge_node.win32-x64-msvc.node +0 -0
- package/dist/type-bridge-node-1.5.6.tgz +0 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Immutable public envelope for one validated distinct-root page. */
|
|
2
|
+
export interface Page<T> {
|
|
3
|
+
readonly items: readonly T[];
|
|
4
|
+
readonly offset: number;
|
|
5
|
+
readonly limit: number;
|
|
6
|
+
readonly total: bigint | undefined;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Preserve a native-validated page as defensively copied, frozen JavaScript
|
|
10
|
+
* data. This helper is intentionally not re-exported from the package subpath;
|
|
11
|
+
* typed query terminals are its only caller.
|
|
12
|
+
*/
|
|
13
|
+
export declare function pageFromValidatedResult<T>(items: readonly T[], offset: bigint, limit: bigint, total?: bigint): Page<T>;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.pageFromValidatedResult = pageFromValidatedResult;
|
|
4
|
+
/**
|
|
5
|
+
* Preserve a native-validated page as defensively copied, frozen JavaScript
|
|
6
|
+
* data. This helper is intentionally not re-exported from the package subpath;
|
|
7
|
+
* typed query terminals are its only caller.
|
|
8
|
+
*/
|
|
9
|
+
function pageFromValidatedResult(items, offset, limit, total) {
|
|
10
|
+
if (offset < 0n) {
|
|
11
|
+
throw new RangeError("page offset must be non-negative");
|
|
12
|
+
}
|
|
13
|
+
if (limit <= 0n) {
|
|
14
|
+
throw new RangeError("page limit must be positive");
|
|
15
|
+
}
|
|
16
|
+
if (total !== undefined && total < 0n) {
|
|
17
|
+
throw new RangeError("page total must be non-negative");
|
|
18
|
+
}
|
|
19
|
+
const publicOffset = pageWindowNumber(offset, "offset");
|
|
20
|
+
const publicLimit = pageWindowNumber(limit, "limit");
|
|
21
|
+
const page = {
|
|
22
|
+
items: Object.freeze(Array.from(items)),
|
|
23
|
+
offset: publicOffset,
|
|
24
|
+
limit: publicLimit,
|
|
25
|
+
total,
|
|
26
|
+
};
|
|
27
|
+
return Object.freeze(page);
|
|
28
|
+
}
|
|
29
|
+
function pageWindowNumber(value, name) {
|
|
30
|
+
const result = Number(value);
|
|
31
|
+
if (!Number.isSafeInteger(result)) {
|
|
32
|
+
throw new RangeError(`page ${name} must fit a JavaScript safe integer`);
|
|
33
|
+
}
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { RustDatabase, RustTransactionContext } from "../index.js";
|
|
2
|
+
import type { ModelOwnerToken } from "../model.js";
|
|
3
|
+
import { loadNative } from "../native.js";
|
|
4
|
+
import type { Page } from "./page.js";
|
|
5
|
+
import { type BoundVar, type Predicate, type QueryModelClass, type QueryOrder, type Selection } from "./references.js";
|
|
6
|
+
type NativeModule = ReturnType<typeof loadNative>;
|
|
7
|
+
type NativeRegistry = InstanceType<NativeModule["NodeDescriptorRegistry"]>;
|
|
8
|
+
type NativeSession = InstanceType<NativeModule["NodeMatchSessionHandle"]>;
|
|
9
|
+
type NativeQueryHandle = ReturnType<NativeSession["query"]>;
|
|
10
|
+
/** The canonical selected-output arity accepted by every typed binding. */
|
|
11
|
+
export type QuerySlotCount = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16;
|
|
12
|
+
/** A non-empty readonly output tuple within the canonical arity cap. */
|
|
13
|
+
export type QuerySlots = readonly [unknown, ...unknown[]] & {
|
|
14
|
+
readonly length: QuerySlotCount;
|
|
15
|
+
};
|
|
16
|
+
/** A non-empty readonly selection tuple within the canonical arity cap. */
|
|
17
|
+
export type QuerySelections = readonly [Selection<unknown>, ...Selection<unknown>[]] & {
|
|
18
|
+
readonly length: QuerySlotCount;
|
|
19
|
+
};
|
|
20
|
+
type SelectionOutput<Selected> = Selected extends Selection<infer Output> ? Output : never;
|
|
21
|
+
/** Map an inferred tuple of selections to its exact readonly output tuple. */
|
|
22
|
+
export type SelectedOutputs<Selections extends QuerySelections> = {
|
|
23
|
+
readonly [Index in keyof Selections]: SelectionOutput<Selections[Index]>;
|
|
24
|
+
} extends infer Outputs extends QuerySlots ? Outputs : never;
|
|
25
|
+
/** Scalarize one output slot and preserve the exact readonly tuple otherwise. */
|
|
26
|
+
export type QueryRow<Slots extends QuerySlots> = Slots extends readonly [infer Only] ? Only : Slots;
|
|
27
|
+
/** Exact readonly object output inferred from a named selection declaration. */
|
|
28
|
+
export type NamedQueryRow<Shape extends object> = Readonly<{
|
|
29
|
+
[Name in keyof Shape]: SelectionOutput<Shape[Name]>;
|
|
30
|
+
}>;
|
|
31
|
+
type UnionToIntersection<Union> = (Union extends unknown ? (value: Union) => void : never) extends (value: infer Intersection) => void ? Intersection : never;
|
|
32
|
+
type LastUnionMember<Union> = UnionToIntersection<Union extends unknown ? () => Union : never> extends () => infer Last ? Last : never;
|
|
33
|
+
type UnionToTuple<Union, Last = LastUnionMember<Union>> = [Union] extends [never] ? [] : [...UnionToTuple<Exclude<Union, Last>>, Last];
|
|
34
|
+
type NamedSelectionKeys<Shape extends object> = Extract<keyof Shape, string>;
|
|
35
|
+
/** @internal Exact-key constraint used by QuerySession.queryNamed. */
|
|
36
|
+
export type NamedSelectionInput<Shape extends object> = keyof Shape extends never ? never : string extends keyof Shape ? never : Exclude<keyof Shape, string> extends never ? "" extends keyof Shape ? never : UnionToTuple<NamedSelectionKeys<Shape>>["length"] extends QuerySlotCount ? Shape & Readonly<{
|
|
37
|
+
[Name in keyof Shape]: Selection<unknown>;
|
|
38
|
+
}> : never : never;
|
|
39
|
+
type IsCollection<Value> = Value extends readonly unknown[] ? true : false;
|
|
40
|
+
type IsModel<Value> = [ModelOwnerToken<Value>] extends [never] ? false : true;
|
|
41
|
+
type IsNamedRow<Value> = Value extends object ? IsCollection<Value> extends true ? false : IsModel<Value> extends true ? false : true : false;
|
|
42
|
+
type NamedRowContainsCollection<Row extends object> = true extends {
|
|
43
|
+
[Name in keyof Row]: IsCollection<Row[Name]>;
|
|
44
|
+
}[keyof Row] ? true : false;
|
|
45
|
+
type SlotContainsCollection<Slot> = IsCollection<Slot> extends true ? true : IsNamedRow<Slot> extends true ? NamedRowContainsCollection<Extract<Slot, object>> : false;
|
|
46
|
+
type QueryContainsCollection<Slots extends QuerySlots> = true extends {
|
|
47
|
+
[Index in keyof Slots]: SlotContainsCollection<Slots[Index]>;
|
|
48
|
+
}[number] ? true : false;
|
|
49
|
+
type PositionalSingularValues<Slots extends readonly unknown[], Found extends readonly unknown[] = []> = Slots extends readonly [infer First, ...infer Rest] ? IsCollection<First> extends true ? PositionalSingularValues<Rest, Found> : PositionalSingularValues<Rest, readonly [...Found, First]> : Found;
|
|
50
|
+
type NamedSingularKeys<Row extends object> = {
|
|
51
|
+
[Name in keyof Row]-?: IsCollection<Row[Name]> extends true ? never : Name;
|
|
52
|
+
}[keyof Row];
|
|
53
|
+
type IsUnion<Value, Whole = Value> = Value extends Whole ? [Whole] extends [Value] ? false : true : never;
|
|
54
|
+
type OnlyNamedSingular<Row extends object> = NamedSingularKeys<Row> extends infer Keys ? [Keys] extends [never] ? never : true extends IsUnion<Keys> ? never : Row[Extract<Keys, keyof Row>] : never;
|
|
55
|
+
type PageRootFor<Slots extends QuerySlots> = Slots extends readonly [infer Only] ? IsNamedRow<Only> extends true ? OnlyNamedSingular<Extract<Only, object>> : IsCollection<Only> extends true ? never : Only : PositionalSingularValues<Slots> extends readonly [infer Root] ? Root : never;
|
|
56
|
+
type SingularOutputsFor<Slots extends QuerySlots> = Slots extends readonly [infer Only] ? IsNamedRow<Only> extends true ? Extract<Only, object>[NamedSingularKeys<Extract<Only, object>>] : IsCollection<Only> extends true ? never : Only : PositionalSingularValues<Slots>[number];
|
|
57
|
+
/** Bounds and operation-local ordering for a distinct selected-row fetch. */
|
|
58
|
+
export interface RowsOptions {
|
|
59
|
+
readonly limit: number;
|
|
60
|
+
readonly offset?: number;
|
|
61
|
+
readonly orderBy?: readonly QueryOrder[];
|
|
62
|
+
}
|
|
63
|
+
/** Bounds and operation-local ordering for a distinct-root page. */
|
|
64
|
+
export interface PageOptions extends RowsOptions {
|
|
65
|
+
readonly includeTotal?: boolean;
|
|
66
|
+
}
|
|
67
|
+
export type QueryConnection = RustDatabase | RustTransactionContext;
|
|
68
|
+
/** @internal Runtime ownership passed by QuerySession without semantic plan state. */
|
|
69
|
+
export interface QueryRuntimeContext {
|
|
70
|
+
readonly registry: NativeRegistry;
|
|
71
|
+
readonly connection: QueryConnection | undefined;
|
|
72
|
+
readonly models: ReadonlyMap<string, QueryModelClass>;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* The single immutable positional/named typed-query facade.
|
|
76
|
+
*
|
|
77
|
+
* Runtime instances retain only an opaque native query handle and their
|
|
78
|
+
* session's connection context. Each builder delegates to Rust and returns a
|
|
79
|
+
* fresh frozen facade, leaving its ancestors unchanged.
|
|
80
|
+
*/
|
|
81
|
+
export declare class Query<Slots extends QuerySlots> {
|
|
82
|
+
private constructor();
|
|
83
|
+
match<const Models extends readonly [object, ...object[]]>(...bindings: {
|
|
84
|
+
readonly [Index in keyof Models]: BoundVar<Models[Index]>;
|
|
85
|
+
}): Query<Slots>;
|
|
86
|
+
where(...predicates: readonly [Predicate, ...Predicate[]]): Query<Slots>;
|
|
87
|
+
allowCrossJoin<Left extends object, Right extends object>(left: BoundVar<Left>, right: BoundVar<Right>): Query<Slots>;
|
|
88
|
+
one(this: QueryContainsCollection<Slots> extends true ? never : Query<Slots>): QueryRow<Slots>;
|
|
89
|
+
rows(this: QueryContainsCollection<Slots> extends true ? never : Query<Slots>, options: RowsOptions): readonly QueryRow<Slots>[];
|
|
90
|
+
pageBy(this: [PageRootFor<Slots>] extends [never] ? never : Query<Slots>, root: BoundVar<Extract<PageRootFor<Slots>, object>>, options: PageOptions): Page<QueryRow<Slots>>;
|
|
91
|
+
countBy<Root extends Extract<SingularOutputsFor<Slots>, object>>(root: BoundVar<Root>): bigint;
|
|
92
|
+
existsBy<Root extends Extract<SingularOutputsFor<Slots>, object>>(root: BoundVar<Root>): boolean;
|
|
93
|
+
}
|
|
94
|
+
/** @internal Construct a facade around one native persistent query handle. */
|
|
95
|
+
export declare function createQuery<Slots extends QuerySlots>(handle: NativeQueryHandle, context: QueryRuntimeContext): Query<Slots>;
|
|
96
|
+
export {};
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Query = void 0;
|
|
4
|
+
exports.createQuery = createQuery;
|
|
5
|
+
const native_js_1 = require("../native.js");
|
|
6
|
+
const runtime_handles_js_1 = require("./runtime-handles.js");
|
|
7
|
+
const references_js_1 = require("./references.js");
|
|
8
|
+
const results_js_1 = require("./results.js");
|
|
9
|
+
const queryStates = new WeakMap();
|
|
10
|
+
/**
|
|
11
|
+
* The single immutable positional/named typed-query facade.
|
|
12
|
+
*
|
|
13
|
+
* Runtime instances retain only an opaque native query handle and their
|
|
14
|
+
* session's connection context. Each builder delegates to Rust and returns a
|
|
15
|
+
* fresh frozen facade, leaving its ancestors unchanged.
|
|
16
|
+
*/
|
|
17
|
+
class Query {
|
|
18
|
+
constructor(handle, context) {
|
|
19
|
+
queryStates.set(this, { handle, context });
|
|
20
|
+
Object.freeze(this);
|
|
21
|
+
}
|
|
22
|
+
match(...bindings) {
|
|
23
|
+
const state = queryState(this);
|
|
24
|
+
let handle = state.handle;
|
|
25
|
+
for (const binding of bindings) {
|
|
26
|
+
const nativeBinding = (0, references_js_1.nativeBindingHandle)(binding);
|
|
27
|
+
handle = (0, references_js_1.nativeCall)(() => handle.addHidden(nativeBinding));
|
|
28
|
+
}
|
|
29
|
+
return createQuery(handle, state.context);
|
|
30
|
+
}
|
|
31
|
+
where(...predicates) {
|
|
32
|
+
const state = queryState(this);
|
|
33
|
+
let handle = state.handle;
|
|
34
|
+
for (const predicate of predicates) {
|
|
35
|
+
const nativePredicate = (0, references_js_1.nativePredicateHandle)(predicate);
|
|
36
|
+
handle = (0, references_js_1.nativeCall)(() => handle.wherePredicate(nativePredicate));
|
|
37
|
+
}
|
|
38
|
+
return createQuery(handle, state.context);
|
|
39
|
+
}
|
|
40
|
+
allowCrossJoin(left, right) {
|
|
41
|
+
const state = queryState(this);
|
|
42
|
+
const handle = (0, references_js_1.nativeCall)(() => state.handle.allowCrossJoin((0, references_js_1.nativeBindingHandle)(left), (0, references_js_1.nativeBindingHandle)(right)));
|
|
43
|
+
return createQuery(handle, state.context);
|
|
44
|
+
}
|
|
45
|
+
one() {
|
|
46
|
+
const state = queryState(this);
|
|
47
|
+
prepareDiagnostic(state, () => state.handle.fetchRowsDiagnostic([], 0n, 1n, "exactly_one"));
|
|
48
|
+
return terminalAdapter.one(state);
|
|
49
|
+
}
|
|
50
|
+
rows(options) {
|
|
51
|
+
const state = queryState(this);
|
|
52
|
+
const window = terminalWindow(options);
|
|
53
|
+
const orders = nativeOrders(options.orderBy);
|
|
54
|
+
prepareDiagnostic(state, () => state.handle.fetchRowsDiagnostic(Array.from(orders), window.offset, window.limit, "bounded_many"));
|
|
55
|
+
return terminalAdapter.rows(state, orders, window);
|
|
56
|
+
}
|
|
57
|
+
pageBy(root, options) {
|
|
58
|
+
const state = queryState(this);
|
|
59
|
+
const window = terminalWindow(options);
|
|
60
|
+
const orders = nativeOrders(options.orderBy);
|
|
61
|
+
const nativeRoot = (0, references_js_1.nativeBindingHandle)(root);
|
|
62
|
+
prepareDiagnostic(state, () => state.handle.pageByDiagnostic(nativeRoot, Array.from(orders), window.offset, window.limit, options.includeTotal ?? false));
|
|
63
|
+
return terminalAdapter.pageBy(state, nativeRoot, orders, window, options.includeTotal ?? false);
|
|
64
|
+
}
|
|
65
|
+
countBy(root) {
|
|
66
|
+
const state = queryState(this);
|
|
67
|
+
const nativeRoot = (0, references_js_1.nativeBindingHandle)(root);
|
|
68
|
+
prepareDiagnostic(state, () => state.handle.countByDiagnostic(nativeRoot));
|
|
69
|
+
return terminalAdapter.countBy(state, nativeRoot);
|
|
70
|
+
}
|
|
71
|
+
existsBy(root) {
|
|
72
|
+
const state = queryState(this);
|
|
73
|
+
const nativeRoot = (0, references_js_1.nativeBindingHandle)(root);
|
|
74
|
+
prepareDiagnostic(state, () => state.handle.existsByDiagnostic(nativeRoot));
|
|
75
|
+
return terminalAdapter.existsBy(state, nativeRoot);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
exports.Query = Query;
|
|
79
|
+
/** @internal Construct a facade around one native persistent query handle. */
|
|
80
|
+
function createQuery(handle, context) {
|
|
81
|
+
const Constructor = Query;
|
|
82
|
+
return new Constructor(handle, context);
|
|
83
|
+
}
|
|
84
|
+
/** One adapter boundary from immutable query facades to opaque native proofs. */
|
|
85
|
+
const terminalAdapter = Object.freeze({
|
|
86
|
+
one(state) {
|
|
87
|
+
const result = executeFetchRows(state, [], { offset: 0n, limit: 1n }, "exactly_one");
|
|
88
|
+
return (0, results_js_1.materializeValidatedOne)(state.handle, result, state.context.models);
|
|
89
|
+
},
|
|
90
|
+
rows(state, orders, window) {
|
|
91
|
+
const result = executeFetchRows(state, orders, window, "bounded_many");
|
|
92
|
+
return (0, results_js_1.materializeValidatedRows)(state.handle, result, state.context.models);
|
|
93
|
+
},
|
|
94
|
+
pageBy(state, root, orders, window, includeTotal) {
|
|
95
|
+
const result = executePageBy(state, root, orders, window, includeTotal);
|
|
96
|
+
return (0, results_js_1.materializeValidatedPage)(state.handle, result, state.context.models, window.offset, window.limit, includeTotal);
|
|
97
|
+
},
|
|
98
|
+
countBy(state, root) {
|
|
99
|
+
const result = executeCountBy(state, root);
|
|
100
|
+
return (0, results_js_1.materializeValidatedCount)(state.handle, result);
|
|
101
|
+
},
|
|
102
|
+
existsBy(state, root) {
|
|
103
|
+
const result = executeExistsBy(state, root);
|
|
104
|
+
return (0, results_js_1.materializeValidatedExists)(state.handle, result);
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
function queryState(query) {
|
|
108
|
+
const state = queryStates.get(query);
|
|
109
|
+
if (state === undefined) {
|
|
110
|
+
throw new TypeError("Query was not constructed by QuerySession.query");
|
|
111
|
+
}
|
|
112
|
+
return state;
|
|
113
|
+
}
|
|
114
|
+
function nativeOrders(orders) {
|
|
115
|
+
return orders?.map(references_js_1.nativeQueryOrderHandle) ?? [];
|
|
116
|
+
}
|
|
117
|
+
function terminalWindow(options) {
|
|
118
|
+
const limit = windowBound(options.limit, "limit", false);
|
|
119
|
+
const offset = windowBound(options.offset ?? 0, "offset", true);
|
|
120
|
+
if (!Number.isSafeInteger(options.limit + (options.offset ?? 0))) {
|
|
121
|
+
throw windowError("window_overflow", "window offset plus limit must remain a safe integer", options.limit + (options.offset ?? 0));
|
|
122
|
+
}
|
|
123
|
+
return { offset, limit };
|
|
124
|
+
}
|
|
125
|
+
function windowBound(value, name, allowZero) {
|
|
126
|
+
if (!Number.isSafeInteger(value) ||
|
|
127
|
+
value < 0 ||
|
|
128
|
+
(!allowZero && value === 0)) {
|
|
129
|
+
throw windowError(name === "limit" ? "invalid_window_limit" : "invalid_window_offset", name === "limit"
|
|
130
|
+
? "row and page limits must be positive safe integers"
|
|
131
|
+
: "row and page offsets must be non-negative safe integers", value);
|
|
132
|
+
}
|
|
133
|
+
return BigInt(value);
|
|
134
|
+
}
|
|
135
|
+
function windowError(code, message, value) {
|
|
136
|
+
return new references_js_1.TypedMatchError("invalid_plan", code, message, Object.freeze([{ kind: "operation" }]), Object.freeze({
|
|
137
|
+
value: Object.freeze({ kind: "text", value: String(value) }),
|
|
138
|
+
}));
|
|
139
|
+
}
|
|
140
|
+
function prepareDiagnostic(state, construct) {
|
|
141
|
+
const diagnostic = (0, references_js_1.nativeCall)(construct);
|
|
142
|
+
const native = (0, native_js_1.loadNative)();
|
|
143
|
+
(0, references_js_1.nativeCall)(() => native.revalidateMatchDiagnostic(state.context.registry, diagnostic));
|
|
144
|
+
}
|
|
145
|
+
function executionUnavailable(state) {
|
|
146
|
+
void state;
|
|
147
|
+
throw new references_js_1.TypedMatchError("invalid_plan", "execution_connection_required", "typed match execution requires a RustDatabase or RustTransactionContext", Object.freeze([{ kind: "operation" }]), Object.freeze({}));
|
|
148
|
+
}
|
|
149
|
+
function executePageBy(state, root, orders, window, includeTotal) {
|
|
150
|
+
const connection = state.context.connection;
|
|
151
|
+
if (connection === undefined) {
|
|
152
|
+
return executionUnavailable(state);
|
|
153
|
+
}
|
|
154
|
+
const database = (0, runtime_handles_js_1.rustDatabaseHandle)(connection);
|
|
155
|
+
if (database !== undefined) {
|
|
156
|
+
return (0, references_js_1.nativeCall)(() => state.handle.executePageByOwned(database, root, Array.from(orders), window.offset, window.limit, includeTotal));
|
|
157
|
+
}
|
|
158
|
+
const transaction = (0, runtime_handles_js_1.rustTransactionHandle)(connection);
|
|
159
|
+
if (transaction !== undefined) {
|
|
160
|
+
return (0, references_js_1.nativeCall)(() => state.handle.executePageByBorrowed(transaction, root, Array.from(orders), window.offset, window.limit, includeTotal));
|
|
161
|
+
}
|
|
162
|
+
return invalidQueryConnection();
|
|
163
|
+
}
|
|
164
|
+
function executeCountBy(state, root) {
|
|
165
|
+
const connection = state.context.connection;
|
|
166
|
+
if (connection === undefined) {
|
|
167
|
+
return executionUnavailable(state);
|
|
168
|
+
}
|
|
169
|
+
const database = (0, runtime_handles_js_1.rustDatabaseHandle)(connection);
|
|
170
|
+
if (database !== undefined) {
|
|
171
|
+
return (0, references_js_1.nativeCall)(() => state.handle.executeCountByOwned(database, root));
|
|
172
|
+
}
|
|
173
|
+
const transaction = (0, runtime_handles_js_1.rustTransactionHandle)(connection);
|
|
174
|
+
if (transaction !== undefined) {
|
|
175
|
+
return (0, references_js_1.nativeCall)(() => state.handle.executeCountByBorrowed(transaction, root));
|
|
176
|
+
}
|
|
177
|
+
return invalidQueryConnection();
|
|
178
|
+
}
|
|
179
|
+
function executeExistsBy(state, root) {
|
|
180
|
+
const connection = state.context.connection;
|
|
181
|
+
if (connection === undefined) {
|
|
182
|
+
return executionUnavailable(state);
|
|
183
|
+
}
|
|
184
|
+
const database = (0, runtime_handles_js_1.rustDatabaseHandle)(connection);
|
|
185
|
+
if (database !== undefined) {
|
|
186
|
+
return (0, references_js_1.nativeCall)(() => state.handle.executeExistsByOwned(database, root));
|
|
187
|
+
}
|
|
188
|
+
const transaction = (0, runtime_handles_js_1.rustTransactionHandle)(connection);
|
|
189
|
+
if (transaction !== undefined) {
|
|
190
|
+
return (0, references_js_1.nativeCall)(() => state.handle.executeExistsByBorrowed(transaction, root));
|
|
191
|
+
}
|
|
192
|
+
return invalidQueryConnection();
|
|
193
|
+
}
|
|
194
|
+
function invalidQueryConnection() {
|
|
195
|
+
throw new references_js_1.TypedMatchError("invalid_plan", "invalid_query_connection", "QuerySession connection was not constructed by the root Node package", Object.freeze([{ kind: "operation" }]), Object.freeze({}));
|
|
196
|
+
}
|
|
197
|
+
function executeFetchRows(state, orders, window, cardinality) {
|
|
198
|
+
const connection = state.context.connection;
|
|
199
|
+
if (connection === undefined) {
|
|
200
|
+
return executionUnavailable(state);
|
|
201
|
+
}
|
|
202
|
+
const database = (0, runtime_handles_js_1.rustDatabaseHandle)(connection);
|
|
203
|
+
if (database !== undefined) {
|
|
204
|
+
return (0, references_js_1.nativeCall)(() => state.handle.executeFetchRowsOwned(database, Array.from(orders), window.offset, window.limit, cardinality));
|
|
205
|
+
}
|
|
206
|
+
const transaction = (0, runtime_handles_js_1.rustTransactionHandle)(connection);
|
|
207
|
+
if (transaction !== undefined) {
|
|
208
|
+
return (0, references_js_1.nativeCall)(() => state.handle.executeFetchRowsBorrowed(transaction, Array.from(orders), window.offset, window.limit, cardinality));
|
|
209
|
+
}
|
|
210
|
+
return invalidQueryConnection();
|
|
211
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { Attribute, type AttributeBase } from "../attribute.js";
|
|
2
|
+
import type { EntityDescriptor, RelationDescriptor, ValueType } from "../index.js";
|
|
3
|
+
import { FieldSpec, ListFieldSpec, RoleSpec, type AttributeClass, type ModelOwnerToken, type SchemaSpec } from "../model.js";
|
|
4
|
+
import { loadNative } from "../native.js";
|
|
5
|
+
type NativeModule = ReturnType<typeof loadNative>;
|
|
6
|
+
type NativeSessionHandle = InstanceType<NativeModule["NodeMatchSessionHandle"]>;
|
|
7
|
+
export type NativeBindingHandle = ReturnType<NativeSessionHandle["exact"]>;
|
|
8
|
+
type NativeFieldHandle = ReturnType<NativeBindingHandle["field"]>;
|
|
9
|
+
type NativePredicateHandle = ReturnType<NativeFieldHandle["compareValueJson"]>;
|
|
10
|
+
type NativeOrderHandle = ReturnType<NativeFieldHandle["order"]>;
|
|
11
|
+
type NativeSelectionHandle = ReturnType<NativeBindingHandle["one"]>;
|
|
12
|
+
/** A model constructor accepted by the owner-aware query-reference layer. */
|
|
13
|
+
export type QueryModelClass<Model extends object = object, Schema extends Record<string, SchemaSpec> = Record<string, SchemaSpec>> = (new (values: never) => Model) & {
|
|
14
|
+
readonly typeName: string;
|
|
15
|
+
readonly schema: Schema;
|
|
16
|
+
descriptor(): EntityDescriptor | RelationDescriptor;
|
|
17
|
+
};
|
|
18
|
+
declare const selectionBrand: unique symbol;
|
|
19
|
+
declare const boundVarBrand: unique symbol;
|
|
20
|
+
declare const collectedBrand: unique symbol;
|
|
21
|
+
declare const fieldRefBrand: unique symbol;
|
|
22
|
+
declare const roleRefBrand: unique symbol;
|
|
23
|
+
declare const boundFieldBrand: unique symbol;
|
|
24
|
+
declare const boundRoleBrand: unique symbol;
|
|
25
|
+
declare const predicateBrand: unique symbol;
|
|
26
|
+
declare const queryOrderBrand: unique symbol;
|
|
27
|
+
/** Covariant output-only view of one native selected binding. */
|
|
28
|
+
export interface Selection<out Output> {
|
|
29
|
+
readonly [selectionBrand]: () => Output;
|
|
30
|
+
}
|
|
31
|
+
/** Owner-aware generated field reference; it has no public string constructor. */
|
|
32
|
+
export interface FieldRef<in out Owner extends object, out Attr extends AttributeClass> {
|
|
33
|
+
readonly [fieldRefBrand]: {
|
|
34
|
+
readonly owner: (value: Owner) => Owner;
|
|
35
|
+
readonly attribute: Attr;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/** Owner/player-aware generated relation-role reference. */
|
|
39
|
+
export interface RoleRef<in out RelationOwner extends object, in out Player extends object> {
|
|
40
|
+
readonly [roleRefBrand]: {
|
|
41
|
+
readonly owner: (value: RelationOwner) => RelationOwner;
|
|
42
|
+
readonly player: (value: Player) => Player;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/** One immutable native predicate handle. */
|
|
46
|
+
export interface Predicate {
|
|
47
|
+
readonly [predicateBrand]: true;
|
|
48
|
+
and(other: Predicate): Predicate;
|
|
49
|
+
or(other: Predicate): Predicate;
|
|
50
|
+
not(): Predicate;
|
|
51
|
+
}
|
|
52
|
+
/** One immutable native ordering handle. */
|
|
53
|
+
export interface QueryOrder {
|
|
54
|
+
readonly [queryOrderBrand]: true;
|
|
55
|
+
}
|
|
56
|
+
type AttributeCategory<Attr extends AttributeClass> = InstanceType<Attr> extends Attribute<infer _Value, string, infer Category> ? Category : never;
|
|
57
|
+
type SameCategory<Left extends AttributeClass, Right extends AttributeClass> = [
|
|
58
|
+
AttributeCategory<Left>
|
|
59
|
+
] extends [AttributeCategory<Right>] ? [AttributeCategory<Right>] extends [AttributeCategory<Left>] ? true : false : false;
|
|
60
|
+
type CompatibleField<Left extends AttributeClass, Right extends AttributeClass> = SameCategory<Left, Right> extends true ? BoundField<Right> : never;
|
|
61
|
+
interface EqualityOperations<Attr extends AttributeClass> {
|
|
62
|
+
eq(value: InstanceType<Attr>): Predicate;
|
|
63
|
+
eq<Other extends AttributeClass>(field: CompatibleField<Attr, Other>): Predicate;
|
|
64
|
+
eqField<Other extends AttributeClass>(field: CompatibleField<Attr, Other>): Predicate;
|
|
65
|
+
ne(value: InstanceType<Attr>): Predicate;
|
|
66
|
+
ne<Other extends AttributeClass>(field: CompatibleField<Attr, Other>): Predicate;
|
|
67
|
+
}
|
|
68
|
+
interface OrderedOperations<Attr extends AttributeClass> {
|
|
69
|
+
gt(value: InstanceType<Attr>): Predicate;
|
|
70
|
+
gt<Other extends AttributeClass>(field: CompatibleField<Attr, Other>): Predicate;
|
|
71
|
+
gte(value: InstanceType<Attr>): Predicate;
|
|
72
|
+
gte<Other extends AttributeClass>(field: CompatibleField<Attr, Other>): Predicate;
|
|
73
|
+
lt(value: InstanceType<Attr>): Predicate;
|
|
74
|
+
lt<Other extends AttributeClass>(field: CompatibleField<Attr, Other>): Predicate;
|
|
75
|
+
lte(value: InstanceType<Attr>): Predicate;
|
|
76
|
+
lte<Other extends AttributeClass>(field: CompatibleField<Attr, Other>): Predicate;
|
|
77
|
+
asc(missing?: "reject" | "first" | "last"): QueryOrder;
|
|
78
|
+
desc(missing?: "reject" | "first" | "last"): QueryOrder;
|
|
79
|
+
}
|
|
80
|
+
interface StringOperations {
|
|
81
|
+
contains(value: string): Predicate;
|
|
82
|
+
startsWith(value: string): Predicate;
|
|
83
|
+
endsWith(value: string): Predicate;
|
|
84
|
+
regex(value: string): Predicate;
|
|
85
|
+
}
|
|
86
|
+
interface BoundFieldIdentity<Attr extends AttributeClass> {
|
|
87
|
+
readonly [boundFieldBrand]: (attribute: Attr) => Attr;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* A field bound to one native variable. Boolean fields expose equality only;
|
|
91
|
+
* ordered categories add range/order methods and strings add match methods.
|
|
92
|
+
*/
|
|
93
|
+
export type BoundField<Attr extends AttributeClass> = BoundFieldIdentity<Attr> & EqualityOperations<Attr> & (AttributeCategory<Attr> extends "boolean" ? object : OrderedOperations<Attr>) & (AttributeCategory<Attr> extends "string" ? StringOperations : object);
|
|
94
|
+
type CompatibleOwner<Declared extends object, Actual extends object> = [
|
|
95
|
+
ModelOwnerToken<Declared>
|
|
96
|
+
] extends [never] ? false : ModelOwnerToken<Declared> extends ModelOwnerToken<Actual> ? true : false;
|
|
97
|
+
type CompatiblePlayer<Allowed extends object, Actual extends object> = Allowed extends object ? CompatibleOwner<Allowed, Actual> extends true ? Actual : never : never;
|
|
98
|
+
/** A role bound to one native relation variable. */
|
|
99
|
+
export interface BoundRole<in out Player extends object> {
|
|
100
|
+
readonly [boundRoleBrand]: (player: Player) => Player;
|
|
101
|
+
connects<Actual extends object>(player: BoundVar<Actual> & ([CompatiblePlayer<Player, Actual>] extends [never] ? never : object)): Predicate;
|
|
102
|
+
is<Actual extends object>(player: BoundVar<Actual> & ([CompatiblePlayer<Player, Actual>] extends [never] ? never : object)): Predicate;
|
|
103
|
+
}
|
|
104
|
+
/** A session-owned variable and singular model selection. */
|
|
105
|
+
export interface BoundVar<in out Model extends object> extends Selection<Model> {
|
|
106
|
+
readonly [boundVarBrand]: (model: Model) => Model;
|
|
107
|
+
field<Owner extends object, Attr extends AttributeClass>(reference: FieldRef<Owner, Attr> & (CompatibleOwner<Owner, Model> extends true ? object : never)): BoundField<Attr>;
|
|
108
|
+
role<Owner extends object, Player extends object>(reference: RoleRef<Owner, Player> & (CompatibleOwner<Owner, Model> extends true ? object : never)): BoundRole<Player>;
|
|
109
|
+
collect(): Collected<Model>;
|
|
110
|
+
}
|
|
111
|
+
/** A persistent collection selection over one native binding. */
|
|
112
|
+
export interface Collected<in out Model extends object> extends Selection<readonly Model[]> {
|
|
113
|
+
readonly [collectedBrand]: (model: Model) => Model;
|
|
114
|
+
distinct(distinct?: boolean): Collected<Model>;
|
|
115
|
+
orderBy(order: QueryOrder): Collected<Model>;
|
|
116
|
+
}
|
|
117
|
+
type FieldAttribute<Spec> = Spec extends FieldSpec<infer Attr, boolean> ? Attr : Spec extends ListFieldSpec<infer Attr, boolean> ? Attr : never;
|
|
118
|
+
type RolePlayer<Token> = Token extends new (values: never) => infer Player ? Player extends object ? Player : never : never;
|
|
119
|
+
type RolePlayers<Spec> = Spec extends RoleSpec<infer Players> ? RolePlayer<Players[number]> : never;
|
|
120
|
+
/** Exact owner-aware references derived from one model's typed schema. */
|
|
121
|
+
export type ModelReferences<Model extends QueryModelClass> = Readonly<{
|
|
122
|
+
fields: Readonly<{
|
|
123
|
+
[Key in keyof Model["schema"] as Model["schema"][Key] extends FieldSpec<AttributeClass, boolean> | ListFieldSpec<AttributeClass, boolean> ? Key : never]: FieldRef<InstanceType<Model>, FieldAttribute<Model["schema"][Key]>>;
|
|
124
|
+
}>;
|
|
125
|
+
roles: Readonly<{
|
|
126
|
+
[Key in keyof Model["schema"] as Model["schema"][Key] extends RoleSpec<infer _Players> ? Key : never]: RoleRef<InstanceType<Model>, RolePlayers<Model["schema"][Key]>>;
|
|
127
|
+
}>;
|
|
128
|
+
}>;
|
|
129
|
+
/**
|
|
130
|
+
* Derive field/role references from a model's typed schema without accepting a
|
|
131
|
+
* public field or role name. Generated packages can re-export these exact
|
|
132
|
+
* values as model-specific constants.
|
|
133
|
+
*/
|
|
134
|
+
export declare function references<Model extends QueryModelClass>(model: Model): ModelReferences<Model>;
|
|
135
|
+
/** Stable canonical match-error categories surfaced by Rust. */
|
|
136
|
+
export type TypedMatchErrorCategory = "invalid_plan" | "cardinality" | "unsupported_capability" | "stale_schema" | "resource_limit" | "provider" | "result_decode";
|
|
137
|
+
export type TypedMatchErrorPathSegment = Readonly<{
|
|
138
|
+
kind: "request" | "plan" | "operation" | "predicate" | "output" | "provider_evidence" | "result";
|
|
139
|
+
}> | Readonly<{
|
|
140
|
+
kind: "binding" | "role_edge" | "output_slot" | "index";
|
|
141
|
+
value: number;
|
|
142
|
+
}> | Readonly<{
|
|
143
|
+
kind: "output_name";
|
|
144
|
+
value: string;
|
|
145
|
+
}> | Readonly<{
|
|
146
|
+
kind: "field" | "role";
|
|
147
|
+
value: Readonly<{
|
|
148
|
+
owner: string;
|
|
149
|
+
name: string;
|
|
150
|
+
}>;
|
|
151
|
+
}>;
|
|
152
|
+
export type TypedMatchErrorDetail = Readonly<{
|
|
153
|
+
kind: "text";
|
|
154
|
+
value: string;
|
|
155
|
+
}> | Readonly<{
|
|
156
|
+
kind: "unsigned" | "signed";
|
|
157
|
+
value: number;
|
|
158
|
+
}> | Readonly<{
|
|
159
|
+
kind: "boolean";
|
|
160
|
+
value: boolean;
|
|
161
|
+
}> | Readonly<{
|
|
162
|
+
kind: "text_list";
|
|
163
|
+
value: readonly string[];
|
|
164
|
+
}>;
|
|
165
|
+
export declare class TypedMatchError extends Error {
|
|
166
|
+
readonly category: TypedMatchErrorCategory;
|
|
167
|
+
readonly code: string;
|
|
168
|
+
readonly path: readonly TypedMatchErrorPathSegment[];
|
|
169
|
+
readonly details: Readonly<Record<string, TypedMatchErrorDetail>>;
|
|
170
|
+
readonly name = "TypedMatchError";
|
|
171
|
+
constructor(category: TypedMatchErrorCategory, code: string, message: string, path: readonly TypedMatchErrorPathSegment[], details: Readonly<Record<string, TypedMatchErrorDetail>>);
|
|
172
|
+
}
|
|
173
|
+
/** @internal Invoke one native transition and preserve structured match errors. */
|
|
174
|
+
export declare function nativeCall<Result>(operation: () => Result): Result;
|
|
175
|
+
export declare class TypedReferenceError extends TypeError {
|
|
176
|
+
readonly name = "TypedReferenceError";
|
|
177
|
+
}
|
|
178
|
+
/** @internal Construct a public variable around one opaque native binding. */
|
|
179
|
+
export declare function createBoundVar<Model extends QueryModelClass>(model: Model, modelTypeName: string, handle: NativeBindingHandle): BoundVar<InstanceType<Model>>;
|
|
180
|
+
/** @internal Extract a fresh/native selection for the future Query facade. */
|
|
181
|
+
export declare function nativeSelectionHandle(selection: Selection<unknown>): NativeSelectionHandle;
|
|
182
|
+
/** @internal Extract the binding behind a session-owned variable. */
|
|
183
|
+
export declare function nativeBindingHandle<Model extends object>(variable: BoundVar<Model>): NativeBindingHandle;
|
|
184
|
+
/** @internal Extract a native predicate for the future Query facade. */
|
|
185
|
+
export declare function nativePredicateHandle(predicate: Predicate): NativePredicateHandle;
|
|
186
|
+
/** @internal Extract a native order for the future Query facade. */
|
|
187
|
+
export declare function nativeQueryOrderHandle(order: QueryOrder): NativeOrderHandle;
|
|
188
|
+
/** Wire category retained by a branded attribute constructor. */
|
|
189
|
+
export type AttributeValueCategory<Attr extends AttributeClass> = AttributeCategory<Attr> & ValueType;
|
|
190
|
+
/** Convenience constraint for generated attributes. */
|
|
191
|
+
export type TypedAttributeBase<Value, Brand extends string, Category extends ValueType> = AttributeBase<Value, Brand, Category>;
|
|
192
|
+
export {};
|