@lunora/server 0.0.0 → 1.0.0-alpha.10

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 (45) hide show
  1. package/LICENSE.md +105 -0
  2. package/README.md +134 -9
  3. package/__assets__/package-og.svg +14 -0
  4. package/dist/data-model.d.mts +416 -0
  5. package/dist/data-model.d.ts +416 -0
  6. package/dist/data-model.mjs +1 -0
  7. package/dist/drizzle.d.mts +1 -0
  8. package/dist/drizzle.d.ts +1 -0
  9. package/dist/drizzle.mjs +1 -0
  10. package/dist/index.d.mts +1985 -0
  11. package/dist/index.d.ts +1985 -0
  12. package/dist/index.mjs +28 -0
  13. package/dist/packem_shared/LunoraEnvError-DjFkpkSP.mjs +187 -0
  14. package/dist/packem_shared/LunoraError-DN7Zhhvu.mjs +54 -0
  15. package/dist/packem_shared/PRESENCE_DEFAULT_TTL_MS-D8viLY1S.mjs +114 -0
  16. package/dist/packem_shared/asBucketStorage-Cnxd9y2q.mjs +11 -0
  17. package/dist/packem_shared/bindOrm-Ce57S3N9.mjs +128 -0
  18. package/dist/packem_shared/buildRlsReadRegistry-1jexWrb3.mjs +107 -0
  19. package/dist/packem_shared/composePluginMiddleware-Ck5_TUO8.mjs +100 -0
  20. package/dist/packem_shared/createPolicyDsl-De67zPDS.mjs +29 -0
  21. package/dist/packem_shared/createSecrets-TsIP9lOa.mjs +55 -0
  22. package/dist/packem_shared/defineAggregateIndex-ZdyU78gh.mjs +291 -0
  23. package/dist/packem_shared/defineMigration-CAJLr6fx.mjs +8 -0
  24. package/dist/packem_shared/defineMutator-EIXAWhs9.mjs +11 -0
  25. package/dist/packem_shared/defineShape-CJ27Wx7o.mjs +17 -0
  26. package/dist/packem_shared/defineStorageRule-qu0mpilX.mjs +20 -0
  27. package/dist/packem_shared/functions-Di9FUNkf.mjs +5 -0
  28. package/dist/packem_shared/httpAction-FLwfsePg.mjs +340 -0
  29. package/dist/packem_shared/initLunora-lxwHTEV3.mjs +100 -0
  30. package/dist/packem_shared/mask-BV_jNzsN.mjs +211 -0
  31. package/dist/packem_shared/onConnect-CIPXKPyw.mjs +13 -0
  32. package/dist/packem_shared/policy-tag-DvpVH2tv.mjs +13 -0
  33. package/dist/packem_shared/protectPublic-BjFkQ_Or.mjs +15 -0
  34. package/dist/packem_shared/rls-2Jhd0uev.mjs +569 -0
  35. package/dist/packem_shared/run-middleware-CYQOuoV6.mjs +18 -0
  36. package/dist/packem_shared/storageRules-Cje6Woea.mjs +88 -0
  37. package/dist/packem_shared/types.d-BDY0FYHK.d.ts +135 -0
  38. package/dist/packem_shared/types.d-DmvyEMD6.d.mts +135 -0
  39. package/dist/rls/testing.d.mts +63 -0
  40. package/dist/rls/testing.d.ts +63 -0
  41. package/dist/rls/testing.mjs +49 -0
  42. package/dist/types.d.mts +1157 -0
  43. package/dist/types.d.ts +1157 -0
  44. package/dist/types.mjs +31 -0
  45. package/package.json +59 -17
