@atscript/db 0.1.127 → 0.1.128

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.
Files changed (33) hide show
  1. package/dist/{db-readable-BkAGccv9.d.mts → db-readable-BTl4NzNN.d.mts} +289 -16
  2. package/dist/{db-readable-C0nDKX8A.d.cts → db-readable-CkbZn_z-.d.cts} +289 -16
  3. package/dist/{db-space-B_ASuDaR.d.mts → db-space-BEB5Jl8M.d.mts} +81 -29
  4. package/dist/{db-space-CSntT6yS.d.cts → db-space-BevOyVrC.d.cts} +81 -29
  5. package/dist/{db-view-BP0Qbeux.cjs → db-view-C8-MsjND.cjs} +696 -97
  6. package/dist/{db-view-C8rZM5_N.mjs → db-view-Ck0I-PMI.mjs} +643 -98
  7. package/dist/index.cjs +46 -2
  8. package/dist/index.d.cts +162 -37
  9. package/dist/index.d.mts +162 -37
  10. package/dist/index.mjs +36 -4
  11. package/dist/{ops-DJRnNTVo.d.cts → ops-AqhV7s9o.d.cts} +24 -1
  12. package/dist/{ops-DJRnNTVo.d.mts → ops-AqhV7s9o.d.mts} +24 -1
  13. package/dist/ops.cjs +43 -0
  14. package/dist/ops.d.cts +2 -2
  15. package/dist/ops.d.mts +2 -2
  16. package/dist/ops.mjs +43 -1
  17. package/dist/plugin.cjs +12 -5
  18. package/dist/plugin.mjs +12 -5
  19. package/dist/rel.d.cts +1 -1
  20. package/dist/rel.d.mts +1 -1
  21. package/dist/sync.cjs +1096 -361
  22. package/dist/sync.d.cts +234 -15
  23. package/dist/sync.d.mts +234 -15
  24. package/dist/sync.mjs +1095 -362
  25. package/dist/{validator-CSGug4vg.cjs → validator-BNUHCXIE.cjs} +31 -2
  26. package/dist/{validator-BcBtg8yW.d.cts → validator-DuAmWc8L.d.cts} +38 -1
  27. package/dist/{validator-BcBtg8yW.d.mts → validator-DuAmWc8L.d.mts} +38 -1
  28. package/dist/{validator-0vRXN51D.mjs → validator-eKYWf3xf.mjs} +20 -3
  29. package/dist/validator.cjs +7 -1
  30. package/dist/validator.d.cts +3 -3
  31. package/dist/validator.d.mts +3 -3
  32. package/dist/validator.mjs +4 -3
  33. package/package.json +8 -8
@@ -1,4 +1,4 @@
1
- import { C as TCascadeResolver, F as TDbDeleteResult, H as TDbInsertResult, K as TDbUpdateResult, V as TDbInsertManyResult, X as TFkLookupResolver, _t as TGenericLogger, a as TDbEncryptionOptions, c as BaseDbAdapter, ct as TWriteTableResolver, i as DbEncryption, ot as TTableResolver, pt as TableMetadata, t as AtscriptDbReadable } from "./db-readable-C0nDKX8A.cjs";
1
+ import { C as NullableOptional, Ct as TWriteTableResolver, E as TCascadeResolver, G as TDbInsertResult, Nt as TGenericLogger, Ot as TableMetadata, Q as TDbUpdateResult, R as TDbDeleteResult, St as TWriteOptions, W as TDbInsertManyResult, a as TDbEncryptionOptions, bt as TTableResolver, c as BaseDbAdapter, ct as TFkLookupResolver, g as DbPatch, ht as TReferencingForeignKey, i as DbEncryption, nt as TDeleteOptions, t as AtscriptDbReadable, v as DbRow } from "./db-readable-CkbZn_z-.cjs";
2
2
  import { FilterExpr } from "@uniqu/core";
3
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
4
 
@@ -24,7 +24,7 @@ declare class NativeIntegrity extends IntegrityStrategy {
24
24
  }
25
25
  //#endregion
