This repository has been archived by the owner on Feb 3, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.ts
259 lines (175 loc) · 6.9 KB
/
types.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
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
export type DeepPartial<T> =
| T
| {
[P in keyof T]?: T[P] extends Array<infer U1>
? Array<DeepPartial<U1>>
: T[P] extends readonly (infer U2)[]
? readonly DeepPartial<U2>[]
: DeepPartial<T[P]>
};
export interface ValueChange<T = any> {
old_val?: T;
new_val?: T;
}
export interface IndexChangeResult {
created?: number;
renamed?: number;
dropped?: number;
}
export interface TableChangeResult {
tables_created?: number;
tables_dropped?: number;
}
export interface WriteResult<T = any> {
deleted: number;
skipped: number;
errors: number;
first_error?: string;
inserted: number;
replaced: number;
unchanged: number;
generated_keys?: string[];
warnings?: string[];
changes?: Array<ValueChange<T>>;
}
export type Value<T = any> = Datum<T> | Query<T> | T;
export interface Query<T = any> {
// FORK/DUPLICATION
fork(): Query<T>;
// EXECUTION
run(): Promise<T>;
}
export interface DatumPartial<T = any> extends Query<T> {
// QUERY
fork(): Datum<T>;
// ANY
eq(...value: Value<T>[]): Datum<boolean>;
ne(...value: Value<T>[]): Datum<boolean>;
// BOOLEAN
or(...bool: Value<boolean>[]): T extends boolean ? Datum<boolean> : never;
and(...bool: Value<boolean>[]): T extends boolean ? Datum<boolean> : never;
not(): T extends boolean ? Datum<boolean> : never;
do<U = any>(func: (value: Datum<T>) => Value<U>): Datum<U>;
branch<U = any, V = any>(trueAction: Value<U> | (() => Value<U>), falseAction: Value<V> | (() => Value<V>)): Datum<U | V>;
branch<U = any, V = any, W = any>(trueAction: Value<U> | (() => Value<U>),
test2: Value<any>, test2Action: Value<V> | (() => Value<V>),
falseAction: Value<W> | (() => Value<W>)): Datum<U | V | W>;
branch<U = any>(trueAction: Value<any> | (() => Value<any>),
...testsActionsAndFalseAction: (Value<any> | (() => Value<any>))[]): Datum<U>;
// STRING
startsWith(str: Value<string>): T extends string ? Datum<boolean> : never;
endsWith(str: Value<string>): T extends string ? Datum<boolean> : never;
substr(str: Value<string>): T extends string ? Datum<boolean> : never;
len(): T extends string ? Datum<number> : never;
// NUMBER
add(...values: Value<number>[]): T extends number ? Datum<number> : never;
sub(...values: Value<number>[]): T extends number ? Datum<number> : never;
mul(...values: Value<number>[]): T extends number ? Datum<number> : never;
div(...values: Value<number>[]): T extends number ? Datum<number> : never;
mod(...values: Value<number>[]): T extends number ? Datum<number> : never;
// NUMBER LOGIC
gt(...values: Value<number>[]): T extends number ? Datum<boolean> : never;
lt(...values: Value<number>[]): T extends number ? Datum<boolean> : never;
ge(...values: Value<number>[]): T extends number ? Datum<boolean> : never;
le(...values: Value<number>[]): T extends number ? Datum<boolean> : never;
// ARRAY
count(): T extends any[] ? Datum<number> : never;
limit(n: Value<number>): T extends any[] ? Datum<T> : never;
difference(value: Value<T>): T extends any[] ? Datum<T> : never;
contains<U>(value: Value<U>): T extends U[] ? Datum<boolean> : never;
filter(predicate: DeepPartial<T> | ((doc: Datum<T>) => Value<boolean>)): T extends any[] ? Datum<T> : never;
pluck<U extends object>(...fields: string[]): T extends U[] ? Datum<Partial<U>[]> : never;
map<U, V = any>(predicate: (doc: Datum<U>) => Datum<V>): T extends U[] ? Datum<V[]> : never;
}
export interface Datum<T = any> extends DatumPartial<T> {
// SELECTION
<U extends string | number>(attribute: Value<U>): U extends keyof T
? Datum<T[U]>
: Datum<any>;
}
export interface SingleSelectionPartial<T = any> extends DatumPartial<T> {
// QUERY
fork(): SingleSelection<T>;
// OPERATIONS
update(obj: Value<DeepPartial<T>>): Datum<WriteResult<T>>;
replace(obj: Value<T>): Datum<WriteResult<T>>;
delete(): Datum<WriteResult<T>>;
}
export interface SingleSelection<T = any> extends SingleSelectionPartial<T>, Datum<T> {
// QUERY (override)
fork(): SingleSelection<T>;
}
export interface StreamPartial<T = any> extends Query<T[]> {
// QUERY
fork(): Stream<T>;
// AGGREGATION
distinct(): Stream<T>;
filter(predicate: DeepPartial<T> | ((doc: Datum<T>) => Value<boolean>)): Stream<T>;
limit(n: Value<number>): Stream<T>;
// TRANSFORMS
count(): Datum<number>;
map<U = any>(predicate: (doc: Datum<T>) => Datum<U>): Stream<U>;
pluck(...fields: (string | number)[]): T extends object ? Stream<Partial<T>> : never;
}
export interface Stream<T> extends StreamPartial<T> {
// QUERY (override)
fork(): Stream<T>;
// SELECTION
<U extends string | number>(attribute: Value<U>): U extends keyof T
? Stream<T[U]>
: Stream<any>;
}
export interface SelectionPartial<T = any> extends StreamPartial<T> {
// QUERY
fork(): Selection<T>;
// OPERATIONS
delete(): Datum<WriteResult<T>>;
filter(predicate: DeepPartial<T> | ((doc: Datum<T>) => Value<boolean>)): Selection<T>;
}
export interface Selection<T = any> extends SelectionPartial<T>, Stream<T> {
// QUERY (override)
fork(): Selection<T>;
// SELECTION
<U extends string | number>(attribute: Value<U>): U extends keyof T
? Selection<T[U]>
: Selection<any>;
// OPERATIONS (overrides)
filter(predicate: DeepPartial<T> | ((doc: Datum<T>) => Value<boolean>)): Selection<T>;
}
export interface TablePartial<T = any> extends SelectionPartial<T> {
// QUERY
fork(): never;
// SELECTION
get(key: any): SingleSelection<T>; // editable
getAll(key: any, options?: { index: string }): Selection<T>; // not editable
getAll(key: any, key2: any, options?: { index: string }): Selection<T>;
getAll(...key: (number | string | { index: string })[]): Selection<T>;
// OPERATIONS
insert(obj: T, options?: { conflict: 'error' | 'replace' | 'update' }): Datum<WriteResult<T>>;
indexCreate<U extends keyof T>(key: U): Datum<IndexChangeResult>;
indexCreate(key: any): Datum<IndexChangeResult>;
// indexCreate(name: Value<String>, indexFunction: (doc: Datum<T>) => Value<boolean>): Datum<IndexChangeResult>;
indexDrop<U extends keyof T>(key: U): Datum<IndexChangeResult>;
// indexDrop(name: Value<string>): Datum<IndexChangeResult>;
indexDrop(key: any): Datum<IndexChangeResult>;
indexList(): Datum<string[]>;
// indexRename(name: Value<string>, newName: Value<string>): Datum<IndexChangeResult>;
}
export interface Table<T = any> extends TablePartial<T>, Selection<T> {
// QUERY (override)
fork(): never; // unecessary
}
export interface SchemaEntry {
name: string;
type: 'string' | 'bool' | 'number' | 'object' | 'any';
index?: boolean;
}
export interface Database {
// TABLE
tableCreate(tableName: Value<string>, schema: readonly SchemaEntry[]): Datum<TableChangeResult>;
tableDrop(tableName: Value<string>): Datum<TableChangeResult>;
tableList(): Datum<string[]>;
table<T = any>(tableName: Value<string>): Table<T>;
// ADMINISTRATION
close(): Promise<void>;
}