@rebasepro/types 0.30.0 → 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.
- package/README.md +16 -18
- package/dist/call_context.d.ts +3 -3
- package/dist/controllers/client.d.ts +29 -0
- package/dist/controllers/data.d.ts +85 -332
- package/dist/controllers/data_driver.d.ts +47 -57
- package/dist/controllers/index.d.ts +0 -3
- package/dist/index.d.ts +1 -0
- package/dist/index.es.js +2567 -1151
- package/dist/index.es.js.map +1 -1
- package/dist/schema/column_options.d.ts +336 -0
- package/dist/schema/column_type.d.ts +116 -0
- package/dist/schema/columns.d.ts +646 -0
- package/dist/schema/compiled.d.ts +114 -0
- package/dist/schema/descriptor.d.ts +349 -0
- package/dist/schema/hooks.d.ts +164 -0
- package/dist/schema/index.d.ts +13 -0
- package/dist/schema/infer.d.ts +340 -0
- package/dist/schema/json.d.ts +237 -0
- package/dist/schema/relations.d.ts +205 -0
- package/dist/schema/table.d.ts +304 -0
- package/dist/schema/table_options.d.ts +103 -0
- package/dist/schema/table_patch.d.ts +338 -0
- package/dist/types/auth_adapter.d.ts +12 -6
- package/dist/types/auth_hooks.d.ts +65 -0
- package/dist/types/backend.d.ts +85 -96
- package/dist/types/data_source.d.ts +4 -9
- package/dist/types/database_adapter.d.ts +30 -31
- package/dist/types/entities.d.ts +0 -162
- package/dist/types/filter-operators.d.ts +2 -26
- package/dist/types/history.d.ts +6 -9
- package/dist/types/index.d.ts +3 -9
- package/dist/types/mcp_app_modules.d.ts +0 -2
- package/dist/types/policy.d.ts +85 -10
- package/dist/types/project_manifest.d.ts +113 -46
- package/dist/types/read_query.d.ts +98 -0
- package/dist/types/register.d.ts +35 -5
- package/dist/types/relations.d.ts +0 -430
- package/dist/types/schema_editing.d.ts +54 -2
- package/dist/types/schema_version.d.ts +24 -4
- package/dist/types/security_rules.d.ts +225 -15
- package/dist/types/storage_authorize.d.ts +2 -2
- package/dist/types/tenancy.d.ts +3 -3
- package/dist/types/ui_preferences.d.ts +228 -0
- package/package.json +3 -2
- package/dist/controllers/auth_state.d.ts +0 -23
- package/dist/controllers/collection_registry.d.ts +0 -67
- package/dist/controllers/effective_role.d.ts +0 -4
- package/dist/types/admin_block.d.ts +0 -96
- package/dist/types/chips.d.ts +0 -16
- package/dist/types/collection_contract.d.ts +0 -43
- package/dist/types/collection_patch.d.ts +0 -74
- package/dist/types/collections.d.ts +0 -783
- package/dist/types/component_ref.d.ts +0 -81
- package/dist/types/conditions-contract.types.d.ts +0 -14
- package/dist/types/entity_callbacks.d.ts +0 -393
- package/dist/types/indexes.d.ts +0 -179
- package/dist/types/properties.d.ts +0 -1640
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import type { JoinStep, OnAction } from "../types/relations.js";
|
|
2
|
+
import type { OrderByTuple } from "../types/filter-operators.js";
|
|
3
|
+
import type { ColumnAccess } from "./column_options.js";
|
|
4
|
+
import type { ColumnsShape } from "./columns.js";
|
|
5
|
+
/**
|
|
6
|
+
* A relation's target, as written: `() => users`.
|
|
7
|
+
*
|
|
8
|
+
* Typed `() => void` on purpose. A thunk typed by what it returns (generic
|
|
9
|
+
* over the returned table, a structural table type, or `() => unknown`) makes
|
|
10
|
+
* TypeScript resolve the returned table's type while it is still inferring the
|
|
11
|
+
* table being defined, so two tables that point at each other — `posts.author`
|
|
12
|
+
* → `users`, `users.posts` → `posts` — failed with TS7022/TS7024 unless one
|
|
13
|
+
* thunk was annotated. A `void` return is the one form TypeScript never
|
|
14
|
+
* resolves, so mutual and self relations need no annotation, in one file or
|
|
15
|
+
* across files.
|
|
16
|
+
*
|
|
17
|
+
* What the thunk returns is checked when the schema is compiled instead: a
|
|
18
|
+
* thunk that does not return a `defineTable` / `defineView` result is refused
|
|
19
|
+
* by `compileTables`, naming the table and the relation.
|
|
20
|
+
*
|
|
21
|
+
* @group Schema
|
|
22
|
+
*/
|
|
23
|
+
export type TableThunk = () => void;
|
|
24
|
+
/** The brand every relation builder result carries. */
|
|
25
|
+
export declare const RELATION_BRAND = "rebase.relation";
|
|
26
|
+
interface RelationDefBase {
|
|
27
|
+
readonly brand: typeof RELATION_BRAND;
|
|
28
|
+
/**
|
|
29
|
+
* The table it points at, behind a thunk so two tables can point at each
|
|
30
|
+
* other. Typed `() => unknown`: read it through `isTableDef` / `isViewDef`.
|
|
31
|
+
*/
|
|
32
|
+
readonly target: () => unknown;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Field-level access on a relation, accepted by every `rel.*`: who may read
|
|
36
|
+
* the relation (`include` it, see its key) and who may write a link through it.
|
|
37
|
+
* The same rules a column states with `.access()` / `.private()`, enforced by
|
|
38
|
+
* the server on every door — an `include` the caller may not read is refused,
|
|
39
|
+
* a nested write the caller may not make is refused.
|
|
40
|
+
*
|
|
41
|
+
* @group Schema
|
|
42
|
+
*/
|
|
43
|
+
export interface RelationAccessOptions {
|
|
44
|
+
/**
|
|
45
|
+
* Per-role read and write access to the relation. `read` lists the roles
|
|
46
|
+
* that may include it; `write` the roles that may link or unlink through
|
|
47
|
+
* it. Omitted: whoever the table's `rls` lets read or write the row.
|
|
48
|
+
*/
|
|
49
|
+
access?: ColumnAccess;
|
|
50
|
+
/**
|
|
51
|
+
* `true` keeps the relation off the wire entirely: not includable, not in
|
|
52
|
+
* the SDK or OpenAPI. Only server code traverses it. Not combined with
|
|
53
|
+
* `access`.
|
|
54
|
+
*/
|
|
55
|
+
private?: boolean;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Options of `rel.belongsTo`.
|
|
59
|
+
*
|
|
60
|
+
* @group Schema
|
|
61
|
+
*/
|
|
62
|
+
export interface BelongsToOptions extends RelationAccessOptions {
|
|
63
|
+
/**
|
|
64
|
+
* The SQL column on this table holding the foreign key. `<relation>_id`
|
|
65
|
+
* when omitted. A column in `columns` with this SQL name is bound to;
|
|
66
|
+
* otherwise the column is derived from the target's key.
|
|
67
|
+
*/
|
|
68
|
+
localKey?: string;
|
|
69
|
+
/** The derived column is `NOT NULL`. A bound column says so itself. */
|
|
70
|
+
notNull?: boolean;
|
|
71
|
+
/** The derived column is `UNIQUE`, making this side one-to-one. */
|
|
72
|
+
unique?: boolean;
|
|
73
|
+
/** `RESTRICT` when the column is `NOT NULL`, `SET NULL` otherwise. */
|
|
74
|
+
onDelete?: OnAction;
|
|
75
|
+
/**
|
|
76
|
+
* What the foreign key does when the referenced key changes. No `ON UPDATE`
|
|
77
|
+
* clause when omitted, so Postgres's default applies: `NO ACTION`.
|
|
78
|
+
*/
|
|
79
|
+
onUpdate?: OnAction;
|
|
80
|
+
/** `false` skips the index on the foreign key that is otherwise created. */
|
|
81
|
+
index?: boolean;
|
|
82
|
+
/** `false` stores the key without a `FOREIGN KEY` constraint. */
|
|
83
|
+
constraint?: boolean;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Options of `rel.hasOne` and `rel.hasMany`.
|
|
87
|
+
*
|
|
88
|
+
* @group Schema
|
|
89
|
+
*/
|
|
90
|
+
export interface HasOptions extends RelationAccessOptions {
|
|
91
|
+
/** The SQL column on the target that points back here. Derived from this table's slug when omitted. */
|
|
92
|
+
foreignKey?: string;
|
|
93
|
+
/** The SQL column on this table it points at, when that is not the primary key. */
|
|
94
|
+
sourceKey?: string;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Options of `rel.hasMany`.
|
|
98
|
+
*
|
|
99
|
+
* @group Schema
|
|
100
|
+
*/
|
|
101
|
+
export interface HasManyOptions extends HasOptions {
|
|
102
|
+
/** The order related rows come back in when the caller names none. */
|
|
103
|
+
orderBy?: OrderByTuple | readonly OrderByTuple[];
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Options of `rel.manyToMany`.
|
|
107
|
+
*
|
|
108
|
+
* @group Schema
|
|
109
|
+
*/
|
|
110
|
+
export interface ManyToManyOptions<P extends ColumnsShape = ColumnsShape> extends RelationAccessOptions {
|
|
111
|
+
/** The junction table. The two table names, sorted and joined by `_`, when omitted. */
|
|
112
|
+
through?: string;
|
|
113
|
+
/** The junction column pointing at this table. */
|
|
114
|
+
sourceKey?: string;
|
|
115
|
+
/** The junction column pointing at the target. */
|
|
116
|
+
targetKey?: string;
|
|
117
|
+
/** Extra columns on the junction, read and written as `_pivot`. */
|
|
118
|
+
pivot?: P;
|
|
119
|
+
/** What happens to a link when either row is deleted. `CASCADE` when omitted. */
|
|
120
|
+
onDelete?: OnAction;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Options of `rel.via`.
|
|
124
|
+
*
|
|
125
|
+
* @group Schema
|
|
126
|
+
*/
|
|
127
|
+
export interface ViaOptions extends RelationAccessOptions {
|
|
128
|
+
/** The joins from this table to the target, in order. */
|
|
129
|
+
steps: readonly JoinStep[];
|
|
130
|
+
/** `many` when omitted. */
|
|
131
|
+
cardinality?: "one" | "many";
|
|
132
|
+
}
|
|
133
|
+
/** `rel.belongsTo(...)`. */
|
|
134
|
+
export interface BelongsToDef extends RelationDefBase, BelongsToOptions {
|
|
135
|
+
readonly kind: "belongsTo";
|
|
136
|
+
}
|
|
137
|
+
/** `rel.hasOne(...)`. */
|
|
138
|
+
export interface HasOneDef extends RelationDefBase, HasOptions {
|
|
139
|
+
readonly kind: "hasOne";
|
|
140
|
+
}
|
|
141
|
+
/** `rel.hasMany(...)`. */
|
|
142
|
+
export interface HasManyDef extends RelationDefBase, HasManyOptions {
|
|
143
|
+
readonly kind: "hasMany";
|
|
144
|
+
}
|
|
145
|
+
/** `rel.manyToMany(...)`. */
|
|
146
|
+
export interface ManyToManyDef<P extends ColumnsShape = ColumnsShape> extends RelationDefBase, ManyToManyOptions<P> {
|
|
147
|
+
readonly kind: "manyToMany";
|
|
148
|
+
}
|
|
149
|
+
/** `rel.via(...)`. */
|
|
150
|
+
export interface ViaDef extends RelationDefBase, ViaOptions {
|
|
151
|
+
readonly kind: "via";
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* A relation as authored: the result of one `rel.*` call.
|
|
155
|
+
*
|
|
156
|
+
* @group Schema
|
|
157
|
+
*/
|
|
158
|
+
export type RelationDef = BelongsToDef | HasOneDef | HasManyDef | ManyToManyDef | ViaDef;
|
|
159
|
+
/**
|
|
160
|
+
* The `relations` of a table: `rel.*` results by relation name.
|
|
161
|
+
*
|
|
162
|
+
* @group Schema
|
|
163
|
+
*/
|
|
164
|
+
export type RelationsShape = Record<string, RelationDef>;
|
|
165
|
+
declare function belongsTo(target: TableThunk): BelongsToDef;
|
|
166
|
+
declare function belongsTo<const O extends BelongsToOptions>(target: TableThunk, options: O): BelongsToDef & O;
|
|
167
|
+
/**
|
|
168
|
+
* The relation builders. All five kinds; the target is a thunk, so two tables
|
|
169
|
+
* can point at each other whatever order their modules load in — and, being a
|
|
170
|
+
* {@link TableThunk}, without a type annotation on either side.
|
|
171
|
+
*
|
|
172
|
+
* ```ts
|
|
173
|
+
* relations: {
|
|
174
|
+
* author: rel.belongsTo(() => users, { localKey: "author_id", notNull: true, onDelete: "restrict" }),
|
|
175
|
+
* tags: rel.manyToMany(() => tags, { through: "posts_tags", pivot: { sortOrder: col.integer().default(0) } }),
|
|
176
|
+
* }
|
|
177
|
+
* ```
|
|
178
|
+
*
|
|
179
|
+
* @group Schema
|
|
180
|
+
*/
|
|
181
|
+
export declare const rel: {
|
|
182
|
+
/**
|
|
183
|
+
* This table holds the foreign key. Binds to the column in `columns` whose
|
|
184
|
+
* SQL name is `localKey`, or derives that column and indexes it.
|
|
185
|
+
*/
|
|
186
|
+
belongsTo: typeof belongsTo;
|
|
187
|
+
/** The target holds a unique foreign key pointing here. */
|
|
188
|
+
hasOne(target: TableThunk, options?: HasOptions): HasOneDef;
|
|
189
|
+
/** The target holds a foreign key pointing here. */
|
|
190
|
+
hasMany(target: TableThunk, options?: HasManyOptions): HasManyDef;
|
|
191
|
+
/**
|
|
192
|
+
* Linked through a junction table, which is created with its own row-level
|
|
193
|
+
* security. `pivot` columns are read and written as `_pivot`.
|
|
194
|
+
*/
|
|
195
|
+
manyToMany<P extends ColumnsShape = Record<never, never>>(target: TableThunk, options?: ManyToManyOptions<P>): ManyToManyDef<P>;
|
|
196
|
+
/** A read-only traversal across several joins. */
|
|
197
|
+
via(target: TableThunk, options: ViaOptions): ViaDef;
|
|
198
|
+
};
|
|
199
|
+
/**
|
|
200
|
+
* Whether a value is the result of a `rel.*` call.
|
|
201
|
+
*
|
|
202
|
+
* @group Schema
|
|
203
|
+
*/
|
|
204
|
+
export declare function isRelationDef(value: unknown): value is RelationDef;
|
|
205
|
+
export {};
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
import type { User } from "../users/index.js";
|
|
2
|
+
import type { ResourceRef } from "../types/resources.js";
|
|
3
|
+
import type { SearchConfig } from "../types/search.js";
|
|
4
|
+
import type { RoleDefinition, ScopeDefinition } from "../types/scopes.js";
|
|
5
|
+
import type { AuthCollectionContext, AuthCollectionCreateResult, AuthCollectionResetResult } from "../types/auth_hooks.js";
|
|
6
|
+
import type { ColumnsShape } from "./columns.js";
|
|
7
|
+
import type { BelongsToDef, RelationsShape } from "./relations.js";
|
|
8
|
+
import type { TableHooks } from "./hooks.js";
|
|
9
|
+
import type { RlsPosture, TableIndex, TableTenant } from "./table_options.js";
|
|
10
|
+
import type { InsertOfColumns, RowOfColumns } from "./infer.js";
|
|
11
|
+
/** The brand of a `defineTable` / `defineAuthTable` result. */
|
|
12
|
+
export declare const TABLE_BRAND = "rebase.table";
|
|
13
|
+
/** The brand of a `defineView` result. */
|
|
14
|
+
export declare const VIEW_BRAND = "rebase.view";
|
|
15
|
+
/**
|
|
16
|
+
* What `defineTable` takes besides the name.
|
|
17
|
+
*
|
|
18
|
+
* @group Schema
|
|
19
|
+
*/
|
|
20
|
+
export interface TableConfig<C extends ColumnsShape, R extends RelationsShape = Record<never, never>, USER extends User = User, S extends string = string> {
|
|
21
|
+
/**
|
|
22
|
+
* The name in the API, the SDK and in scopes. The table's name when
|
|
23
|
+
* omitted. Captured as a literal type, so `TableSlugOf<typeof table>` reads it.
|
|
24
|
+
*/
|
|
25
|
+
slug?: S;
|
|
26
|
+
/** The SQL table name, when it is not the name the table is defined under. */
|
|
27
|
+
table?: string;
|
|
28
|
+
/** The Postgres schema. `public` when omitted. */
|
|
29
|
+
schema?: string;
|
|
30
|
+
/** A non-default Postgres database, declared in `backend/resources.ts`. */
|
|
31
|
+
database?: ResourceRef;
|
|
32
|
+
/** What the table holds, for OpenAPI and MCP tool descriptions. */
|
|
33
|
+
description?: string;
|
|
34
|
+
/**
|
|
35
|
+
* The columns, in physical order. At least one must be `.primaryKey()`;
|
|
36
|
+
* several form a composite key in declaration order.
|
|
37
|
+
*/
|
|
38
|
+
columns: C;
|
|
39
|
+
/** The relations, by name. */
|
|
40
|
+
relations?: R;
|
|
41
|
+
/**
|
|
42
|
+
* The row-level security posture. Required: `"admin-only"`, `"inherit"`
|
|
43
|
+
* (the project's `defaultRls`), or the rules to generate policies from.
|
|
44
|
+
*/
|
|
45
|
+
rls: RlsPosture;
|
|
46
|
+
/**
|
|
47
|
+
* `false` generates only the declared rules, without the injected admin
|
|
48
|
+
* baseline. Refused together with `rls: "admin-only"`. `true` keeps the
|
|
49
|
+
* baseline even when the table's database switches it off for every
|
|
50
|
+
* table (`database({ rls: { defaultPolicies: false } })`). Absent, the
|
|
51
|
+
* database's declaration decides.
|
|
52
|
+
*/
|
|
53
|
+
defaultPolicies?: boolean;
|
|
54
|
+
/**
|
|
55
|
+
* Scope every row to a tenant: a column key, or a `belongsTo` relation's
|
|
56
|
+
* name, which stands for its foreign-key column (as the compiler reads it).
|
|
57
|
+
*/
|
|
58
|
+
tenant?: TableTenant<NoInfer<Extract<keyof C, string> | BelongsToKeys<R>>>;
|
|
59
|
+
/** Deleting stamps a timestamp column instead of removing the row. */
|
|
60
|
+
softDelete?: boolean | {
|
|
61
|
+
field?: NoInfer<Extract<keyof C, string>>;
|
|
62
|
+
};
|
|
63
|
+
/** Record every write in the history table. */
|
|
64
|
+
history?: boolean;
|
|
65
|
+
/** Full-text search over these columns. */
|
|
66
|
+
search?: SearchConfig;
|
|
67
|
+
/**
|
|
68
|
+
* Indexes, each with the query it serves. The keys are not inferred from:
|
|
69
|
+
* without `NoInfer`, a key naming nothing made TypeScript infer a relation
|
|
70
|
+
* of that name instead of refusing it.
|
|
71
|
+
*/
|
|
72
|
+
indexes?: readonly TableIndex<NoInfer<Extract<keyof C, string> | Extract<keyof R, string>>>[];
|
|
73
|
+
/** Server-side lifecycle hooks. */
|
|
74
|
+
hooks?: TableHooks<RowOfColumns<C>, InsertOfColumns<C>, USER>;
|
|
75
|
+
/** `false` accepts writes naming columns the table does not declare. */
|
|
76
|
+
strictWrites?: false;
|
|
77
|
+
/** Opaque data, served with the table's descriptor. */
|
|
78
|
+
metadata?: Record<string, unknown>;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* The names of the `belongsTo` relations in `R`: the relations that own a
|
|
82
|
+
* foreign-key column on this table, and so can stand for it.
|
|
83
|
+
*
|
|
84
|
+
* @group Schema
|
|
85
|
+
*/
|
|
86
|
+
export type BelongsToKeys<R extends RelationsShape> = {
|
|
87
|
+
[K in Extract<keyof R, string>]: R[K] extends BelongsToDef ? K : never;
|
|
88
|
+
}[Extract<keyof R, string>];
|
|
89
|
+
/**
|
|
90
|
+
* A table as authored: the result of `defineTable`.
|
|
91
|
+
*
|
|
92
|
+
* `N` is the name it was defined under and `S` its slug — the `slug` it was
|
|
93
|
+
* given, or the name when it was given none — both as literal types, so a
|
|
94
|
+
* frontend holding only `typeof posts` knows the slug (`TableSlugOf`).
|
|
95
|
+
*
|
|
96
|
+
* @group Schema
|
|
97
|
+
*/
|
|
98
|
+
export interface TableDef<N extends string = string, C extends ColumnsShape = ColumnsShape, R extends RelationsShape = RelationsShape, S extends string = N> {
|
|
99
|
+
readonly brand: typeof TABLE_BRAND;
|
|
100
|
+
/** The name it was defined under. */
|
|
101
|
+
readonly name: N;
|
|
102
|
+
/** What it was defined with, as written. */
|
|
103
|
+
readonly config: TableConfig<C, R, User, S>;
|
|
104
|
+
/** Present when defined with `defineAuthTable`. */
|
|
105
|
+
readonly auth?: AuthTableOptions;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Define a Postgres table.
|
|
109
|
+
*
|
|
110
|
+
* The name is the SQL table and the API slug; `rls` is mandatory. The result
|
|
111
|
+
* is what the backend reads from `backend/schema/index.ts` — it generates the
|
|
112
|
+
* DDL and the policies, serves the REST, realtime and MCP surfaces, and types
|
|
113
|
+
* the SDK.
|
|
114
|
+
*
|
|
115
|
+
* ```ts
|
|
116
|
+
* export const posts = defineTable("posts", {
|
|
117
|
+
* columns: {
|
|
118
|
+
* id: col.uuid().primaryKey().defaultRandom(),
|
|
119
|
+
* title: col.text().notNull(),
|
|
120
|
+
* },
|
|
121
|
+
* rls: [
|
|
122
|
+
* { operation: "select", access: "public" },
|
|
123
|
+
* { operations: ["insert", "update", "delete"], roles: ["editor"] },
|
|
124
|
+
* ],
|
|
125
|
+
* });
|
|
126
|
+
* ```
|
|
127
|
+
*
|
|
128
|
+
* @group Schema
|
|
129
|
+
*/
|
|
130
|
+
export declare function defineTable<const N extends string, C extends ColumnsShape, R extends RelationsShape = Record<never, never>, const S extends string = N>(name: N, config: TableConfig<C, R, User, S>): TableDef<N, C, R, S>;
|
|
131
|
+
/**
|
|
132
|
+
* The auth half of `defineAuthTable`.
|
|
133
|
+
*
|
|
134
|
+
* @group Schema
|
|
135
|
+
*/
|
|
136
|
+
export interface AuthTableOptions {
|
|
137
|
+
/** The roles a user can hold, each with the scopes it grants. */
|
|
138
|
+
roles?: Record<string, RoleDefinition>;
|
|
139
|
+
/** The scopes an API key or token can be granted. */
|
|
140
|
+
scopes?: Record<string, ScopeDefinition>;
|
|
141
|
+
/** Let users create personal API keys. */
|
|
142
|
+
personalKeys?: boolean;
|
|
143
|
+
/** Runs when an admin creates a user: hash the password, send the invitation. */
|
|
144
|
+
onCreateUser?: (values: Record<string, unknown>, ctx: AuthCollectionContext) => Promise<AuthCollectionCreateResult>;
|
|
145
|
+
/** Runs when an admin resets a user's password. */
|
|
146
|
+
onResetPassword?: (uid: string, ctx: AuthCollectionContext) => Promise<AuthCollectionResetResult>;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* What `defineAuthTable` takes besides the name.
|
|
150
|
+
*
|
|
151
|
+
* @group Schema
|
|
152
|
+
*/
|
|
153
|
+
export interface AuthTableConfig<C extends ColumnsShape, R extends RelationsShape = Record<never, never>, USER extends User = User, S extends string = string> extends TableConfig<C, R, USER, S> {
|
|
154
|
+
/** Roles, scopes, personal keys and the user-lifecycle hooks. */
|
|
155
|
+
auth?: AuthTableOptions;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Define the table users are stored in.
|
|
159
|
+
*
|
|
160
|
+
* A `defineTable` that auth reads and writes: the columns auth needs are added
|
|
161
|
+
* when the table does not declare them, a signed-in user can read their own
|
|
162
|
+
* row, and the privilege columns (`roles`) can only be written by an admin —
|
|
163
|
+
* whatever `rls` says. A project has at most one.
|
|
164
|
+
*
|
|
165
|
+
* @group Schema
|
|
166
|
+
*/
|
|
167
|
+
export declare function defineAuthTable<const N extends string, C extends ColumnsShape, R extends RelationsShape = Record<never, never>, const S extends string = N>(name: N, config: AuthTableConfig<C, R, User, S>): TableDef<N, C, R, S>;
|
|
168
|
+
/**
|
|
169
|
+
* What `defineView` takes besides the name.
|
|
170
|
+
*
|
|
171
|
+
* @group Schema
|
|
172
|
+
*/
|
|
173
|
+
export interface ViewConfig<C extends ColumnsShape, R extends RelationsShape = Record<never, never>, USER extends User = User, S extends string = string> {
|
|
174
|
+
/** The defining `SELECT`. */
|
|
175
|
+
query: string;
|
|
176
|
+
/**
|
|
177
|
+
* `true` stores the rows. Postgres cannot attach row policies to a
|
|
178
|
+
* materialized view, so it is read through a gate that admits `roles`.
|
|
179
|
+
*/
|
|
180
|
+
materialized?: boolean;
|
|
181
|
+
/** Who may read a materialized view. `["admin"]` when omitted. Not accepted on a plain view. */
|
|
182
|
+
roles?: readonly string[];
|
|
183
|
+
/** The columns the query returns. `.primaryKey()` marks the ones that identify a row. */
|
|
184
|
+
columns: C;
|
|
185
|
+
/** The name in the API. The view's name when omitted. Captured as a literal type, like a table's. */
|
|
186
|
+
slug?: S;
|
|
187
|
+
/** The SQL view name, when it is not the name the view is defined under. A plain SQL identifier. */
|
|
188
|
+
table?: string;
|
|
189
|
+
/** The Postgres schema. `public` when omitted. */
|
|
190
|
+
schema?: string;
|
|
191
|
+
/** A non-default Postgres database. */
|
|
192
|
+
database?: ResourceRef;
|
|
193
|
+
/** What the view holds, for OpenAPI and MCP. */
|
|
194
|
+
description?: string;
|
|
195
|
+
/** Relations to read through. No constraint is created for a view. */
|
|
196
|
+
relations?: R;
|
|
197
|
+
/** Indexes on a materialized view's stored rows. */
|
|
198
|
+
indexes?: readonly TableIndex<NoInfer<Extract<keyof C, string>>>[];
|
|
199
|
+
/** Read hooks. A view is never written. */
|
|
200
|
+
hooks?: Pick<TableHooks<RowOfColumns<C>, RowOfColumns<C>, USER>, "beforeQuery" | "afterRead">;
|
|
201
|
+
/** Opaque data, served with the view's descriptor. */
|
|
202
|
+
metadata?: Record<string, unknown>;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* A view as authored: the result of `defineView`.
|
|
206
|
+
*
|
|
207
|
+
* @group Schema
|
|
208
|
+
*/
|
|
209
|
+
export interface ViewDef<N extends string = string, C extends ColumnsShape = ColumnsShape, R extends RelationsShape = RelationsShape, S extends string = N> {
|
|
210
|
+
readonly brand: typeof VIEW_BRAND;
|
|
211
|
+
/** The name it was defined under. */
|
|
212
|
+
readonly name: N;
|
|
213
|
+
/** What it was defined with, as written. */
|
|
214
|
+
readonly config: ViewConfig<C, R, User, S>;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Define a SQL view, served read-only.
|
|
218
|
+
*
|
|
219
|
+
* A plain view is created `WITH (security_invoker = true)`: the row-level
|
|
220
|
+
* security of the tables it reads decides what each caller sees. A
|
|
221
|
+
* materialized view stores its rows, so it is gated by `roles` instead.
|
|
222
|
+
*
|
|
223
|
+
* @group Schema
|
|
224
|
+
*/
|
|
225
|
+
export declare function defineView<const N extends string, C extends ColumnsShape, R extends RelationsShape = Record<never, never>, const S extends string = N>(name: N, config: ViewConfig<C, R, User, S>): ViewDef<N, C, R, S>;
|
|
226
|
+
/**
|
|
227
|
+
* The slug of a table or view, from its type: `TableSlugOf<typeof posts>`.
|
|
228
|
+
*
|
|
229
|
+
* The `slug` it was defined with, or the name it was defined under when it was
|
|
230
|
+
* given none — the table's API path, SDK accessor and what scopes name. Reads
|
|
231
|
+
* any `{ name, config: { slug? } }` structurally, so `import type { posts }`
|
|
232
|
+
* is enough; `string` for a table whose type is not literal.
|
|
233
|
+
*
|
|
234
|
+
* ```ts
|
|
235
|
+
* const posts = defineTable("blog_posts", { slug: "posts", … });
|
|
236
|
+
* type S = TableSlugOf<typeof posts>; // "posts"
|
|
237
|
+
* ```
|
|
238
|
+
*
|
|
239
|
+
* @group Schema
|
|
240
|
+
*/
|
|
241
|
+
export type TableSlugOf<T extends {
|
|
242
|
+
readonly name: string;
|
|
243
|
+
}> = T extends {
|
|
244
|
+
readonly config: {
|
|
245
|
+
readonly slug?: infer S;
|
|
246
|
+
};
|
|
247
|
+
} ? [Exclude<S, undefined>] extends [never] ? T["name"] : Exclude<S, undefined> extends string ? Exclude<S, undefined> : T["name"] : T["name"];
|
|
248
|
+
/**
|
|
249
|
+
* A type compared member by member rather than through its type parameters.
|
|
250
|
+
*
|
|
251
|
+
* `TableConfig<C>` names `keyof C` (in `tenant`, `softDelete`, `indexes`), and
|
|
252
|
+
* TypeScript measures a type parameter read through `keyof` as contravariant —
|
|
253
|
+
* so `TableConfig<{ id: … }>` was refused where `TableConfig<ColumnsShape>`
|
|
254
|
+
* was expected, although every member is assignable. Spelling the "any" types
|
|
255
|
+
* out structurally makes the compiler compare the members, which is the
|
|
256
|
+
* question actually being asked.
|
|
257
|
+
*/
|
|
258
|
+
type Structural<T> = {
|
|
259
|
+
[K in keyof T]: T[K];
|
|
260
|
+
};
|
|
261
|
+
/**
|
|
262
|
+
* Any table, whatever its columns: what a list of `defineTable` results is
|
|
263
|
+
* typed as (`compileTables(defs)`, `backend/schema/index.ts`).
|
|
264
|
+
*
|
|
265
|
+
* @group Schema
|
|
266
|
+
*/
|
|
267
|
+
export interface AnyTableDef {
|
|
268
|
+
readonly brand: typeof TABLE_BRAND;
|
|
269
|
+
readonly name: string;
|
|
270
|
+
readonly config: Structural<TableConfig<ColumnsShape, RelationsShape>>;
|
|
271
|
+
readonly auth?: AuthTableOptions;
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Any view, whatever its columns.
|
|
275
|
+
*
|
|
276
|
+
* @group Schema
|
|
277
|
+
*/
|
|
278
|
+
export interface AnyViewDef {
|
|
279
|
+
readonly brand: typeof VIEW_BRAND;
|
|
280
|
+
readonly name: string;
|
|
281
|
+
readonly config: Structural<ViewConfig<ColumnsShape, RelationsShape>>;
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Whether a value is the result of `defineTable` or `defineAuthTable`, from
|
|
285
|
+
* this copy of the package or another.
|
|
286
|
+
*
|
|
287
|
+
* @group Schema
|
|
288
|
+
*/
|
|
289
|
+
export declare function isTableDef(value: unknown): value is AnyTableDef;
|
|
290
|
+
/**
|
|
291
|
+
* Whether a value is the result of `defineAuthTable`.
|
|
292
|
+
*
|
|
293
|
+
* @group Schema
|
|
294
|
+
*/
|
|
295
|
+
export declare function isAuthTableDef(value: unknown): value is AnyTableDef & {
|
|
296
|
+
auth: AuthTableOptions;
|
|
297
|
+
};
|
|
298
|
+
/**
|
|
299
|
+
* Whether a value is the result of `defineView`.
|
|
300
|
+
*
|
|
301
|
+
* @group Schema
|
|
302
|
+
*/
|
|
303
|
+
export declare function isViewDef(value: unknown): value is AnyViewDef;
|
|
304
|
+
export {};
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import type { SecurityRule } from "../types/security_rules.js";
|
|
2
|
+
import type { TenantSource } from "../types/tenancy.js";
|
|
3
|
+
/**
|
|
4
|
+
* The row-level security posture of a table. There is no default: a table
|
|
5
|
+
* without one does not compile and does not boot.
|
|
6
|
+
*
|
|
7
|
+
* - `"admin-only"` — RLS on, and only the admin baseline policy: the `admin`
|
|
8
|
+
* role and the server's own code reach rows; every other caller gets none.
|
|
9
|
+
* - `"inherit"` — the `defaultRls` exported from `backend/schema/index.ts`.
|
|
10
|
+
* Refused when that export is missing.
|
|
11
|
+
* - a non-empty list of {@link SecurityRule} — the policies to generate.
|
|
12
|
+
*
|
|
13
|
+
* @group Schema
|
|
14
|
+
*/
|
|
15
|
+
export type RlsPosture = "admin-only" | "inherit" | readonly [SecurityRule, ...SecurityRule[]];
|
|
16
|
+
/**
|
|
17
|
+
* What `backend/schema/index.ts` may export as `defaultRls`: the rules every
|
|
18
|
+
* `rls: "inherit"` table takes.
|
|
19
|
+
*
|
|
20
|
+
* @group Schema
|
|
21
|
+
*/
|
|
22
|
+
export type DefaultRls = "admin-only" | readonly [SecurityRule, ...SecurityRule[]];
|
|
23
|
+
/**
|
|
24
|
+
* One key of an index: a column, optionally with its sort order.
|
|
25
|
+
*
|
|
26
|
+
* @group Schema
|
|
27
|
+
*/
|
|
28
|
+
export interface TableIndexKey<Keys extends string = string> {
|
|
29
|
+
/** A column key, or a `belongsTo` relation (its foreign-key column). */
|
|
30
|
+
column: Keys | (string & {});
|
|
31
|
+
/**
|
|
32
|
+
* Sort order of this key. Ascending when omitted; `gin` and `brin` indexes
|
|
33
|
+
* take none.
|
|
34
|
+
*/
|
|
35
|
+
direction?: "asc" | "desc";
|
|
36
|
+
/**
|
|
37
|
+
* Where `NULL`s sort. When omitted, Postgres's own placement: last under `asc`,
|
|
38
|
+
* first under `desc`.
|
|
39
|
+
*/
|
|
40
|
+
nulls?: "first" | "last";
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The `WHERE` of a partial index.
|
|
44
|
+
*
|
|
45
|
+
* @group Schema
|
|
46
|
+
*/
|
|
47
|
+
export type TableIndexPredicate<Keys extends string = string> = {
|
|
48
|
+
column: Keys | (string & {});
|
|
49
|
+
op: "=";
|
|
50
|
+
value: string | number | boolean;
|
|
51
|
+
} | {
|
|
52
|
+
column: Keys | (string & {});
|
|
53
|
+
op: "!=" | "<" | "<=" | ">" | ">=";
|
|
54
|
+
value: string | number;
|
|
55
|
+
} | {
|
|
56
|
+
column: Keys | (string & {});
|
|
57
|
+
op: "is null" | "is not null";
|
|
58
|
+
} | {
|
|
59
|
+
column: Keys | (string & {});
|
|
60
|
+
op: "in";
|
|
61
|
+
value: readonly [string | number, ...(string | number)[]];
|
|
62
|
+
} | {
|
|
63
|
+
and: readonly [TableIndexPredicate<Keys>, ...TableIndexPredicate<Keys>[]];
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* An index a table declares.
|
|
67
|
+
*
|
|
68
|
+
* `columns` names what is indexed, in order. `reason` says which query it is
|
|
69
|
+
* for: `rebase doctor` prints it beside the index's usage, the one moment
|
|
70
|
+
* anyone can decide to delete one.
|
|
71
|
+
*
|
|
72
|
+
* @group Schema
|
|
73
|
+
*/
|
|
74
|
+
export interface TableIndex<Keys extends string = string> {
|
|
75
|
+
/**
|
|
76
|
+
* The index's keys, in order: a column key (or `belongsTo` relation), or a
|
|
77
|
+
* `TableIndexKey` to give one a sort order. At least one.
|
|
78
|
+
*/
|
|
79
|
+
columns: readonly [Keys | TableIndexKey<Keys>, ...(Keys | TableIndexKey<Keys>)[]];
|
|
80
|
+
/** `btree` when omitted. `gin` and `brin` take no direction. */
|
|
81
|
+
using?: "btree" | "gin" | "brin";
|
|
82
|
+
/** A unique index (btree only). */
|
|
83
|
+
unique?: boolean;
|
|
84
|
+
/** Columns stored in the index without being part of its key (btree only). */
|
|
85
|
+
include?: readonly (Keys | (string & {}))[];
|
|
86
|
+
/** Index only the rows that match. */
|
|
87
|
+
where?: TableIndexPredicate<Keys>;
|
|
88
|
+
/** The query this index serves. */
|
|
89
|
+
reason?: string;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Scopes every row of a table to a tenant.
|
|
93
|
+
*
|
|
94
|
+
* @group Schema
|
|
95
|
+
*/
|
|
96
|
+
export interface TableTenant<Keys extends string = string> {
|
|
97
|
+
/** The column that holds the tenant. */
|
|
98
|
+
field: Keys;
|
|
99
|
+
/** Where the caller's tenant comes from: a JWT claim, or a membership table. */
|
|
100
|
+
from: TenantSource;
|
|
101
|
+
/** Roles that see every tenant. `["admin"]` when omitted. */
|
|
102
|
+
bypassRoles?: readonly string[];
|
|
103
|
+
}
|