-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
glossary.ts
233 lines (215 loc) · 7.55 KB
/
glossary.ts
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
import { GraphQLSchema } from 'graphql'
import { GraphQLHandler, RestHandler } from 'msw'
import { Database } from './db/Database'
import { NullableProperty } from './nullable'
import { PrimaryKey } from './primaryKey'
import {
BulkQueryOptions,
QuerySelector,
WeakQuerySelector,
} from './query/queryTypes'
import { OneOf, ManyOf } from './relations/Relation'
export const PRIMARY_KEY = Symbol('primaryKey')
export const ENTITY_TYPE = Symbol('type')
export const DATABASE_INSTANCE = Symbol('databaseInstance')
export type KeyType = string | number | symbol
export type AnyObject = Record<KeyType, any>
export type PrimaryKeyType = string | number
export type PrimitiveValueType = string | number | boolean | Date
export type ModelValueType = PrimitiveValueType | PrimitiveValueType[]
export type ModelValueTypeGetter = () => ModelValueType
export type ModelDefinition = Record<string, ModelDefinitionValue>
export type ModelDefinitionValue =
| PrimaryKey<any>
| ModelValueTypeGetter
| NullableProperty<any>
| OneOf<any, boolean>
| ManyOf<any, boolean>
| NestedModelDefinition
export type NestedModelDefinition = {
[propertyName: string]:
| ModelValueTypeGetter
| NullableProperty<any>
| OneOf<any, boolean>
| ManyOf<any, boolean>
| NestedModelDefinition
}
export type FactoryAPI<Dictionary extends Record<string, any>> = {
[ModelName in keyof Dictionary]: ModelAPI<Dictionary, ModelName>
} & {
[DATABASE_INSTANCE]: Database<Dictionary>
}
export type ModelDictionary = Record<KeyType, Limit<ModelDefinition>>
export type Limit<Definition extends ModelDefinition> = {
[Key in keyof Definition]: Definition[Key] extends ModelDefinitionValue
? Definition[Key]
: {
error: 'expected primary key, initial value, or relation'
}
}
export interface InternalEntityProperties<ModelName extends KeyType> {
readonly [ENTITY_TYPE]: ModelName
readonly [PRIMARY_KEY]: PrimaryKeyType
}
export type Entity<
Dictionary extends ModelDictionary,
ModelName extends keyof Dictionary,
> = PublicEntity<Dictionary, ModelName> & InternalEntityProperties<ModelName>
export type PublicEntity<
Dictionary extends ModelDictionary,
ModelName extends keyof Dictionary,
> = Value<Dictionary[ModelName], Dictionary>
export type RequiredExactlyOne<
ObjectType,
KeysType extends keyof ObjectType = keyof ObjectType,
> = {
[Key in KeysType]: Required<Pick<ObjectType, Key>> &
Partial<Record<Exclude<KeysType, Key>, never>>
}[KeysType] &
Pick<ObjectType, Exclude<keyof ObjectType, KeysType>>
export type DeepRequiredExactlyOne<Target extends AnyObject> =
RequiredExactlyOne<{
[Key in keyof Target]: Target[Key] extends AnyObject
? DeepRequiredExactlyOne<Target[Key]>
: Target[Key]
}>
export type InitialValues<
Dictionary extends ModelDictionary,
ModelName extends keyof Dictionary,
> = Partial<Value<Dictionary[ModelName], Dictionary>>
export interface ModelAPI<
Dictionary extends ModelDictionary,
ModelName extends keyof Dictionary,
> {
/**
* Create a single entity for the model.
*/
create(
initialValues?: InitialValues<Dictionary, ModelName>,
): Entity<Dictionary, ModelName>
/**
* Return the total number of entities.
*/
count(query?: QuerySelector<InitialValues<Dictionary, ModelName>>): number
/**
* Find a first entity matching the query.
*/
findFirst(
query: QuerySelector<InitialValues<Dictionary, ModelName>>,
): Entity<Dictionary, ModelName> | null
/**
* Find multiple entities.
*/
findMany(
query: WeakQuerySelector<InitialValues<Dictionary, ModelName>> &
BulkQueryOptions<InitialValues<Dictionary, ModelName>>,
): Entity<Dictionary, ModelName>[]
/**
* Return all entities of the current model.
*/
getAll(): Entity<Dictionary, ModelName>[]
/**
* Update a single entity with the next data.
*/
update(
query: QuerySelector<InitialValues<Dictionary, ModelName>> & {
data: Partial<UpdateManyValue<Dictionary[ModelName], Dictionary>>
},
): Entity<Dictionary, ModelName> | null
/**
* Update many entities with the next data.
*/
updateMany(
query: QuerySelector<InitialValues<Dictionary, ModelName>> & {
data: Partial<UpdateManyValue<Dictionary[ModelName], Dictionary>>
},
): Entity<Dictionary, ModelName>[] | null
/**
* Delete a single entity.
*/
delete(
query: QuerySelector<InitialValues<Dictionary, ModelName>>,
): Entity<Dictionary, ModelName> | null
/**
* Delete multiple entities.
*/
deleteMany(
query: QuerySelector<InitialValues<Dictionary, ModelName>>,
): Entity<Dictionary, ModelName>[] | null
/**
* Generate request handlers of the given type based on the model definition.
*/
toHandlers(type: 'rest', baseUrl?: string): RestHandler[]
/**
* Generate request handlers of the given type based on the model definition.
*/
toHandlers(type: 'graphql', baseUrl?: string): GraphQLHandler[]
/**
* Generate a graphql schema based on the model definition.
*/
toGraphQLSchema(): GraphQLSchema
}
export type UpdateManyValue<
Target extends AnyObject,
Dictionary extends ModelDictionary,
ModelRoot extends AnyObject = Target,
> =
| Value<Target, Dictionary>
| {
[Key in keyof Target]?: Target[Key] extends PrimaryKey
? (
prevValue: ReturnType<Target[Key]['getPrimaryKeyValue']>,
entity: Value<Target, Dictionary>,
) => ReturnType<Target[Key]['getPrimaryKeyValue']>
: Target[Key] extends ModelValueTypeGetter
? (
prevValue: ReturnType<Target[Key]>,
entity: Value<ModelRoot, Dictionary>,
) => ReturnType<Target[Key]>
: Target[Key] extends OneOf<infer ModelName>
? (
prevValue: PublicEntity<Dictionary, ModelName>,
entity: Value<Target, Dictionary>,
) => PublicEntity<Dictionary, ModelName>
: Target[Key] extends ManyOf<infer ModelName>
? (
prevValue: PublicEntity<Dictionary, ModelName>[],
entity: Value<Target, Dictionary>,
) => PublicEntity<Dictionary, ModelName>[]
: Target[Key] extends AnyObject
? Partial<UpdateManyValue<Target[Key], Target, ModelRoot>>
: (
prevValue: ReturnType<Target[Key]>,
entity: Value<Target, Dictionary>,
) => ReturnType<Target[Key]>
}
export type Value<
Target extends AnyObject,
Dictionary extends ModelDictionary,
> = {
[Key in keyof Target]: Target[Key] extends PrimaryKey<any>
? ReturnType<Target[Key]['getPrimaryKeyValue']>
: // Extract underlying value type of nullable properties
Target[Key] extends NullableProperty<any>
? ReturnType<Target[Key]['getValue']>
: // Extract value type from OneOf relations.
Target[Key] extends OneOf<infer ModelName, infer Nullable>
? Nullable extends true
? PublicEntity<Dictionary, ModelName> | null
: PublicEntity<Dictionary, ModelName> | undefined
: // Extract value type from ManyOf relations.
Target[Key] extends ManyOf<infer ModelName, infer Nullable>
? Nullable extends true
? PublicEntity<Dictionary, ModelName>[] | null
: PublicEntity<Dictionary, ModelName>[] | undefined
: // Account for primitive value getters because
// native constructors (i.e. StringConstructor) satisfy
// the "AnyObject" predicate below.
Target[Key] extends ModelValueTypeGetter
? ReturnType<Target[Key]>
: // Handle nested objects.
Target[Key] extends AnyObject
? Partial<Value<Target[Key], Dictionary>>
: // Otherwise, return the return type of primitive value getters.
ReturnType<Target[Key]>
}