@@ -0,0 +1,416 @@
1
+ /**
2
+ * Schema-independent type machinery for the generated data model.
3
+ *
4
+ * `@lunora/codegen` emits `lunora/_generated/dataModel.ts` with the
5
+ * schema-specific pieces (the per-table `Doc_*` / `Insert_*` interfaces, the
6
+ * `DataModel` / `Relations` / index-name maps) and then binds the generics
7
+ * below to them. Everything here is identical for every project, so it lives
8
+ * in the shipped package rather than in generated output — evolving the query
9
+ * DSL or the table-facade API no longer regenerates a single line of a user's
10
+ * `_generated` directory.
11
+ *
12
+ * The generics are parameterized over the generated maps:
13
+ * - `DM` — `DataModel`: table name → document type
14
+ * - `IM` — `InsertModel`: table name → insert shape
15
+ * - `REL` — `Relations`: table name → relation-descriptor map
16
+ * - `RANK` — `RankIndexNamesByTable`: table name → declared rank-index names
17
+ * - `SEARCH` — `SearchIndexNamesByTable`: table name → declared search-index names
18
+ *
19
+ * Relation descriptors are matched structurally (`{ __relationKind; __target }`)
20
+ * so this module needs no reference to the project-local `OneRelation` /
21
+ * `ManyRelation` aliases the codegen still emits.
22
+ */
23
+ /** A branded id for table `TName`. Structurally a `string` at runtime. */
24
+ type Id<TName extends string> = string & {
25
+ readonly __table: TName;
26
+ };
27
+ /** Field-level operators for the typed `where` DSL (see `@lunora/do`'s compiler). */
28
+ interface WhereOperators<T> {
29
+ contains?: string;
30
+ eq?: T;
31
+ gt?: T;
32
+ gte?: T;
33
+ in?: T[];
34
+ isNull?: boolean;
35
+ lt?: T;
36
+ lte?: T;
37
+ ne?: T;
38
+ notIn?: T[];
39
+ }
40
+ /** A typed `where` tree over a document's columns. */
41
+ type Where<TDocument> = { [K in keyof TDocument]?: TDocument[K] | WhereOperators<TDocument[K]> } & {
42
+ AND?: Where<TDocument>[];
43
+ NOT?: Where<TDocument>;
44
+ OR?: Where<TDocument>[];
45
+ };
46
+ /** One `{ field: "asc" | "desc" }` ordering entry; `orderBy` is an ordered list. */
47
+ type OrderBy<TDocument> = Partial<Record<keyof TDocument, "asc" | "desc">>;
48
+ interface QueryArgs<TDocument> {
49
+ cursor?: null | string;
50
+ limit?: number;
51
+ orderBy?: OrderBy<TDocument>[];
52
+ /**
53
+ * Project each returned row down to these columns (plus the system fields
54
+ * `_id`/`_creationTime`, always retained). Trims wire payload for wide rows;
55
+ * relations requested via `with` are still attached. Reactivity is unaffected —
56
+ * the engine still reads the whole row to track dependencies.
57
+ */
58
+ select?: ReadonlyArray<keyof TDocument & string>;
59
+ where?: Where<TDocument>;
60
+ }
61
+ /**
62
+ * A to-one relation predicate node. `is` matches rows whose related record
63
+ * satisfies `W`; `isNot` matches rows whose related record fails `W` *or* has
64
+ * no related record at all (a null/dangling FK) — Prisma's semantics.
65
+ */
66
+ interface OneRelationWhere<W> {
67
+ is?: W;
68
+ isNot?: W;
69
+ }
70
+ /**
71
+ * A to-many relation predicate node. `some` ⇒ at least one related row matches
72
+ * `W`; `none` ⇒ no related row matches (childless parents included); `every` ⇒
73
+ * every *readable* related row matches (vacuously true for childless parents).
74
+ */
75
+ interface ManyRelationWhere<W> {
76
+ every?: W;
77
+ none?: W;
78
+ some?: W;
79
+ }
80
+ /**
81
+ * The relation-predicate portion of {@link WhereOf}: each declared relation on
82
+ * `T` contributes a kind-dispatched node — `one` → `{ is?; isNot? }`, `many` →
83
+ * `{ some?; none?; every? }` — whose inner type is the target table's own
84
+ * relation-aware `where` (so multi-hop predicates type-check inside-out).
85
+ */
86
+ type RelationWhere<DM, REL extends Record<keyof DM, object>, T extends keyof DM> = { [K in keyof REL[T]]?: REL[T][K] extends {
87
+ __relationKind: "one";
88
+ __target: infer Target extends keyof DM;
89
+ } ? OneRelationWhere<WhereOf<DM, REL, Target>> : REL[T][K] extends {
90
+ __relationKind: "many";
91
+ __target: infer Target extends keyof DM;
92
+ } ? ManyRelationWhere<WhereOf<DM, REL, Target>> : never };
93
+ /**
94
+ * Relation-aware `where` tree — the column predicates of {@link Where} plus
95
+ * Prisma-style relation predicates resolved by the `@lunora/do` pre-resolver.
96
+ * `Where&lt;DM[T]>` stays the column-only structural mirror for back-compat; the
97
+ * table facade threads `REL` through this richer form.
98
+ */
99
+ type WhereOf<DM, REL extends Record<keyof DM, object>, T extends keyof DM> = RelationWhere<DM, REL, T> & { [K in keyof DM[T]]?: DM[T][K] | WhereOperators<DM[T][K]> } & {
100
+ AND?: WhereOf<DM, REL, T>[];
101
+ NOT?: WhereOf<DM, REL, T>;
102
+ OR?: WhereOf<DM, REL, T>[];
103
+ };
104
+ /** {@link QueryArgs} with the relation-aware {@link WhereOf} `where` typing. */
105
+ interface QueryArgsOf<DM, REL extends Record<keyof DM, object>, T extends keyof DM> {
106
+ cursor?: null | string;
107
+ /**
108
+ * Include soft-deleted rows (`.softDelete()` tables only). Default hides them;
109
+ * `true` returns deleted rows alongside live ones. No effect on a table
110
+ * without `.softDelete()`.
111
+ */
112
+ includeDeleted?: boolean;
113
+ limit?: number;
114
+ orderBy?: OrderBy<DM[T]>[];
115
+ where?: WhereOf<DM, REL, T>;
116
+ }
117
+ interface QueryPage<TDocument> {
118
+ continueCursor: null | string;
119
+ isDone: boolean;
120
+ page: TDocument[];
121
+ }
122
+ /** The nested `with` sub-argument inside a relation's with-value, or `{}`. */
123
+ type NestedWithArgument<WK> = WK extends {
124
+ with: infer NW;
125
+ } ? NW : {};
126
+ /** The nested `select` tuple inside a relation's with-value, or `undefined` (no projection). */
127
+ type NestedSelectArgument<WK> = WK extends {
128
+ select: infer S;
129
+ } ? S : undefined;
130
+ /**
131
+ * The `with` argument for table `T`: each relation can be `true` (load with no
132
+ * refinements) or an object. `many` relations accept `where`/`orderBy`/`limit`/
133
+ * `select` plus a nested `with`; `one` relations accept `select` + a nested
134
+ * `with`. The reserved `_count` key requests per-relation aggregate counts.
135
+ */
136
+ type WithArg<DM, REL extends Record<keyof DM, object>, T extends keyof DM> = { [K in keyof REL[T]]?: REL[T][K] extends {
137
+ __relationKind: "many";
138
+ __target: infer Target extends keyof DM;
139
+ } ? boolean | (QueryArgs<DM[Target]> & {
140
+ with?: WithArg<DM, REL, Target>;
141
+ }) : REL[T][K] extends {
142
+ __relationKind: "one";
143
+ __target: infer Target extends keyof DM;
144
+ } ? boolean | {
145
+ select?: ReadonlyArray<keyof DM[Target] & string>;
146
+ with?: WithArg<DM, REL, Target>;
147
+ } : never } & {
148
+ _count?: { [K in keyof REL[T]]?: true };
149
+ };
150
+ /**
151
+ * Resolve a single relation descriptor + its with-value to the loaded type,
152
+ * threading the nested `select` tuple into the projected child shape (the 5th
153
+ * `LoadWith` arg) so `with: { author: { select: ["name"] } }` narrows the loaded
154
+ * `author` to the selected columns + system fields.
155
+ */
156
+ type LoadRelation<DM, REL extends Record<keyof DM, object>, R, WK> = R extends {
157
+ __relationKind: "one";
158
+ __target: infer Target extends keyof DM;
159
+ } ? LoadWith<DM, REL, Target, NestedWithArgument<WK>, NestedSelectArgument<WK>> | null : R extends {
160
+ __relationKind: "many";
161
+ __target: infer Target extends keyof DM;
162
+ } ? LoadWith<DM, REL, Target, NestedWithArgument<WK>, NestedSelectArgument<WK>>[] : never;
163
+ /** The relation keys of `W` that were actually requested (not `false`/`undefined`). */
164
+ type LoadedRelations<DM, REL extends Record<keyof DM, object>, T extends keyof DM, W> = { [K in keyof W as K extends keyof REL[T] ? (W[K] extends false | undefined ? never : K) : never]: K extends keyof REL[T] ? LoadRelation<DM, REL, REL[T][K], W[K]> : never };
165
+ /** The `_count` projection of `W`, if any. */
166
+ type LoadedCount<W> = W extends {
167
+ _count: infer C;
168
+ } ? {
169
+ _count: { [K in keyof C]: number };
170
+ } : {};
171
+ /** System columns a `select` projection always retains, so cursors and by-id reuse keep working. */
172
+ type SelectAlwaysKeep<DM, T extends keyof DM> = ("_creationTime" | "_id") & keyof DM[T];
173
+ /**
174
+ * `DM[T]` narrowed to the columns named by a `select` tuple `S` (plus the system
175
+ * fields). `undefined` (the default — no `select`) keeps the full document.
176
+ */
177
+ type ProjectDoc<DM, T extends keyof DM, S> = S extends ReadonlyArray<infer K> ? (K extends keyof DM[T] ? Pick<DM[T], (K & keyof DM[T]) | SelectAlwaysKeep<DM, T>> : DM[T]) : DM[T];
178
+ /**
179
+ * `Doc&lt;T>` narrowed to exactly the relations requested in the with-arg `W` and,
180
+ * when a `select` tuple `S` is supplied, to its projected columns. `S` defaults
181
+ * to `undefined` so the 4-argument form (the codegen-emitted callers) keeps the
182
+ * full document.
183
+ */
184
+ type LoadWith<DM, REL extends Record<keyof DM, object>, T extends keyof DM, W, S = undefined> = LoadedCount<W> & LoadedRelations<DM, REL, T, W> & ProjectDoc<DM, T, S>;
185
+ /** Reducer applied by an aggregate (`avg`/`count`/`max`/`min`/`sum`). */
186
+ type AggregateOp = "avg" | "count" | "max" | "min" | "sum";
187
+ /**
188
+ * Query-options shape shared by every aggregate reader. The RLS-aware ctx
189
+ * populates `baseWhere` so it composes here without a hard import.
190
+ * `restrictsCounts: true` flips `count()` into a thrown `COUNT_RLS_UNSUPPORTED`
191
+ * `LunoraError` rather than silently undercount.
192
+ */
193
+ interface RestrictableQueryOptions<TDocument> {
194
+ baseWhere?: Where<TDocument>;
195
+ restrictsCounts?: boolean;
196
+ where?: Where<TDocument>;
197
+ }
198
+ /**
199
+ * Relation-aware twin of {@link RestrictableQueryOptions}. The `@lunora/do`
200
+ * pre-resolver now resolves relation predicates on the `count`/`aggregate`/
201
+ * `groupBy` paths too (semijoin), so the typed surface threads `REL` through
202
+ * `where`/`baseWhere` to match. `rank`/`rankPage` stay column-only — they use
203
+ * `where` solely to pin a partition and fail closed on a relation predicate.
204
+ */
205
+ interface RestrictableQueryOptionsOf<DM, REL extends Record<keyof DM, object>, T extends keyof DM> {
206
+ baseWhere?: WhereOf<DM, REL, T>;
207
+ restrictsCounts?: boolean;
208
+ where?: WhereOf<DM, REL, T>;
209
+ }
210
+ /** Args for `ctx.db.&lt;table>.aggregate({ op, field?, where? })`. */
211
+ interface TableAggregateOptions<TDocument> extends RestrictableQueryOptions<TDocument> {
212
+ field?: keyof TDocument & string;
213
+ op: AggregateOp;
214
+ }
215
+ /** Relation-aware twin of {@link TableAggregateOptions} (see {@link RestrictableQueryOptionsOf}). */
216
+ interface TableAggregateOptionsOf<DM, REL extends Record<keyof DM, object>, T extends keyof DM> extends RestrictableQueryOptionsOf<DM, REL, T> {
217
+ field?: keyof DM[T] & string;
218
+ op: AggregateOp;
219
+ }
220
+ /** Args for `ctx.db.&lt;table>.groupBy({ by, agg?, where? })`. */
221
+ interface TableGroupByOptions<TDocument> extends RestrictableQueryOptions<TDocument> {
222
+ agg?: {
223
+ field?: keyof TDocument & string;
224
+ op: AggregateOp;
225
+ };
226
+ by: ReadonlyArray<keyof TDocument & string>;
227
+ }
228
+ /** Relation-aware twin of {@link TableGroupByOptions} (see {@link RestrictableQueryOptionsOf}). */
229
+ interface TableGroupByOptionsOf<DM, REL extends Record<keyof DM, object>, T extends keyof DM> extends RestrictableQueryOptionsOf<DM, REL, T> {
230
+ agg?: {
231
+ field?: keyof DM[T] & string;
232
+ op: AggregateOp;
233
+ };
234
+ by: ReadonlyArray<keyof DM[T] & string>;
235
+ }
236
+ /** One entry returned by `groupBy` — the group's by-tuple plus the reducer value. */
237
+ interface GroupByEntry<TDocument> {
238
+ key: Partial<TDocument>;
239
+ value: null | number;
240
+ }
241
+ /** Args for `ctx.db.&lt;table>.rank(name, args)`. `row` is either an id or a row doc. */
242
+ interface TableRankOptions<TDocument> extends RestrictableQueryOptions<TDocument> {
243
+ row: string | TDocument;
244
+ }
245
+ /** Result of `rank` — 1-based position within the partition + partition total. */
246
+ interface RankResult {
247
+ position: number;
248
+ total: number;
249
+ }
250
+ /** Args for `ctx.db.&lt;table>.rankPage(name, args)`. */
251
+ interface TableRankPageOptions<TDocument> extends RestrictableQueryOptions<TDocument> {
252
+ cursor?: null | string;
253
+ take?: number;
254
+ }
255
+ /** One page returned by `rankPage`. */
256
+ interface RankPage<TDocument> {
257
+ continueCursor: null | string;
258
+ isDone: boolean;
259
+ page: TDocument[];
260
+ }
261
+ /**
262
+ * Builder passed to `.withSearchIndex(name, q => …)`. `.search(field, query)`
263
+ * runs the full-text match against the index's searchable field; `.eq(field,
264
+ * value)` narrows by a declared filter field. Field names are constrained to
265
+ * the table's columns.
266
+ */
267
+ interface SearchFilterBuilder<TDocument> {
268
+ eq: <F extends keyof TDocument & string>(field: F, value: TDocument[F]) => SearchFilterBuilder<TDocument>;
269
+ search: (field: keyof TDocument & string, query: string) => SearchFilterBuilder<TDocument>;
270
+ }
271
+ /**
272
+ * Chainable reader returned by `.withSearchIndex()` — rows come back ordered
273
+ * by relevance. `.paginate()` is intentionally absent (a relevance-ordered
274
+ * search can't keyset-paginate); cap the result set with `.take(n)`.
275
+ */
276
+ interface SearchReader<TDocument> {
277
+ collect: () => Promise<TDocument[]>;
278
+ first: () => Promise<TDocument | null>;
279
+ take: (limit: number) => Promise<TDocument[]>;
280
+ unique: () => Promise<TDocument | null>;
281
+ }
282
+ /** Read-only typed table accessor exposed on `QueryCtx.db.&lt;table>`. */
283
+ interface TableReaderFacade<DM, REL extends Record<keyof DM, object>, RANK extends Record<keyof DM, string>, SEARCH extends Record<keyof DM, string>, T extends keyof DM> {
284
+ /**
285
+ * Reduce rows in this table to a scalar (`avg`/`max`/`min`/`sum` — `count`
286
+ * lives on its own method). Routes through a declared `aggregateIndex` when
287
+ * the planner can prove the request is answerable; otherwise scans.
288
+ */
289
+ aggregate: (options: TableAggregateOptionsOf<DM, REL, T>) => Promise<null | number>;
290
+ /**
291
+ * Count rows. The planner routes `where` keys that match a declared
292
+ * `aggregateIndex.by` set to the indexed counter (no scan); otherwise
293
+ * falls back to a SCAN. Accepts either a bare `where` tree or the broader
294
+ * `RestrictableQueryOptions` shape; the latter is the seam the RLS layer
295
+ * uses to inject `baseWhere` and `restrictsCounts`.
296
+ */
297
+ count: (where?: RestrictableQueryOptionsOf<DM, REL, T> | WhereOf<DM, REL, T>) => Promise<number>;
298
+ /** `true` when at least one row matches `where` (any row when omitted). RLS-filtered exactly like `findFirst`. */
299
+ exists: (where?: WhereOf<DM, REL, T>) => Promise<boolean>;
300
+ findFirst: <W extends WithArg<DM, REL, T> = {}, S extends ReadonlyArray<keyof DM[T] & string> | undefined = undefined>(args?: QueryArgsOf<DM, REL, T> & {
301
+ select?: S;
302
+ with?: W;
303
+ }) => Promise<LoadWith<DM, REL, T, W, S> | null>;
304
+ findFirstOrThrow: <W extends WithArg<DM, REL, T> = {}, S extends ReadonlyArray<keyof DM[T] & string> | undefined = undefined>(args?: QueryArgsOf<DM, REL, T> & {
305
+ select?: S;
306
+ with?: W;
307
+ }) => Promise<LoadWith<DM, REL, T, W, S>>;
308
+ findMany: <W extends WithArg<DM, REL, T> = {}, S extends ReadonlyArray<keyof DM[T] & string> | undefined = undefined>(args?: QueryArgsOf<DM, REL, T> & {
309
+ select?: S;
310
+ with?: W;
311
+ }) => Promise<QueryPage<LoadWith<DM, REL, T, W, S>>>;
312
+ get: (id: Id<string & T>) => Promise<DM[T] | null>;
313
+ /**
314
+ * Group rows by the named keys and apply `agg` per group (defaults to
315
+ * `count`). Answered from the counter table when an aggregate index's
316
+ * `by` matches `options.by` exactly; otherwise scans.
317
+ */
318
+ groupBy: (options: TableGroupByOptionsOf<DM, REL, T>) => Promise<ReadonlyArray<GroupByEntry<DM[T]>>>;
319
+ /**
320
+ * Return the 1-based position of `options.row` within its partition
321
+ * under the declared rankIndex `indexName`, plus the partition's total
322
+ * row count. `null` when the row isn't in the index. Honors the same
323
+ * `baseWhere` / `restrictsCounts` RLS seam as `count()`.
324
+ */
325
+ rank: (indexName: RANK[T], options: TableRankOptions<DM[T]>) => Promise<null | RankResult>;
326
+ /**
327
+ * Walk the rank companion in declared sort order — sorted pagination
328
+ * accelerator. `options.where` may pin the partition; `cursor`/`take`
329
+ * follow the Convex-style keyset shape.
330
+ */
331
+ rankPage: (indexName: RANK[T], options?: TableRankPageOptions<DM[T]>) => Promise<RankPage<DM[T]>>;
332
+ /**
333
+ * Restrict the query to a declared `.searchIndex()` and run a full-text
334
+ * match. `indexName` is constrained to this table's search indexes
335
+ * (`never` when it declares none). Returns a relevance-ordered reader —
336
+ * finish with `.take(n)` / `.collect()`.
337
+ */
338
+ withSearchIndex: (indexName: SEARCH[T], search: (q: SearchFilterBuilder<DM[T]>) => SearchFilterBuilder<DM[T]>) => SearchReader<DM[T]>;
339
+ }
340
+ /** Read-write typed table accessor exposed on `MutationCtx.db.&lt;table>` / `ActionCtx.db.&lt;table>`. */
341
+ interface TableWriterFacade<DM, IM extends Record<keyof DM, object>, REL extends Record<keyof DM, object>, RANK extends Record<keyof DM, string>, SEARCH extends Record<keyof DM, string>, T extends keyof DM> extends TableReaderFacade<DM, REL, RANK, SEARCH, T> {
342
+ /**
343
+ * Delete a row by id. On a `.softDelete()` table this flips the marker column
344
+ * (and cascades as a soft delete) instead of removing the row; use
345
+ * {@link TableWriterFacade.hardDelete} to force physical removal.
346
+ */
347
+ delete: (id: Id<string & T>) => Promise<void>;
348
+ /** Delete many rows in this table by id in one call; returns the *requested* id count (unknown ids are no-ops). Atomic within a mutation (a throw rolls the mutation back); an action has no transaction span. */
349
+ deleteMany: (ids: ReadonlyArray<Id<string & T>>, options?: {
350
+ limit?: number;
351
+ }) => Promise<{
352
+ deleted: number;
353
+ }>;
354
+ /** Physically remove a row (and physically cascade `onDelete`), bypassing `.softDelete()`. Same as `delete()` on a non-soft table. */
355
+ hardDelete: (id: Id<string & T>) => Promise<void>;
356
+ /**
357
+ * Insert a document, returning its minted id. With `{ skipDuplicates: true }`
358
+ * a UNIQUE-constraint breach resolves to `null` (the row already exists)
359
+ * instead of throwing — the return type widens to `Id | null` on that overload.
360
+ */
361
+ insert: {
362
+ (values: IM[T], options: {
363
+ skipDuplicates: true;
364
+ }): Promise<Id<string & T> | null>;
365
+ (values: IM[T], options?: {
366
+ skipDuplicates?: boolean;
367
+ }): Promise<Id<string & T>>;
368
+ };
369
+ /** Insert many documents into this table in one call, returning the minted ids in input order. Atomic within a mutation (a throw rolls the mutation back); an action has no transaction span. */
370
+ insertMany: (values: ReadonlyArray<IM[T]>, options?: {
371
+ limit?: number;
372
+ }) => Promise<Id<string & T>[]>;
373
+ patch: (id: Id<string & T>, values: Partial<IM[T]>) => Promise<void>;
374
+ /** Patch many rows in this table by id in one call. Atomic within a mutation (a throw rolls the mutation back); an action has no transaction span. */
375
+ patchMany: (patches: ReadonlyArray<{
376
+ id: Id<string & T>;
377
+ values: Partial<IM[T]>;
378
+ }>, options?: {
379
+ limit?: number;
380
+ }) => Promise<void>;
381
+ replace: (id: Id<string & T>, values: IM[T]) => Promise<void>;
382
+ /** Un-soft-delete a row by id: clears the `.softDelete()` marker so list reads see it again. Throws on a non-soft table. */
383
+ restore: (id: Id<string & T>) => Promise<void>;
384
+ /**
385
+ * Insert when no existing row matches `target`, otherwise patch the match with
386
+ * `update` (defaulting to `create`). `target` names a `.unique()` column (or a
387
+ * tuple) used to look it up. Returns the row id and whether it was `created`.
388
+ * Composes `findFirst` + `insert`/`patch`, so RLS gates each step.
389
+ */
390
+ upsert: (args: {
391
+ create: IM[T];
392
+ target: UpsertTargetOf<DM, T>;
393
+ update?: Partial<IM[T]>;
394
+ }) => Promise<{
395
+ created: boolean;
396
+ id: Id<string & T>;
397
+ }>;
398
+ /** Sequential {@link TableWriterFacade.upsert} over many rows sharing one `target`; one result per input row, in order. */
399
+ upsertMany: (args: {
400
+ rows: ReadonlyArray<{
401
+ create: IM[T];
402
+ update?: Partial<IM[T]>;
403
+ }>;
404
+ target: UpsertTargetOf<DM, T>;
405
+ }) => Promise<{
406
+ created: boolean;
407
+ id: Id<string & T>;
408
+ }[]>;
409
+ }
410
+ /** Conflict target for `upsert`/`upsertMany`: one column of table `T`, or a tuple of them. */
411
+ type UpsertTargetOf<DM, T extends keyof DM> = ReadonlyArray<keyof DM[T] & string> | (keyof DM[T] & string);
412
+ /** Per-table read facade — `ctx.db.&lt;table>` on a `QueryCtx`. */
413
+ type DatabaseReaderFacade<DM, REL extends Record<keyof DM, object>, RANK extends Record<keyof DM, string>, SEARCH extends Record<keyof DM, string>> = { readonly [T in keyof DM]: TableReaderFacade<DM, REL, RANK, SEARCH, T> };
414
+ /** Per-table read-write facade — `ctx.db.&lt;table>` on a `MutationCtx` / `ActionCtx`. */
415
+ type DatabaseWriterFacade<DM, IM extends Record<keyof DM, object>, REL extends Record<keyof DM, object>, RANK extends Record<keyof DM, string>, SEARCH extends Record<keyof DM, string>> = { readonly [T in keyof DM]: TableWriterFacade<DM, IM, REL, RANK, SEARCH, T> };
416
+ export { AggregateOp, DatabaseReaderFacade, DatabaseWriterFacade, GroupByEntry, Id, LoadWith, ManyRelationWhere, OneRelationWhere, OrderBy, QueryArgs, QueryArgsOf, QueryPage, RankPage, RankResult, RestrictableQueryOptions, RestrictableQueryOptionsOf, SearchFilterBuilder, SearchReader, TableAggregateOptions, TableAggregateOptionsOf, TableGroupByOptions, TableGroupByOptionsOf, TableRankOptions, TableRankPageOptions, TableReaderFacade, TableWriterFacade, UpsertTargetOf, Where, WhereOf, WhereOperators, WithArg };