@atscript/db 0.1.135 → 0.1.137
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{agg-DKuf_v2L.d.cts → agg-CV7y8nC6.d.cts} +13 -3
- package/dist/{agg-BtlGeRfj.d.mts → agg-D5DHsAby.d.mts} +13 -3
- package/dist/agg.cjs +35 -3
- package/dist/agg.d.cts +2 -2
- package/dist/agg.d.mts +2 -2
- package/dist/agg.mjs +34 -1
- package/dist/aggregate-fns-CGBv3E8S.cjs +87 -0
- package/dist/aggregate-fns-CfsveE1w.mjs +58 -0
- package/dist/{buckets-GruoVxH5.d.mts → buckets-BFG2RYRW.d.mts} +662 -31
- package/dist/{buckets-D6PBXKRJ.d.cts → buckets-C-27xmtq.d.cts} +662 -31
- package/dist/column-diff-BmqvgBWw.d.cts +24 -0
- package/dist/{db-view-Doh8vPg3.mjs → column-diff-BwOA5101.mjs} +795 -125
- package/dist/{db-view-Dm7MpUGB.cjs → column-diff-CgxgFKzx.cjs} +873 -125
- package/dist/column-diff-DPkbZIVE.d.mts +24 -0
- package/dist/index.cjs +79 -55
- package/dist/index.d.cts +12 -10
- package/dist/index.d.mts +12 -10
- package/dist/index.mjs +30 -11
- package/dist/{nested-writer-wk1EFUNY.cjs → nested-writer-BZNCuqI6.cjs} +16 -2
- package/dist/{nested-writer-CqL24ojl.mjs → nested-writer-FWD5oOYh.mjs} +11 -3
- package/dist/plugin.cjs +217 -77
- package/dist/plugin.mjs +217 -78
- package/dist/rel.cjs +2 -2
- package/dist/rel.d.cts +2 -20
- package/dist/rel.d.mts +2 -20
- package/dist/rel.mjs +2 -2
- package/dist/relation-helpers-D3Zu0Mta.d.mts +30 -0
- package/dist/relation-helpers-DxrvS6ar.d.cts +30 -0
- package/dist/{relation-loader-BD4xANQJ.cjs → relation-loader-6ZB_5KFq.cjs} +1 -1
- package/dist/{relation-loader-B68R1LET.mjs → relation-loader-CTFaZpVa.mjs} +1 -1
- package/dist/shared.cjs +5 -1
- package/dist/shared.d.cts +16 -3
- package/dist/shared.d.mts +16 -3
- package/dist/shared.mjs +2 -2
- package/dist/sync.cjs +38 -321
- package/dist/sync.d.cts +29 -17
- package/dist/sync.d.mts +29 -17
- package/dist/sync.mjs +6 -289
- package/dist/{validation-utils-MWOP1Ts4.mjs → validation-utils-B4h-GW4d.mjs} +29 -7
- package/dist/{validation-utils-B7SXPkm7.cjs → validation-utils-Dg0hW6dn.cjs} +52 -6
- package/dist/{validator-CewfnGZj.d.mts → validator-Drb2N-YL.d.cts} +1 -1
- package/dist/{validator-CewfnGZj.d.cts → validator-Drb2N-YL.d.mts} +1 -1
- package/dist/validator.d.cts +1 -1
- package/dist/validator.d.mts +1 -1
- package/package.json +1 -1
- package/dist/agg-CvXDGnKi.mjs +0 -61
- package/dist/agg-EcIbAFnJ.cjs +0 -78
- package/dist/db-space-9CH5neN7.d.mts +0 -439
- package/dist/db-space-BdtBNTeH.d.cts +0 -439
|
@@ -1,439 +0,0 @@
|
|
|
1
|
-
import { Ct as TWriteTableResolver, H as TDbInsertManyResult, Ht as TGenericLogger, I as TDbDeleteResult, Lt as DbEncryption, Nt as BaseDbAdapter, Ot as TableMetadata, Pt as AtscriptDbReadable, Rt as TDbEncryptionOptions, St as TWriteOptions, U as TDbInsertResult, X as TDbUpdateResult, bt as TTouchManyOptions, et as TDeleteOptions, g as DbRow, lt as TIdResolveOptions, m as DbPatch, mt as TReferencingForeignKey, ot as TFkLookupResolver, w as TCascadeResolver, x as NullableOptional, yt as TTableResolver } from "./buckets-D6PBXKRJ.cjs";
|
|
2
|
-
import { FilterExpr } from "@uniqu/core";
|
|
3
|
-
import { AtscriptQueryComparison, AtscriptQueryFieldRef, AtscriptQueryFieldRef as AtscriptQueryFieldRef$1, AtscriptQueryNode, AtscriptQueryNode as AtscriptQueryNode$1, AtscriptRef, FlatOf, NavPropsOf, OwnPropsOf, PrimaryKeyOf, TAtscriptAnnotatedType, TAtscriptDataType, Validator } from "@atscript/typescript/utils";
|
|
4
|
-
|
|
5
|
-
//#region src/strategies/integrity.d.ts
|
|
6
|
-
/**
|
|
7
|
-
* Strategy for referential integrity enforcement.
|
|
8
|
-
* Two implementations: {@link NativeIntegrity} (DB handles FK constraints)
|
|
9
|
-
* and `ApplicationIntegrity` (generic layer validates + cascades).
|
|
10
|
-
*/
|
|
11
|
-
declare abstract class IntegrityStrategy {
|
|
12
|
-
abstract validateForeignKeys(items: Array<Record<string, unknown>>, meta: TableMetadata, fkLookupResolver: TFkLookupResolver | undefined, writeTableResolver: TWriteTableResolver | undefined, partial?: boolean, excludeTargetTable?: string): Promise<void>;
|
|
13
|
-
abstract cascadeBeforeDelete(filter: FilterExpr, tableName: string, meta: TableMetadata, cascadeResolver: TCascadeResolver, translateFilter: (f: FilterExpr) => FilterExpr, adapter: BaseDbAdapter): Promise<void>;
|
|
14
|
-
abstract needsCascade(cascadeResolver: TCascadeResolver | undefined): boolean;
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* Integrity strategy for adapters with native FK support (e.g. SQLite, MySQL).
|
|
18
|
-
* All operations are no-ops — the database engine enforces constraints.
|
|
19
|
-
*/
|
|
20
|
-
declare class NativeIntegrity extends IntegrityStrategy {
|
|
21
|
-
validateForeignKeys(): Promise<void>;
|
|
22
|
-
cascadeBeforeDelete(): Promise<void>;
|
|
23
|
-
needsCascade(): boolean;
|
|
24
|
-
}
|
|
25
|
-
//#endregion
|
|
26
|
-
//#region src/table/db-table.d.ts
|
|
27
|
-
declare class AtscriptDbTable<T extends TAtscriptAnnotatedType = TAtscriptAnnotatedType, DataType = TAtscriptDataType<T>, FlatType = NullableOptional<FlatOf<T>>, A extends BaseDbAdapter = BaseDbAdapter, IdType = PrimaryKeyOf<T>, OwnProps = NullableOptional<OwnPropsOf<T>>, NavType extends Record<string, unknown> = NavPropsOf<T>> extends AtscriptDbReadable<T, DataType, FlatType, A, IdType, OwnProps, NavType> {
|
|
28
|
-
protected _cascadeResolver?: TCascadeResolver;
|
|
29
|
-
protected _fkLookupResolver?: TFkLookupResolver;
|
|
30
|
-
protected readonly _integrity: IntegrityStrategy;
|
|
31
|
-
protected readonly validators: Map<string, Validator<T, DataType>>;
|
|
32
|
-
private _fromDepthMap?;
|
|
33
|
-
constructor(_type: T, adapter: A, logger?: TGenericLogger, _tableResolver?: TTableResolver, _writeTableResolver?: TWriteTableResolver);
|
|
34
|
-
/**
|
|
35
|
-
* Sets the cascade resolver for application-level cascade deletes.
|
|
36
|
-
* Called by DbSpace after table creation.
|
|
37
|
-
*/
|
|
38
|
-
setCascadeResolver(resolver: TCascadeResolver): void;
|
|
39
|
-
/**
|
|
40
|
-
* Sets the FK lookup resolver for application-level FK validation.
|
|
41
|
-
* Called by DbSpace after table creation.
|
|
42
|
-
*/
|
|
43
|
-
setFkLookupResolver(resolver: TFkLookupResolver): void;
|
|
44
|
-
/**
|
|
45
|
-
* Returns a cached validator for the given purpose.
|
|
46
|
-
* Built with adapter plugins from {@link BaseDbAdapter.getValidatorPlugins}.
|
|
47
|
-
*
|
|
48
|
-
* Standard purposes: `'insert'`, `'update'`, `'patch'`.
|
|
49
|
-
* Adapters may define additional purposes.
|
|
50
|
-
*/
|
|
51
|
-
getValidator(purpose: string): Validator<T, DataType>;
|
|
52
|
-
/**
|
|
53
|
-
* Inserts a single record. Delegates to {@link insertMany} for unified
|
|
54
|
-
* nested creation support.
|
|
55
|
-
*/
|
|
56
|
-
insertOne(payload: DbPatch<DataType>, opts?: TWriteOptions<DataType>): Promise<TDbInsertResult>;
|
|
57
|
-
/**
|
|
58
|
-
* Inserts multiple records with batch-optimized nested creation.
|
|
59
|
-
*
|
|
60
|
-
* Supports **nested creation**: if payloads include data for navigation
|
|
61
|
-
* fields (`@db.rel.to` / `@db.rel.from`), related records are created
|
|
62
|
-
* automatically in batches. TO dependencies are batch-created first
|
|
63
|
-
* (their PKs become our FKs), FROM dependents are batch-created after
|
|
64
|
-
* (they receive our PKs as their FKs). Fully recursive — nested records
|
|
65
|
-
* with their own nav data trigger further batch inserts at each level.
|
|
66
|
-
* Recursive up to `maxDepth` (default 3).
|
|
67
|
-
*
|
|
68
|
-
* `opts.guard` (since 0.1.128) runs once inside the transaction, after
|
|
69
|
-
* defaults + validation, with the prepared rows — see {@link TWriteOptions}.
|
|
70
|
-
*/
|
|
71
|
-
insertMany(payloads: Array<DbPatch<DataType>>, opts?: TWriteOptions<DataType>): Promise<TDbInsertManyResult>;
|
|
72
|
-
/**
|
|
73
|
-
* Replaces a single record identified by primary key(s).
|
|
74
|
-
* Delegates to {@link bulkReplace} for unified nested relation support.
|
|
75
|
-
*/
|
|
76
|
-
replaceOne(payload: DbRow<DataType>, opts?: TWriteOptions<DataType>): Promise<TDbUpdateResult>;
|
|
77
|
-
/**
|
|
78
|
-
* Replaces multiple records with deep nested relation support.
|
|
79
|
-
*
|
|
80
|
-
* Supports all relation types (TO, FROM, VIA). TO dependencies are
|
|
81
|
-
* replaced first (their PKs become our FKs), FROM dependents are replaced
|
|
82
|
-
* after (they receive our PKs as their FKs), VIA relations clear and
|
|
83
|
-
* re-create junction rows. Fully recursive up to `maxDepth` (default 3).
|
|
84
|
-
*
|
|
85
|
-
* `opts.guard` (since 0.1.128) runs once inside the transaction, after
|
|
86
|
-
* `$cas` extraction, defaults + validation — see {@link TWriteOptions}.
|
|
87
|
-
*/
|
|
88
|
-
bulkReplace(payloads: Array<DbRow<DataType>>, opts?: TWriteOptions<DataType>): Promise<TDbUpdateResult>;
|
|
89
|
-
/**
|
|
90
|
-
* Partially updates a single record identified by primary key(s).
|
|
91
|
-
* Delegates to {@link bulkUpdate} for unified nested relation support.
|
|
92
|
-
*/
|
|
93
|
-
updateOne(payload: DbPatch<DataType>, opts?: TWriteOptions<DataType>): Promise<TDbUpdateResult>;
|
|
94
|
-
/**
|
|
95
|
-
* Partially updates multiple records with deep nested relation support.
|
|
96
|
-
*
|
|
97
|
-
* Only TO relations (1:1, N:1) are supported for patching. FROM/VIA
|
|
98
|
-
* relations will error — use {@link bulkReplace} for those.
|
|
99
|
-
* Recursive up to `maxDepth` (default 3).
|
|
100
|
-
*
|
|
101
|
-
* `opts.guard` (since 0.1.128) runs once inside the transaction, after
|
|
102
|
-
* `$cas` extraction and validation, with the patches (identifying fields
|
|
103
|
-
* present, `$cas` removed) — see {@link TWriteOptions}.
|
|
104
|
-
*/
|
|
105
|
-
bulkUpdate(payloads: Array<DbPatch<DataType>>, opts?: TWriteOptions<DataType>): Promise<TDbUpdateResult>;
|
|
106
|
-
/**
|
|
107
|
-
* Batch versioned touch (since 0.1.129): bumps the version of every listed
|
|
108
|
-
* row by exactly one, each row guarded by its own expected version. This is
|
|
109
|
-
* the batch fence `updateMany(orFilter, {})` used to be before 0.1.128 (an
|
|
110
|
-
* empty patch is a no-op since then and takes no lock).
|
|
111
|
-
*
|
|
112
|
-
* Each key carries the primary key field(s) (composite supported) plus the
|
|
113
|
-
* version column and NOTHING else — a touch has no payload. Unique indexes
|
|
114
|
-
* do not identify a touch key. `undefined`-valued properties are ignored,
|
|
115
|
-
* like in every write payload. Empty `keys` → `{ 0, 0 }` without a statement.
|
|
116
|
-
*
|
|
117
|
-
* `require: 'all'` (default): one count over the whole key set runs FIRST;
|
|
118
|
-
* a stale or missing row throws {@link CasMismatchError} before any write.
|
|
119
|
-
* The bumps then run as `updateMany(orFilter, {})` chunks of at most
|
|
120
|
-
* {@link TOUCH_MANY_CHUNK} keys inside one adapter transaction; a summed
|
|
121
|
-
* `matchedCount` short of `keys.length` (a row moved between the count and
|
|
122
|
-
* the bump) throws the same error — SQL engines roll every bump back. The
|
|
123
|
-
* pre-count is therefore a deliberate double check on SQL: it is what makes
|
|
124
|
-
* the guarantee hold on adapters whose `withTransaction` is a passthrough
|
|
125
|
-
* (the memory adapter, a Mongo standalone topology) — there it covers the
|
|
126
|
-
* common stale case and the residual race window is accepted.
|
|
127
|
-
* `require: 'any'`: no pre-count, the honest summed result is returned.
|
|
128
|
-
*
|
|
129
|
-
* No `guard`, no `onWrite`; not exposed over HTTP.
|
|
130
|
-
*/
|
|
131
|
-
touchMany(keys: Array<DbPatch<DataType>>, opts?: TTouchManyOptions): Promise<TDbUpdateResult>;
|
|
132
|
-
/**
|
|
133
|
-
* Deletes a single record by any type-compatible identifier — primary key
|
|
134
|
-
* or single-field unique index. Uses the same resolution logic as `findById`.
|
|
135
|
-
*
|
|
136
|
-
* When the adapter does not support native foreign keys (e.g. MongoDB),
|
|
137
|
-
* cascade and setNull actions are applied before the delete.
|
|
138
|
-
*
|
|
139
|
-
* `opts.guard` (since 0.1.128) runs inside the transaction once the id has
|
|
140
|
-
* resolved to a filter, before cascade / delete — see {@link TDeleteOptions}.
|
|
141
|
-
* An id that resolves to no filter answers `{ deletedCount: 0 }` without
|
|
142
|
-
* calling the guard.
|
|
143
|
-
*/
|
|
144
|
-
deleteOne(id: IdType, opts?: TDeleteOptions<DataType>): Promise<TDbDeleteResult>;
|
|
145
|
-
updateMany(filter: FilterExpr<FlatType>, data: DbPatch<DataType>): Promise<TDbUpdateResult>;
|
|
146
|
-
replaceMany(filter: FilterExpr<FlatType>, data: DbRow<DataType>): Promise<TDbUpdateResult>;
|
|
147
|
-
deleteMany(filter: FilterExpr<FlatType>): Promise<TDbDeleteResult>;
|
|
148
|
-
/**
|
|
149
|
-
* Synchronizes indexes between Atscript definitions and the database.
|
|
150
|
-
*/
|
|
151
|
-
syncIndexes(): Promise<void>;
|
|
152
|
-
/**
|
|
153
|
-
* Ensures the table/collection exists in the database.
|
|
154
|
-
*/
|
|
155
|
-
ensureTable(): Promise<void>;
|
|
156
|
-
/** Engine-agnostic guard for user-supplied mutation filters (updateMany/deleteMany/…). */
|
|
157
|
-
protected _guardMutationFilter(filter: FilterExpr): void;
|
|
158
|
-
/**
|
|
159
|
-
* Encrypts `@db.encrypted` field values in place on (already validated)
|
|
160
|
-
* write payloads — between validation and `prepareForWrite`, so adapters
|
|
161
|
-
* only ever see envelope strings.
|
|
162
|
-
*
|
|
163
|
-
* Parent objects along an encrypted path are shallow-cloned before
|
|
164
|
-
* mutation so caller-shared nested objects are never modified.
|
|
165
|
-
*
|
|
166
|
-
* In `patch` mode, operator objects (`$inc`, `$insert`, …) targeting an
|
|
167
|
-
* encrypted field are rejected with `ENC_FIELD_PATCH_OP` — ciphertext is
|
|
168
|
-
* opaque; only plain re-assignment (which re-encrypts) is allowed.
|
|
169
|
-
*/
|
|
170
|
-
protected _encryptItems(items: Array<Record<string, unknown>>, mode: "write" | "patch"): Promise<void>;
|
|
171
|
-
/**
|
|
172
|
-
* Lazy pre-image read for a guard's `current(i)`: `null` when the row has
|
|
173
|
-
* no identifying key (e.g. an auto-increment insert) or the key cannot be
|
|
174
|
-
* resolved — never throws for a missing key.
|
|
175
|
-
* @internal
|
|
176
|
-
*/
|
|
177
|
-
_readPreImage(row: unknown): Promise<DataType | null>;
|
|
178
|
-
/**
|
|
179
|
-
* Applies `@db.default` values in place to a row's absent fields — the
|
|
180
|
-
* defaults pass every insert / replace path runs before validation.
|
|
181
|
-
* Static value defaults (`@db.default 'x'`) are filled on EVERY adapter
|
|
182
|
-
* (since 0.1.128 — writing the column's own default explicitly is
|
|
183
|
-
* equivalent to leaving it to the DDL `DEFAULT`, and write guards see the
|
|
184
|
-
* full row). Function defaults (`now` / `uuid` / `increment` / custom) the
|
|
185
|
-
* adapter handles natively are NOT filled — the field stays absent so the
|
|
186
|
-
* engine's own default applies. The version column is never touched.
|
|
187
|
-
*/
|
|
188
|
-
protected _applyDefaults(data: Record<string, unknown>): Record<string, unknown>;
|
|
189
|
-
/**
|
|
190
|
-
* The JS value for a `@db.default 'literal'`: strings (including unions of
|
|
191
|
-
* string literals) are used as-is, every other design type is parsed as
|
|
192
|
-
* JSON — the same value the SQL adapters put into the DDL `DEFAULT` clause.
|
|
193
|
-
* A literal that is not valid JSON falls back to the raw string so the
|
|
194
|
-
* validator reports it against the field instead of a bare `SyntaxError`.
|
|
195
|
-
*/
|
|
196
|
-
private _parseValueDefault;
|
|
197
|
-
/**
|
|
198
|
-
* Extracts a record-identifying filter from a payload.
|
|
199
|
-
*
|
|
200
|
-
* Resolution order:
|
|
201
|
-
* 1. Primary key field(s) — if all PK fields are present in the payload.
|
|
202
|
-
* 2. Single-field unique index — first `@db.index.unique` field found.
|
|
203
|
-
* 3. Compound unique index — first compound unique index whose fields are all present.
|
|
204
|
-
*
|
|
205
|
-
* Throws when no identifying fields can be found. With `isFieldVisible`
|
|
206
|
-
* (since 0.1.134), a unique index over a hidden field is skipped, as if it
|
|
207
|
-
* did not exist — see {@link identificationsVisibleTo}.
|
|
208
|
-
*/
|
|
209
|
-
protected _extractRecordFilter(payload: Record<string, unknown>, opts?: TIdResolveOptions): FilterExpr;
|
|
210
|
-
private _prepareFilterValue;
|
|
211
|
-
/**
|
|
212
|
-
* Lazy — builds a `normalized-path → from-depth` map from `this._meta.flatMap`
|
|
213
|
-
* on first use. Only paths reachable through an unbroken chain of `db.rel.from`
|
|
214
|
-
* nav fields from the root are included (chains crossing `to`/`via` are excluded).
|
|
215
|
-
*/
|
|
216
|
-
private _getFromDepthMap;
|
|
217
|
-
/**
|
|
218
|
-
* Populate the depth-limit bundle on a `DbValidationContext`. Only the root
|
|
219
|
-
* write call (`depth === 0`) enforces — nested re-entries leave `depthCheck`
|
|
220
|
-
* unset so the full tree is validated once at the root.
|
|
221
|
-
*/
|
|
222
|
-
private _applyDepthCtx;
|
|
223
|
-
/**
|
|
224
|
-
* Pre-validate items (type validation + FK constraints) without inserting them.
|
|
225
|
-
* Used by parent tables to validate FROM children before the main insert,
|
|
226
|
-
* ensuring errors are caught before the parent is committed.
|
|
227
|
-
*
|
|
228
|
-
* @param opts.excludeFkTargetTable - Skip FK validation to this table (the parent).
|
|
229
|
-
*/
|
|
230
|
-
preValidateItems(items: Array<Record<string, unknown>>, opts?: {
|
|
231
|
-
excludeFkTargetTable?: string;
|
|
232
|
-
}): Promise<void>;
|
|
233
|
-
/**
|
|
234
|
-
* Builds a validator for a given purpose with adapter plugins.
|
|
235
|
-
*
|
|
236
|
-
* Uses annotation-based `replace` callback to make `@meta.id` and
|
|
237
|
-
* `@db.default` fields optional — works at all nesting levels
|
|
238
|
-
* (including inside nav field target types).
|
|
239
|
-
*/
|
|
240
|
-
protected _buildValidator(purpose: string): Validator<T, DataType>;
|
|
241
|
-
}
|
|
242
|
-
//#endregion
|
|
243
|
-
//#region src/query/query-tree.d.ts
|
|
244
|
-
/** A single join in a view query plan. */
|
|
245
|
-
interface TViewJoin {
|
|
246
|
-
targetType: () => TAtscriptAnnotatedType;
|
|
247
|
-
targetTable: string;
|
|
248
|
-
condition: AtscriptQueryNode;
|
|
249
|
-
}
|
|
250
|
-
/** Resolved view query plan produced by AtscriptDbView. */
|
|
251
|
-
interface TViewPlan {
|
|
252
|
-
entryType: () => TAtscriptAnnotatedType;
|
|
253
|
-
entryTable: string;
|
|
254
|
-
joins: TViewJoin[];
|
|
255
|
-
filter?: AtscriptQueryNode;
|
|
256
|
-
having?: AtscriptQueryNode;
|
|
257
|
-
materialized: boolean;
|
|
258
|
-
}
|
|
259
|
-
/**
|
|
260
|
-
* Translates a JS-emitted query tree into a FilterExpr.
|
|
261
|
-
* Resolves field references (type + field path) to physical column names
|
|
262
|
-
* via the provided resolver function.
|
|
263
|
-
*/
|
|
264
|
-
declare function translateQueryTree(node: AtscriptQueryNode, resolveField: (ref: AtscriptQueryFieldRef) => string): FilterExpr;
|
|
265
|
-
//#endregion
|
|
266
|
-
//#region src/table/db-view.d.ts
|
|
267
|
-
interface TViewColumnMapping {
|
|
268
|
-
viewColumn: string;
|
|
269
|
-
sourceTable: string;
|
|
270
|
-
sourceColumn: string;
|
|
271
|
-
/** Aggregate function name ('sum'|'avg'|'count'|'min'|'max') if this is an aggregate column. */
|
|
272
|
-
aggFn?: string;
|
|
273
|
-
/** Source field for the aggregate function ('*' for COUNT(*)). */
|
|
274
|
-
aggField?: string;
|
|
275
|
-
}
|
|
276
|
-
/**
|
|
277
|
-
* Database view abstraction driven by Atscript `@db.view.*` annotations.
|
|
278
|
-
*
|
|
279
|
-
* Extends {@link AtscriptDbReadable} with view plan resolution — entry table,
|
|
280
|
-
* joins, filter, and materialization flag. Read operations are inherited;
|
|
281
|
-
* write operations are not available on views.
|
|
282
|
-
*
|
|
283
|
-
* ```typescript
|
|
284
|
-
* const adapter = new SqliteAdapter(db)
|
|
285
|
-
* const activeUsers = new AtscriptDbView(ActiveUsersType, adapter)
|
|
286
|
-
* const users = await activeUsers.findMany({ filter: {}, controls: {} })
|
|
287
|
-
* ```
|
|
288
|
-
*/
|
|
289
|
-
declare class AtscriptDbView<T extends TAtscriptAnnotatedType = TAtscriptAnnotatedType, DataType = TAtscriptDataType<T>, FlatType = NullableOptional<FlatOf<T>>, A extends BaseDbAdapter = BaseDbAdapter, IdType = PrimaryKeyOf<T>, OwnProps = NullableOptional<OwnPropsOf<T>>, NavType extends Record<string, unknown> = NavPropsOf<T>> extends AtscriptDbReadable<T, DataType, FlatType, A, IdType, OwnProps, NavType> {
|
|
290
|
-
private _viewPlan?;
|
|
291
|
-
get isView(): boolean;
|
|
292
|
-
/**
|
|
293
|
-
* Whether this is an external view — declared with `@db.view` only,
|
|
294
|
-
* without `@db.view.for`. External views reference pre-existing DB views
|
|
295
|
-
* and are not managed (created/dropped) by schema sync.
|
|
296
|
-
*/
|
|
297
|
-
get isExternal(): boolean;
|
|
298
|
-
/**
|
|
299
|
-
* Lazily resolves the view plan from `@db.view.*` metadata.
|
|
300
|
-
*
|
|
301
|
-
* - `db.view.for` → entry type ref (required)
|
|
302
|
-
* - `db.view.joins` → array of `{ target, condition }` (optional, multiple)
|
|
303
|
-
* - `db.view.filter` → query tree (optional)
|
|
304
|
-
* - `db.view.materialized` → boolean (optional)
|
|
305
|
-
*/
|
|
306
|
-
get viewPlan(): TViewPlan;
|
|
307
|
-
/**
|
|
308
|
-
* Resolves a query field ref to a quoted `table.column` SQL fragment.
|
|
309
|
-
*
|
|
310
|
-
* @param ref - The field reference from the query tree.
|
|
311
|
-
* @param qi - Identifier quoting function (e.g. backtick for MySQL, double-quote for SQLite).
|
|
312
|
-
* Defaults to double-quote wrapping for backwards compatibility.
|
|
313
|
-
*/
|
|
314
|
-
resolveFieldRef(ref: AtscriptQueryFieldRef, qi?: (name: string) => string): string;
|
|
315
|
-
/**
|
|
316
|
-
* Maps each view field to its source table and column via ref chain.
|
|
317
|
-
* Fields without refs (inline definitions) map to the entry table with the same name.
|
|
318
|
-
*/
|
|
319
|
-
getViewColumnMappings(): TViewColumnMapping[];
|
|
320
|
-
}
|
|
321
|
-
/**
|
|
322
|
-
* Structural type guard for views: `true` when the readable reports
|
|
323
|
-
* `isView`, whether or not it is an `AtscriptDbView` instance of THIS copy
|
|
324
|
-
* of `@atscript/db`. Adapters must use this (or `readable.isView`) instead of
|
|
325
|
-
* `instanceof AtscriptDbView` — in a bundle that carries two copies of the
|
|
326
|
-
* core (app bundle + external adapter), `instanceof` is false and the adapter
|
|
327
|
-
* would create an empty physical table under the view's name.
|
|
328
|
-
* @since 0.1.128
|
|
329
|
-
*/
|
|
330
|
-
declare function isAtscriptDbView(readable: AtscriptDbReadable<any, any, any, any, any, any, any>): readable is AtscriptDbView<any, any, any, any, any, any, any>;
|
|
331
|
-
//#endregion
|
|
332
|
-
//#region src/table/db-space.d.ts
|
|
333
|
-
/**
|
|
334
|
-
* Adapter factory function. Called once per table/view to create a fresh adapter instance.
|
|
335
|
-
* Each readable gets its own adapter (1:1 relationship required by BaseDbAdapter).
|
|
336
|
-
*/
|
|
337
|
-
type TAdapterFactory = () => BaseDbAdapter;
|
|
338
|
-
/** Options bag for {@link DbSpace} (second constructor argument). */
|
|
339
|
-
interface TDbSpaceOptions {
|
|
340
|
-
/** Logger shared by all tables/views in the space. */
|
|
341
|
-
logger?: TGenericLogger;
|
|
342
|
-
/** Field-level encryption configuration for `@db.encrypted` fields. */
|
|
343
|
-
encryption?: TDbEncryptionOptions;
|
|
344
|
-
}
|
|
345
|
-
/**
|
|
346
|
-
* A database space — a registry of tables and views sharing the same adapter type and driver.
|
|
347
|
-
*
|
|
348
|
-
* `DbSpace` solves the cross-table discovery problem: when table A has a relation
|
|
349
|
-
* to table B, it needs to find and query table B. The space acts as the registry
|
|
350
|
-
* that makes this possible via the table resolver callback.
|
|
351
|
-
*
|
|
352
|
-
* Each table/view gets its own adapter instance (created by the factory), but all
|
|
353
|
-
* share the same space and can discover each other for `$with` relation loading.
|
|
354
|
-
*
|
|
355
|
-
* ```typescript
|
|
356
|
-
* // SQLite
|
|
357
|
-
* const driver = new BetterSqlite3Driver(':memory:')
|
|
358
|
-
* const db = new DbSpace(() => new SqliteAdapter(driver))
|
|
359
|
-
* const users = db.getTable(UsersType)
|
|
360
|
-
* const activeUsers = db.getView(ActiveUsersType)
|
|
361
|
-
* ```
|
|
362
|
-
*/
|
|
363
|
-
declare class DbSpace {
|
|
364
|
-
protected readonly adapterFactory: TAdapterFactory;
|
|
365
|
-
private _readables;
|
|
366
|
-
/** All tables created in this space — used for reverse FK lookup during cascade. */
|
|
367
|
-
private _allTables;
|
|
368
|
-
/** Lazily created adapter for administrative ops (drop table/view) that don't need a registered readable. */
|
|
369
|
-
private _adminAdapter?;
|
|
370
|
-
protected readonly logger: TGenericLogger;
|
|
371
|
-
/** Encryption service for `@db.encrypted` fields — validated eagerly at construction. */
|
|
372
|
-
protected readonly _encryption?: DbEncryption;
|
|
373
|
-
/**
|
|
374
|
-
* @param adapterFactory - Creates a fresh adapter per table/view.
|
|
375
|
-
* @param loggerOrOptions - Either a logger (legacy signature) or a
|
|
376
|
-
* {@link TDbSpaceOptions} bag carrying `logger` and/or `encryption`.
|
|
377
|
-
*/
|
|
378
|
-
constructor(adapterFactory: TAdapterFactory, loggerOrOptions?: TGenericLogger | TDbSpaceOptions);
|
|
379
|
-
/**
|
|
380
|
-
* Auto-detects whether the type is a table or view and returns the
|
|
381
|
-
* appropriate instance. Uses `@db.view` or `@db.view.for` presence to distinguish.
|
|
382
|
-
*/
|
|
383
|
-
get<T extends TAtscriptAnnotatedType>(type: T, logger?: TGenericLogger): AtscriptDbReadable<T>;
|
|
384
|
-
/**
|
|
385
|
-
* Returns the table for the given annotated type.
|
|
386
|
-
* Creates the table + adapter on first access, caches for subsequent calls.
|
|
387
|
-
*/
|
|
388
|
-
getTable<T extends TAtscriptAnnotatedType>(type: T, logger?: TGenericLogger): AtscriptDbTable<T>;
|
|
389
|
-
/**
|
|
390
|
-
* Returns the view for the given annotated type.
|
|
391
|
-
* Creates the view + adapter on first access, caches for subsequent calls.
|
|
392
|
-
*/
|
|
393
|
-
getView<T extends TAtscriptAnnotatedType>(type: T, logger?: TGenericLogger): AtscriptDbView<T>;
|
|
394
|
-
/**
|
|
395
|
-
* Returns the adapter for the given annotated type.
|
|
396
|
-
* Creates the table/view + adapter on first access if needed.
|
|
397
|
-
*/
|
|
398
|
-
getAdapter(type: TAtscriptAnnotatedType): BaseDbAdapter;
|
|
399
|
-
/**
|
|
400
|
-
* Drops a table by name. Used by schema sync to remove tables no longer in the schema.
|
|
401
|
-
*/
|
|
402
|
-
dropTableByName(tableName: string): Promise<void>;
|
|
403
|
-
/**
|
|
404
|
-
* Drops a view by name. Used by schema sync to remove views no longer in the schema.
|
|
405
|
-
*/
|
|
406
|
-
dropViewByName(viewName: string): Promise<void>;
|
|
407
|
-
/**
|
|
408
|
-
* Drops a group of mutually referencing tables as one operation.
|
|
409
|
-
* Used by schema sync to remove a foreign-key cycle no longer in the schema.
|
|
410
|
-
* @since 0.1.128
|
|
411
|
-
*/
|
|
412
|
-
dropTablesByName(tableNames: string[]): Promise<void>;
|
|
413
|
-
/**
|
|
414
|
-
* Live foreign keys referencing `tableName`, or `undefined` when the
|
|
415
|
-
* adapter cannot introspect them. Used by schema sync for drop ordering
|
|
416
|
-
* and surviving-reference checks of tables without a registered readable.
|
|
417
|
-
* @since 0.1.128
|
|
418
|
-
*/
|
|
419
|
-
getReferencingForeignKeys(tableName: string): Promise<TReferencingForeignKey[] | undefined>;
|
|
420
|
-
/**
|
|
421
|
-
* A factory-fresh adapter with NO registered readable. Only the name-taking
|
|
422
|
-
* primitives may run on it (`dropTableByName`, `dropViewByName`,
|
|
423
|
-
* `dropTablesByName`, `getReferencingForeignKeys`) — adapters derive the
|
|
424
|
-
* schema for those from the driver/connection, not from a bound table.
|
|
425
|
-
*/
|
|
426
|
-
private _getAdminAdapter;
|
|
427
|
-
/**
|
|
428
|
-
* Finds all child tables with FKs pointing to the given parent table name.
|
|
429
|
-
* Accesses `table.foreignKeys` which triggers `_flatten()` if needed.
|
|
430
|
-
*/
|
|
431
|
-
private _getCascadeTargets;
|
|
432
|
-
/**
|
|
433
|
-
* Resolves a table name to a queryable target for FK validation.
|
|
434
|
-
* Searches all registered tables for one with the matching table name.
|
|
435
|
-
*/
|
|
436
|
-
private _getFkLookupTarget;
|
|
437
|
-
}
|
|
438
|
-
//#endregion
|
|
439
|
-
export { TViewColumnMapping as a, AtscriptQueryFieldRef$1 as c, TViewJoin as d, TViewPlan as f, NativeIntegrity as g, IntegrityStrategy as h, AtscriptDbView as i, AtscriptQueryNode$1 as l, AtscriptDbTable as m, TAdapterFactory as n, isAtscriptDbView as o, translateQueryTree as p, TDbSpaceOptions as r, AtscriptQueryComparison as s, DbSpace as t, AtscriptRef as u };
|