@rebasepro/types 0.30.1 → 0.31.0-canary.g59cd6fd

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 (57) hide show
  1. package/README.md +16 -18
  2. package/dist/call_context.d.ts +3 -3
  3. package/dist/controllers/client.d.ts +29 -0
  4. package/dist/controllers/data.d.ts +85 -332
  5. package/dist/controllers/data_driver.d.ts +47 -57
  6. package/dist/controllers/index.d.ts +0 -3
  7. package/dist/index.d.ts +1 -0
  8. package/dist/index.es.js +2567 -1151
  9. package/dist/index.es.js.map +1 -1
  10. package/dist/schema/column_options.d.ts +336 -0
  11. package/dist/schema/column_type.d.ts +116 -0
  12. package/dist/schema/columns.d.ts +646 -0
  13. package/dist/schema/compiled.d.ts +114 -0
  14. package/dist/schema/descriptor.d.ts +349 -0
  15. package/dist/schema/hooks.d.ts +164 -0
  16. package/dist/schema/index.d.ts +13 -0
  17. package/dist/schema/infer.d.ts +340 -0
  18. package/dist/schema/json.d.ts +237 -0
  19. package/dist/schema/relations.d.ts +205 -0
  20. package/dist/schema/table.d.ts +304 -0
  21. package/dist/schema/table_options.d.ts +103 -0
  22. package/dist/schema/table_patch.d.ts +338 -0
  23. package/dist/types/auth_adapter.d.ts +5 -5
  24. package/dist/types/auth_hooks.d.ts +65 -0
  25. package/dist/types/backend.d.ts +85 -96
  26. package/dist/types/data_source.d.ts +4 -9
  27. package/dist/types/database_adapter.d.ts +30 -31
  28. package/dist/types/entities.d.ts +0 -162
  29. package/dist/types/filter-operators.d.ts +2 -26
  30. package/dist/types/history.d.ts +6 -9
  31. package/dist/types/index.d.ts +3 -9
  32. package/dist/types/mcp_app_modules.d.ts +0 -2
  33. package/dist/types/policy.d.ts +85 -10
  34. package/dist/types/project_manifest.d.ts +113 -46
  35. package/dist/types/read_query.d.ts +98 -0
  36. package/dist/types/register.d.ts +35 -5
  37. package/dist/types/relations.d.ts +0 -430
  38. package/dist/types/schema_editing.d.ts +54 -2
  39. package/dist/types/schema_version.d.ts +24 -4
  40. package/dist/types/security_rules.d.ts +225 -15
  41. package/dist/types/storage_authorize.d.ts +2 -2
  42. package/dist/types/tenancy.d.ts +3 -3
  43. package/dist/types/ui_preferences.d.ts +228 -0
  44. package/package.json +3 -2
  45. package/dist/controllers/auth_state.d.ts +0 -23
  46. package/dist/controllers/collection_registry.d.ts +0 -67
  47. package/dist/controllers/effective_role.d.ts +0 -4
  48. package/dist/types/admin_block.d.ts +0 -96
  49. package/dist/types/chips.d.ts +0 -16
  50. package/dist/types/collection_contract.d.ts +0 -43
  51. package/dist/types/collection_patch.d.ts +0 -74
  52. package/dist/types/collections.d.ts +0 -783
  53. package/dist/types/component_ref.d.ts +0 -81
  54. package/dist/types/conditions-contract.types.d.ts +0 -14
  55. package/dist/types/entity_callbacks.d.ts +0 -393
  56. package/dist/types/indexes.d.ts +0 -179
  57. package/dist/types/properties.d.ts +0 -1640
