@tapedb/tapedb 0.0.0-stage → 0.1.0
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/API.md +301 -0
- package/LICENSE +21 -0
- package/LIMITATIONS.md +34 -0
- package/PRIVACY.md +119 -0
- package/README.md +720 -2
- package/RELEASING.md +117 -0
- package/SECURITY.md +121 -0
- package/dist/chunk-3AL32Q4K.js +241 -0
- package/dist/chunk-3AL32Q4K.js.map +1 -0
- package/dist/chunk-CYDXFAEB.js +33 -0
- package/dist/chunk-CYDXFAEB.js.map +1 -0
- package/dist/chunk-UYZJAMBR.js +27 -0
- package/dist/chunk-UYZJAMBR.js.map +1 -0
- package/dist/chunk-VWW5OS5B.js +841 -0
- package/dist/chunk-VWW5OS5B.js.map +1 -0
- package/dist/cli.d.ts +44 -0
- package/dist/cli.js +1501 -0
- package/dist/cli.js.map +1 -0
- package/dist/client-DgSnn9BE.d.ts +646 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +3007 -0
- package/dist/index.js.map +1 -0
- package/dist/react.d.ts +41 -0
- package/dist/react.js +59 -0
- package/dist/react.js.map +1 -0
- package/dist/schema-CVcGZJfR.d.ts +691 -0
- package/dist/schema-entry-CpO_IGFv.d.ts +522 -0
- package/dist/schema.d.ts +2 -0
- package/dist/schema.js +22 -0
- package/dist/schema.js.map +1 -0
- package/dist/worker.d.ts +437 -0
- package/dist/worker.js +9270 -0
- package/dist/worker.js.map +1 -0
- package/package.json +93 -4
|
@@ -0,0 +1,522 @@
|
|
|
1
|
+
import { a as WorkspaceRole, m as TapeDBSchema } from './schema-CVcGZJfR.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Field and relation builders: the `t` in `model({ title: t.string() })`.
|
|
5
|
+
*
|
|
6
|
+
* Every builder is immutable: a modifier returns a new builder, so a shared
|
|
7
|
+
* base field cannot be changed by one model's `.optional()`. The type
|
|
8
|
+
* parameters carry what the client needs to infer rows and inputs.
|
|
9
|
+
*/
|
|
10
|
+
type ScalarKind = "string" | "number" | "boolean" | "date" | "json";
|
|
11
|
+
/** The TypeScript type a stored value comes back as. */
|
|
12
|
+
interface KindOutput {
|
|
13
|
+
string: string;
|
|
14
|
+
number: number;
|
|
15
|
+
boolean: boolean;
|
|
16
|
+
/** Epoch milliseconds. */
|
|
17
|
+
date: number;
|
|
18
|
+
json: unknown;
|
|
19
|
+
}
|
|
20
|
+
/** What a write accepts for each kind. */
|
|
21
|
+
interface KindInput {
|
|
22
|
+
string: string;
|
|
23
|
+
number: number;
|
|
24
|
+
boolean: boolean;
|
|
25
|
+
date: number | Date | string;
|
|
26
|
+
json: unknown;
|
|
27
|
+
}
|
|
28
|
+
type DeleteBehaviour = "setNull" | "cascade" | "restrict";
|
|
29
|
+
declare class Field<K extends ScalarKind = ScalarKind, O extends boolean = boolean, D extends boolean = boolean> {
|
|
30
|
+
readonly kind: K;
|
|
31
|
+
readonly isOptional: O;
|
|
32
|
+
readonly hasDefault: D;
|
|
33
|
+
readonly defaultValue: unknown;
|
|
34
|
+
readonly isIndexed: boolean;
|
|
35
|
+
readonly isUnique: boolean;
|
|
36
|
+
readonly tag: "field";
|
|
37
|
+
constructor(kind: K, isOptional: O, hasDefault: D, defaultValue?: unknown, isIndexed?: boolean, isUnique?: boolean);
|
|
38
|
+
/** Encrypt this value under the named subject's erasable key. */
|
|
39
|
+
personal(this: Field<K, O, D> & (K extends "string" | "json" ? unknown : never), options: {
|
|
40
|
+
subject: string;
|
|
41
|
+
}): PersonalField<Extract<K, "string" | "json">, O, D>;
|
|
42
|
+
/** The field may be null, and may be left out of `create`. */
|
|
43
|
+
optional(): Field<K, true, D>;
|
|
44
|
+
/** Filled in by `create` when the field is left out. */
|
|
45
|
+
default(value: KindInput[K]): Field<K, O, true>;
|
|
46
|
+
/** Adds an index, for filters and sorts on this field. */
|
|
47
|
+
index(): Field<K, O, D>;
|
|
48
|
+
/** Adds a unique index. */
|
|
49
|
+
unique(): Field<K, O, D>;
|
|
50
|
+
}
|
|
51
|
+
declare class PersonalField<K extends "string" | "json" = "string" | "json", O extends boolean = boolean, D extends boolean = boolean> extends Field<K, O, D> {
|
|
52
|
+
readonly personalSubject: string;
|
|
53
|
+
constructor(kind: K, optional: O, hasDefault: D, personalSubject: string, defaultValue?: unknown);
|
|
54
|
+
optional(): PersonalField<K, true, D>;
|
|
55
|
+
default(value: KindInput[K]): PersonalField<K, O, true>;
|
|
56
|
+
index(this: never): never;
|
|
57
|
+
unique(this: never): never;
|
|
58
|
+
}
|
|
59
|
+
interface BelongsToOptions {
|
|
60
|
+
/** What happens to this row when the row it points at is deleted. Default `setNull`. */
|
|
61
|
+
onDelete?: DeleteBehaviour;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* This model holds the foreign key. A field `board` stores `boardId`.
|
|
65
|
+
*/
|
|
66
|
+
declare class BelongsTo<T extends string = string, O extends boolean = boolean> {
|
|
67
|
+
readonly target: T;
|
|
68
|
+
readonly isOptional: O;
|
|
69
|
+
readonly onDelete: DeleteBehaviour;
|
|
70
|
+
readonly tag: "belongsTo";
|
|
71
|
+
constructor(target: T, isOptional: O, onDelete?: DeleteBehaviour);
|
|
72
|
+
/** The key may be null: the row can exist without a parent. */
|
|
73
|
+
optional(): BelongsTo<T, true>;
|
|
74
|
+
}
|
|
75
|
+
interface HasManyOptions {
|
|
76
|
+
/**
|
|
77
|
+
* The `belongsTo` field on the target that points back here. Needed only
|
|
78
|
+
* when the target has more than one `belongsTo` to this model.
|
|
79
|
+
*/
|
|
80
|
+
inverse?: string;
|
|
81
|
+
}
|
|
82
|
+
/** The inverse of a `belongsTo`. Stores nothing; named in `include`. */
|
|
83
|
+
declare class HasMany<T extends string = string> {
|
|
84
|
+
readonly target: T;
|
|
85
|
+
readonly inverse?: string | undefined;
|
|
86
|
+
readonly tag: "hasMany";
|
|
87
|
+
constructor(target: T, inverse?: string | undefined);
|
|
88
|
+
}
|
|
89
|
+
type AnyField = Field<ScalarKind, boolean, boolean>;
|
|
90
|
+
type ModelMember = AnyField | BelongsTo | HasMany;
|
|
91
|
+
declare const t: {
|
|
92
|
+
string: () => Field<"string", false, false>;
|
|
93
|
+
number: () => Field<"number", false, false>;
|
|
94
|
+
boolean: () => Field<"boolean", false, false>;
|
|
95
|
+
/** Stored as epoch milliseconds; accepts a `Date`, milliseconds or an ISO string. */
|
|
96
|
+
date: () => Field<"date", false, false>;
|
|
97
|
+
/** Any JSON value. Not filterable or sortable. */
|
|
98
|
+
json: () => Field<"json", false, false>;
|
|
99
|
+
belongsTo: <T extends string>(target: T, options?: BelongsToOptions) => BelongsTo<T, false>;
|
|
100
|
+
hasMany: <T extends string>(target: T, options?: HasManyOptions) => HasMany<T>;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Public filters, ordering and includes, translated into the engine's query
|
|
105
|
+
* document.
|
|
106
|
+
*
|
|
107
|
+
* The public shape is `{ title: "x", due: { lt: now }, OR: [...] }`; the engine
|
|
108
|
+
* reads `{ title: "x", due: { $lt: now }, $or: [...] }`. The translation is
|
|
109
|
+
* strict: an unknown field, operator or relation is an error here rather than
|
|
110
|
+
* a filter the engine silently drops.
|
|
111
|
+
*/
|
|
112
|
+
|
|
113
|
+
type Scalar = string | number | boolean | null | Date;
|
|
114
|
+
interface FieldFilter<T = Scalar> {
|
|
115
|
+
equals?: T;
|
|
116
|
+
not?: T | FieldFilter<T>;
|
|
117
|
+
in?: T[];
|
|
118
|
+
notIn?: T[];
|
|
119
|
+
gt?: T;
|
|
120
|
+
gte?: T;
|
|
121
|
+
lt?: T;
|
|
122
|
+
lte?: T;
|
|
123
|
+
}
|
|
124
|
+
/** Filters on a to-many relation: at least one, all, or no related row. */
|
|
125
|
+
interface ManyFilter<W> {
|
|
126
|
+
some?: W;
|
|
127
|
+
every?: W;
|
|
128
|
+
none?: W;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Filters, typed at every depth. Relation keys resolve lazily, as property
|
|
132
|
+
* types of a mapped object, so models that point at each other (boards hold
|
|
133
|
+
* columns hold boards) are unfolded only as far as a filter actually goes.
|
|
134
|
+
*/
|
|
135
|
+
type Scalars<F extends Fields> = {
|
|
136
|
+
[K in FilterKeys<F> | {
|
|
137
|
+
[P in keyof F]: F[P] extends {
|
|
138
|
+
personalSubject: string;
|
|
139
|
+
} ? P : never;
|
|
140
|
+
}[keyof F]]?: K extends FilterKeys<F> ? Scalar | FieldFilter : never;
|
|
141
|
+
};
|
|
142
|
+
type Where<F extends Fields = Fields, M extends Models = Models> = Scalars<F> & {
|
|
143
|
+
[K in RelationKeys<F>]?: F[K] extends HasMany ? ManyFilter<Where<TargetFieldsOf<F[K], M>, M>> : Where<TargetFieldsOf<F[K], M>, M> | null;
|
|
144
|
+
} & {
|
|
145
|
+
AND?: Where<F, M> | Where<F, M>[];
|
|
146
|
+
OR?: Where<F, M>[];
|
|
147
|
+
NOT?: Where<F, M> | Where<F, M>[];
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* `schema()` and `model()`: the public way to describe a database.
|
|
152
|
+
*
|
|
153
|
+
* A schema compiles to the same registry, permissions and migrations the
|
|
154
|
+
* engine already runs on, so the Worker accepts it unchanged. It also keeps a
|
|
155
|
+
* small relation graph that the client uses to turn `include`, `connect` and
|
|
156
|
+
* friends into engine queries and ops.
|
|
157
|
+
*/
|
|
158
|
+
|
|
159
|
+
type RoleName = "guest" | "user" | "member" | "editor" | "moderator" | "admin" | "owner";
|
|
160
|
+
interface AccessAuth {
|
|
161
|
+
/** The caller's subject, from your identity provider. */
|
|
162
|
+
sub: string;
|
|
163
|
+
role: WorkspaceRole;
|
|
164
|
+
workspaceId: string;
|
|
165
|
+
}
|
|
166
|
+
interface CheckContext<F extends Fields = Fields> {
|
|
167
|
+
auth: AccessAuth;
|
|
168
|
+
/** The stored row, for update, delete and read. */
|
|
169
|
+
row?: Partial<Row<F>> & {
|
|
170
|
+
id: string;
|
|
171
|
+
};
|
|
172
|
+
/** The incoming values, for create and update. */
|
|
173
|
+
data?: Record<string, unknown>;
|
|
174
|
+
/** Field names this write sets. */
|
|
175
|
+
changed?: string[];
|
|
176
|
+
}
|
|
177
|
+
interface RoleDecision {
|
|
178
|
+
readonly decision: "role";
|
|
179
|
+
readonly level: WorkspaceRole;
|
|
180
|
+
}
|
|
181
|
+
interface FilterDecision<F extends Fields = Fields> {
|
|
182
|
+
readonly decision: "filter";
|
|
183
|
+
readonly build: (context: {
|
|
184
|
+
auth: AccessAuth;
|
|
185
|
+
}) => Where<F> | Where<F>[];
|
|
186
|
+
}
|
|
187
|
+
interface CheckDecision<F extends Fields = Fields> {
|
|
188
|
+
readonly decision: "check";
|
|
189
|
+
readonly test: (context: CheckContext<F>) => boolean;
|
|
190
|
+
}
|
|
191
|
+
/** A minimum role, checked before any SQL. */
|
|
192
|
+
declare function role(name: RoleName): RoleDecision;
|
|
193
|
+
/**
|
|
194
|
+
* `read` only: the rows the caller may see, as a filter compiled into SQL. An
|
|
195
|
+
* array is OR of its filters.
|
|
196
|
+
*/
|
|
197
|
+
declare function filter<F extends Fields = Fields>(build: (context: {
|
|
198
|
+
auth: AccessAuth;
|
|
199
|
+
}) => Where<F> | Where<F>[]): FilterDecision<F>;
|
|
200
|
+
/** A predicate over the caller and the row. Throwing denies. */
|
|
201
|
+
declare function check<F extends Fields = Fields>(test: (context: CheckContext<F>) => boolean): CheckDecision<F>;
|
|
202
|
+
/** A file as file rules see it. */
|
|
203
|
+
interface FileInfo {
|
|
204
|
+
path: string;
|
|
205
|
+
/** Bytes; null for a write whose size is not known yet (a multipart start). */
|
|
206
|
+
size: number | null;
|
|
207
|
+
contentType: string | null;
|
|
208
|
+
/** The uploader's claim subject. */
|
|
209
|
+
uploadedBy: string;
|
|
210
|
+
}
|
|
211
|
+
interface FileCheckContext {
|
|
212
|
+
auth: AccessAuth;
|
|
213
|
+
/** For read and delete, the stored file; for write, the incoming one. */
|
|
214
|
+
file: FileInfo;
|
|
215
|
+
/** For write, the stored file it would replace, if any. */
|
|
216
|
+
existing?: FileInfo;
|
|
217
|
+
}
|
|
218
|
+
interface FileCheckDecision {
|
|
219
|
+
readonly decision: "check";
|
|
220
|
+
readonly test: (context: FileCheckContext) => boolean;
|
|
221
|
+
}
|
|
222
|
+
interface AttachedToDecision<K extends string = string> {
|
|
223
|
+
readonly decision: "attachedTo";
|
|
224
|
+
readonly model: K;
|
|
225
|
+
readonly locate: (path: string) => string | null | undefined;
|
|
226
|
+
}
|
|
227
|
+
/** A file rule: a predicate over the caller and the file. Throwing denies. */
|
|
228
|
+
declare function fileCheck(test: (context: FileCheckContext) => boolean): FileCheckDecision;
|
|
229
|
+
/**
|
|
230
|
+
* A file rule: the file belongs to a row and follows its rules. `locate`
|
|
231
|
+
* maps a path to the row's id (null when the path names none, which denies).
|
|
232
|
+
* Reading the file needs the row to be readable by the caller; writing or
|
|
233
|
+
* deleting it needs the caller to be allowed to update the row.
|
|
234
|
+
*
|
|
235
|
+
* files: { read: attachedTo("task", (path) => path.split("/")[1]) }
|
|
236
|
+
*/
|
|
237
|
+
declare function attachedTo<K extends string>(model: K, locate: (path: string) => string | null | undefined): AttachedToDecision<K>;
|
|
238
|
+
type FileRuleDecision<K extends string = string> = RoleDecision | FileCheckDecision | AttachedToDecision<K>;
|
|
239
|
+
/** Who may read, write and delete each file. Defaults: read member, write and delete editor. */
|
|
240
|
+
interface FileAccess<K extends string = string> {
|
|
241
|
+
read?: FileRuleDecision<K>;
|
|
242
|
+
write?: FileRuleDecision<K>;
|
|
243
|
+
delete?: FileRuleDecision<K>;
|
|
244
|
+
}
|
|
245
|
+
interface SchemaOptions<K extends string = string> {
|
|
246
|
+
files?: FileAccess<K>;
|
|
247
|
+
}
|
|
248
|
+
interface Access<F extends Fields = Fields> {
|
|
249
|
+
read?: RoleDecision | FilterDecision<F> | CheckDecision<F>;
|
|
250
|
+
create?: RoleDecision | CheckDecision<F>;
|
|
251
|
+
update?: RoleDecision | CheckDecision<F>;
|
|
252
|
+
delete?: RoleDecision | CheckDecision<F>;
|
|
253
|
+
}
|
|
254
|
+
type Fields = Record<string, ModelMember>;
|
|
255
|
+
interface ModelOptions {
|
|
256
|
+
/** An existing table to map this model onto. Default: the key in snake_case. */
|
|
257
|
+
table?: string;
|
|
258
|
+
}
|
|
259
|
+
declare class Model<F extends Fields = Fields> {
|
|
260
|
+
readonly fields: F;
|
|
261
|
+
readonly options: ModelOptions;
|
|
262
|
+
readonly rules: Access<F>;
|
|
263
|
+
constructor(fields: F, options?: ModelOptions, rules?: Access<F>);
|
|
264
|
+
/** Who may read, create, update and delete rows of this model. */
|
|
265
|
+
access(rules: Access<F>): Model<F>;
|
|
266
|
+
}
|
|
267
|
+
declare function model<F extends Fields>(fields: F, options?: ModelOptions): Model<F>;
|
|
268
|
+
type Keys<F, Tag> = {
|
|
269
|
+
[K in keyof F]: F[K] extends Tag ? K : never;
|
|
270
|
+
}[keyof F] & string;
|
|
271
|
+
type ScalarKeys<F> = Keys<F, Field>;
|
|
272
|
+
type BelongsToKeys<F> = Keys<F, BelongsTo>;
|
|
273
|
+
type HasManyKeys<F> = Keys<F, HasMany>;
|
|
274
|
+
type RelationKeys<F> = BelongsToKeys<F> | HasManyKeys<F>;
|
|
275
|
+
type OutputOf<S> = S extends Field<infer K, infer O, boolean> ? KindOutput[K] | (S extends {
|
|
276
|
+
personalSubject: string;
|
|
277
|
+
} ? null : O extends true ? null : never) : never;
|
|
278
|
+
type ForeignKeyOf<S> = S extends BelongsTo<string, infer O> ? string | (O extends true ? null : never) : never;
|
|
279
|
+
/** A stored row: its fields, the foreign keys of its `belongsTo` relations, and the system fields. */
|
|
280
|
+
type Row<F> = {
|
|
281
|
+
id: string;
|
|
282
|
+
createdAt: number;
|
|
283
|
+
updatedAt: number;
|
|
284
|
+
$erased?: string[];
|
|
285
|
+
} & {
|
|
286
|
+
[K in ScalarKeys<F>]: OutputOf<F[K]>;
|
|
287
|
+
} & {
|
|
288
|
+
[K in BelongsToKeys<F> as `${K}Id`]: ForeignKeyOf<F[K]>;
|
|
289
|
+
};
|
|
290
|
+
/** Fields filterable and sortable: scalars except json, foreign keys and `id`. */
|
|
291
|
+
type FilterKeys<F> = "id" | {
|
|
292
|
+
[K in ScalarKeys<F>]: F[K] extends {
|
|
293
|
+
personalSubject: string;
|
|
294
|
+
} ? never : F[K] extends Field<"json", boolean, boolean> ? never : K;
|
|
295
|
+
}[ScalarKeys<F>] | `${BelongsToKeys<F>}Id`;
|
|
296
|
+
type InputOf<S> = S extends Field<infer K, infer O, boolean> ? KindInput[K] | (O extends true ? null : never) : never;
|
|
297
|
+
type RequiredScalarKeys<F> = {
|
|
298
|
+
[K in ScalarKeys<F>]: F[K] extends Field<ScalarKind, false, false> ? K : never;
|
|
299
|
+
}[ScalarKeys<F>];
|
|
300
|
+
type OptionalScalarKeys<F> = Exclude<ScalarKeys<F>, RequiredScalarKeys<F>>;
|
|
301
|
+
/**
|
|
302
|
+
* Rows a write may create alongside the row it addresses, typed at every
|
|
303
|
+
* depth. Relation keys resolve lazily (as property types of a mapped object),
|
|
304
|
+
* so models that point at each other do not expand the graph up front. The
|
|
305
|
+
* key that links a nested row to the row creating it is filled in by the
|
|
306
|
+
* write, so every relation is optional here; the engine still validates
|
|
307
|
+
* every op.
|
|
308
|
+
*/
|
|
309
|
+
type NestedCreate<F, M extends Models> = {
|
|
310
|
+
id?: string;
|
|
311
|
+
} & {
|
|
312
|
+
[K in RequiredScalarKeys<F>]: InputOf<F[K]>;
|
|
313
|
+
} & {
|
|
314
|
+
[K in OptionalScalarKeys<F>]?: InputOf<F[K]>;
|
|
315
|
+
} & {
|
|
316
|
+
[K in BelongsToKeys<F>]?: BelongsToInput<F[K], M>;
|
|
317
|
+
} & {
|
|
318
|
+
[K in HasManyKeys<F>]?: HasManyInput<F[K], M>;
|
|
319
|
+
} & {
|
|
320
|
+
[K in BelongsToKeys<F> as `${K}Id`]?: string | null;
|
|
321
|
+
};
|
|
322
|
+
type BelongsToInput<F, M extends Models> = {
|
|
323
|
+
connect: string;
|
|
324
|
+
} | {
|
|
325
|
+
create: NestedCreate<TargetFieldsOf<F, M>, M>;
|
|
326
|
+
} | {
|
|
327
|
+
disconnect: true;
|
|
328
|
+
} | null;
|
|
329
|
+
type RequiredBelongsToKeys<F> = {
|
|
330
|
+
[K in BelongsToKeys<F>]: F[K] extends BelongsTo<string, false> ? K : never;
|
|
331
|
+
}[BelongsToKeys<F>];
|
|
332
|
+
type UnionToIntersection<U> = (U extends unknown ? (value: U) => void : never) extends (value: infer I) => void ? I : never;
|
|
333
|
+
/**
|
|
334
|
+
* A required belongsTo must be set on create, as `{ connect }` or as its key.
|
|
335
|
+
* Each key's two forms are boxed before the union of keys becomes an
|
|
336
|
+
* intersection, so "board or boardId" stays a choice instead of becoming both.
|
|
337
|
+
*/
|
|
338
|
+
type RequiredParents<F> = [RequiredBelongsToKeys<F>] extends [never] ? unknown : Unbox<UnionToIntersection<{
|
|
339
|
+
[K in RequiredBelongsToKeys<F>]: {
|
|
340
|
+
box: {
|
|
341
|
+
[P in K]: {
|
|
342
|
+
connect: string;
|
|
343
|
+
};
|
|
344
|
+
} | {
|
|
345
|
+
[P in `${K}Id`]: string;
|
|
346
|
+
};
|
|
347
|
+
};
|
|
348
|
+
}[RequiredBelongsToKeys<F>]>>;
|
|
349
|
+
type Unbox<T> = T extends {
|
|
350
|
+
box: infer B;
|
|
351
|
+
} ? B : never;
|
|
352
|
+
type HasManyInput<F, M extends Models> = {
|
|
353
|
+
connect?: string[];
|
|
354
|
+
disconnect?: string[];
|
|
355
|
+
/** Rows created with this one, carrying its key. */
|
|
356
|
+
create?: NestedCreate<TargetFieldsOf<F, M>, M> | NestedCreate<TargetFieldsOf<F, M>, M>[];
|
|
357
|
+
};
|
|
358
|
+
type CreateData<F, M extends Models = Models> = {
|
|
359
|
+
id?: string;
|
|
360
|
+
} & RequiredParents<F> & {
|
|
361
|
+
[K in RequiredScalarKeys<F>]: InputOf<F[K]>;
|
|
362
|
+
} & {
|
|
363
|
+
[K in OptionalScalarKeys<F>]?: InputOf<F[K]>;
|
|
364
|
+
} & {
|
|
365
|
+
[K in BelongsToKeys<F>]?: BelongsToInput<F[K], M>;
|
|
366
|
+
} & {
|
|
367
|
+
[K in BelongsToKeys<F> as `${K}Id`]?: string | null;
|
|
368
|
+
} & {
|
|
369
|
+
[K in HasManyKeys<F>]?: HasManyInput<F[K], M>;
|
|
370
|
+
};
|
|
371
|
+
type UpdateData<F, M extends Models = Models> = {
|
|
372
|
+
[K in ScalarKeys<F>]?: InputOf<F[K]>;
|
|
373
|
+
} & {
|
|
374
|
+
[K in BelongsToKeys<F>]?: BelongsToInput<F[K], M>;
|
|
375
|
+
} & {
|
|
376
|
+
[K in BelongsToKeys<F> as `${K}Id`]?: string | null;
|
|
377
|
+
} & {
|
|
378
|
+
[K in HasManyKeys<F>]?: HasManyInput<F[K], M>;
|
|
379
|
+
};
|
|
380
|
+
type Models = Record<string, Model>;
|
|
381
|
+
type FieldsOf<M> = M extends Model<infer F> ? F : never;
|
|
382
|
+
type TargetOf<S> = S extends BelongsTo<infer T, boolean> ? T : S extends HasMany<infer T> ? T : never;
|
|
383
|
+
type TargetFields<S, M extends Models> = TargetOf<S> extends keyof M ? FieldsOf<M[TargetOf<S>]> : Fields;
|
|
384
|
+
/** The fields of the model a relation points at. */
|
|
385
|
+
type TargetFieldsOf<S, M extends Models> = TargetFields<S, M>;
|
|
386
|
+
type OrderBy<F> = Partial<Record<FilterKeys<F>, "asc" | "desc">> | Partial<Record<FilterKeys<F>, "asc" | "desc">>[];
|
|
387
|
+
interface IncludeArgs<M extends Models, F extends Fields> {
|
|
388
|
+
where?: Where<F, M>;
|
|
389
|
+
orderBy?: OrderBy<F>;
|
|
390
|
+
take?: number;
|
|
391
|
+
include?: Include<M, F>;
|
|
392
|
+
select?: Select<M, F>;
|
|
393
|
+
}
|
|
394
|
+
type Include<M extends Models, F extends Fields> = {
|
|
395
|
+
[K in RelationKeys<F>]?: true | IncludeArgs<M, TargetFields<F[K], M>>;
|
|
396
|
+
};
|
|
397
|
+
/**
|
|
398
|
+
* Fields to return instead of the whole row. Relations listed here are
|
|
399
|
+
* included, and may carry their own `select`.
|
|
400
|
+
*/
|
|
401
|
+
type Select<M extends Models, F extends Fields> = {
|
|
402
|
+
[K in keyof Row<F>]?: boolean;
|
|
403
|
+
} & {
|
|
404
|
+
[K in RelationKeys<F>]?: boolean | IncludeArgs<M, TargetFields<F[K], M>>;
|
|
405
|
+
};
|
|
406
|
+
type Included<M extends Models, S, A> = (A extends {
|
|
407
|
+
select: infer Sel;
|
|
408
|
+
} ? Selected<M, TargetFields<S, M>, Sel> : Row<TargetFields<S, M>>) & (A extends {
|
|
409
|
+
include: infer I;
|
|
410
|
+
} ? WithInclude<M, TargetFields<S, M>, I> : unknown);
|
|
411
|
+
type RelationOutput<M extends Models, S, A> = S extends HasMany ? Included<M, S, A>[] : S extends BelongsTo<string, infer O> ? Included<M, S, A> | (O extends true ? null : never) : never;
|
|
412
|
+
/** A row plus whatever `include` asked for, at every level. */
|
|
413
|
+
type WithInclude<M extends Models, F extends Fields, I> = I extends object ? {
|
|
414
|
+
[K in keyof I & RelationKeys<F>]: RelationOutput<M, F[K], I[K]>;
|
|
415
|
+
} : unknown;
|
|
416
|
+
/** Exactly the fields a `select` named, and the relations it included. */
|
|
417
|
+
type Selected<M extends Models, F extends Fields, S> = {
|
|
418
|
+
[K in keyof S & keyof Row<F> as S[K] extends false | undefined ? never : K]: Row<F>[K];
|
|
419
|
+
} & {
|
|
420
|
+
[K in keyof S & RelationKeys<F> as S[K] extends false | undefined ? never : K]: RelationOutput<M, F[K], S[K]>;
|
|
421
|
+
};
|
|
422
|
+
type NumberKeys<F> = {
|
|
423
|
+
[K in ScalarKeys<F>]: F[K] extends Field<"number", boolean, boolean> ? K : never;
|
|
424
|
+
}[ScalarKeys<F>];
|
|
425
|
+
/** Fields a group can be keyed by: anything filterable, and the timestamps. */
|
|
426
|
+
type GroupKeys<F> = FilterKeys<F> | "createdAt" | "updatedAt";
|
|
427
|
+
/** Fields with an order, so a minimum and maximum: not booleans or json. */
|
|
428
|
+
type OrderedKeys<F> = Exclude<GroupKeys<F>, {
|
|
429
|
+
[K in ScalarKeys<F>]: F[K] extends Field<"boolean", boolean, boolean> ? K : never;
|
|
430
|
+
}[ScalarKeys<F>]>;
|
|
431
|
+
type Flags<K extends PropertyKey> = {
|
|
432
|
+
[P in K]?: boolean;
|
|
433
|
+
};
|
|
434
|
+
interface AggregateMeasures<F> {
|
|
435
|
+
/** `true` counts rows; an object counts rows (`_all`) and non-null values per field. */
|
|
436
|
+
_count?: true | ({
|
|
437
|
+
_all?: boolean;
|
|
438
|
+
} & Flags<Exclude<keyof Row<F>, {
|
|
439
|
+
[K in ScalarKeys<F>]: F[K] extends {
|
|
440
|
+
personalSubject: string;
|
|
441
|
+
} ? K : never;
|
|
442
|
+
}[ScalarKeys<F>] | "$erased">>);
|
|
443
|
+
_sum?: Flags<NumberKeys<F>>;
|
|
444
|
+
_avg?: Flags<NumberKeys<F>>;
|
|
445
|
+
_min?: Flags<OrderedKeys<F>>;
|
|
446
|
+
_max?: Flags<OrderedKeys<F>>;
|
|
447
|
+
}
|
|
448
|
+
type On<S> = {
|
|
449
|
+
[K in keyof S]: S[K] extends false | undefined ? never : K;
|
|
450
|
+
}[keyof S];
|
|
451
|
+
/** What a set of measures returns. */
|
|
452
|
+
type AggregateOutput<F, A> = (A extends {
|
|
453
|
+
_count: infer C;
|
|
454
|
+
} ? {
|
|
455
|
+
_count: C extends true ? number : {
|
|
456
|
+
[K in On<C>]: number;
|
|
457
|
+
};
|
|
458
|
+
} : unknown) & (A extends {
|
|
459
|
+
_sum: infer S;
|
|
460
|
+
} ? {
|
|
461
|
+
_sum: {
|
|
462
|
+
[K in On<S>]: number | null;
|
|
463
|
+
};
|
|
464
|
+
} : unknown) & (A extends {
|
|
465
|
+
_avg: infer S;
|
|
466
|
+
} ? {
|
|
467
|
+
_avg: {
|
|
468
|
+
[K in On<S>]: number | null;
|
|
469
|
+
};
|
|
470
|
+
} : unknown) & (A extends {
|
|
471
|
+
_min: infer S;
|
|
472
|
+
} ? {
|
|
473
|
+
_min: {
|
|
474
|
+
[K in On<S> & keyof Row<F>]: Row<F>[K] | null;
|
|
475
|
+
};
|
|
476
|
+
} : unknown) & (A extends {
|
|
477
|
+
_max: infer S;
|
|
478
|
+
} ? {
|
|
479
|
+
_max: {
|
|
480
|
+
[K in On<S> & keyof Row<F>]: Row<F>[K] | null;
|
|
481
|
+
};
|
|
482
|
+
} : unknown);
|
|
483
|
+
/** One `groupBy` row: the key fields, then the measures. */
|
|
484
|
+
type GroupRow<F, A> = (A extends {
|
|
485
|
+
by: ReadonlyArray<infer K>;
|
|
486
|
+
} ? {
|
|
487
|
+
[P in K & keyof Row<F>]: Row<F>[P];
|
|
488
|
+
} : unknown) & AggregateOutput<F, A>;
|
|
489
|
+
interface RelationInfo {
|
|
490
|
+
kind: "belongsTo" | "hasMany";
|
|
491
|
+
target: string;
|
|
492
|
+
/** Engine link api name shared by both sides. */
|
|
493
|
+
link: string;
|
|
494
|
+
/** belongsTo: the foreign-key attribute on this model. */
|
|
495
|
+
foreignKey?: string;
|
|
496
|
+
optional?: boolean;
|
|
497
|
+
}
|
|
498
|
+
interface ModelInfo {
|
|
499
|
+
key: string;
|
|
500
|
+
scalars: Record<string, {
|
|
501
|
+
kind: ScalarKind;
|
|
502
|
+
optional: boolean;
|
|
503
|
+
hasDefault: boolean;
|
|
504
|
+
defaultValue: unknown;
|
|
505
|
+
}>;
|
|
506
|
+
relations: Record<string, RelationInfo>;
|
|
507
|
+
/** Attributes a filter may name: id, non-json scalars, foreign keys. */
|
|
508
|
+
filterable: Set<string>;
|
|
509
|
+
}
|
|
510
|
+
interface Schema<M extends Models = Models> extends TapeDBSchema {
|
|
511
|
+
readonly models: M;
|
|
512
|
+
readonly graph: Record<string, ModelInfo>;
|
|
513
|
+
}
|
|
514
|
+
/**
|
|
515
|
+
* Compiles models into the engine schema plus the client's relation graph.
|
|
516
|
+
* Throws on anything the engine would only discover later: a relation to a
|
|
517
|
+
* model that does not exist, an ambiguous inverse, a reserved name.
|
|
518
|
+
*/
|
|
519
|
+
declare function schema<M extends Models>(models: M, options?: SchemaOptions<Extract<keyof M, string>>): Schema<M>;
|
|
520
|
+
|
|
521
|
+
export { check as d, filter as f, model as m, role as r, schema as s, t, attachedTo as w, fileCheck as x };
|
|
522
|
+
export type { Access as A, CheckContext as C, DeleteBehaviour as D, FieldFilter as F, GroupKeys as G, Include as I, Models as M, OrderBy as O, Row as R, ScalarKind as S, UpdateData as U, Where as W, AccessAuth as a, CreateData as b, Schema as c, Fields as e, Select as g, Selected as h, WithInclude as i, FieldsOf as j, ModelInfo as k, AggregateMeasures as l, AggregateOutput as n, GroupRow as o, FileAccess as p, FileCheckContext as q, FileInfo as u, ModelOptions as v };
|
package/dist/schema.d.ts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { A as Access, a as AccessAuth, C as CheckContext, b as CreateData, D as DeleteBehaviour, F as FieldFilter, p as FileAccess, q as FileCheckContext, u as FileInfo, I as Include, v as ModelOptions, O as OrderBy, R as Row, S as ScalarKind, c as Schema, U as UpdateData, W as Where, w as attachedTo, d as check, x as fileCheck, f as filter, m as model, r as role, s as schema, t } from './schema-entry-CpO_IGFv.js';
|
|
2
|
+
import './schema-CVcGZJfR.js';
|
package/dist/schema.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import {
|
|
2
|
+
attachedTo,
|
|
3
|
+
check,
|
|
4
|
+
fileCheck,
|
|
5
|
+
filter,
|
|
6
|
+
model,
|
|
7
|
+
role,
|
|
8
|
+
schema,
|
|
9
|
+
t
|
|
10
|
+
} from "./chunk-VWW5OS5B.js";
|
|
11
|
+
import "./chunk-3AL32Q4K.js";
|
|
12
|
+
export {
|
|
13
|
+
attachedTo,
|
|
14
|
+
check,
|
|
15
|
+
fileCheck,
|
|
16
|
+
filter,
|
|
17
|
+
model,
|
|
18
|
+
role,
|
|
19
|
+
schema,
|
|
20
|
+
t
|
|
21
|
+
};
|
|
22
|
+
//# sourceMappingURL=schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|