26
26
  //#region src/table/db-table.d.ts
27
- declare class AtscriptDbTable<T extends TAtscriptAnnotatedType = TAtscriptAnnotatedType, DataType = TAtscriptDataType<T>, FlatType = FlatOf<T>, A extends BaseDbAdapter = BaseDbAdapter, IdType = PrimaryKeyOf<T>, OwnProps = OwnPropsOf<T>, NavType extends Record<string, unknown> = NavPropsOf<T>> extends AtscriptDbReadable<T, DataType, FlatType, A, IdType, OwnProps, NavType> {
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
28
  protected _cascadeResolver?: TCascadeResolver;
29
29
  protected _fkLookupResolver?: TFkLookupResolver;
30
30
  protected readonly _integrity: IntegrityStrategy;
@@ -53,9 +53,7 @@ declare class AtscriptDbTable<T extends TAtscriptAnnotatedType = TAtscriptAnnota
53
53
  * Inserts a single record. Delegates to {@link insertMany} for unified
54
54
  * nested creation support.
55
55
  */
56
- insertOne(payload: Partial<DataType> & Record<string, unknown>, opts?: {
57
- maxDepth?: number;
58
- }): Promise<TDbInsertResult>;
56
+ insertOne(payload: DbPatch<DataType>, opts?: TWriteOptions<DataType>): Promise<TDbInsertResult>;
59
57
  /**
60
58
  * Inserts multiple records with batch-optimized nested creation.
61
59
  *
@@ -66,17 +64,16 @@ declare class AtscriptDbTable<T extends TAtscriptAnnotatedType = TAtscriptAnnota
66
64
  * (they receive our PKs as their FKs). Fully recursive — nested records
67
65
  * with their own nav data trigger further batch inserts at each level.
68
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}.
69
70
  */
70
- insertMany(payloads: Array<Partial<DataType> & Record<string, unknown>>, opts?: {
71
- maxDepth?: number;
72
- }): Promise<TDbInsertManyResult>;
71
+ insertMany(payloads: Array<DbPatch<DataType>>, opts?: TWriteOptions<DataType>): Promise<TDbInsertManyResult>;
73
72
  /**
74
73
  * Replaces a single record identified by primary key(s).
75
74
  * Delegates to {@link bulkReplace} for unified nested relation support.
76
75
  */
77
- replaceOne(payload: DataType & Record<string, unknown>, opts?: {
78
- maxDepth?: number;
79
- }): Promise<TDbUpdateResult>;
76
+ replaceOne(payload: DbRow<DataType>, opts?: TWriteOptions<DataType>): Promise<TDbUpdateResult>;
80
77
  /**
81
78
  * Replaces multiple records with deep nested relation support.
82
79
  *
@@ -84,37 +81,43 @@ declare class AtscriptDbTable<T extends TAtscriptAnnotatedType = TAtscriptAnnota
84
81
  * replaced first (their PKs become our FKs), FROM dependents are replaced
85
82
  * after (they receive our PKs as their FKs), VIA relations clear and
86
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
87
  */
88
- bulkReplace(payloads: Array<DataType & Record<string, unknown>>, opts?: {
89
- maxDepth?: number;
90
- }): Promise<TDbUpdateResult>;
88
+ bulkReplace(payloads: Array<DbRow<DataType>>, opts?: TWriteOptions<DataType>): Promise<TDbUpdateResult>;
91
89
  /**
92
90
  * Partially updates a single record identified by primary key(s).
93
91
  * Delegates to {@link bulkUpdate} for unified nested relation support.
94
92
  */
95
- updateOne(payload: Partial<DataType> & Record<string, unknown>, opts?: {
96
- maxDepth?: number;
97
- }): Promise<TDbUpdateResult>;
93
+ updateOne(payload: DbPatch<DataType>, opts?: TWriteOptions<DataType>): Promise<TDbUpdateResult>;
98
94
  /**
99
95
  * Partially updates multiple records with deep nested relation support.
100
96
  *
101
97
  * Only TO relations (1:1, N:1) are supported for patching. FROM/VIA
102
98
  * relations will error — use {@link bulkReplace} for those.
103
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
104
  */
105
- bulkUpdate(payloads: Array<Partial<DataType> & Record<string, unknown>>, opts?: {
106
- maxDepth?: number;
107
- }): Promise<TDbUpdateResult>;
105
+ bulkUpdate(payloads: Array<DbPatch<DataType>>, opts?: TWriteOptions<DataType>): Promise<TDbUpdateResult>;
108
106
  /**
109
107
  * Deletes a single record by any type-compatible identifier — primary key
110
108
  * or single-field unique index. Uses the same resolution logic as `findById`.
111
109
  *
112
110
  * When the adapter does not support native foreign keys (e.g. MongoDB),
113
111
  * cascade and setNull actions are applied before the delete.
114
- */
115
- deleteOne(id: IdType): Promise<TDbDeleteResult>;
116
- updateMany(filter: FilterExpr<FlatType>, data: Partial<DataType> & Record<string, unknown>): Promise<TDbUpdateResult>;
117
- replaceMany(filter: FilterExpr<FlatType>, data: Record<string, unknown>): Promise<TDbUpdateResult>;
112
+ *
113
+ * `opts.guard` (since 0.1.128) runs inside the transaction once the id has
114
+ * resolved to a filter, before cascade / delete — see {@link TDeleteOptions}.
115
+ * An id that resolves to no filter answers `{ deletedCount: 0 }` without
116
+ * calling the guard.
117
+ */
118
+ deleteOne(id: IdType, opts?: TDeleteOptions<DataType>): Promise<TDbDeleteResult>;
119
+ updateMany(filter: FilterExpr<FlatType>, data: DbPatch<DataType>): Promise<TDbUpdateResult>;
120
+ replaceMany(filter: FilterExpr<FlatType>, data: DbRow<DataType>): Promise<TDbUpdateResult>;
118
121
  deleteMany(filter: FilterExpr<FlatType>): Promise<TDbDeleteResult>;
119
122
  /**
120
123
  * Synchronizes indexes between Atscript definitions and the database.
@@ -140,11 +143,31 @@ declare class AtscriptDbTable<T extends TAtscriptAnnotatedType = TAtscriptAnnota
140
143
  */
141
144
  protected _encryptItems(items: Array<Record<string, unknown>>, mode: "write" | "patch"): Promise<void>;
142
145
  /**
143
- * Applies default values for fields that are missing from the payload.
144
- * Defaults handled natively by the DB engine are skipped — the field stays
145
- * absent so the DB's own DEFAULT clause applies.
146
+ * Lazy pre-image read for a guard's `current(i)`: `null` when the row has
147
+ * no identifying key (e.g. an auto-increment insert) or the key cannot be
148
+ * resolved — never throws for a missing key.
149
+ * @internal
150
+ */
151
+ _readPreImage(row: unknown): Promise<DataType | null>;
152
+ /**
153
+ * Applies `@db.default` values in place to a row's absent fields — the
154
+ * defaults pass every insert / replace path runs before validation.
155
+ * Static value defaults (`@db.default 'x'`) are filled on EVERY adapter
156
+ * (since 0.1.128 — writing the column's own default explicitly is
157
+ * equivalent to leaving it to the DDL `DEFAULT`, and write guards see the
158
+ * full row). Function defaults (`now` / `uuid` / `increment` / custom) the
159
+ * adapter handles natively are NOT filled — the field stays absent so the
160
+ * engine's own default applies. The version column is never touched.
146
161
  */
147
162
  protected _applyDefaults(data: Record<string, unknown>): Record<string, unknown>;
163
+ /**
164
+ * The JS value for a `@db.default 'literal'`: strings (including unions of
165
+ * string literals) are used as-is, every other design type is parsed as
166
+ * JSON — the same value the SQL adapters put into the DDL `DEFAULT` clause.
167
+ * A literal that is not valid JSON falls back to the raw string so the
168
+ * validator reports it against the field instead of a bare `SyntaxError`.
169
+ */
170
+ private _parseValueDefault;
148
171
  /**
149
172
  * Extracts a record-identifying filter from a payload.
150
173
  *
@@ -235,7 +258,7 @@ interface TViewColumnMapping {
235
258
  * const users = await activeUsers.findMany({ filter: {}, controls: {} })
236
259
  * ```
237
260
  */
238
- declare class AtscriptDbView<T extends TAtscriptAnnotatedType = TAtscriptAnnotatedType, DataType = TAtscriptDataType<T>, FlatType = FlatOf<T>, A extends BaseDbAdapter = BaseDbAdapter, IdType = PrimaryKeyOf<T>, OwnProps = OwnPropsOf<T>, NavType extends Record<string, unknown> = NavPropsOf<T>> extends AtscriptDbReadable<T, DataType, FlatType, A, IdType, OwnProps, NavType> {
261
+ 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> {
239
262
  private _viewPlan?;
240
263
  get isView(): boolean;
241
264
  /**
@@ -267,6 +290,16 @@ declare class AtscriptDbView<T extends TAtscriptAnnotatedType = TAtscriptAnnotat
267
290
  */
268
291
  getViewColumnMappings(): TViewColumnMapping[];
269
292
  }
293
+ /**
294
+ * Structural type guard for views: `true` when the readable reports
295
+ * `isView`, whether or not it is an `AtscriptDbView` instance of THIS copy
296
+ * of `@atscript/db`. Adapters must use this (or `readable.isView`) instead of
297
+ * `instanceof AtscriptDbView` — in a bundle that carries two copies of the
298
+ * core (app bundle + external adapter), `instanceof` is false and the adapter
299
+ * would create an empty physical table under the view's name.
300
+ * @since 0.1.128
301
+ */
302
+ declare function isAtscriptDbView(readable: AtscriptDbReadable<any, any, any, any, any, any, any>): readable is AtscriptDbView<any, any, any, any, any, any, any>;
270
303
  //#endregion
271
304
  //#region src/table/db-space.d.ts
272
305
  /**
@@ -343,6 +376,25 @@ declare class DbSpace {
343
376
  * Drops a view by name. Used by schema sync to remove views no longer in the schema.
344
377
  */
345
378
  dropViewByName(viewName: string): Promise<void>;
379
+ /**
380
+ * Drops a group of mutually referencing tables as one operation.
381
+ * Used by schema sync to remove a foreign-key cycle no longer in the schema.
382
+ * @since 0.1.128
383
+ */
384
+ dropTablesByName(tableNames: string[]): Promise<void>;
385
+ /**
386
+ * Live foreign keys referencing `tableName`, or `undefined` when the
387
+ * adapter cannot introspect them. Used by schema sync for drop ordering
388
+ * and surviving-reference checks of tables without a registered readable.
389
+ * @since 0.1.128
390
+ */
391
+ getReferencingForeignKeys(tableName: string): Promise<TReferencingForeignKey[] | undefined>;
392
+ /**
393
+ * A factory-fresh adapter with NO registered readable. Only the name-taking
394
+ * primitives may run on it (`dropTableByName`, `dropViewByName`,
395
+ * `dropTablesByName`, `getReferencingForeignKeys`) — adapters derive the
396
+ * schema for those from the driver/connection, not from a bound table.
397
+ */
346
398
  private _getAdminAdapter;
347
399
  /**
348
400
  * Finds all child tables with FKs pointing to the given parent table name.
@@ -356,4 +408,4 @@ declare class DbSpace {
356
408
  private _getFkLookupTarget;
357
409
  }
358
410
  //#endregion
359
- export { TViewColumnMapping as a, AtscriptQueryNode$1 as c, TViewPlan as d, translateQueryTree as f, NativeIntegrity as h, AtscriptDbView as i, AtscriptRef as l, IntegrityStrategy as m, TAdapterFactory as n, AtscriptQueryComparison as o, AtscriptDbTable as p, TDbSpaceOptions as r, AtscriptQueryFieldRef$1 as s, DbSpace as t, TViewJoin as u };
411
+ 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 };