@@ -0,0 +1,114 @@
1
+ import type { AnyTableDef, AnyViewDef, AuthTableOptions } from "./table.js";
2
+ import type { ColumnTransform, TableHooks } from "./hooks.js";
3
+ import type { TableDescriptor } from "./descriptor.js";
4
+ /**
5
+ * Where a field-level hook sits inside one column, and what runs there.
6
+ *
7
+ * A tree that mirrors the column's value: the node for a column holds the
8
+ * column's own `.transform()`, and its branches lead to the nested positions
9
+ * that declare one — a field of a `json.object`, the items of an array (a
10
+ * native array column's element, or a `json.array`), the value of one variant
11
+ * of a `json.oneOf`. Only branches that lead to a hook are present, so a row
12
+ * pipeline walks the value and the node side by side and stops as soon as the
13
+ * node has nothing below — it never re-reads the definition, and it costs
14
+ * nothing on a column without hooks (which has no node at all).
15
+ *
16
+ * Server only: hooks are code, and never reach the wire. The descriptor says
17
+ * which exist (`ColumnDescriptor.transforms`); this says what they do.
18
+ *
19
+ * @group Schema
20
+ */
21
+ export interface TransformNode {
22
+ /** The hook declared on the value at this position. */
23
+ transform?: ColumnTransform;
24
+ /** The fields of a `json.object` that lead to a hook, by field key. */
25
+ fields?: Record<string, TransformNode>;
26
+ /** Each item of an array — a native array column's element, or a `json.array`. */
27
+ items?: TransformNode;
28
+ /**
29
+ * The variants of a `json.oneOf` that lead to a hook, by variant name. The
30
+ * node is for the variant's value: what sits under the block's
31
+ * `valueField` when its `typeField` names that variant.
32
+ */
33
+ variants?: Record<string, TransformNode>;
34
+ }
35
+ /**
36
+ * One step from a column's value to a nested position: a field of a
37
+ * `json.object`, every item of an array (a native array's element or a
38
+ * `json.array`), or the value of one variant of a `json.oneOf`.
39
+ *
40
+ * - `{ field }` — a key of an object;
41
+ * - `{ items: true }` — every item of an array;
42
+ * - `{ variant }` — the value of a `oneOf` block of that variant (what sits
43
+ * under its `valueField` when its `typeField` names the variant).
44
+ *
45
+ * @group Schema
46
+ */
47
+ export type JsonPathStep = {
48
+ field: string;
49
+ } | {
50
+ items: true;
51
+ } | {
52
+ variant: string;
53
+ };
54
+ /**
55
+ * One field-level hook with its full address: the column key and the path
56
+ * inside the column's value (`[]` for the column itself). The flat view of a
57
+ * table's {@link TransformNode} trees, for a reader that wants a list — a
58
+ * report, a check, a test — rather than a walk.
59
+ *
60
+ * @group Schema
61
+ */
62
+ export interface CompiledTransform {
63
+ /** The key of the column. */
64
+ column: string;
65
+ /** From the column's value to the position the hook is declared on. */
66
+ path: JsonPathStep[];
67
+ /** The hook. */
68
+ transform: ColumnTransform;
69
+ }
70
+ /**
71
+ * The server-only half of `defineAuthTable`'s `auth` options: its
72
+ * user-lifecycle hooks. Roles, scopes and `personalKeys` are data, and live on
73
+ * the descriptor (`TableDescriptor.auth`).
74
+ *
75
+ * @group Schema
76
+ */
77
+ export interface CompiledAuthHooks {
78
+ /** Runs when an admin creates a user. */
79
+ onCreateUser?: NonNullable<AuthTableOptions["onCreateUser"]>;
80
+ /** Runs when an admin resets a user's password. */
81
+ onResetPassword?: NonNullable<AuthTableOptions["onResetPassword"]>;
82
+ }
83
+ /**
84
+ * A table (or view) compiled for the backend: what the server runs on.
85
+ *
86
+ * The {@link TableDescriptor} is the wire-safe half — plain JSON, the only
87
+ * thing a client ever sees, and every fact about columns, relations and table
88
+ * options. Beside it, the server-only half the descriptor cannot carry: the
89
+ * table's hooks, its columns' field-level hooks, the auth table's lifecycle
90
+ * hooks, and the definition it was compiled from (for the planner, which reads
91
+ * relation thunks and index predicates).
92
+ *
93
+ * Produced by `compileSchema` (`@rebasepro/common`) and handed by server boot
94
+ * to the data driver, which wraps each in its own runtime record. Nothing here
95
+ * is ever serialized: serve `descriptor`, never the compiled table.
96
+ *
97
+ * @group Schema
98
+ */
99
+ export interface CompiledTable {
100
+ /** The table as the backend and the wire know it. */
101
+ descriptor: TableDescriptor;
102
+ /** The definition it was compiled from: `defineTable`, `defineAuthTable` or `defineView`. */
103
+ def: AnyTableDef | AnyViewDef;
104
+ /** The table's own lifecycle hooks, when it declares any. A view declares read hooks only. */
105
+ hooks?: TableHooks;
106
+ /**
107
+ * The field-level hooks, by column key: one {@link TransformNode} tree per
108
+ * column that declares a hook on itself or anywhere inside its value.
109
+ * Absent when no column does.
110
+ */
111
+ transforms?: Record<string, TransformNode>;
112
+ /** `defineAuthTable`'s `onCreateUser` / `onResetPassword`, when declared. */
113
+ auth?: CompiledAuthHooks;
114
+ }
@@ -0,0 +1,349 @@
1
+ import type { OnAction, JoinStep } from "../types/relations.js";
2
+ import type { SecurityRule } from "../types/security_rules.js";
3
+ import type { SearchConfig } from "../types/search.js";
4
+ import type { TenantSource } from "../types/tenancy.js";
5
+ import type { OrderByTuple } from "../types/filter-operators.js";
6
+ import type { RoleDefinition, ScopeDefinition } from "../types/scopes.js";
7
+ import type { ColumnType } from "./column_type.js";
8
+ import type { ColumnAccess, ColumnDefault, ColumnStamp, ColumnStorage, ColumnValidation, VectorIndexOptions } from "./column_options.js";
9
+ import type { JsonShape } from "./json.js";
10
+ import type { TableIndex } from "./table_options.js";
11
+ /**
12
+ * A table as the backend knows it and as `GET /api/schema/tables` serves it.
13
+ *
14
+ * Plain JSON: no functions, no thunks, no UI. It is the compiled form of a
15
+ * `defineTable(...)` — every default applied, every name derived, every
16
+ * relation resolved to a slug — and equally what introspection reports for a
17
+ * table nobody declared. The server, the SDK generator, the Studio and the
18
+ * panel all read this one shape.
19
+ *
20
+ * @group Schema
21
+ */
22
+ export interface TableDescriptor {
23
+ /** The name the table was defined under: `defineTable("posts", …)`. */
24
+ name: string;
25
+ /** The name in the API, the SDK and in scopes: `/api/data/<slug>`. */
26
+ slug: string;
27
+ /** The SQL table (or view) name, unqualified. */
28
+ table: string;
29
+ /** The Postgres schema. */
30
+ schema: string;
31
+ /** The key of the Postgres database it lives in, when not the default one. */
32
+ database?: string;
33
+ /** A table, or a view declared with `defineView`. */
34
+ kind: "table" | "view";
35
+ /** What the table holds, for OpenAPI and MCP tool descriptions. */
36
+ description?: string;
37
+ /** The columns, in physical order. */
38
+ columns: ColumnDescriptor[];
39
+ /** The keys of the primary-key columns, in key order. */
40
+ primaryKey: string[];
41
+ /** The relations, in declaration order. */
42
+ relations: RelationDescriptor[];
43
+ /** The row-level security posture. */
44
+ rls: RlsDescriptor;
45
+ /** Rows are scoped to a tenant. */
46
+ tenant?: TenantDescriptor;
47
+ /** Deleting stamps this column instead of removing the row. */
48
+ softDelete?: {
49
+ field: string;
50
+ };
51
+ /** Every write is recorded in the history table. */
52
+ history?: boolean;
53
+ /** Full-text search. */
54
+ search?: SearchConfig;
55
+ /** Declared indexes. */
56
+ indexes?: TableIndex[];
57
+ /** Present on a view. */
58
+ view?: ViewDescriptor;
59
+ /** Present on the auth table. */
60
+ auth?: AuthTableDescriptor;
61
+ /** Which server hooks the table declares. Their code never leaves the server. */
62
+ hooks?: TableHookFlags;
63
+ /** `false` when writes may name columns the table does not declare. */
64
+ strictWrites?: false;
65
+ /** Opaque data the project attached. */
66
+ metadata?: Record<string, unknown>;
67
+ }
68
+ /**
69
+ * A column as the backend knows it and as the wire carries it.
70
+ *
71
+ * @group Schema
72
+ */
73
+ export interface ColumnDescriptor {
74
+ /** The key on a JSON row, in the SDK, in filters and sorts. */
75
+ key: string;
76
+ /** The SQL column name. */
77
+ column: string;
78
+ /** The Postgres type. */
79
+ type: ColumnType;
80
+ /** `false` for a `NOT NULL` column: required by the API on create. */
81
+ nullable: boolean;
82
+ /** Part of the primary key. */
83
+ primaryKey?: true;
84
+ /** Carries a single-column `UNIQUE` constraint. */
85
+ unique?: true;
86
+ /** The DEFAULT. A create may omit the column. */
87
+ default?: ColumnDefault;
88
+ /** `GENERATED ALWAYS AS (…) STORED`. Never writable. */
89
+ generated?: {
90
+ expression: string;
91
+ };
92
+ /** Client writes are refused; the database or the server fills it. */
93
+ readOnly?: true;
94
+ /** Never on the wire, in the SDK or in OpenAPI. Only server code sees it. */
95
+ private?: true;
96
+ /** Per-role read and write access. */
97
+ access?: ColumnAccess;
98
+ /** The server writes the time or the caller's id into it. */
99
+ stamp?: ColumnStamp;
100
+ /** Rules the API checks before a write. */
101
+ validate?: ColumnValidation;
102
+ /** A closed set of values that is not a Postgres enum type. */
103
+ oneOf?: readonly (string | number)[];
104
+ /** What the column holds, for OpenAPI and MCP. */
105
+ description?: string;
106
+ /** The column holds the path of a stored file (or several). */
107
+ storage?: ColumnStorage;
108
+ /**
109
+ * A meaning the type does not state: `"geopoint"` on a `jsonb` point
110
+ * (`col.geopoint()`), `"user"` on a text or uuid column that holds a
111
+ * user's id (`.userId()`) — the panel offers a user picker for it, and a
112
+ * caller who may write the table may look users up to fill it.
113
+ */
114
+ semantic?: "geopoint" | "user";
115
+ /** The vector index, or `false` when the column has none. */
116
+ vectorIndex?: VectorIndexOptions | false;
117
+ /** The shape of a `json`/`jsonb` document. */
118
+ json?: JsonShape;
119
+ /** What each item of an array column is. */
120
+ element?: ColumnElementDescriptor;
121
+ /** Which field-level hooks the column declares. */
122
+ transforms?: {
123
+ beforeSave?: true;
124
+ afterRead?: true;
125
+ };
126
+ /** The `belongsTo` relation this column is the foreign key of. */
127
+ foreignKeyOf?: string;
128
+ /** The column was derived from a relation rather than declared in `columns`. */
129
+ derived?: true;
130
+ }
131
+ /**
132
+ * The rules that apply to each item of an array column.
133
+ *
134
+ * @group Schema
135
+ */
136
+ export interface ColumnElementDescriptor {
137
+ validate?: ColumnValidation;
138
+ oneOf?: readonly (string | number)[];
139
+ storage?: ColumnStorage;
140
+ /** `"user"`: each item is a user's id (`col.text().userId().array()`). */
141
+ semantic?: "user";
142
+ }
143
+ /** What every relation descriptor carries. */
144
+ export interface RelationDescriptorBase {
145
+ /** The relation's key: what `include` names. */
146
+ name: string;
147
+ /** The slug of the table it points at. */
148
+ target: string;
149
+ /** One row or many. */
150
+ cardinality: "one" | "many";
151
+ /** Per-role read and write access to the relation (`rel.*(…, { access })`). */
152
+ access?: ColumnAccess;
153
+ /** Never on the wire, in the SDK or in OpenAPI (`rel.*(…, { private: true })`). Only server code sees it. */
154
+ private?: true;
155
+ }
156
+ /**
157
+ * A resolved relation: every key and name it uses is spelled out.
158
+ *
159
+ * @group Schema
160
+ */
161
+ export type RelationDescriptor = (RelationDescriptorBase & {
162
+ kind: "belongsTo";
163
+ cardinality: "one";
164
+ /** The SQL column on this table that holds the foreign key. */
165
+ localKey: string;
166
+ /** The row key of that column. */
167
+ localField: string;
168
+ onDelete?: OnAction;
169
+ onUpdate?: OnAction;
170
+ /** Whether an index on the foreign key is created. */
171
+ index: boolean;
172
+ /** `false` for a column that stores a key without a constraint. */
173
+ constraint: boolean;
174
+ }) | (RelationDescriptorBase & {
175
+ kind: "hasOne" | "hasMany";
176
+ /** The SQL column on the target table that points back here. */
177
+ foreignKey: string;
178
+ /** The SQL column on this table it points at, when not the primary key. */
179
+ sourceKey?: string;
180
+ /** The order related rows come back in. */
181
+ orderBy?: OrderByTuple[];
182
+ }) | (RelationDescriptorBase & {
183
+ kind: "manyToMany";
184
+ cardinality: "many";
185
+ through: {
186
+ /** The junction table. */
187
+ table: string;
188
+ /** The junction column pointing at this table. */
189
+ sourceKey: string;
190
+ /** The junction column pointing at the target. */
191
+ targetKey: string;
192
+ /** Extra columns on the junction, served as `_pivot`. */
193
+ pivot: ColumnDescriptor[];
194
+ };
195
+ onDelete?: OnAction;
196
+ }) | (RelationDescriptorBase & {
197
+ kind: "via";
198
+ /** The joins from this table to the target. */
199
+ steps: JoinStep[];
200
+ /** Whether a link can be written through it. */
201
+ writable: boolean;
202
+ });
203
+ /**
204
+ * A table's row-level security, resolved.
205
+ *
206
+ * `posture` is what the author wrote; `"inherit"` is already replaced by the
207
+ * project's `defaultRls`, with `inherited` set. `rules` is what the policies
208
+ * are generated from. `admin-only` has no rules: only the injected admin
209
+ * baseline grants access, so a reader must never take an empty `rules` to mean
210
+ * "open".
211
+ *
212
+ * `"view"` is the posture of every `defineView`: a view has no policies of its
213
+ * own. A plain view runs `security_invoker`, so the row-level security of the
214
+ * tables it reads decides each row; a materialized one is read by
215
+ * `view.roles`. It carries no rules and `defaultPolicies: false`, and a reader
216
+ * decides access from `view`, never from `rules`.
217
+ *
218
+ * A table introspected from a database nobody declared (`introspectTables`)
219
+ * is described from the catalogue: `posture: "rules"`, its `pg_policies` as
220
+ * the rules (each with its policy's `name`), and `defaultPolicies: false`,
221
+ * because Rebase injected nothing. Row-level security on with **no** policy is
222
+ * `{ posture: "rules", rules: [], defaultPolicies: false }`: no rule and no
223
+ * baseline, so nothing grants a row — closed, the one shape `defineTable`
224
+ * itself refuses to write. A reader must take `rules: []` with
225
+ * `defaultPolicies: false` to mean "nobody", never "open".
226
+ *
227
+ * @group Schema
228
+ */
229
+ export interface RlsDescriptor {
230
+ /** `"admin-only"` or `"rules"` for a table, `"view"` for every view. */
231
+ posture: "admin-only" | "rules" | "view";
232
+ /** The rules came from `defaultRls`. */
233
+ inherited?: true;
234
+ /** The declared rules, exactly as written. */
235
+ rules: SecurityRule[];
236
+ /** `false` when the injected admin baseline is switched off. */
237
+ defaultPolicies: boolean;
238
+ /**
239
+ * The table wrote `defaultPolicies: true`: the baseline is injected
240
+ * whatever its database's `rls.defaultPolicies` says. Absent, the
241
+ * database's declaration decides whether a `defaultPolicies: true` table
242
+ * gets it.
243
+ */
244
+ declaredDefaultPolicies?: true;
245
+ /**
246
+ * The policies Postgres holds for the table, when the server includes them
247
+ * (admins and `schema:read` holders): the Studio's RLS editor reads these.
248
+ */
249
+ policies?: PolicyDescriptor[];
250
+ /**
251
+ * The junction tables of this table's `manyToMany` relations, each with
252
+ * the policies Postgres holds for it — included exactly when
253
+ * {@link policies} is. A junction has no descriptor of its own (it is the
254
+ * platform's table, not the author's), so its policies are listed under
255
+ * every table that declares a relation through it.
256
+ */
257
+ junctions?: JunctionPolicyDescriptor[];
258
+ }
259
+ /**
260
+ * A junction table of a `manyToMany` relation and its generated policies.
261
+ *
262
+ * @group Schema
263
+ */
264
+ export interface JunctionPolicyDescriptor {
265
+ /** The junction table, unqualified. */
266
+ table: string;
267
+ /** Its Postgres schema. */
268
+ schema: string;
269
+ /** Its policies: derived from the endpoints' rules, so every one is `injected`. */
270
+ policies: PolicyDescriptor[];
271
+ }
272
+ /**
273
+ * One generated policy.
274
+ *
275
+ * @group Schema
276
+ */
277
+ export interface PolicyDescriptor {
278
+ name: string;
279
+ operation: string;
280
+ mode: "permissive" | "restrictive";
281
+ roles: string[];
282
+ using: string | null;
283
+ withCheck: string | null;
284
+ /** Rebase injected it (admin baseline, auth, tenant, junction). */
285
+ injected: boolean;
286
+ /** The index in `rules` of the rule it was generated from. */
287
+ rule?: number;
288
+ }
289
+ /**
290
+ * How rows are scoped to a tenant.
291
+ *
292
+ * @group Schema
293
+ */
294
+ export interface TenantDescriptor {
295
+ /** The key of the tenant column. */
296
+ field: string;
297
+ /** Where the caller's tenant comes from. */
298
+ from: TenantSource;
299
+ /** Roles that see every tenant. */
300
+ bypassRoles?: readonly string[];
301
+ }
302
+ /**
303
+ * A view's definition.
304
+ *
305
+ * @group Schema
306
+ */
307
+ export interface ViewDescriptor {
308
+ materialized: boolean;
309
+ /** Roles that may read a materialized view. */
310
+ roles?: string[];
311
+ /** The defining query. Served to admins only. */
312
+ query?: string;
313
+ }
314
+ /**
315
+ * The serializable half of `defineAuthTable`'s `auth` options.
316
+ *
317
+ * @group Schema
318
+ */
319
+ export interface AuthTableDescriptor {
320
+ /** The roles users can hold, with what each may do. */
321
+ roles?: Record<string, RoleDefinition>;
322
+ /** The scopes an API key or token can be granted. */
323
+ scopes?: Record<string, ScopeDefinition>;
324
+ /** Whether users may create personal API keys. */
325
+ personalKeys?: boolean;
326
+ /**
327
+ * Which user-lifecycle hooks `defineAuthTable`'s `auth` declares. Their
328
+ * code never leaves the server; a client reads this to know, for example,
329
+ * that creating a user sends an invitation.
330
+ */
331
+ hooks?: {
332
+ onCreateUser?: true;
333
+ onResetPassword?: true;
334
+ };
335
+ }
336
+ /**
337
+ * Which hooks a table declares.
338
+ *
339
+ * @group Schema
340
+ */
341
+ export interface TableHookFlags {
342
+ beforeQuery?: true;
343
+ afterRead?: true;
344
+ beforeSave?: true;
345
+ afterSave?: true;
346
+ afterSaveError?: true;
347
+ beforeDelete?: true;
348
+ afterDelete?: true;
349
+ }
@@ -0,0 +1,164 @@
1
+ import type { RebaseCallContext } from "../call_context.js";
2
+ import type { GeneratedDatabase } from "../types/register.js";
3
+ import type { User } from "../users/index.js";
4
+ import type { EntityStatus } from "../types/entities.js";
5
+ import type { QueryNarrowing, ReadOperation, ReadQuery } from "../types/read_query.js";
6
+ import type { TableDescriptor } from "./descriptor.js";
7
+ /** A row, keyed by column key. */
8
+ export type TableRow = Record<string, unknown>;
9
+ /**
10
+ * What every table hook receives.
11
+ *
12
+ * @group Schema
13
+ */
14
+ export interface TableHookProps<USER extends User = User> {
15
+ /** The table the hook runs for, as the server knows it. */
16
+ table: TableDescriptor;
17
+ /** The path the request addressed: the slug, or a nested path. */
18
+ path: string;
19
+ /**
20
+ * The caller, and `context.data` scoped to them — typed from a registered
21
+ * generated `Database` (`Register.database`). Not from `Register.tables`:
22
+ * a schema's own hooks typed from that same schema is a cycle TypeScript
23
+ * refuses.
24
+ */
25
+ context: RebaseCallContext<USER, GeneratedDatabase>;
26
+ }
27
+ /**
28
+ * Props of `hooks.beforeQuery`: runs before every read, and may narrow it.
29
+ *
30
+ * @group Schema
31
+ */
32
+ export interface TableBeforeQueryProps<Row extends TableRow = TableRow, USER extends User = User> extends TableHookProps<USER> {
33
+ /** Which kind of read is about to run. */
34
+ operation: ReadOperation;
35
+ /** The query as the caller sent it. */
36
+ query: ReadQuery<Row>;
37
+ }
38
+ /**
39
+ * Props of `hooks.afterRead`: runs on every row leaving the server — REST,
40
+ * realtime and MCP alike — and returns the row to serve.
41
+ *
42
+ * @group Schema
43
+ */
44
+ export interface TableAfterReadProps<Row extends TableRow = TableRow, USER extends User = User> extends TableHookProps<USER> {
45
+ /** The row as stored, after field-level access has been applied. */
46
+ values: Row;
47
+ }
48
+ /**
49
+ * Props of `hooks.beforeSave`: runs before a create or an update, and returns
50
+ * the values to write.
51
+ *
52
+ * @group Schema
53
+ */
54
+ export interface TableBeforeSaveProps<Row extends TableRow = TableRow, Insert extends TableRow = Row, USER extends User = User> extends TableHookProps<USER> {
55
+ /** The row's id. Absent on a create whose id the database generates. */
56
+ id?: string | number;
57
+ /** The values being written: all of them on a create, the changed ones on an update. */
58
+ values: Partial<Insert>;
59
+ /** The row as it was, on an update. */
60
+ previousValues?: Partial<Row>;
61
+ /** Whether the row is new, existing, or a copy. */
62
+ status: EntityStatus;
63
+ }
64
+ /**
65
+ * Props of `hooks.afterSave`: runs after the write committed.
66
+ *
67
+ * @group Schema
68
+ */
69
+ export interface TableAfterSaveProps<Row extends TableRow = TableRow, USER extends User = User> extends TableHookProps<USER> {
70
+ id: string | number;
71
+ /** The values that were written. */
72
+ values: Partial<Row>;
73
+ /** The row as it was, on an update. */
74
+ previousValues?: Partial<Row>;
75
+ status: EntityStatus;
76
+ }
77
+ /**
78
+ * Props of `hooks.afterSaveError`: runs when the write failed.
79
+ *
80
+ * @group Schema
81
+ */
82
+ export interface TableAfterSaveErrorProps<Row extends TableRow = TableRow, USER extends User = User> extends TableHookProps<USER> {
83
+ id?: string | number;
84
+ values: Partial<Row>;
85
+ previousValues?: Partial<Row>;
86
+ status: EntityStatus;
87
+ /** What the write failed with. */
88
+ error: unknown;
89
+ }
90
+ /**
91
+ * Props of `hooks.beforeDelete` and `hooks.afterDelete`.
92
+ *
93
+ * @group Schema
94
+ */
95
+ export interface TableDeleteProps<Row extends TableRow = TableRow, USER extends User = User> extends TableHookProps<USER> {
96
+ id: string | number;
97
+ /** The row being deleted. */
98
+ values: Row;
99
+ }
100
+ /**
101
+ * The server-side lifecycle hooks of a table.
102
+ *
103
+ * They run in the backend only, for every door into the table. Their code is
104
+ * never serialized: the wire says which hooks exist, not what they do.
105
+ *
106
+ * @group Schema
107
+ */
108
+ export interface TableHooks<Row extends TableRow = TableRow, Insert extends TableRow = Row, USER extends User = User> {
109
+ /** Narrow a read before it runs. Return a filter to AND into the query. */
110
+ beforeQuery?(props: TableBeforeQueryProps<Row, USER>): QueryNarrowing<Row> | void | Promise<QueryNarrowing<Row> | void>;
111
+ /** Shape a row on its way out: mask, redact, derive. Return the row to serve. */
112
+ afterRead?(props: TableAfterReadProps<Row, USER>): TableRow | Promise<TableRow>;
113
+ /** Validate or complete a write. Return the values to store; throw to refuse. */
114
+ beforeSave?(props: TableBeforeSaveProps<Row, Insert, USER>): Partial<Insert> | Promise<Partial<Insert>>;
115
+ /** React to a committed write. */
116
+ afterSave?(props: TableAfterSaveProps<Row, USER>): void | Promise<void>;
117
+ /** React to a failed write. */
118
+ afterSaveError?(props: TableAfterSaveErrorProps<Row, USER>): void | Promise<void>;
119
+ /** Runs before a delete. Return `false` or throw to refuse it. */
120
+ beforeDelete?(props: TableDeleteProps<Row, USER>): boolean | void | Promise<boolean | void>;
121
+ /** React to a committed delete. */
122
+ afterDelete?(props: TableDeleteProps<Row, USER>): void | Promise<void>;
123
+ }
124
+ /**
125
+ * Props of a column's `transform.beforeSave`.
126
+ *
127
+ * @group Schema
128
+ */
129
+ export interface ColumnBeforeSaveProps<T = unknown, USER extends User = User> extends TableHookProps<USER> {
130
+ /** The value being written to this column. */
131
+ value: T;
132
+ /** The value the column held, on an update. */
133
+ previousValue: T | undefined;
134
+ /** Every value being written. */
135
+ values: TableRow;
136
+ /** The row as it was, on an update. */
137
+ previousValues?: TableRow;
138
+ /** The row's id. Absent on a create whose id the database generates. */
139
+ id?: string | number;
140
+ /** Whether the row is new, existing, or a copy. */
141
+ status: EntityStatus;
142
+ }
143
+ /**
144
+ * Props of a column's `transform.afterRead`.
145
+ *
146
+ * @group Schema
147
+ */
148
+ export interface ColumnAfterReadProps<T = unknown, USER extends User = User> extends TableHookProps<USER> {
149
+ /** The stored value of this column. */
150
+ value: T;
151
+ /** The row being read. */
152
+ values: TableRow;
153
+ }
154
+ /**
155
+ * Field-level hooks: `col.text().transform({ beforeSave, afterRead })`.
156
+ *
157
+ * @group Schema
158
+ */
159
+ export interface ColumnTransform<T = unknown, USER extends User = User> {
160
+ /** Returns the value to store. */
161
+ beforeSave?(props: ColumnBeforeSaveProps<T, USER>): T | Promise<T>;
162
+ /** Returns the value to serve. */
163
+ afterRead?(props: ColumnAfterReadProps<T, USER>): T | Promise<T>;
164
+ }
@@ -0,0 +1,13 @@
1
+ export * from "./column_type.js";
2
+ export * from "./column_options.js";
3
+ export * from "./json.js";
4
+ export * from "./hooks.js";
5
+ export type { AnyColumn, BigintConfig, BuilderKind, ColumnDef, ColumnFlags, ColumnsShape, ColumnTransformValue, CustomConfig, DateConfig, DefaultValueFor, InitialColumnFlags, NumericConfig, PgEnum, SqlTemplate, StringConfig, TextConfig, TimeConfig, TimestampConfig, ValidationFor, VectorConfig } from "./columns.js";
6
+ export { COLUMN_BRAND, ColumnBuilder, col, isColumnBuilder, isSqlTemplate, pgEnum, sql } from "./columns.js";
7
+ export * from "./relations.js";
8
+ export * from "./table_options.js";
9
+ export * from "./table.js";
10
+ export * from "./descriptor.js";
11
+ export * from "./infer.js";
12
+ export * from "./table_patch.js";
13
+ export * from "./compiled.js";