-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathdata_model.ts
More file actions
297 lines (265 loc) · 8 KB
/
data_model.ts
File metadata and controls
297 lines (265 loc) · 8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import { Value } from "../values/index.js";
import type { SystemIndexes } from "./system_fields.js";
// Document Types /////////////////////////////////////////////////////////////
/**
* A document stored in Convex.
* @public
*/
export type GenericDocument = Record<string, Value>;
/**
* A type describing all of the document fields in a table.
*
* These can either be field names (like "name") or references to fields on
* nested objects (like "properties.name").
* @public
*/
export type GenericFieldPaths = string;
// Index Types ///////////////////////////////////////////////////////////////
/**
* A type describing the ordered fields in an index.
*
* These can either be field names (like "name") or references to fields on
* nested objects (like "properties.name").
* @public
*/
export type GenericIndexFields = string[];
/**
* A type describing the indexes in a table.
*
* It's an object mapping each index name to the fields in the index.
* @public
*/
export type GenericTableIndexes = Record<string, GenericIndexFields>;
/**
* A type describing the configuration of a search index.
* @public
*/
export type GenericSearchIndexConfig = {
searchField: string;
filterFields: string;
};
/**
* A type describing all of the search indexes in a table.
*
* This is an object mapping each index name to the config for the index.
* @public
*/
export type GenericTableSearchIndexes = Record<
string,
GenericSearchIndexConfig
>;
/**
* A type describing the configuration of a vector index.
* @public
*/
export type GenericVectorIndexConfig = {
vectorField: string;
dimensions: number;
filterFields: string;
};
/**
* A type describing all of the vector indexes in a table.
*
* This is an object mapping each index name to the config for the index.
* @public
*/
export type GenericTableVectorIndexes = Record<
string,
GenericVectorIndexConfig
>;
/**
* If we have A | B | C, this finds A[Key] | B[Key] | C[Key], where we default to
* `Default` if the Key isn't found.
*
* Conditional types apparently loop over the variants in a union, so the `T extends T`
* is enough to force this behavior.
* https://stackoverflow.com/questions/49401866/all-possible-keys-of-an-union-type
*/
type ValueFromUnion<T, Key, Default> = T extends T
? Key extends keyof T
? T[Key]
: Default
: never;
/**
* The type of a field in a document.
*
* Note that this supports both simple fields like "name" and nested fields like
* "properties.name".
*
* If the field is not present in the document it is considered to be `undefined`.
*
* @public
*/
export type FieldTypeFromFieldPath<
Document extends GenericDocument,
FieldPath extends string,
> =
FieldTypeFromFieldPathInner<Document, FieldPath> extends Value | undefined
? FieldTypeFromFieldPathInner<Document, FieldPath>
: Value | undefined;
/**
* The inner type of {@link FieldTypeFromFieldPath}.
*
* It's wrapped in a helper to coerce the type to `Value | undefined` since some
* versions of TypeScript fail to infer this type correctly.
*
* @public
*/
export type FieldTypeFromFieldPathInner<
Document extends GenericDocument,
FieldPath extends string,
> = FieldPath extends `${infer First}.${infer Second}`
? ValueFromUnion<
Document,
First,
Record<never, never>
> extends infer FieldValue
? // The fact that `extends infer` extracts the generic document out of a union of a
// Value and record of Values (GenericDocument) is due to the feature
// "Distributive Conditional Types" in the TypeScript Handbook:
// https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types
FieldValue extends GenericDocument
? FieldTypeFromFieldPath<FieldValue, Second>
: undefined
: undefined
: ValueFromUnion<Document, FieldPath, undefined>;
// Table Types /////////////////////////////////////////////////////////////////
/**
* A type describing the document type and indexes in a table.
* @public
*/
export type GenericTableInfo = {
document: GenericDocument;
fieldPaths: GenericFieldPaths;
indexes: GenericTableIndexes;
searchIndexes: GenericTableSearchIndexes;
vectorIndexes: GenericTableVectorIndexes;
};
/**
* The type of a document in a table for a given {@link GenericTableInfo}.
* @public
*/
export type DocumentByInfo<TableInfo extends GenericTableInfo> =
TableInfo["document"];
/**
* The field paths in a table for a given {@link GenericTableInfo}.
*
* These can either be field names (like "name") or references to fields on
* nested objects (like "properties.name").
* @public
*/
export type FieldPaths<TableInfo extends GenericTableInfo> =
TableInfo["fieldPaths"];
/**
* The database indexes in a table for a given {@link GenericTableInfo}.
*
* This will be an object mapping index names to the fields in the index.
* @public
*/
export type Indexes<TableInfo extends GenericTableInfo> = TableInfo["indexes"];
/**
* The names of indexes in a table for a given {@link GenericTableInfo}.
* @public
*/
export type IndexNames<TableInfo extends GenericTableInfo> =
keyof Indexes<TableInfo>;
/**
* Extract the fields of an index from a {@link GenericTableInfo} by name.
* @public
*/
export type NamedIndex<
TableInfo extends GenericTableInfo,
IndexName extends IndexNames<TableInfo>,
> = Indexes<TableInfo>[IndexName];
/**
* The search indexes in a table for a given {@link GenericTableInfo}.
*
* This will be an object mapping index names to the search index config.
* @public
*/
export type SearchIndexes<TableInfo extends GenericTableInfo> =
TableInfo["searchIndexes"];
/**
* The names of search indexes in a table for a given {@link GenericTableInfo}.
* @public
*/
export type SearchIndexNames<TableInfo extends GenericTableInfo> =
keyof SearchIndexes<TableInfo>;
/**
* Extract the config of a search index from a {@link GenericTableInfo} by name.
* @public
*/
export type NamedSearchIndex<
TableInfo extends GenericTableInfo,
IndexName extends SearchIndexNames<TableInfo>,
> = SearchIndexes<TableInfo>[IndexName];
/**
* The vector indexes in a table for a given {@link GenericTableInfo}.
*
* This will be an object mapping index names to the vector index config.
* @public
*/
export type VectorIndexes<TableInfo extends GenericTableInfo> =
TableInfo["vectorIndexes"];
/**
* The names of vector indexes in a table for a given {@link GenericTableInfo}.
* @public
*/
export type VectorIndexNames<TableInfo extends GenericTableInfo> =
keyof VectorIndexes<TableInfo>;
/**
* Extract the config of a vector index from a {@link GenericTableInfo} by name.
* @public
*/
export type NamedVectorIndex<
TableInfo extends GenericTableInfo,
IndexName extends VectorIndexNames<TableInfo>,
> = VectorIndexes<TableInfo>[IndexName];
// Data Model Types ////////////////////////////////////////////////////////////
/**
* A type describing the tables in a Convex project.
*
* This is designed to be code generated with `npx convex dev`.
* @public
*/
export type GenericDataModel = Record<string, GenericTableInfo>;
/**
* A {@link GenericDataModel} that considers documents to be `any` and does not
* support indexes.
*
* This is the default before a schema is defined.
* @public
*/
export type AnyDataModel = {
[tableName: string]: {
document: any;
fieldPaths: GenericFieldPaths;
indexes: SystemIndexes;
searchIndexes: {};
vectorIndexes: {};
};
};
/**
* A type of all of the table names defined in a {@link GenericDataModel}.
* @public
*/
export type TableNamesInDataModel<DataModel extends GenericDataModel> =
keyof DataModel & string;
/**
* Extract the `TableInfo` for a table in a {@link GenericDataModel} by table
* name.
*
* @public
*/
export type NamedTableInfo<
DataModel extends GenericDataModel,
TableName extends keyof DataModel,
> = DataModel[TableName];
/**
* The type of a document in a {@link GenericDataModel} by table name.
* @public
*/
export type DocumentByName<
DataModel extends GenericDataModel,
TableName extends TableNamesInDataModel<DataModel>,
> = DataModel[TableName]["document"];