@prisma-next/framework-components 0.5.0-dev.7 → 0.5.0-dev.9
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 +13 -0
- package/dist/codec-types-DQ1Agjom.d.mts +58 -0
- package/dist/codec-types-DQ1Agjom.d.mts.map +1 -0
- package/dist/codec.d.mts +1 -1
- package/dist/codec.mjs.map +1 -1
- package/dist/components.d.mts +1 -1
- package/dist/control.d.mts +2 -2
- package/dist/execution.d.mts +1 -1
- package/dist/{framework-components-EJXe-pum.d.mts → framework-components-DFZMi2h7.d.mts} +2 -2
- package/dist/{framework-components-EJXe-pum.d.mts.map → framework-components-DFZMi2h7.d.mts.map} +1 -1
- package/dist/runtime.d.mts +169 -22
- package/dist/runtime.d.mts.map +1 -1
- package/dist/runtime.mjs +117 -1
- package/dist/runtime.mjs.map +1 -1
- package/package.json +6 -6
- package/src/codec-types.ts +35 -17
- package/src/exports/runtime.ts +5 -1
- package/src/query-plan.ts +53 -0
- package/src/run-with-middleware.ts +77 -0
- package/src/runtime-core.ts +109 -0
- package/src/runtime-error.ts +16 -0
- package/src/runtime-middleware.ts +15 -11
- package/dist/codec-types-B58nCJiu.d.mts +0 -40
- package/dist/codec-types-B58nCJiu.d.mts.map +0 -1
package/README.md
CHANGED
|
@@ -10,19 +10,32 @@ Framework component types, authoring logic, control stack assembly, and emission
|
|
|
10
10
|
|
|
11
11
|
- **Component types** (`./components`): Base descriptor and instance interfaces for framework components (family, target, adapter, driver, extension), pack refs, and type renderer system
|
|
12
12
|
- **Authoring types** (`./authoring`): Declarative authoring contribution types, template resolution, and validation for type constructors and field presets
|
|
13
|
+
- **Codec base interface** (`./codec`): The cross-family `Codec` base type that SQL `Codec` and Mongo `MongoCodec` extend
|
|
13
14
|
- **Control stack** (`./control`): Assembly functions that combine component descriptors into a unified `ControlStack` with derived state (codec imports, renderers, authoring contributions)
|
|
14
15
|
- **Emission SPI** (`./emission`): Types for the emission pipeline — `TargetFamilyHook`, `ValidationContext`, `GenerateContractTypesOptions`, `TypeRenderEntry`, `TypeRenderer`, `ParameterizedCodecDescriptor`, and related types
|
|
15
16
|
- **Execution types** (`./execution`): Execution-plane stack and instance interfaces
|
|
17
|
+
- **Runtime SPI** (`./runtime`): Abstract `RuntimeCore<TPlan, TExec, TMiddleware>` base class, `RuntimeMiddleware` interface, and the canonical `runWithMiddleware` orchestrator helper. Family runtimes (`@prisma-next/sql-runtime`, `@prisma-next/mongo-runtime`) extend `RuntimeCore` directly per [ADR 204](../../../../../docs/architecture%20docs/adrs/ADR%20204%20-%20Single-tier%20runtime.md).
|
|
16
18
|
|
|
17
19
|
## Subpath exports
|
|
18
20
|
|
|
19
21
|
```typescript
|
|
20
22
|
import { ComponentMetadata, FamilyDescriptor, normalizeRenderer } from '@prisma-next/framework-components/components';
|
|
21
23
|
import { AuthoringContributions, instantiateAuthoringTypeConstructor } from '@prisma-next/framework-components/authoring';
|
|
24
|
+
import type { Codec } from '@prisma-next/framework-components/codec';
|
|
22
25
|
import { createControlStack, ControlStack } from '@prisma-next/framework-components/control';
|
|
23
26
|
import type { EmissionSpi } from '@prisma-next/framework-components/emission';
|
|
27
|
+
import { RuntimeCore, runWithMiddleware, type RuntimeMiddleware } from '@prisma-next/framework-components/runtime';
|
|
24
28
|
```
|
|
25
29
|
|
|
30
|
+
## `Codec` interface
|
|
31
|
+
|
|
32
|
+
The base `Codec` interface lands on the seam between **query-time** methods (per-row, IO-relevant) and **build-time** methods (per-contract-load):
|
|
33
|
+
|
|
34
|
+
- Query-time: `encode(value): Promise<TWire>` and `decode(wire): Promise<TInput>` are required and **Promise-returning at the public boundary**, regardless of whether the codec body is synchronous or asynchronous. Family factories (`codec()` for SQL, `mongoCodec()` for Mongo) accept either sync or async author functions and lift sync ones to Promise-shaped methods, so authors write whichever shape is natural per method without annotations.
|
|
35
|
+
- Build-time: `encodeJson`, `decodeJson`, and the optional `renderOutputType` are **synchronous** so `validateContract` and client construction stay synchronous.
|
|
36
|
+
|
|
37
|
+
There is no `runtime` / `kind` / equivalent async marker on the interface and no `TRuntime` generic. The runtime always awaits the query-time methods. See [ADR 204 — Single-Path Async Codec Runtime](../../../../../docs/architecture%20docs/adrs/ADR%20204%20-%20Single-Path%20Async%20Codec%20Runtime.md) for the full design.
|
|
38
|
+
|
|
26
39
|
## Why SPI types live here (dependency inversion)
|
|
27
40
|
|
|
28
41
|
This package sits in the **core** layer — below the tooling layer where family-specific emitters and control implementations live. SPI interfaces like `EmissionSpi` define the contract between framework orchestration code (control-plane emission, CLI) and family-specific implementations (SQL emitter, Mongo emitter).
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { JsonValue } from "@prisma-next/contract/types";
|
|
2
|
+
|
|
3
|
+
//#region src/codec-types.d.ts
|
|
4
|
+
type CodecTrait = 'equality' | 'order' | 'boolean' | 'numeric' | 'textual';
|
|
5
|
+
/**
|
|
6
|
+
* A codec is the contract between an application value and its on-wire and
|
|
7
|
+
* on-contract-disk representations.
|
|
8
|
+
*
|
|
9
|
+
* The author's mental model is two JS-side types — `TInput` (the
|
|
10
|
+
* application JS type) and `TWire` (the database driver wire format) —
|
|
11
|
+
* plus `JsonValue` for build-time contract artifacts. The codec translates
|
|
12
|
+
* `TInput` to `TWire` on writes and back on reads, and to/from `JsonValue`
|
|
13
|
+
* during contract emission and loading.
|
|
14
|
+
*
|
|
15
|
+
* Three representations participate:
|
|
16
|
+
* - **Input** (`TInput`): the JS type at the application boundary.
|
|
17
|
+
* - **Wire** (`TWire`): the format exchanged with the database driver.
|
|
18
|
+
* - **JSON** (`JsonValue`): a JSON-safe form used in contract artifacts.
|
|
19
|
+
*
|
|
20
|
+
* Codec methods split into two groups:
|
|
21
|
+
*
|
|
22
|
+
* - **Query-time** methods (`encode`, `decode`) run per row/parameter at the
|
|
23
|
+
* IO boundary; they are required and Promise-returning. The per-family
|
|
24
|
+
* codec factory accepts sync or async author functions and lifts sync
|
|
25
|
+
* ones to Promise-shaped methods automatically.
|
|
26
|
+
* - **Build-time** methods (`encodeJson`, `decodeJson`, `renderOutputType`)
|
|
27
|
+
* run when the contract is serialized, loaded, or when client types are
|
|
28
|
+
* emitted. They stay synchronous so contract validation and client
|
|
29
|
+
* construction are synchronous.
|
|
30
|
+
*
|
|
31
|
+
* Target-family codec interfaces extend this base with target-shaped
|
|
32
|
+
* metadata.
|
|
33
|
+
*/
|
|
34
|
+
interface Codec<Id extends string = string, TTraits extends readonly CodecTrait[] = readonly CodecTrait[], TWire = unknown, TInput = unknown> {
|
|
35
|
+
/** Unique codec identifier in `namespace/name@version` format (e.g. `pg/timestamptz@1`). */
|
|
36
|
+
readonly id: Id;
|
|
37
|
+
/** Database-native type names this codec handles (e.g. `['timestamptz']`). */
|
|
38
|
+
readonly targetTypes: readonly string[];
|
|
39
|
+
/** Semantic traits for operator gating (e.g. equality, order, numeric). */
|
|
40
|
+
readonly traits?: TTraits;
|
|
41
|
+
/** Converts a JS value to the wire format expected by the database driver. Always Promise-returning at the boundary. */
|
|
42
|
+
encode(value: TInput): Promise<TWire>;
|
|
43
|
+
/** Converts a wire value from the database driver into the JS application type. Always Promise-returning at the boundary. */
|
|
44
|
+
decode(wire: TWire): Promise<TInput>;
|
|
45
|
+
/** Converts a JS value to a JSON-safe representation for contract serialization. Synchronous; called during contract emission. */
|
|
46
|
+
encodeJson(value: TInput): JsonValue;
|
|
47
|
+
/** Converts a JSON representation back to the JS input type. Synchronous; called during contract loading via `validateContract`. */
|
|
48
|
+
decodeJson(json: JsonValue): TInput;
|
|
49
|
+
/** Produces the TypeScript output type expression for a field given its `typeParams`. Synchronous; used during contract.d.ts emission. */
|
|
50
|
+
renderOutputType?(typeParams: Record<string, unknown>): string | undefined;
|
|
51
|
+
}
|
|
52
|
+
interface CodecLookup {
|
|
53
|
+
get(id: string): Codec | undefined;
|
|
54
|
+
}
|
|
55
|
+
declare const emptyCodecLookup: CodecLookup;
|
|
56
|
+
//#endregion
|
|
57
|
+
export { emptyCodecLookup as i, CodecLookup as n, CodecTrait as r, Codec as t };
|
|
58
|
+
//# sourceMappingURL=codec-types-DQ1Agjom.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codec-types-DQ1Agjom.d.mts","names":[],"sources":["../src/codec-types.ts"],"sourcesContent":[],"mappings":";;;KAEY,UAAA;;AAAZ;AA+BA;;;;;;;;;;;;;;;;;AAwBA;AAIA;;;;;;;;;UA5BiB,2DAEU,wBAAwB;;eAKpC;;;;oBAIK;;gBAEJ,SAAS,QAAQ;;eAElB,QAAQ,QAAQ;;oBAEX,SAAS;;mBAEV,YAAY;;gCAEC;;UAGf,WAAA;mBACE;;cAGN,kBAAkB"}
|
package/dist/codec.d.mts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { i as emptyCodecLookup, n as CodecLookup, r as CodecTrait, t as Codec } from "./codec-types-
|
|
1
|
+
import { i as emptyCodecLookup, n as CodecLookup, r as CodecTrait, t as Codec } from "./codec-types-DQ1Agjom.mjs";
|
|
2
2
|
export { type Codec, type CodecLookup, type CodecTrait, emptyCodecLookup };
|
package/dist/codec.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"codec.mjs","names":["emptyCodecLookup: CodecLookup"],"sources":["../src/codec-types.ts"],"sourcesContent":["import type { JsonValue } from '@prisma-next/contract/types';\n\nexport type CodecTrait = 'equality' | 'order' | 'boolean' | 'numeric' | 'textual';\n\n/**\n *
|
|
1
|
+
{"version":3,"file":"codec.mjs","names":["emptyCodecLookup: CodecLookup"],"sources":["../src/codec-types.ts"],"sourcesContent":["import type { JsonValue } from '@prisma-next/contract/types';\n\nexport type CodecTrait = 'equality' | 'order' | 'boolean' | 'numeric' | 'textual';\n\n/**\n * A codec is the contract between an application value and its on-wire and\n * on-contract-disk representations.\n *\n * The author's mental model is two JS-side types — `TInput` (the\n * application JS type) and `TWire` (the database driver wire format) —\n * plus `JsonValue` for build-time contract artifacts. The codec translates\n * `TInput` to `TWire` on writes and back on reads, and to/from `JsonValue`\n * during contract emission and loading.\n *\n * Three representations participate:\n * - **Input** (`TInput`): the JS type at the application boundary.\n * - **Wire** (`TWire`): the format exchanged with the database driver.\n * - **JSON** (`JsonValue`): a JSON-safe form used in contract artifacts.\n *\n * Codec methods split into two groups:\n *\n * - **Query-time** methods (`encode`, `decode`) run per row/parameter at the\n * IO boundary; they are required and Promise-returning. The per-family\n * codec factory accepts sync or async author functions and lifts sync\n * ones to Promise-shaped methods automatically.\n * - **Build-time** methods (`encodeJson`, `decodeJson`, `renderOutputType`)\n * run when the contract is serialized, loaded, or when client types are\n * emitted. They stay synchronous so contract validation and client\n * construction are synchronous.\n *\n * Target-family codec interfaces extend this base with target-shaped\n * metadata.\n */\nexport interface Codec<\n Id extends string = string,\n TTraits extends readonly CodecTrait[] = readonly CodecTrait[],\n TWire = unknown,\n TInput = unknown,\n> {\n /** Unique codec identifier in `namespace/name@version` format (e.g. `pg/timestamptz@1`). */\n readonly id: Id;\n /** Database-native type names this codec handles (e.g. `['timestamptz']`). */\n readonly targetTypes: readonly string[];\n /** Semantic traits for operator gating (e.g. equality, order, numeric). */\n readonly traits?: TTraits;\n /** Converts a JS value to the wire format expected by the database driver. Always Promise-returning at the boundary. */\n encode(value: TInput): Promise<TWire>;\n /** Converts a wire value from the database driver into the JS application type. Always Promise-returning at the boundary. */\n decode(wire: TWire): Promise<TInput>;\n /** Converts a JS value to a JSON-safe representation for contract serialization. Synchronous; called during contract emission. */\n encodeJson(value: TInput): JsonValue;\n /** Converts a JSON representation back to the JS input type. Synchronous; called during contract loading via `validateContract`. */\n decodeJson(json: JsonValue): TInput;\n /** Produces the TypeScript output type expression for a field given its `typeParams`. Synchronous; used during contract.d.ts emission. */\n renderOutputType?(typeParams: Record<string, unknown>): string | undefined;\n}\n\nexport interface CodecLookup {\n get(id: string): Codec | undefined;\n}\n\nexport const emptyCodecLookup: CodecLookup = {\n get: () => undefined,\n};\n"],"mappings":";AA6DA,MAAaA,mBAAgC,EAC3C,WAAW,QACZ"}
|
package/dist/components.d.mts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { S as checkContractComponentRequirements, _ as PackRefBase, a as ComponentMetadata, b as TargetInstance, c as DriverDescriptor, d as ExtensionDescriptor, f as ExtensionInstance, g as FamilyPackRef, h as FamilyInstance, i as ComponentDescriptor, l as DriverInstance, m as FamilyDescriptor, n as AdapterInstance, o as ContractComponentRequirementsCheckInput, p as ExtensionPackRef, r as AdapterPackRef, s as ContractComponentRequirementsCheckResult, t as AdapterDescriptor, u as DriverPackRef, v as TargetBoundComponentDescriptor, x as TargetPackRef, y as TargetDescriptor } from "./framework-components-
|
|
1
|
+
import { S as checkContractComponentRequirements, _ as PackRefBase, a as ComponentMetadata, b as TargetInstance, c as DriverDescriptor, d as ExtensionDescriptor, f as ExtensionInstance, g as FamilyPackRef, h as FamilyInstance, i as ComponentDescriptor, l as DriverInstance, m as FamilyDescriptor, n as AdapterInstance, o as ContractComponentRequirementsCheckInput, p as ExtensionPackRef, r as AdapterPackRef, s as ContractComponentRequirementsCheckResult, t as AdapterDescriptor, u as DriverPackRef, v as TargetBoundComponentDescriptor, x as TargetPackRef, y as TargetDescriptor } from "./framework-components-DFZMi2h7.mjs";
|
|
2
2
|
export { type AdapterDescriptor, type AdapterInstance, type AdapterPackRef, type ComponentDescriptor, type ComponentMetadata, type ContractComponentRequirementsCheckInput, type ContractComponentRequirementsCheckResult, type DriverDescriptor, type DriverInstance, type DriverPackRef, type ExtensionDescriptor, type ExtensionInstance, type ExtensionPackRef, type FamilyDescriptor, type FamilyInstance, type FamilyPackRef, type PackRefBase, type TargetBoundComponentDescriptor, type TargetDescriptor, type TargetInstance, type TargetPackRef, checkContractComponentRequirements };
|
package/dist/control.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { a as AuthoringFieldNamespace, d as AuthoringTypeNamespace, i as AuthoringContributions } from "./framework-authoring-D1-JZ37B.mjs";
|
|
2
|
-
import { n as CodecLookup } from "./codec-types-
|
|
3
|
-
import { A as LoweredDefaultResult, C as ControlMutationDefaultEntry, D as DefaultFunctionLoweringHandler, E as DefaultFunctionLoweringContext, F as SourceSpan, M as MutationDefaultGeneratorDescriptor, N as ParsedDefaultFunctionCall, O as DefaultFunctionRegistry, P as SourceDiagnostic, T as ControlMutationDefaults, a as ComponentMetadata, b as TargetInstance, c as DriverDescriptor, d as ExtensionDescriptor, f as ExtensionInstance, h as FamilyInstance, j as LoweredDefaultValue, k as DefaultFunctionRegistryEntry, l as DriverInstance, m as FamilyDescriptor, n as AdapterInstance, t as AdapterDescriptor, v as TargetBoundComponentDescriptor, w as ControlMutationDefaultRegistry, y as TargetDescriptor } from "./framework-components-
|
|
2
|
+
import { n as CodecLookup } from "./codec-types-DQ1Agjom.mjs";
|
|
3
|
+
import { A as LoweredDefaultResult, C as ControlMutationDefaultEntry, D as DefaultFunctionLoweringHandler, E as DefaultFunctionLoweringContext, F as SourceSpan, M as MutationDefaultGeneratorDescriptor, N as ParsedDefaultFunctionCall, O as DefaultFunctionRegistry, P as SourceDiagnostic, T as ControlMutationDefaults, a as ComponentMetadata, b as TargetInstance, c as DriverDescriptor, d as ExtensionDescriptor, f as ExtensionInstance, h as FamilyInstance, j as LoweredDefaultValue, k as DefaultFunctionRegistryEntry, l as DriverInstance, m as FamilyDescriptor, n as AdapterInstance, t as AdapterDescriptor, v as TargetBoundComponentDescriptor, w as ControlMutationDefaultRegistry, y as TargetDescriptor } from "./framework-components-DFZMi2h7.mjs";
|
|
4
4
|
import { t as TypesImportSpec } from "./types-import-spec-C4sc7wbb.mjs";
|
|
5
5
|
import { t as EmissionSpi } from "./emission-types-BPAALJbF.mjs";
|
|
6
6
|
import { Contract, ContractMarkerRecord } from "@prisma-next/contract/types";
|
package/dist/execution.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { b as TargetInstance, c as DriverDescriptor, d as ExtensionDescriptor, f as ExtensionInstance, h as FamilyInstance, l as DriverInstance, m as FamilyDescriptor, n as AdapterInstance, t as AdapterDescriptor, y as TargetDescriptor } from "./framework-components-
|
|
1
|
+
import { b as TargetInstance, c as DriverDescriptor, d as ExtensionDescriptor, f as ExtensionInstance, h as FamilyInstance, l as DriverInstance, m as FamilyDescriptor, n as AdapterInstance, t as AdapterDescriptor, y as TargetDescriptor } from "./framework-components-DFZMi2h7.mjs";
|
|
2
2
|
|
|
3
3
|
//#region src/execution-instances.d.ts
|
|
4
4
|
interface RuntimeFamilyInstance<TFamilyId extends string> extends FamilyInstance<TFamilyId> {}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { i as AuthoringContributions } from "./framework-authoring-D1-JZ37B.mjs";
|
|
2
|
-
import { t as Codec } from "./codec-types-
|
|
2
|
+
import { t as Codec } from "./codec-types-DQ1Agjom.mjs";
|
|
3
3
|
import { t as TypesImportSpec } from "./types-import-spec-C4sc7wbb.mjs";
|
|
4
4
|
import { ColumnDefault, ExecutionMutationDefaultValue } from "@prisma-next/contract/types";
|
|
5
5
|
|
|
@@ -421,4 +421,4 @@ interface ExtensionInstance<TFamilyId extends string, TTargetId extends string>
|
|
|
421
421
|
}
|
|
422
422
|
//#endregion
|
|
423
423
|
export { LoweredDefaultResult as A, ControlMutationDefaultEntry as C, DefaultFunctionLoweringHandler as D, DefaultFunctionLoweringContext as E, SourceSpan as F, MutationDefaultGeneratorDescriptor as M, ParsedDefaultFunctionCall as N, DefaultFunctionRegistry as O, SourceDiagnostic as P, checkContractComponentRequirements as S, ControlMutationDefaults as T, PackRefBase as _, ComponentMetadata as a, TargetInstance as b, DriverDescriptor as c, ExtensionDescriptor as d, ExtensionInstance as f, FamilyPackRef as g, FamilyInstance as h, ComponentDescriptor as i, LoweredDefaultValue as j, DefaultFunctionRegistryEntry as k, DriverInstance as l, FamilyDescriptor as m, AdapterInstance as n, ContractComponentRequirementsCheckInput as o, ExtensionPackRef as p, AdapterPackRef as r, ContractComponentRequirementsCheckResult as s, AdapterDescriptor as t, DriverPackRef as u, TargetBoundComponentDescriptor as v, ControlMutationDefaultRegistry as w, TargetPackRef as x, TargetDescriptor as y };
|
|
424
|
-
//# sourceMappingURL=framework-components-
|
|
424
|
+
//# sourceMappingURL=framework-components-DFZMi2h7.d.mts.map
|
package/dist/{framework-components-EJXe-pum.d.mts.map → framework-components-DFZMi2h7.d.mts.map}
RENAMED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"framework-components-
|
|
1
|
+
{"version":3,"file":"framework-components-DFZMi2h7.d.mts","names":[],"sources":["../src/mutation-default-types.ts","../src/framework-components.ts"],"sourcesContent":[],"mappings":";;;;;;UAEU,cAAA;;;;;AAAA,UAMO,UAAA,CANO;EAMP,SAAA,KAAU,EACT,cAAA;EAID,SAAA,GAAA,EAHD,cAGiB;;AAKN,UALV,gBAAA,CAKU;EAAT,SAAA,IAAA,EAAA,MAAA;EAAQ,SAAA,OAAA,EAAA,MAAA;EAGhB,SAAA,QAAA,CAAA,EAAA,MAAuB;EAKhB,SAAA,IAAA,CAAA,EATC,UASD;EAOA,SAAA,IAAA,CAAA,EAfC,QAeD,CAfU,MAeV,CAAA,MAA8B,EAAA,OAAA,CAAA,CAAA;AAO/C;AAIA,UAvBU,uBAAA,CAuBsB;EAIpB,SAAA,GAAA,EAAA,MAAA;EACK,SAAA,IAAA,EA1BA,UA0BA;;AAEX,UAzBW,yBAAA,CAyBX;EAAoB,SAAA,IAAA,EAAA,MAAA;EAET,SAAA,GAAA,EAAA,MAAA;EAKL,SAAA,IAAA,EAAA,SA7Bc,uBA6BgC,EAAA;EAEzC,SAAA,IAAA,EA9BA,UA8BA;AAejB;AAEmB,UA5CF,8BAAA,CA4CE;EACG,SAAA,QAAA,EAAA,MAAA;EACd,SAAA,SAAA,EAAA,MAAA;EAAoB,SAAA,SAAA,EAAA,MAAA;EAIhB,SAAA,aAAA,CAAA,EAAA,MAAA;AAEZ;KA7CY,mBAAA;;yBAC2C;ACjCvD,CAAA,GAAi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J,8EACP;;qBAEW;;qBAGA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;UAiCJ,6EACP;;qBAEW;;qBAGA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;UA8BJ,gFACP;;qBAEW;;qBAGA;;;KAIT,qFACR,iBAAiB,WAAW,aAC5B,kBAAkB,WAAW,aAC7B,iBAAiB,WAAW,aAC5B,oBAAoB,WAAW;UAElB;qBACI;;UAGJ;qBACI;qBACA;;UAGJ;qBACI;qBACA;;UAGJ;qBACI;qBACA;;UAGJ;qBACI;qBACA"}
|
package/dist/runtime.d.mts
CHANGED
|
@@ -14,14 +14,54 @@ declare class AsyncIterableResult<Row> implements AsyncIterable<Row>, PromiseLik
|
|
|
14
14
|
then<TResult1 = Row[], TResult2 = never>(onfulfilled?: ((value: Row[]) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | undefined | null): PromiseLike<TResult1 | TResult2>;
|
|
15
15
|
}
|
|
16
16
|
//#endregion
|
|
17
|
-
//#region src/
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
17
|
+
//#region src/query-plan.d.ts
|
|
18
|
+
/**
|
|
19
|
+
* Family-agnostic plan marker.
|
|
20
|
+
*
|
|
21
|
+
* Carries only `meta` (the family-agnostic plan metadata) and the optional
|
|
22
|
+
* phantom `_row` parameter that lets type-level utilities recover the row
|
|
23
|
+
* type from a plan value. SQL and Mongo extend this marker with their own
|
|
24
|
+
* concrete shapes (`SqlQueryPlan`, `MongoQueryPlan`).
|
|
25
|
+
*
|
|
26
|
+
* `QueryPlan` is the *pre-lowering* marker — i.e. the surface a builder
|
|
27
|
+
* produces before family-specific lowering turns it into an executable
|
|
28
|
+
* plan (`ExecutionPlan`).
|
|
29
|
+
*/
|
|
30
|
+
interface QueryPlan<Row = unknown> {
|
|
31
|
+
readonly meta: PlanMeta;
|
|
32
|
+
/**
|
|
33
|
+
* Phantom property to carry the Row generic for type-level utilities.
|
|
34
|
+
* Not set at runtime; used only for `ResultType` extraction.
|
|
35
|
+
*/
|
|
36
|
+
readonly _row?: Row;
|
|
23
37
|
}
|
|
24
|
-
|
|
38
|
+
/**
|
|
39
|
+
* Family-agnostic execution-plan marker.
|
|
40
|
+
*
|
|
41
|
+
* Extends `QueryPlan` with no additional structural fields — the marker
|
|
42
|
+
* exists to nominally distinguish executable plans from pre-lowering plans
|
|
43
|
+
* in the type system. Family-specific execution plans (`SqlExecutionPlan`,
|
|
44
|
+
* `MongoExecutionPlan`) extend this marker with their concrete shapes
|
|
45
|
+
* (e.g. `sql + params` for SQL, `wireCommand` for Mongo).
|
|
46
|
+
*/
|
|
47
|
+
interface ExecutionPlan<Row = unknown> extends QueryPlan<Row> {}
|
|
48
|
+
/**
|
|
49
|
+
* Extracts the `Row` type from a plan via the phantom `_row` property.
|
|
50
|
+
*
|
|
51
|
+
* Works with any plan that extends `QueryPlan<Row>` — including
|
|
52
|
+
* `ExecutionPlan<Row>`, `SqlQueryPlan<Row>`, `SqlExecutionPlan<Row>`,
|
|
53
|
+
* `MongoQueryPlan<Row>`, and `MongoExecutionPlan<Row>`.
|
|
54
|
+
*
|
|
55
|
+
* The `_row` property must be present in the plan's static type for the
|
|
56
|
+
* conditional to bind `R`; objects whose type lacks `_row` resolve to
|
|
57
|
+
* `never`. Without the `keyof` guard, `extends { _row?: infer R }` would
|
|
58
|
+
* silently match any object and infer `unknown`.
|
|
59
|
+
*
|
|
60
|
+
* Example: `type Row = ResultType<typeof plan>`.
|
|
61
|
+
*/
|
|
62
|
+
type ResultType<P> = '_row' extends keyof P ? P extends {
|
|
63
|
+
readonly _row?: infer R;
|
|
64
|
+
} ? R : never : never;
|
|
25
65
|
//#endregion
|
|
26
66
|
//#region src/runtime-middleware.d.ts
|
|
27
67
|
interface RuntimeLog {
|
|
@@ -41,19 +81,21 @@ interface AfterExecuteResult {
|
|
|
41
81
|
readonly latencyMs: number;
|
|
42
82
|
readonly completed: boolean;
|
|
43
83
|
}
|
|
44
|
-
|
|
84
|
+
/**
|
|
85
|
+
* Family-agnostic middleware SPI parameterized over the plan marker.
|
|
86
|
+
*
|
|
87
|
+
* `TPlan` defaults to the framework `QueryPlan` marker so a generic
|
|
88
|
+
* middleware (e.g. cross-family telemetry) can be authored without
|
|
89
|
+
* naming a family. Family-specific middleware (`SqlMiddleware`,
|
|
90
|
+
* `MongoMiddleware`) narrow `TPlan` to their concrete plan type.
|
|
91
|
+
*/
|
|
92
|
+
interface RuntimeMiddleware<TPlan extends QueryPlan = QueryPlan> {
|
|
45
93
|
readonly name: string;
|
|
46
94
|
readonly familyId?: string;
|
|
47
95
|
readonly targetId?: string;
|
|
48
|
-
beforeExecute?(plan:
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
onRow?(row: Record<string, unknown>, plan: {
|
|
52
|
-
readonly meta: PlanMeta;
|
|
53
|
-
}, ctx: RuntimeMiddlewareContext): Promise<void>;
|
|
54
|
-
afterExecute?(plan: {
|
|
55
|
-
readonly meta: PlanMeta;
|
|
56
|
-
}, result: AfterExecuteResult, ctx: RuntimeMiddlewareContext): Promise<void>;
|
|
96
|
+
beforeExecute?(plan: TPlan, ctx: RuntimeMiddlewareContext): Promise<void>;
|
|
97
|
+
onRow?(row: Record<string, unknown>, plan: TPlan, ctx: RuntimeMiddlewareContext): Promise<void>;
|
|
98
|
+
afterExecute?(plan: TPlan, result: AfterExecuteResult, ctx: RuntimeMiddlewareContext): Promise<void>;
|
|
57
99
|
}
|
|
58
100
|
/**
|
|
59
101
|
* Cross-family SPI for any runtime that can execute plans and be shut down.
|
|
@@ -61,11 +103,9 @@ interface RuntimeMiddleware {
|
|
|
61
103
|
* Mongo structurally (due to its phantom Row parameter using a unique symbol).
|
|
62
104
|
*
|
|
63
105
|
* The `_row` intersection on `execute` connects the `Row` type parameter to the
|
|
64
|
-
* plan, mirroring how `
|
|
106
|
+
* plan, mirroring how `QueryPlan<Row>` carries a phantom `_row?: Row`.
|
|
65
107
|
*/
|
|
66
|
-
interface RuntimeExecutor<TPlan extends {
|
|
67
|
-
readonly meta: PlanMeta;
|
|
68
|
-
}> {
|
|
108
|
+
interface RuntimeExecutor<TPlan extends QueryPlan> {
|
|
69
109
|
execute<Row>(plan: TPlan & {
|
|
70
110
|
readonly _row?: Row;
|
|
71
111
|
}): AsyncIterableResult<Row>;
|
|
@@ -73,5 +113,112 @@ interface RuntimeExecutor<TPlan extends {
|
|
|
73
113
|
}
|
|
74
114
|
declare function checkMiddlewareCompatibility(middleware: RuntimeMiddleware, runtimeFamilyId: string, runtimeTargetId: string): void;
|
|
75
115
|
//#endregion
|
|
76
|
-
|
|
116
|
+
//#region src/run-with-middleware.d.ts
|
|
117
|
+
/**
|
|
118
|
+
* Drives a single execution of `runDriver()` through the middleware lifecycle.
|
|
119
|
+
*
|
|
120
|
+
* Lifecycle, in order:
|
|
121
|
+
* 1. For each middleware in registration order: `beforeExecute(exec, ctx)`.
|
|
122
|
+
* 2. For each row yielded by `runDriver()`: for each middleware in registration
|
|
123
|
+
* order: `onRow(row, exec, ctx)`; then yield the row to the consumer.
|
|
124
|
+
* 3. On successful completion: for each middleware in registration order:
|
|
125
|
+
* `afterExecute(exec, { rowCount, latencyMs, completed: true }, ctx)`.
|
|
126
|
+
* 4. On any error thrown by the driver loop: for each middleware in
|
|
127
|
+
* registration order: `afterExecute(exec, { rowCount, latencyMs,
|
|
128
|
+
* completed: false }, ctx)`. Errors thrown by `afterExecute` during the
|
|
129
|
+
* error path are swallowed so they do not mask the original driver error.
|
|
130
|
+
* The original error is then rethrown.
|
|
131
|
+
*
|
|
132
|
+
* This helper is the single canonical implementation of the middleware
|
|
133
|
+
* orchestration loop; family runtimes should not reimplement it.
|
|
134
|
+
*/
|
|
135
|
+
declare function runWithMiddleware<TExec extends ExecutionPlan, Row>(exec: TExec, middleware: ReadonlyArray<RuntimeMiddleware<TExec>>, ctx: RuntimeMiddlewareContext, runDriver: () => AsyncIterable<Row>): AsyncIterableResult<Row>;
|
|
136
|
+
//#endregion
|
|
137
|
+
//#region src/runtime-core.d.ts
|
|
138
|
+
/**
|
|
139
|
+
* Constructor options shared by every concrete `RuntimeCore` subclass.
|
|
140
|
+
*
|
|
141
|
+
* Family runtimes typically build the middleware list and the
|
|
142
|
+
* `RuntimeMiddlewareContext` themselves (running compatibility checks,
|
|
143
|
+
* narrowing the context's `contract` field, etc.) before calling `super`.
|
|
144
|
+
*/
|
|
145
|
+
interface RuntimeCoreOptions<TMiddleware extends RuntimeMiddleware<ExecutionPlan>> {
|
|
146
|
+
readonly middleware: ReadonlyArray<TMiddleware>;
|
|
147
|
+
readonly ctx: RuntimeMiddlewareContext;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Family-agnostic abstract runtime base.
|
|
151
|
+
*
|
|
152
|
+
* Defines the entire `execute(plan)` template in one place:
|
|
153
|
+
*
|
|
154
|
+
* 1. `runBeforeCompile(plan)` — concrete; defaults to identity. SQL overrides
|
|
155
|
+
* this to run its `beforeCompile` middleware-hook chain.
|
|
156
|
+
* 2. `lower(plan)` — abstract. Each family produces its `*ExecutionPlan`
|
|
157
|
+
* (SQL via `lowerSqlPlan`, Mongo via `adapter.lower`).
|
|
158
|
+
* 3. `runWithMiddleware(exec, this.middleware, this.ctx,
|
|
159
|
+
* () => runDriver(exec))` — concrete; lifts the middleware lifecycle
|
|
160
|
+
* out of the family runtimes into the canonical helper.
|
|
161
|
+
*
|
|
162
|
+
* Concrete subclasses must implement `lower`, `runDriver`, and `close`.
|
|
163
|
+
*
|
|
164
|
+
* The class is generic over:
|
|
165
|
+
* - `TPlan` — the family's pre-lowering plan type.
|
|
166
|
+
* - `TExec` — the family's post-lowering (executable) plan type.
|
|
167
|
+
* - `TMiddleware` — the family's middleware type. Constrained to
|
|
168
|
+
* `RuntimeMiddleware<TExec>` because `runWithMiddleware` invokes the
|
|
169
|
+
* `beforeExecute` / `onRow` / `afterExecute` hooks with the lowered
|
|
170
|
+
* `TExec`. (The spec/plan wording "RuntimeMiddleware<TPlan>" is
|
|
171
|
+
* tightened to `<TExec>` here so the helper call typechecks; the
|
|
172
|
+
* intent is unchanged — middleware sees the post-lowering plan.)
|
|
173
|
+
*/
|
|
174
|
+
declare abstract class RuntimeCore<TPlan extends QueryPlan, TExec extends ExecutionPlan, TMiddleware extends RuntimeMiddleware<TExec>> implements RuntimeExecutor<TPlan> {
|
|
175
|
+
protected readonly middleware: ReadonlyArray<TMiddleware>;
|
|
176
|
+
protected readonly ctx: RuntimeMiddlewareContext;
|
|
177
|
+
constructor(options: RuntimeCoreOptions<TMiddleware>);
|
|
178
|
+
/**
|
|
179
|
+
* Pre-lowering hook for plan rewriting. Defaults to identity. Subclasses
|
|
180
|
+
* may override to run a `beforeCompile` middleware chain (SQL does this
|
|
181
|
+
* to support typed AST rewrites — see `before-compile-chain.ts`).
|
|
182
|
+
*/
|
|
183
|
+
protected runBeforeCompile(plan: TPlan): TPlan | Promise<TPlan>;
|
|
184
|
+
/**
|
|
185
|
+
* Lower a pre-lowering `TPlan` into the family's executable `TExec`.
|
|
186
|
+
* Family-specific: SQL produces `{ sql, params, ast?, ... }`; Mongo
|
|
187
|
+
* produces `{ command, ... }`.
|
|
188
|
+
*/
|
|
189
|
+
protected abstract lower(plan: TPlan): TExec | Promise<TExec>;
|
|
190
|
+
/**
|
|
191
|
+
* Drive the underlying transport for a lowered `TExec`. Yields raw rows
|
|
192
|
+
* directly from the driver as `Record<string, unknown>`; codec decoding
|
|
193
|
+
* (if any) is the subclass's responsibility, applied by wrapping
|
|
194
|
+
* `execute()` rather than living inside this hook.
|
|
195
|
+
*
|
|
196
|
+
* The `Row` type parameter on `execute()` is satisfied by the caller via
|
|
197
|
+
* the plan's phantom `_row`; the runtime treats rows as opaque records
|
|
198
|
+
* here and trusts the caller's row typing.
|
|
199
|
+
*/
|
|
200
|
+
protected abstract runDriver(exec: TExec): AsyncIterable<Record<string, unknown>>;
|
|
201
|
+
abstract close(): Promise<void>;
|
|
202
|
+
execute<Row>(plan: TPlan & {
|
|
203
|
+
readonly _row?: Row;
|
|
204
|
+
}): AsyncIterableResult<Row>;
|
|
205
|
+
}
|
|
206
|
+
//#endregion
|
|
207
|
+
//#region src/runtime-error.d.ts
|
|
208
|
+
interface RuntimeErrorEnvelope extends Error {
|
|
209
|
+
readonly code: string;
|
|
210
|
+
readonly category: 'PLAN' | 'CONTRACT' | 'LINT' | 'BUDGET' | 'RUNTIME';
|
|
211
|
+
readonly severity: 'error';
|
|
212
|
+
readonly details?: Record<string, unknown>;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Type guard for the runtime-error envelope produced by `runtimeError`.
|
|
216
|
+
*
|
|
217
|
+
* Prefer this over duck-typing on `error.code` directly so consumers stay
|
|
218
|
+
* insulated from the envelope's internal shape.
|
|
219
|
+
*/
|
|
220
|
+
declare function isRuntimeError(error: unknown): error is RuntimeErrorEnvelope;
|
|
221
|
+
declare function runtimeError(code: string, message: string, details?: Record<string, unknown>): RuntimeErrorEnvelope;
|
|
222
|
+
//#endregion
|
|
223
|
+
export { type AfterExecuteResult, AsyncIterableResult, type ExecutionPlan, type QueryPlan, type ResultType, RuntimeCore, type RuntimeCoreOptions, type RuntimeErrorEnvelope, type RuntimeExecutor, type RuntimeLog, type RuntimeMiddleware, type RuntimeMiddlewareContext, checkMiddlewareCompatibility, isRuntimeError, runWithMiddleware, runtimeError };
|
|
77
224
|
//# sourceMappingURL=runtime.d.mts.map
|
package/dist/runtime.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runtime.d.mts","names":[],"sources":["../src/async-iterable-result.ts","../src/
|
|
1
|
+
{"version":3,"file":"runtime.d.mts","names":[],"sources":["../src/async-iterable-result.ts","../src/query-plan.ts","../src/runtime-middleware.ts","../src/run-with-middleware.ts","../src/runtime-core.ts","../src/runtime-error.ts"],"sourcesContent":[],"mappings":";;;cAEa,oCAAoC,cAAc,MAAM,YAAY;;;EAApE,QAA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KAAA,CAAA,CAAA,EE8BI,OF9BJ,CAAA,IAAA,CAA4B;qBEgCvB;oBAA0B;MAAQ,oBAAoB;ADpE3E;;;UEtBiB,oBAAA,SAA6B;;;ELEjC,SAAA,QAAA,EAAA,OAAmB;EAA+B,SAAA,OAAA,CAAA,EKE1C,MLF0C,CAAA,MAAA,EAAA,OAAA,CAAA;;;;;;;;AA6BlD,iBKlBG,cAAA,CLkBH,KAAA,EAAA,OAAA,CAAA,EAAA,KAAA,IKlB4C,oBLkB5C;AA+BY,iBKvCT,YAAA,CLuCS,IAAA,EAAA,MAAA,EAAA,OAAA,EAAA,MAAA,EAAA,OAAA,CAAA,EKpCb,MLoCa,CAAA,MAAA,EAAA,OAAA,CAAA,CAAA,EKnCtB,oBLmCsB"}
|
package/dist/runtime.mjs
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
//#region src/runtime-error.ts
|
|
2
|
+
/**
|
|
3
|
+
* Type guard for the runtime-error envelope produced by `runtimeError`.
|
|
4
|
+
*
|
|
5
|
+
* Prefer this over duck-typing on `error.code` directly so consumers stay
|
|
6
|
+
* insulated from the envelope's internal shape.
|
|
7
|
+
*/
|
|
8
|
+
function isRuntimeError(error) {
|
|
9
|
+
return error instanceof Error && "code" in error && typeof error.code === "string" && "category" in error && "severity" in error;
|
|
10
|
+
}
|
|
2
11
|
function runtimeError(code, message, details) {
|
|
3
12
|
const error = new Error(message);
|
|
4
13
|
Object.defineProperty(error, "name", {
|
|
@@ -71,6 +80,113 @@ var AsyncIterableResult = class {
|
|
|
71
80
|
}
|
|
72
81
|
};
|
|
73
82
|
|
|
83
|
+
//#endregion
|
|
84
|
+
//#region src/run-with-middleware.ts
|
|
85
|
+
/**
|
|
86
|
+
* Drives a single execution of `runDriver()` through the middleware lifecycle.
|
|
87
|
+
*
|
|
88
|
+
* Lifecycle, in order:
|
|
89
|
+
* 1. For each middleware in registration order: `beforeExecute(exec, ctx)`.
|
|
90
|
+
* 2. For each row yielded by `runDriver()`: for each middleware in registration
|
|
91
|
+
* order: `onRow(row, exec, ctx)`; then yield the row to the consumer.
|
|
92
|
+
* 3. On successful completion: for each middleware in registration order:
|
|
93
|
+
* `afterExecute(exec, { rowCount, latencyMs, completed: true }, ctx)`.
|
|
94
|
+
* 4. On any error thrown by the driver loop: for each middleware in
|
|
95
|
+
* registration order: `afterExecute(exec, { rowCount, latencyMs,
|
|
96
|
+
* completed: false }, ctx)`. Errors thrown by `afterExecute` during the
|
|
97
|
+
* error path are swallowed so they do not mask the original driver error.
|
|
98
|
+
* The original error is then rethrown.
|
|
99
|
+
*
|
|
100
|
+
* This helper is the single canonical implementation of the middleware
|
|
101
|
+
* orchestration loop; family runtimes should not reimplement it.
|
|
102
|
+
*/
|
|
103
|
+
function runWithMiddleware(exec, middleware, ctx, runDriver) {
|
|
104
|
+
const iterator = async function* () {
|
|
105
|
+
const startedAt = Date.now();
|
|
106
|
+
let rowCount = 0;
|
|
107
|
+
let completed = false;
|
|
108
|
+
try {
|
|
109
|
+
for (const mw of middleware) if (mw.beforeExecute) await mw.beforeExecute(exec, ctx);
|
|
110
|
+
for await (const row of runDriver()) {
|
|
111
|
+
for (const mw of middleware) if (mw.onRow) await mw.onRow(row, exec, ctx);
|
|
112
|
+
rowCount++;
|
|
113
|
+
yield row;
|
|
114
|
+
}
|
|
115
|
+
completed = true;
|
|
116
|
+
} catch (error) {
|
|
117
|
+
const latencyMs$1 = Date.now() - startedAt;
|
|
118
|
+
for (const mw of middleware) if (mw.afterExecute) try {
|
|
119
|
+
await mw.afterExecute(exec, {
|
|
120
|
+
rowCount,
|
|
121
|
+
latencyMs: latencyMs$1,
|
|
122
|
+
completed
|
|
123
|
+
}, ctx);
|
|
124
|
+
} catch {}
|
|
125
|
+
throw error;
|
|
126
|
+
}
|
|
127
|
+
const latencyMs = Date.now() - startedAt;
|
|
128
|
+
for (const mw of middleware) if (mw.afterExecute) await mw.afterExecute(exec, {
|
|
129
|
+
rowCount,
|
|
130
|
+
latencyMs,
|
|
131
|
+
completed
|
|
132
|
+
}, ctx);
|
|
133
|
+
};
|
|
134
|
+
return new AsyncIterableResult(iterator());
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
//#endregion
|
|
138
|
+
//#region src/runtime-core.ts
|
|
139
|
+
/**
|
|
140
|
+
* Family-agnostic abstract runtime base.
|
|
141
|
+
*
|
|
142
|
+
* Defines the entire `execute(plan)` template in one place:
|
|
143
|
+
*
|
|
144
|
+
* 1. `runBeforeCompile(plan)` — concrete; defaults to identity. SQL overrides
|
|
145
|
+
* this to run its `beforeCompile` middleware-hook chain.
|
|
146
|
+
* 2. `lower(plan)` — abstract. Each family produces its `*ExecutionPlan`
|
|
147
|
+
* (SQL via `lowerSqlPlan`, Mongo via `adapter.lower`).
|
|
148
|
+
* 3. `runWithMiddleware(exec, this.middleware, this.ctx,
|
|
149
|
+
* () => runDriver(exec))` — concrete; lifts the middleware lifecycle
|
|
150
|
+
* out of the family runtimes into the canonical helper.
|
|
151
|
+
*
|
|
152
|
+
* Concrete subclasses must implement `lower`, `runDriver`, and `close`.
|
|
153
|
+
*
|
|
154
|
+
* The class is generic over:
|
|
155
|
+
* - `TPlan` — the family's pre-lowering plan type.
|
|
156
|
+
* - `TExec` — the family's post-lowering (executable) plan type.
|
|
157
|
+
* - `TMiddleware` — the family's middleware type. Constrained to
|
|
158
|
+
* `RuntimeMiddleware<TExec>` because `runWithMiddleware` invokes the
|
|
159
|
+
* `beforeExecute` / `onRow` / `afterExecute` hooks with the lowered
|
|
160
|
+
* `TExec`. (The spec/plan wording "RuntimeMiddleware<TPlan>" is
|
|
161
|
+
* tightened to `<TExec>` here so the helper call typechecks; the
|
|
162
|
+
* intent is unchanged — middleware sees the post-lowering plan.)
|
|
163
|
+
*/
|
|
164
|
+
var RuntimeCore = class {
|
|
165
|
+
middleware;
|
|
166
|
+
ctx;
|
|
167
|
+
constructor(options) {
|
|
168
|
+
this.middleware = options.middleware;
|
|
169
|
+
this.ctx = options.ctx;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Pre-lowering hook for plan rewriting. Defaults to identity. Subclasses
|
|
173
|
+
* may override to run a `beforeCompile` middleware chain (SQL does this
|
|
174
|
+
* to support typed AST rewrites — see `before-compile-chain.ts`).
|
|
175
|
+
*/
|
|
176
|
+
runBeforeCompile(plan) {
|
|
177
|
+
return plan;
|
|
178
|
+
}
|
|
179
|
+
execute(plan) {
|
|
180
|
+
const self = this;
|
|
181
|
+
async function* generator() {
|
|
182
|
+
const compiled = await self.runBeforeCompile(plan);
|
|
183
|
+
const exec = await self.lower(compiled);
|
|
184
|
+
yield* runWithMiddleware(exec, self.middleware, self.ctx, () => self.runDriver(exec));
|
|
185
|
+
}
|
|
186
|
+
return new AsyncIterableResult(generator());
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
|
|
74
190
|
//#endregion
|
|
75
191
|
//#region src/runtime-middleware.ts
|
|
76
192
|
function checkMiddlewareCompatibility(middleware, runtimeFamilyId, runtimeTargetId) {
|
|
@@ -91,5 +207,5 @@ function checkMiddlewareCompatibility(middleware, runtimeFamilyId, runtimeTarget
|
|
|
91
207
|
}
|
|
92
208
|
|
|
93
209
|
//#endregion
|
|
94
|
-
export { AsyncIterableResult, checkMiddlewareCompatibility, runtimeError };
|
|
210
|
+
export { AsyncIterableResult, RuntimeCore, checkMiddlewareCompatibility, isRuntimeError, runWithMiddleware, runtimeError };
|
|
95
211
|
//# sourceMappingURL=runtime.mjs.map
|
package/dist/runtime.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runtime.mjs","names":["out: Row[]"],"sources":["../src/runtime-error.ts","../src/async-iterable-result.ts","../src/runtime-middleware.ts"],"sourcesContent":["export interface RuntimeErrorEnvelope extends Error {\n readonly code: string;\n readonly category: 'PLAN' | 'CONTRACT' | 'LINT' | 'BUDGET' | 'RUNTIME';\n readonly severity: 'error';\n readonly details?: Record<string, unknown>;\n}\n\nexport function runtimeError(\n code: string,\n message: string,\n details?: Record<string, unknown>,\n): RuntimeErrorEnvelope {\n const error = new Error(message) as RuntimeErrorEnvelope;\n Object.defineProperty(error, 'name', {\n value: 'RuntimeError',\n configurable: true,\n });\n\n return Object.assign(error, {\n code,\n category: resolveCategory(code),\n severity: 'error' as const,\n message,\n details,\n });\n}\n\nfunction resolveCategory(code: string): RuntimeErrorEnvelope['category'] {\n const prefix = code.split('.')[0] ?? 'RUNTIME';\n switch (prefix) {\n case 'PLAN':\n case 'CONTRACT':\n case 'LINT':\n case 'BUDGET':\n return prefix;\n default:\n return 'RUNTIME';\n }\n}\n","import { runtimeError } from './runtime-error';\n\nexport class AsyncIterableResult<Row> implements AsyncIterable<Row>, PromiseLike<Row[]> {\n private readonly generator: AsyncGenerator<Row, void, unknown>;\n private consumed = false;\n private consumedBy: 'bufferedArray' | 'iterator' | undefined;\n private bufferedArrayPromise: Promise<Row[]> | undefined;\n\n constructor(generator: AsyncGenerator<Row, void, unknown>) {\n this.generator = generator;\n }\n\n [Symbol.asyncIterator](): AsyncIterator<Row> {\n if (this.consumed) {\n throw runtimeError(\n 'RUNTIME.ITERATOR_CONSUMED',\n `AsyncIterableResult iterator has already been consumed via ${this.consumedBy === 'bufferedArray' ? 'toArray()/then()' : 'for-await loop'}. Each AsyncIterableResult can only be iterated once.`,\n {\n consumedBy: this.consumedBy,\n suggestion:\n this.consumedBy === 'bufferedArray'\n ? 'If you need to iterate multiple times, store the results from toArray() in a variable and reuse that.'\n : 'If you need to iterate multiple times, use toArray() to collect all results first.',\n },\n );\n }\n this.consumed = true;\n this.consumedBy = 'iterator';\n return this.generator;\n }\n\n toArray(): Promise<Row[]> {\n if (this.consumedBy === 'iterator') {\n return Promise.reject(\n runtimeError(\n 'RUNTIME.ITERATOR_CONSUMED',\n 'AsyncIterableResult iterator has already been consumed via for-await loop. Each AsyncIterableResult can only be iterated once.',\n {\n consumedBy: this.consumedBy,\n suggestion:\n 'The iterator was already consumed by a for-await loop. Use toArray() or await the result before iterating.',\n },\n ),\n );\n }\n\n if (this.bufferedArrayPromise) {\n return this.bufferedArrayPromise;\n }\n\n this.consumed = true;\n this.consumedBy = 'bufferedArray';\n this.bufferedArrayPromise = (async () => {\n const out: Row[] = [];\n for await (const item of this.generator) {\n out.push(item);\n }\n return out;\n })();\n return this.bufferedArrayPromise;\n }\n\n async first(): Promise<Row | null> {\n const rows = await this.toArray();\n return rows[0] ?? null;\n }\n\n async firstOrThrow(): Promise<Row> {\n const row = await this.first();\n if (row === null)\n throw runtimeError(\n 'RUNTIME.NO_ROWS',\n 'Expected at least one row, but none were returned',\n {},\n );\n return row;\n }\n\n // biome-ignore lint/suspicious/noThenProperty: PromiseLike implementation is intentional for await support.\n then<TResult1 = Row[], TResult2 = never>(\n onfulfilled?: ((value: Row[]) => TResult1 | PromiseLike<TResult1>) | undefined | null,\n onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | undefined | null,\n ): PromiseLike<TResult1 | TResult2> {\n return this.toArray().then(onfulfilled, onrejected);\n }\n}\n","import type { PlanMeta } from '@prisma-next/contract/types';\nimport type { AsyncIterableResult } from './async-iterable-result';\nimport { runtimeError } from './runtime-error';\n\nexport interface RuntimeLog {\n info(event: unknown): void;\n warn(event: unknown): void;\n error(event: unknown): void;\n debug?(event: unknown): void;\n}\n\nexport interface RuntimeMiddlewareContext {\n readonly contract: unknown;\n readonly mode: 'strict' | 'permissive';\n readonly now: () => number;\n readonly log: RuntimeLog;\n}\n\nexport interface AfterExecuteResult {\n readonly rowCount: number;\n readonly latencyMs: number;\n readonly completed: boolean;\n}\n\nexport interface RuntimeMiddleware {\n readonly name: string;\n readonly familyId?: string;\n readonly targetId?: string;\n beforeExecute?(plan: { readonly meta: PlanMeta }, ctx: RuntimeMiddlewareContext): Promise<void>;\n onRow?(\n row: Record<string, unknown>,\n plan: { readonly meta: PlanMeta },\n ctx: RuntimeMiddlewareContext,\n ): Promise<void>;\n afterExecute?(\n plan: { readonly meta: PlanMeta },\n result: AfterExecuteResult,\n ctx: RuntimeMiddlewareContext,\n ): Promise<void>;\n}\n\n/**\n * Cross-family SPI for any runtime that can execute plans and be shut down.\n * Each family runtime (SQL, Mongo) satisfies this interface — SQL nominally,\n * Mongo structurally (due to its phantom Row parameter using a unique symbol).\n *\n * The `_row` intersection on `execute` connects the `Row` type parameter to the\n * plan, mirroring how `ExecutionPlan<Row>` carries a phantom `_row?: Row`.\n */\nexport interface RuntimeExecutor<TPlan extends { readonly meta: PlanMeta }> {\n execute<Row>(plan: TPlan & { readonly _row?: Row }): AsyncIterableResult<Row>;\n close(): Promise<void>;\n}\n\nexport function checkMiddlewareCompatibility(\n middleware: RuntimeMiddleware,\n runtimeFamilyId: string,\n runtimeTargetId: string,\n): void {\n if (middleware.targetId !== undefined && middleware.familyId === undefined) {\n throw runtimeError(\n 'RUNTIME.MIDDLEWARE_INCOMPATIBLE',\n `Middleware '${middleware.name}' specifies targetId '${middleware.targetId}' without familyId`,\n { middleware: middleware.name, targetId: middleware.targetId },\n );\n }\n\n if (middleware.familyId !== undefined && middleware.familyId !== runtimeFamilyId) {\n throw runtimeError(\n 'RUNTIME.MIDDLEWARE_FAMILY_MISMATCH',\n `Middleware '${middleware.name}' requires family '${middleware.familyId}' but the runtime is configured for family '${runtimeFamilyId}'`,\n { middleware: middleware.name, middlewareFamilyId: middleware.familyId, runtimeFamilyId },\n );\n }\n\n if (middleware.targetId !== undefined && middleware.targetId !== runtimeTargetId) {\n throw runtimeError(\n 'RUNTIME.MIDDLEWARE_TARGET_MISMATCH',\n `Middleware '${middleware.name}' requires target '${middleware.targetId}' but the runtime is configured for target '${runtimeTargetId}'`,\n { middleware: middleware.name, middlewareTargetId: middleware.targetId, runtimeTargetId },\n );\n }\n}\n"],"mappings":";AAOA,SAAgB,aACd,MACA,SACA,SACsB;CACtB,MAAM,QAAQ,IAAI,MAAM,QAAQ;AAChC,QAAO,eAAe,OAAO,QAAQ;EACnC,OAAO;EACP,cAAc;EACf,CAAC;AAEF,QAAO,OAAO,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gB,6BACd,YACA,iBACA,iBACM;AACN,KAAI,WAAW,aAAa,UAAa,WAAW,aAAa,OAC/D,OAAM,aACJ,mCACA,eAAe,WAAW,KAAK,wBAAwB,WAAW,SAAS,qBAC3E;EAAE,YAAY,WAAW;EAAM,UAAU,WAAW;EAAU,CAC/D;AAGH,KAAI,WAAW,aAAa,UAAa,WAAW,aAAa,gBAC/D,OAAM,aACJ,sCACA,eAAe,WAAW,KAAK,qBAAqB,WAAW,SAAS,8CAA8C,gBAAgB,IACtI;EAAE,YAAY,WAAW;EAAM,oBAAoB,WAAW;EAAU;EAAiB,CAC1F;AAGH,KAAI,WAAW,aAAa,UAAa,WAAW,aAAa,gBAC/D,OAAM,aACJ,sCACA,eAAe,WAAW,KAAK,qBAAqB,WAAW,SAAS,8CAA8C,gBAAgB,IACtI;EAAE,YAAY,WAAW;EAAM,oBAAoB,WAAW;EAAU;EAAiB,CAC1F"}
|
|
1
|
+
{"version":3,"file":"runtime.mjs","names":["out: Row[]","latencyMs"],"sources":["../src/runtime-error.ts","../src/async-iterable-result.ts","../src/run-with-middleware.ts","../src/runtime-core.ts","../src/runtime-middleware.ts"],"sourcesContent":["export interface RuntimeErrorEnvelope extends Error {\n readonly code: string;\n readonly category: 'PLAN' | 'CONTRACT' | 'LINT' | 'BUDGET' | 'RUNTIME';\n readonly severity: 'error';\n readonly details?: Record<string, unknown>;\n}\n\n/**\n * Type guard for the runtime-error envelope produced by `runtimeError`.\n *\n * Prefer this over duck-typing on `error.code` directly so consumers stay\n * insulated from the envelope's internal shape.\n */\nexport function isRuntimeError(error: unknown): error is RuntimeErrorEnvelope {\n return (\n error instanceof Error &&\n 'code' in error &&\n typeof (error as { code?: unknown }).code === 'string' &&\n 'category' in error &&\n 'severity' in error\n );\n}\n\nexport function runtimeError(\n code: string,\n message: string,\n details?: Record<string, unknown>,\n): RuntimeErrorEnvelope {\n const error = new Error(message) as RuntimeErrorEnvelope;\n Object.defineProperty(error, 'name', {\n value: 'RuntimeError',\n configurable: true,\n });\n\n return Object.assign(error, {\n code,\n category: resolveCategory(code),\n severity: 'error' as const,\n message,\n details,\n });\n}\n\nfunction resolveCategory(code: string): RuntimeErrorEnvelope['category'] {\n const prefix = code.split('.')[0] ?? 'RUNTIME';\n switch (prefix) {\n case 'PLAN':\n case 'CONTRACT':\n case 'LINT':\n case 'BUDGET':\n return prefix;\n default:\n return 'RUNTIME';\n }\n}\n","import { runtimeError } from './runtime-error';\n\nexport class AsyncIterableResult<Row> implements AsyncIterable<Row>, PromiseLike<Row[]> {\n private readonly generator: AsyncGenerator<Row, void, unknown>;\n private consumed = false;\n private consumedBy: 'bufferedArray' | 'iterator' | undefined;\n private bufferedArrayPromise: Promise<Row[]> | undefined;\n\n constructor(generator: AsyncGenerator<Row, void, unknown>) {\n this.generator = generator;\n }\n\n [Symbol.asyncIterator](): AsyncIterator<Row> {\n if (this.consumed) {\n throw runtimeError(\n 'RUNTIME.ITERATOR_CONSUMED',\n `AsyncIterableResult iterator has already been consumed via ${this.consumedBy === 'bufferedArray' ? 'toArray()/then()' : 'for-await loop'}. Each AsyncIterableResult can only be iterated once.`,\n {\n consumedBy: this.consumedBy,\n suggestion:\n this.consumedBy === 'bufferedArray'\n ? 'If you need to iterate multiple times, store the results from toArray() in a variable and reuse that.'\n : 'If you need to iterate multiple times, use toArray() to collect all results first.',\n },\n );\n }\n this.consumed = true;\n this.consumedBy = 'iterator';\n return this.generator;\n }\n\n toArray(): Promise<Row[]> {\n if (this.consumedBy === 'iterator') {\n return Promise.reject(\n runtimeError(\n 'RUNTIME.ITERATOR_CONSUMED',\n 'AsyncIterableResult iterator has already been consumed via for-await loop. Each AsyncIterableResult can only be iterated once.',\n {\n consumedBy: this.consumedBy,\n suggestion:\n 'The iterator was already consumed by a for-await loop. Use toArray() or await the result before iterating.',\n },\n ),\n );\n }\n\n if (this.bufferedArrayPromise) {\n return this.bufferedArrayPromise;\n }\n\n this.consumed = true;\n this.consumedBy = 'bufferedArray';\n this.bufferedArrayPromise = (async () => {\n const out: Row[] = [];\n for await (const item of this.generator) {\n out.push(item);\n }\n return out;\n })();\n return this.bufferedArrayPromise;\n }\n\n async first(): Promise<Row | null> {\n const rows = await this.toArray();\n return rows[0] ?? null;\n }\n\n async firstOrThrow(): Promise<Row> {\n const row = await this.first();\n if (row === null)\n throw runtimeError(\n 'RUNTIME.NO_ROWS',\n 'Expected at least one row, but none were returned',\n {},\n );\n return row;\n }\n\n // biome-ignore lint/suspicious/noThenProperty: PromiseLike implementation is intentional for await support.\n then<TResult1 = Row[], TResult2 = never>(\n onfulfilled?: ((value: Row[]) => TResult1 | PromiseLike<TResult1>) | undefined | null,\n onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | undefined | null,\n ): PromiseLike<TResult1 | TResult2> {\n return this.toArray().then(onfulfilled, onrejected);\n }\n}\n","import { AsyncIterableResult } from './async-iterable-result';\nimport type { ExecutionPlan } from './query-plan';\nimport type { RuntimeMiddleware, RuntimeMiddlewareContext } from './runtime-middleware';\n\n/**\n * Drives a single execution of `runDriver()` through the middleware lifecycle.\n *\n * Lifecycle, in order:\n * 1. For each middleware in registration order: `beforeExecute(exec, ctx)`.\n * 2. For each row yielded by `runDriver()`: for each middleware in registration\n * order: `onRow(row, exec, ctx)`; then yield the row to the consumer.\n * 3. On successful completion: for each middleware in registration order:\n * `afterExecute(exec, { rowCount, latencyMs, completed: true }, ctx)`.\n * 4. On any error thrown by the driver loop: for each middleware in\n * registration order: `afterExecute(exec, { rowCount, latencyMs,\n * completed: false }, ctx)`. Errors thrown by `afterExecute` during the\n * error path are swallowed so they do not mask the original driver error.\n * The original error is then rethrown.\n *\n * This helper is the single canonical implementation of the middleware\n * orchestration loop; family runtimes should not reimplement it.\n */\nexport function runWithMiddleware<TExec extends ExecutionPlan, Row>(\n exec: TExec,\n middleware: ReadonlyArray<RuntimeMiddleware<TExec>>,\n ctx: RuntimeMiddlewareContext,\n runDriver: () => AsyncIterable<Row>,\n): AsyncIterableResult<Row> {\n const iterator = async function* (): AsyncGenerator<Row, void, unknown> {\n const startedAt = Date.now();\n let rowCount = 0;\n let completed = false;\n\n try {\n for (const mw of middleware) {\n if (mw.beforeExecute) {\n await mw.beforeExecute(exec, ctx);\n }\n }\n\n for await (const row of runDriver()) {\n for (const mw of middleware) {\n if (mw.onRow) {\n await mw.onRow(row as Record<string, unknown>, exec, ctx);\n }\n }\n rowCount++;\n yield row;\n }\n\n completed = true;\n } catch (error) {\n const latencyMs = Date.now() - startedAt;\n for (const mw of middleware) {\n if (mw.afterExecute) {\n try {\n await mw.afterExecute(exec, { rowCount, latencyMs, completed }, ctx);\n } catch {\n // Swallow afterExecute errors during the error path so they do not\n // mask the original driver error.\n }\n }\n }\n\n throw error;\n }\n\n const latencyMs = Date.now() - startedAt;\n for (const mw of middleware) {\n if (mw.afterExecute) {\n await mw.afterExecute(exec, { rowCount, latencyMs, completed }, ctx);\n }\n }\n };\n\n return new AsyncIterableResult(iterator());\n}\n","import { AsyncIterableResult } from './async-iterable-result';\nimport type { ExecutionPlan, QueryPlan } from './query-plan';\nimport { runWithMiddleware } from './run-with-middleware';\nimport type {\n RuntimeExecutor,\n RuntimeMiddleware,\n RuntimeMiddlewareContext,\n} from './runtime-middleware';\n\n/**\n * Constructor options shared by every concrete `RuntimeCore` subclass.\n *\n * Family runtimes typically build the middleware list and the\n * `RuntimeMiddlewareContext` themselves (running compatibility checks,\n * narrowing the context's `contract` field, etc.) before calling `super`.\n */\nexport interface RuntimeCoreOptions<TMiddleware extends RuntimeMiddleware<ExecutionPlan>> {\n readonly middleware: ReadonlyArray<TMiddleware>;\n readonly ctx: RuntimeMiddlewareContext;\n}\n\n/**\n * Family-agnostic abstract runtime base.\n *\n * Defines the entire `execute(plan)` template in one place:\n *\n * 1. `runBeforeCompile(plan)` — concrete; defaults to identity. SQL overrides\n * this to run its `beforeCompile` middleware-hook chain.\n * 2. `lower(plan)` — abstract. Each family produces its `*ExecutionPlan`\n * (SQL via `lowerSqlPlan`, Mongo via `adapter.lower`).\n * 3. `runWithMiddleware(exec, this.middleware, this.ctx,\n * () => runDriver(exec))` — concrete; lifts the middleware lifecycle\n * out of the family runtimes into the canonical helper.\n *\n * Concrete subclasses must implement `lower`, `runDriver`, and `close`.\n *\n * The class is generic over:\n * - `TPlan` — the family's pre-lowering plan type.\n * - `TExec` — the family's post-lowering (executable) plan type.\n * - `TMiddleware` — the family's middleware type. Constrained to\n * `RuntimeMiddleware<TExec>` because `runWithMiddleware` invokes the\n * `beforeExecute` / `onRow` / `afterExecute` hooks with the lowered\n * `TExec`. (The spec/plan wording \"RuntimeMiddleware<TPlan>\" is\n * tightened to `<TExec>` here so the helper call typechecks; the\n * intent is unchanged — middleware sees the post-lowering plan.)\n */\nexport abstract class RuntimeCore<\n TPlan extends QueryPlan,\n TExec extends ExecutionPlan,\n TMiddleware extends RuntimeMiddleware<TExec>,\n> implements RuntimeExecutor<TPlan>\n{\n protected readonly middleware: ReadonlyArray<TMiddleware>;\n protected readonly ctx: RuntimeMiddlewareContext;\n\n constructor(options: RuntimeCoreOptions<TMiddleware>) {\n this.middleware = options.middleware;\n this.ctx = options.ctx;\n }\n\n /**\n * Pre-lowering hook for plan rewriting. Defaults to identity. Subclasses\n * may override to run a `beforeCompile` middleware chain (SQL does this\n * to support typed AST rewrites — see `before-compile-chain.ts`).\n */\n protected runBeforeCompile(plan: TPlan): TPlan | Promise<TPlan> {\n return plan;\n }\n\n /**\n * Lower a pre-lowering `TPlan` into the family's executable `TExec`.\n * Family-specific: SQL produces `{ sql, params, ast?, ... }`; Mongo\n * produces `{ command, ... }`.\n */\n protected abstract lower(plan: TPlan): TExec | Promise<TExec>;\n\n /**\n * Drive the underlying transport for a lowered `TExec`. Yields raw rows\n * directly from the driver as `Record<string, unknown>`; codec decoding\n * (if any) is the subclass's responsibility, applied by wrapping\n * `execute()` rather than living inside this hook.\n *\n * The `Row` type parameter on `execute()` is satisfied by the caller via\n * the plan's phantom `_row`; the runtime treats rows as opaque records\n * here and trusts the caller's row typing.\n */\n protected abstract runDriver(exec: TExec): AsyncIterable<Record<string, unknown>>;\n\n abstract close(): Promise<void>;\n\n execute<Row>(plan: TPlan & { readonly _row?: Row }): AsyncIterableResult<Row> {\n const self = this;\n\n async function* generator(): AsyncGenerator<Row, void, unknown> {\n const compiled = await self.runBeforeCompile(plan);\n const exec = await self.lower(compiled);\n // The driver yields raw `Record<string, unknown>`; we cast to `Row` here.\n // The Row contract is enforced by the caller via `plan._row`.\n yield* runWithMiddleware<TExec, Row>(\n exec,\n self.middleware,\n self.ctx,\n () => self.runDriver(exec) as AsyncIterable<Row>,\n );\n }\n\n return new AsyncIterableResult(generator());\n }\n}\n","import type { AsyncIterableResult } from './async-iterable-result';\nimport type { QueryPlan } from './query-plan';\nimport { runtimeError } from './runtime-error';\n\nexport interface RuntimeLog {\n info(event: unknown): void;\n warn(event: unknown): void;\n error(event: unknown): void;\n debug?(event: unknown): void;\n}\n\nexport interface RuntimeMiddlewareContext {\n readonly contract: unknown;\n readonly mode: 'strict' | 'permissive';\n readonly now: () => number;\n readonly log: RuntimeLog;\n}\n\nexport interface AfterExecuteResult {\n readonly rowCount: number;\n readonly latencyMs: number;\n readonly completed: boolean;\n}\n\n/**\n * Family-agnostic middleware SPI parameterized over the plan marker.\n *\n * `TPlan` defaults to the framework `QueryPlan` marker so a generic\n * middleware (e.g. cross-family telemetry) can be authored without\n * naming a family. Family-specific middleware (`SqlMiddleware`,\n * `MongoMiddleware`) narrow `TPlan` to their concrete plan type.\n */\nexport interface RuntimeMiddleware<TPlan extends QueryPlan = QueryPlan> {\n readonly name: string;\n readonly familyId?: string;\n readonly targetId?: string;\n beforeExecute?(plan: TPlan, ctx: RuntimeMiddlewareContext): Promise<void>;\n onRow?(row: Record<string, unknown>, plan: TPlan, ctx: RuntimeMiddlewareContext): Promise<void>;\n afterExecute?(\n plan: TPlan,\n result: AfterExecuteResult,\n ctx: RuntimeMiddlewareContext,\n ): Promise<void>;\n}\n\n/**\n * Cross-family SPI for any runtime that can execute plans and be shut down.\n * Each family runtime (SQL, Mongo) satisfies this interface — SQL nominally,\n * Mongo structurally (due to its phantom Row parameter using a unique symbol).\n *\n * The `_row` intersection on `execute` connects the `Row` type parameter to the\n * plan, mirroring how `QueryPlan<Row>` carries a phantom `_row?: Row`.\n */\nexport interface RuntimeExecutor<TPlan extends QueryPlan> {\n execute<Row>(plan: TPlan & { readonly _row?: Row }): AsyncIterableResult<Row>;\n close(): Promise<void>;\n}\n\nexport function checkMiddlewareCompatibility(\n middleware: RuntimeMiddleware,\n runtimeFamilyId: string,\n runtimeTargetId: string,\n): void {\n if (middleware.targetId !== undefined && middleware.familyId === undefined) {\n throw runtimeError(\n 'RUNTIME.MIDDLEWARE_INCOMPATIBLE',\n `Middleware '${middleware.name}' specifies targetId '${middleware.targetId}' without familyId`,\n { middleware: middleware.name, targetId: middleware.targetId },\n );\n }\n\n if (middleware.familyId !== undefined && middleware.familyId !== runtimeFamilyId) {\n throw runtimeError(\n 'RUNTIME.MIDDLEWARE_FAMILY_MISMATCH',\n `Middleware '${middleware.name}' requires family '${middleware.familyId}' but the runtime is configured for family '${runtimeFamilyId}'`,\n { middleware: middleware.name, middlewareFamilyId: middleware.familyId, runtimeFamilyId },\n );\n }\n\n if (middleware.targetId !== undefined && middleware.targetId !== runtimeTargetId) {\n throw runtimeError(\n 'RUNTIME.MIDDLEWARE_TARGET_MISMATCH',\n `Middleware '${middleware.name}' requires target '${middleware.targetId}' but the runtime is configured for target '${runtimeTargetId}'`,\n { middleware: middleware.name, middlewareTargetId: middleware.targetId, runtimeTargetId },\n );\n }\n}\n"],"mappings":";;;;;;;AAaA,SAAgB,eAAe,OAA+C;AAC5E,QACE,iBAAiB,SACjB,UAAU,SACV,OAAQ,MAA6B,SAAS,YAC9C,cAAc,SACd,cAAc;;AAIlB,SAAgB,aACd,MACA,SACA,SACsB;CACtB,MAAM,QAAQ,IAAI,MAAM,QAAQ;AAChC,QAAO,eAAe,OAAO,QAAQ;EACnC,OAAO;EACP,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AAO,IAAI,oBAAoB,WAAW,CAAC;;;;;;AChD/C,SAAgB,6BACd,YACA,iBACA,iBACM;AACN,KAAI,WAAW,aAAa,UAAa,WAAW,aAAa,OAC/D,OAAM,aACJ,mCACA,eAAe,WAAW,KAAK,wBAAwB,WAAW,SAAS,qBAC3E;EAAE,YAAY,WAAW;EAAM,UAAU,WAAW;EAAU,CAC/D;AAGH,KAAI,WAAW,aAAa,UAAa,WAAW,aAAa,gBAC/D,OAAM,aACJ,sCACA,eAAe,WAAW,KAAK,qBAAqB,WAAW,SAAS,8CAA8C,gBAAgB,IACtI;EAAE,YAAY,WAAW;EAAM,oBAAoB,WAAW;EAAU;EAAiB,CAC1F;AAGH,KAAI,WAAW,aAAa,UAAa,WAAW,aAAa,gBAC/D,OAAM,aACJ,sCACA,eAAe,WAAW,KAAK,qBAAqB,WAAW,SAAS,8CAA8C,gBAAgB,IACtI;EAAE,YAAY,WAAW;EAAM,oBAAoB,WAAW;EAAU;EAAiB,CAC1F"}
|
package/package.json
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prisma-next/framework-components",
|
|
3
|
-
"version": "0.5.0-dev.
|
|
3
|
+
"version": "0.5.0-dev.9",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"description": "Framework component types, assembly logic, and stack creation for Prisma Next",
|
|
7
7
|
"dependencies": {
|
|
8
|
-
"@prisma-next/contract": "0.5.0-dev.
|
|
9
|
-
"@prisma-next/operations": "0.5.0-dev.
|
|
10
|
-
"@prisma-next/utils": "0.5.0-dev.
|
|
8
|
+
"@prisma-next/contract": "0.5.0-dev.9",
|
|
9
|
+
"@prisma-next/operations": "0.5.0-dev.9",
|
|
10
|
+
"@prisma-next/utils": "0.5.0-dev.9"
|
|
11
11
|
},
|
|
12
12
|
"devDependencies": {
|
|
13
13
|
"tsdown": "0.18.4",
|
|
14
14
|
"typescript": "5.9.3",
|
|
15
15
|
"vitest": "4.0.17",
|
|
16
|
-
"@prisma-next/
|
|
17
|
-
"@prisma-next/
|
|
16
|
+
"@prisma-next/tsconfig": "0.0.0",
|
|
17
|
+
"@prisma-next/tsdown": "0.0.0"
|
|
18
18
|
},
|
|
19
19
|
"files": [
|
|
20
20
|
"dist",
|
package/src/codec-types.ts
CHANGED
|
@@ -3,21 +3,39 @@ import type { JsonValue } from '@prisma-next/contract/types';
|
|
|
3
3
|
export type CodecTrait = 'equality' | 'order' | 'boolean' | 'numeric' | 'textual';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
6
|
+
* A codec is the contract between an application value and its on-wire and
|
|
7
|
+
* on-contract-disk representations.
|
|
7
8
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
9
|
+
* The author's mental model is two JS-side types — `TInput` (the
|
|
10
|
+
* application JS type) and `TWire` (the database driver wire format) —
|
|
11
|
+
* plus `JsonValue` for build-time contract artifacts. The codec translates
|
|
12
|
+
* `TInput` to `TWire` on writes and back on reads, and to/from `JsonValue`
|
|
13
|
+
* during contract emission and loading.
|
|
12
14
|
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
+
* Three representations participate:
|
|
16
|
+
* - **Input** (`TInput`): the JS type at the application boundary.
|
|
17
|
+
* - **Wire** (`TWire`): the format exchanged with the database driver.
|
|
18
|
+
* - **JSON** (`JsonValue`): a JSON-safe form used in contract artifacts.
|
|
19
|
+
*
|
|
20
|
+
* Codec methods split into two groups:
|
|
21
|
+
*
|
|
22
|
+
* - **Query-time** methods (`encode`, `decode`) run per row/parameter at the
|
|
23
|
+
* IO boundary; they are required and Promise-returning. The per-family
|
|
24
|
+
* codec factory accepts sync or async author functions and lifts sync
|
|
25
|
+
* ones to Promise-shaped methods automatically.
|
|
26
|
+
* - **Build-time** methods (`encodeJson`, `decodeJson`, `renderOutputType`)
|
|
27
|
+
* run when the contract is serialized, loaded, or when client types are
|
|
28
|
+
* emitted. They stay synchronous so contract validation and client
|
|
29
|
+
* construction are synchronous.
|
|
30
|
+
*
|
|
31
|
+
* Target-family codec interfaces extend this base with target-shaped
|
|
32
|
+
* metadata.
|
|
15
33
|
*/
|
|
16
34
|
export interface Codec<
|
|
17
35
|
Id extends string = string,
|
|
18
36
|
TTraits extends readonly CodecTrait[] = readonly CodecTrait[],
|
|
19
37
|
TWire = unknown,
|
|
20
|
-
|
|
38
|
+
TInput = unknown,
|
|
21
39
|
> {
|
|
22
40
|
/** Unique codec identifier in `namespace/name@version` format (e.g. `pg/timestamptz@1`). */
|
|
23
41
|
readonly id: Id;
|
|
@@ -25,15 +43,15 @@ export interface Codec<
|
|
|
25
43
|
readonly targetTypes: readonly string[];
|
|
26
44
|
/** Semantic traits for operator gating (e.g. equality, order, numeric). */
|
|
27
45
|
readonly traits?: TTraits;
|
|
28
|
-
/** Converts a JS value to the wire format expected by the database driver.
|
|
29
|
-
encode
|
|
30
|
-
/** Converts a wire value from the database driver into the JS type. */
|
|
31
|
-
decode(wire: TWire):
|
|
32
|
-
/** Converts a JS value to a JSON-safe representation for contract serialization.
|
|
33
|
-
encodeJson(value:
|
|
34
|
-
/** Converts a JSON representation back to the JS type.
|
|
35
|
-
decodeJson(json: JsonValue):
|
|
36
|
-
/** Produces the TypeScript output type expression for a field given its `typeParams`.
|
|
46
|
+
/** Converts a JS value to the wire format expected by the database driver. Always Promise-returning at the boundary. */
|
|
47
|
+
encode(value: TInput): Promise<TWire>;
|
|
48
|
+
/** Converts a wire value from the database driver into the JS application type. Always Promise-returning at the boundary. */
|
|
49
|
+
decode(wire: TWire): Promise<TInput>;
|
|
50
|
+
/** Converts a JS value to a JSON-safe representation for contract serialization. Synchronous; called during contract emission. */
|
|
51
|
+
encodeJson(value: TInput): JsonValue;
|
|
52
|
+
/** Converts a JSON representation back to the JS input type. Synchronous; called during contract loading via `validateContract`. */
|
|
53
|
+
decodeJson(json: JsonValue): TInput;
|
|
54
|
+
/** Produces the TypeScript output type expression for a field given its `typeParams`. Synchronous; used during contract.d.ts emission. */
|
|
37
55
|
renderOutputType?(typeParams: Record<string, unknown>): string | undefined;
|
|
38
56
|
}
|
|
39
57
|
|
package/src/exports/runtime.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
export { AsyncIterableResult } from '../async-iterable-result';
|
|
2
|
+
export type { ExecutionPlan, QueryPlan, ResultType } from '../query-plan';
|
|
3
|
+
export { runWithMiddleware } from '../run-with-middleware';
|
|
4
|
+
export type { RuntimeCoreOptions } from '../runtime-core';
|
|
5
|
+
export { RuntimeCore } from '../runtime-core';
|
|
2
6
|
export type { RuntimeErrorEnvelope } from '../runtime-error';
|
|
3
|
-
export { runtimeError } from '../runtime-error';
|
|
7
|
+
export { isRuntimeError, runtimeError } from '../runtime-error';
|
|
4
8
|
export type {
|
|
5
9
|
AfterExecuteResult,
|
|
6
10
|
RuntimeExecutor,
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { PlanMeta } from '@prisma-next/contract/types';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Family-agnostic plan marker.
|
|
5
|
+
*
|
|
6
|
+
* Carries only `meta` (the family-agnostic plan metadata) and the optional
|
|
7
|
+
* phantom `_row` parameter that lets type-level utilities recover the row
|
|
8
|
+
* type from a plan value. SQL and Mongo extend this marker with their own
|
|
9
|
+
* concrete shapes (`SqlQueryPlan`, `MongoQueryPlan`).
|
|
10
|
+
*
|
|
11
|
+
* `QueryPlan` is the *pre-lowering* marker — i.e. the surface a builder
|
|
12
|
+
* produces before family-specific lowering turns it into an executable
|
|
13
|
+
* plan (`ExecutionPlan`).
|
|
14
|
+
*/
|
|
15
|
+
export interface QueryPlan<Row = unknown> {
|
|
16
|
+
readonly meta: PlanMeta;
|
|
17
|
+
/**
|
|
18
|
+
* Phantom property to carry the Row generic for type-level utilities.
|
|
19
|
+
* Not set at runtime; used only for `ResultType` extraction.
|
|
20
|
+
*/
|
|
21
|
+
readonly _row?: Row;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Family-agnostic execution-plan marker.
|
|
26
|
+
*
|
|
27
|
+
* Extends `QueryPlan` with no additional structural fields — the marker
|
|
28
|
+
* exists to nominally distinguish executable plans from pre-lowering plans
|
|
29
|
+
* in the type system. Family-specific execution plans (`SqlExecutionPlan`,
|
|
30
|
+
* `MongoExecutionPlan`) extend this marker with their concrete shapes
|
|
31
|
+
* (e.g. `sql + params` for SQL, `wireCommand` for Mongo).
|
|
32
|
+
*/
|
|
33
|
+
export interface ExecutionPlan<Row = unknown> extends QueryPlan<Row> {}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Extracts the `Row` type from a plan via the phantom `_row` property.
|
|
37
|
+
*
|
|
38
|
+
* Works with any plan that extends `QueryPlan<Row>` — including
|
|
39
|
+
* `ExecutionPlan<Row>`, `SqlQueryPlan<Row>`, `SqlExecutionPlan<Row>`,
|
|
40
|
+
* `MongoQueryPlan<Row>`, and `MongoExecutionPlan<Row>`.
|
|
41
|
+
*
|
|
42
|
+
* The `_row` property must be present in the plan's static type for the
|
|
43
|
+
* conditional to bind `R`; objects whose type lacks `_row` resolve to
|
|
44
|
+
* `never`. Without the `keyof` guard, `extends { _row?: infer R }` would
|
|
45
|
+
* silently match any object and infer `unknown`.
|
|
46
|
+
*
|
|
47
|
+
* Example: `type Row = ResultType<typeof plan>`.
|
|
48
|
+
*/
|
|
49
|
+
export type ResultType<P> = '_row' extends keyof P
|
|
50
|
+
? P extends { readonly _row?: infer R }
|
|
51
|
+
? R
|
|
52
|
+
: never
|
|
53
|
+
: never;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { AsyncIterableResult } from './async-iterable-result';
|
|
2
|
+
import type { ExecutionPlan } from './query-plan';
|
|
3
|
+
import type { RuntimeMiddleware, RuntimeMiddlewareContext } from './runtime-middleware';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Drives a single execution of `runDriver()` through the middleware lifecycle.
|
|
7
|
+
*
|
|
8
|
+
* Lifecycle, in order:
|
|
9
|
+
* 1. For each middleware in registration order: `beforeExecute(exec, ctx)`.
|
|
10
|
+
* 2. For each row yielded by `runDriver()`: for each middleware in registration
|
|
11
|
+
* order: `onRow(row, exec, ctx)`; then yield the row to the consumer.
|
|
12
|
+
* 3. On successful completion: for each middleware in registration order:
|
|
13
|
+
* `afterExecute(exec, { rowCount, latencyMs, completed: true }, ctx)`.
|
|
14
|
+
* 4. On any error thrown by the driver loop: for each middleware in
|
|
15
|
+
* registration order: `afterExecute(exec, { rowCount, latencyMs,
|
|
16
|
+
* completed: false }, ctx)`. Errors thrown by `afterExecute` during the
|
|
17
|
+
* error path are swallowed so they do not mask the original driver error.
|
|
18
|
+
* The original error is then rethrown.
|
|
19
|
+
*
|
|
20
|
+
* This helper is the single canonical implementation of the middleware
|
|
21
|
+
* orchestration loop; family runtimes should not reimplement it.
|
|
22
|
+
*/
|
|
23
|
+
export function runWithMiddleware<TExec extends ExecutionPlan, Row>(
|
|
24
|
+
exec: TExec,
|
|
25
|
+
middleware: ReadonlyArray<RuntimeMiddleware<TExec>>,
|
|
26
|
+
ctx: RuntimeMiddlewareContext,
|
|
27
|
+
runDriver: () => AsyncIterable<Row>,
|
|
28
|
+
): AsyncIterableResult<Row> {
|
|
29
|
+
const iterator = async function* (): AsyncGenerator<Row, void, unknown> {
|
|
30
|
+
const startedAt = Date.now();
|
|
31
|
+
let rowCount = 0;
|
|
32
|
+
let completed = false;
|
|
33
|
+
|
|
34
|
+
try {
|
|
35
|
+
for (const mw of middleware) {
|
|
36
|
+
if (mw.beforeExecute) {
|
|
37
|
+
await mw.beforeExecute(exec, ctx);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
for await (const row of runDriver()) {
|
|
42
|
+
for (const mw of middleware) {
|
|
43
|
+
if (mw.onRow) {
|
|
44
|
+
await mw.onRow(row as Record<string, unknown>, exec, ctx);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
rowCount++;
|
|
48
|
+
yield row;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
completed = true;
|
|
52
|
+
} catch (error) {
|
|
53
|
+
const latencyMs = Date.now() - startedAt;
|
|
54
|
+
for (const mw of middleware) {
|
|
55
|
+
if (mw.afterExecute) {
|
|
56
|
+
try {
|
|
57
|
+
await mw.afterExecute(exec, { rowCount, latencyMs, completed }, ctx);
|
|
58
|
+
} catch {
|
|
59
|
+
// Swallow afterExecute errors during the error path so they do not
|
|
60
|
+
// mask the original driver error.
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
throw error;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const latencyMs = Date.now() - startedAt;
|
|
69
|
+
for (const mw of middleware) {
|
|
70
|
+
if (mw.afterExecute) {
|
|
71
|
+
await mw.afterExecute(exec, { rowCount, latencyMs, completed }, ctx);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
return new AsyncIterableResult(iterator());
|
|
77
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { AsyncIterableResult } from './async-iterable-result';
|
|
2
|
+
import type { ExecutionPlan, QueryPlan } from './query-plan';
|
|
3
|
+
import { runWithMiddleware } from './run-with-middleware';
|
|
4
|
+
import type {
|
|
5
|
+
RuntimeExecutor,
|
|
6
|
+
RuntimeMiddleware,
|
|
7
|
+
RuntimeMiddlewareContext,
|
|
8
|
+
} from './runtime-middleware';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Constructor options shared by every concrete `RuntimeCore` subclass.
|
|
12
|
+
*
|
|
13
|
+
* Family runtimes typically build the middleware list and the
|
|
14
|
+
* `RuntimeMiddlewareContext` themselves (running compatibility checks,
|
|
15
|
+
* narrowing the context's `contract` field, etc.) before calling `super`.
|
|
16
|
+
*/
|
|
17
|
+
export interface RuntimeCoreOptions<TMiddleware extends RuntimeMiddleware<ExecutionPlan>> {
|
|
18
|
+
readonly middleware: ReadonlyArray<TMiddleware>;
|
|
19
|
+
readonly ctx: RuntimeMiddlewareContext;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Family-agnostic abstract runtime base.
|
|
24
|
+
*
|
|
25
|
+
* Defines the entire `execute(plan)` template in one place:
|
|
26
|
+
*
|
|
27
|
+
* 1. `runBeforeCompile(plan)` — concrete; defaults to identity. SQL overrides
|
|
28
|
+
* this to run its `beforeCompile` middleware-hook chain.
|
|
29
|
+
* 2. `lower(plan)` — abstract. Each family produces its `*ExecutionPlan`
|
|
30
|
+
* (SQL via `lowerSqlPlan`, Mongo via `adapter.lower`).
|
|
31
|
+
* 3. `runWithMiddleware(exec, this.middleware, this.ctx,
|
|
32
|
+
* () => runDriver(exec))` — concrete; lifts the middleware lifecycle
|
|
33
|
+
* out of the family runtimes into the canonical helper.
|
|
34
|
+
*
|
|
35
|
+
* Concrete subclasses must implement `lower`, `runDriver`, and `close`.
|
|
36
|
+
*
|
|
37
|
+
* The class is generic over:
|
|
38
|
+
* - `TPlan` — the family's pre-lowering plan type.
|
|
39
|
+
* - `TExec` — the family's post-lowering (executable) plan type.
|
|
40
|
+
* - `TMiddleware` — the family's middleware type. Constrained to
|
|
41
|
+
* `RuntimeMiddleware<TExec>` because `runWithMiddleware` invokes the
|
|
42
|
+
* `beforeExecute` / `onRow` / `afterExecute` hooks with the lowered
|
|
43
|
+
* `TExec`. (The spec/plan wording "RuntimeMiddleware<TPlan>" is
|
|
44
|
+
* tightened to `<TExec>` here so the helper call typechecks; the
|
|
45
|
+
* intent is unchanged — middleware sees the post-lowering plan.)
|
|
46
|
+
*/
|
|
47
|
+
export abstract class RuntimeCore<
|
|
48
|
+
TPlan extends QueryPlan,
|
|
49
|
+
TExec extends ExecutionPlan,
|
|
50
|
+
TMiddleware extends RuntimeMiddleware<TExec>,
|
|
51
|
+
> implements RuntimeExecutor<TPlan>
|
|
52
|
+
{
|
|
53
|
+
protected readonly middleware: ReadonlyArray<TMiddleware>;
|
|
54
|
+
protected readonly ctx: RuntimeMiddlewareContext;
|
|
55
|
+
|
|
56
|
+
constructor(options: RuntimeCoreOptions<TMiddleware>) {
|
|
57
|
+
this.middleware = options.middleware;
|
|
58
|
+
this.ctx = options.ctx;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Pre-lowering hook for plan rewriting. Defaults to identity. Subclasses
|
|
63
|
+
* may override to run a `beforeCompile` middleware chain (SQL does this
|
|
64
|
+
* to support typed AST rewrites — see `before-compile-chain.ts`).
|
|
65
|
+
*/
|
|
66
|
+
protected runBeforeCompile(plan: TPlan): TPlan | Promise<TPlan> {
|
|
67
|
+
return plan;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Lower a pre-lowering `TPlan` into the family's executable `TExec`.
|
|
72
|
+
* Family-specific: SQL produces `{ sql, params, ast?, ... }`; Mongo
|
|
73
|
+
* produces `{ command, ... }`.
|
|
74
|
+
*/
|
|
75
|
+
protected abstract lower(plan: TPlan): TExec | Promise<TExec>;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Drive the underlying transport for a lowered `TExec`. Yields raw rows
|
|
79
|
+
* directly from the driver as `Record<string, unknown>`; codec decoding
|
|
80
|
+
* (if any) is the subclass's responsibility, applied by wrapping
|
|
81
|
+
* `execute()` rather than living inside this hook.
|
|
82
|
+
*
|
|
83
|
+
* The `Row` type parameter on `execute()` is satisfied by the caller via
|
|
84
|
+
* the plan's phantom `_row`; the runtime treats rows as opaque records
|
|
85
|
+
* here and trusts the caller's row typing.
|
|
86
|
+
*/
|
|
87
|
+
protected abstract runDriver(exec: TExec): AsyncIterable<Record<string, unknown>>;
|
|
88
|
+
|
|
89
|
+
abstract close(): Promise<void>;
|
|
90
|
+
|
|
91
|
+
execute<Row>(plan: TPlan & { readonly _row?: Row }): AsyncIterableResult<Row> {
|
|
92
|
+
const self = this;
|
|
93
|
+
|
|
94
|
+
async function* generator(): AsyncGenerator<Row, void, unknown> {
|
|
95
|
+
const compiled = await self.runBeforeCompile(plan);
|
|
96
|
+
const exec = await self.lower(compiled);
|
|
97
|
+
// The driver yields raw `Record<string, unknown>`; we cast to `Row` here.
|
|
98
|
+
// The Row contract is enforced by the caller via `plan._row`.
|
|
99
|
+
yield* runWithMiddleware<TExec, Row>(
|
|
100
|
+
exec,
|
|
101
|
+
self.middleware,
|
|
102
|
+
self.ctx,
|
|
103
|
+
() => self.runDriver(exec) as AsyncIterable<Row>,
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return new AsyncIterableResult(generator());
|
|
108
|
+
}
|
|
109
|
+
}
|
package/src/runtime-error.ts
CHANGED
|
@@ -5,6 +5,22 @@ export interface RuntimeErrorEnvelope extends Error {
|
|
|
5
5
|
readonly details?: Record<string, unknown>;
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Type guard for the runtime-error envelope produced by `runtimeError`.
|
|
10
|
+
*
|
|
11
|
+
* Prefer this over duck-typing on `error.code` directly so consumers stay
|
|
12
|
+
* insulated from the envelope's internal shape.
|
|
13
|
+
*/
|
|
14
|
+
export function isRuntimeError(error: unknown): error is RuntimeErrorEnvelope {
|
|
15
|
+
return (
|
|
16
|
+
error instanceof Error &&
|
|
17
|
+
'code' in error &&
|
|
18
|
+
typeof (error as { code?: unknown }).code === 'string' &&
|
|
19
|
+
'category' in error &&
|
|
20
|
+
'severity' in error
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
8
24
|
export function runtimeError(
|
|
9
25
|
code: string,
|
|
10
26
|
message: string,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { PlanMeta } from '@prisma-next/contract/types';
|
|
2
1
|
import type { AsyncIterableResult } from './async-iterable-result';
|
|
2
|
+
import type { QueryPlan } from './query-plan';
|
|
3
3
|
import { runtimeError } from './runtime-error';
|
|
4
4
|
|
|
5
5
|
export interface RuntimeLog {
|
|
@@ -22,18 +22,22 @@ export interface AfterExecuteResult {
|
|
|
22
22
|
readonly completed: boolean;
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
/**
|
|
26
|
+
* Family-agnostic middleware SPI parameterized over the plan marker.
|
|
27
|
+
*
|
|
28
|
+
* `TPlan` defaults to the framework `QueryPlan` marker so a generic
|
|
29
|
+
* middleware (e.g. cross-family telemetry) can be authored without
|
|
30
|
+
* naming a family. Family-specific middleware (`SqlMiddleware`,
|
|
31
|
+
* `MongoMiddleware`) narrow `TPlan` to their concrete plan type.
|
|
32
|
+
*/
|
|
33
|
+
export interface RuntimeMiddleware<TPlan extends QueryPlan = QueryPlan> {
|
|
26
34
|
readonly name: string;
|
|
27
35
|
readonly familyId?: string;
|
|
28
36
|
readonly targetId?: string;
|
|
29
|
-
beforeExecute?(plan:
|
|
30
|
-
onRow?(
|
|
31
|
-
row: Record<string, unknown>,
|
|
32
|
-
plan: { readonly meta: PlanMeta },
|
|
33
|
-
ctx: RuntimeMiddlewareContext,
|
|
34
|
-
): Promise<void>;
|
|
37
|
+
beforeExecute?(plan: TPlan, ctx: RuntimeMiddlewareContext): Promise<void>;
|
|
38
|
+
onRow?(row: Record<string, unknown>, plan: TPlan, ctx: RuntimeMiddlewareContext): Promise<void>;
|
|
35
39
|
afterExecute?(
|
|
36
|
-
plan:
|
|
40
|
+
plan: TPlan,
|
|
37
41
|
result: AfterExecuteResult,
|
|
38
42
|
ctx: RuntimeMiddlewareContext,
|
|
39
43
|
): Promise<void>;
|
|
@@ -45,9 +49,9 @@ export interface RuntimeMiddleware {
|
|
|
45
49
|
* Mongo structurally (due to its phantom Row parameter using a unique symbol).
|
|
46
50
|
*
|
|
47
51
|
* The `_row` intersection on `execute` connects the `Row` type parameter to the
|
|
48
|
-
* plan, mirroring how `
|
|
52
|
+
* plan, mirroring how `QueryPlan<Row>` carries a phantom `_row?: Row`.
|
|
49
53
|
*/
|
|
50
|
-
export interface RuntimeExecutor<TPlan extends
|
|
54
|
+
export interface RuntimeExecutor<TPlan extends QueryPlan> {
|
|
51
55
|
execute<Row>(plan: TPlan & { readonly _row?: Row }): AsyncIterableResult<Row>;
|
|
52
56
|
close(): Promise<void>;
|
|
53
57
|
}
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { JsonValue } from "@prisma-next/contract/types";
|
|
2
|
-
|
|
3
|
-
//#region src/codec-types.d.ts
|
|
4
|
-
type CodecTrait = 'equality' | 'order' | 'boolean' | 'numeric' | 'textual';
|
|
5
|
-
/**
|
|
6
|
-
* Base codec interface for all target families.
|
|
7
|
-
*
|
|
8
|
-
* A codec maps between three representations of a value:
|
|
9
|
-
* - **JS** (`TJs`): the JavaScript type used in application code
|
|
10
|
-
* - **Wire** (`TWire`): the format sent to/from the database driver
|
|
11
|
-
* - **JSON** (`JsonValue`): the JSON-safe form stored in contract artifacts
|
|
12
|
-
*
|
|
13
|
-
* Family-specific codec interfaces (SQL `Codec`, Mongo `MongoCodec`) extend
|
|
14
|
-
* this base to add family-specific metadata.
|
|
15
|
-
*/
|
|
16
|
-
interface Codec<Id extends string = string, TTraits extends readonly CodecTrait[] = readonly CodecTrait[], TWire = unknown, TJs = unknown> {
|
|
17
|
-
/** Unique codec identifier in `namespace/name@version` format (e.g. `pg/timestamptz@1`). */
|
|
18
|
-
readonly id: Id;
|
|
19
|
-
/** Database-native type names this codec handles (e.g. `['timestamptz']`). */
|
|
20
|
-
readonly targetTypes: readonly string[];
|
|
21
|
-
/** Semantic traits for operator gating (e.g. equality, order, numeric). */
|
|
22
|
-
readonly traits?: TTraits;
|
|
23
|
-
/** Converts a JS value to the wire format expected by the database driver. Optional when the driver accepts the JS type directly. */
|
|
24
|
-
encode?(value: TJs): TWire;
|
|
25
|
-
/** Converts a wire value from the database driver into the JS type. */
|
|
26
|
-
decode(wire: TWire): TJs;
|
|
27
|
-
/** Converts a JS value to a JSON-safe representation for contract serialization. Called during contract emission. */
|
|
28
|
-
encodeJson(value: TJs): JsonValue;
|
|
29
|
-
/** Converts a JSON representation back to the JS type. Called during contract loading via `validateContract`. */
|
|
30
|
-
decodeJson(json: JsonValue): TJs;
|
|
31
|
-
/** Produces the TypeScript output type expression for a field given its `typeParams`. Used during contract.d.ts emission. */
|
|
32
|
-
renderOutputType?(typeParams: Record<string, unknown>): string | undefined;
|
|
33
|
-
}
|
|
34
|
-
interface CodecLookup {
|
|
35
|
-
get(id: string): Codec | undefined;
|
|
36
|
-
}
|
|
37
|
-
declare const emptyCodecLookup: CodecLookup;
|
|
38
|
-
//#endregion
|
|
39
|
-
export { emptyCodecLookup as i, CodecLookup as n, CodecTrait as r, Codec as t };
|
|
40
|
-
//# sourceMappingURL=codec-types-B58nCJiu.d.mts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"codec-types-B58nCJiu.d.mts","names":[],"sources":["../src/codec-types.ts"],"sourcesContent":[],"mappings":";;;KAEY,UAAA;;AAAZ;AAaA;;;;;;;;;AAiBoB,UAjBH,KAiBG,CAAA,WAAA,MAAA,GAAA,MAAA,EAAA,gBAAA,SAfO,UAeP,EAAA,GAAA,SAf+B,UAe/B,EAAA,EAAA,QAAA,OAAA,EAAA,MAAA,OAAA,CAAA,CAAA;EAAM;EAEP,SAAA,EAAA,EAZJ,EAYI;EAAY;EAEC,SAAA,WAAA,EAAA,SAAA,MAAA,EAAA;EAAM;EAGrB,SAAA,MAAW,CAAA,EAbR,OAcD;EAGN;iBAfI,MAAM;;eAER,QAAQ;;oBAEH,MAAM;;mBAEP,YAAY;;gCAEC;;UAGf,WAAA;mBACE;;cAGN,kBAAkB"}
|