evikit 2.11.1 → 2.13.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/db.d.ts CHANGED
@@ -1,265 +1,272 @@
1
- import type {
2
- InferInput,
3
- InferMetadata,
4
- InferOutput,
5
- MetadataAction,
6
- ObjectSchema,
7
- } from "valibot";
8
-
9
- export type Db<
10
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
11
- Schema extends Record<string, ObjectSchema<any, any>>,
12
- R extends Relations<Schema> = Relations<Schema>,
13
- F extends Flatten<Schema> = Flatten<Schema>,
14
- > = {
15
- [Table in keyof R]: Table extends keyof F & keyof Schema & string
16
- ? {
17
- delete(where: Where<Schema, Table>): void;
18
-
19
- select<
20
- W extends Partial<Join<R, Table>> & {
21
- limit?: number;
22
- offset?: number;
23
- order?: Order<Schema, Table>;
24
- where?: Where<Schema, Table>;
25
- },
26
- >(
27
- props?: W,
28
- ): Select<Schema, R, F, Table, W>[];
29
-
30
- update(query: {
31
- set: Partial<InferOutput<Schema[Table]>>;
32
- where: Where<Schema, Table>;
33
- }): void;
34
-
35
- upsert(
36
- ...values: InferInput<Schema[Table]>[]
37
- ): InferOutput<Schema[Table]>[];
38
- }
39
- : never;
40
- } & {
41
- schema: Schema;
42
- subscribe(onMessage: ({ table }: { table: string }) => void): () => void;
43
- };
44
-
45
- export type Flatten<
46
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
47
- Schema extends Record<string, ObjectSchema<any, any>>,
48
- R extends Relations<Schema> = Relations<Schema>,
49
- > = {
50
- [Self in keyof R]: Self extends string
51
- ? UnionToIntersection<
52
- {
53
- [Own in keyof R[Self]]: Own extends string
54
- ? {
55
- [Other in keyof R[Self][Own]]: Other extends string
56
- ? R[Self][Own][Other] extends Record<string, { name: string }>
57
- ? {
58
- [
59
- Its in keyof R[Self][Own][Other] as R[Self][Own][Other][Its]["name"]
60
- ]: Its extends string
61
- ? R[Self][Own][Other][Its]
62
- : never;
63
- }
64
- : never
65
- : never;
66
- }[keyof R[Self][Own]]
67
- : never;
68
- }[keyof R[Self]]
69
- >
70
- : never;
71
- };
72
-
73
- export type Join<Relations, Self extends keyof Relations> = UnionToIntersection<
74
- {
75
- [Own in keyof Relations[Self]]: Own extends string
76
- ? {
77
- [Other in keyof Relations[Self][Own]]: Other extends keyof Relations
78
- ? {
79
- [Its in keyof Relations[Self][Own][Other]]: Its extends string
80
- ? Relations[Self][Own][Other] extends Record<
81
- string,
82
- { name: string }
83
- >
84
- ? {
85
- // Turning this into partial record would cause infinite depth.
86
- // eslint-disable-next-line @typescript-eslint/consistent-indexed-object-style
87
- with: {
88
- [K in Relations[Self][Own][Other][Its]["name"]]?:
89
- Join<Relations, Other> | true;
90
- };
91
- }
92
- : never
93
- : never;
94
- }[keyof Relations[Self][Own][Other]]
95
- : never;
96
- }[keyof Relations[Self][Own]]
97
- : never;
98
- }[keyof Relations[Self]]
99
- >;
100
-
101
- export type Order<
102
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
103
- Schema extends Record<string, ObjectSchema<any, any>>,
104
- Table extends keyof Schema,
105
- > =
106
- | { [K in keyof Schema[Table]["entries"]]?: null | { desc: true } }
107
- | { [K in keyof Schema[Table]["entries"]]?: null | { desc: true } }[];
108
-
109
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
110
- export type Relations<Schema extends Record<string, ObjectSchema<any, any>>> = {
111
- [Self in keyof Schema]: Self extends string
112
- ? OmitExtendsRecordUndefined<{
113
- [Own in keyof Schema[Self]["entries"]]: Own extends string
114
- ? OmitExtendsRecordUndefined<{
115
- [Other in keyof Schema]: Other extends string
116
- ? OmitExtendsUndefined<{
117
- [Its in keyof Schema[Other]["entries"]]: Its extends string
118
- ? InferMetadata<Schema[Self]["entries"][Own]> extends {
119
- key: Schema[Other]["entries"][Its];
120
- references: Schema[Other];
121
- }
122
- ? Schema[Other] extends InferMetadata<
123
- Schema[Self]["entries"][Own]
124
- >["references"]
125
- ? Schema[Other]["entries"][Its] extends InferMetadata<
126
- Schema[Self]["entries"][Own]
127
- >["key"]
128
- ? {
129
- Its: Its;
130
- name: Rewrite<Self, Own, Other, Its>;
131
- Other: Other;
132
- Own: Own;
133
- type: "references";
134
- }
135
- : undefined
136
- : undefined
137
- : InferMetadata<Schema[Other]["entries"][Its]> extends {
138
- key: Schema[Self]["entries"][Own];
139
- references: Schema[Self];
140
- }
141
- ? Schema[Self] extends InferMetadata<
142
- Schema[Other]["entries"][Its]
143
- >["references"]
144
- ? Schema[Self]["entries"][Own] extends InferMetadata<
145
- Schema[Other]["entries"][Its]
146
- >["key"]
147
- ? {
148
- Its: Its;
149
- name: Rewrite<Self, Own, Other, Its>;
150
- Other: Other;
151
- Own: Own;
152
- type: "referencedBy";
153
- }
154
- : undefined
155
- : undefined
156
- : undefined
157
- : undefined;
158
- }>
159
- : undefined;
160
- }>
161
- : undefined;
162
- }>
163
- : undefined;
164
- };
165
-
166
- export type Where<
167
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
168
- Schema extends Record<string, ObjectSchema<any, any>>,
169
- Table extends keyof Schema,
170
- > = {
171
- [K in keyof Schema[Table]["entries"]]?:
172
- | InferOutput<Schema[Table]["entries"][K]>
173
- | InferOutput<Schema[Table]["entries"][K]>[]
174
- | Partial<
175
- Record<
176
- "gt" | "gte" | "lt" | "lte" | "not",
177
- InferOutput<Schema[Table]["entries"][K]>
178
- >
179
- >;
180
- };
181
-
182
- type OmitExtends<T, U> = {
183
- [K in keyof T as T[K] extends U ? never : K]: T[K];
184
- };
185
-
186
- type OmitExtendsRecordUndefined<T> = OmitExtends<T, Record<string, undefined>>;
187
-
188
- type OmitExtendsUndefined<T> = OmitExtends<T, undefined>;
189
-
190
- type Rewrite<
191
- Self extends string,
192
- Own extends string,
193
- Other extends string,
194
- Its extends string,
195
- > =
196
- // Image.id, Question.imageId -> Image.Question.
197
- Its extends `${Uncapitalize<Self>}${Capitalize<Own>}`
198
- ? Other
199
- : // Quiz.coverId, Image.id -> Quiz.cover.
200
- Own extends `${infer R}${Capitalize<Its>}`
201
- ? R
202
- : // Image.id, Quiz.coverId -> Image.CoverQuiz.
203
- Its extends `${infer R}${Capitalize<Own>}`
204
- ? `${Capitalize<R>}${Other}`
205
- : never;
206
-
207
- type Select<
208
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
209
- Schema extends Record<string, ObjectSchema<any, any>>,
210
- R extends Relations<Schema> = Relations<Schema>,
211
- F extends Flatten<Schema> = Flatten<Schema>,
212
- Table extends keyof F & keyof Schema = keyof F & keyof Schema,
213
- W extends Join<R, Table> = Join<R, Table>,
214
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
215
- > = W extends { with: any }
216
- ? InferOutput<Schema[Table]> & {
217
- [K in keyof W["with"]]: K extends keyof F[Table]
218
- ? F[Table][K] extends {
219
- Other: keyof F & keyof Schema;
220
- type: "referencedBy";
221
- }
222
- ? // eslint-disable-next-line @typescript-eslint/no-explicit-any
223
- (W["with"][K] extends { with: any }
224
- ? Select<Schema, R, F, F[Table][K]["Other"], W["with"][K]>
225
- : InferOutput<Schema[F[Table][K]["Other"]]>)[]
226
- : F[Table][K] extends {
227
- Other: keyof F & keyof Schema;
228
- type: "references";
229
- }
230
- ? | undefined
231
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
232
- | (W["with"][K] extends { with: any }
233
- ? Select<Schema, R, F, F[Table][K]["Other"], W["with"][K]>
234
- : InferOutput<Schema[F[Table][K]["Other"]]>)
235
- : never
236
- : never;
237
- }
238
- : InferOutput<Schema[Table]>;
239
-
240
- // https://www.geeksforgeeks.org/typescript/union-type-to-intersection-type-in-typescript/
241
- type UnionToIntersection<U> = (
242
- U extends object ? (k: U) => void : never
243
- ) extends (k: infer I) => void
244
- ? I
245
- : never;
246
-
247
- export function flatten<
248
- Schema extends Record<string, ObjectSchema<any, any>>, // eslint-disable-line @typescript-eslint/no-explicit-any
249
- >(relations: Relations<Schema>): Flatten<Schema>;
250
-
251
- export function primary<T>(): MetadataAction<T, { primary: true }>;
252
-
253
- export function references<
254
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
255
- R extends { entries: any },
256
- K extends keyof R["entries"],
257
- T,
258
- >(
259
- references: R,
260
- key: K,
261
- ): MetadataAction<T, { key: R["entries"][K]; references: R }>;
262
-
263
- export function relations<
264
- Schema extends Record<string, ObjectSchema<any, any>>, // eslint-disable-line @typescript-eslint/no-explicit-any
265
- >(schema: Schema): Relations<Schema>;
1
+ import type {
2
+ InferInput,
3
+ InferMetadata,
4
+ InferOutput,
5
+ MetadataAction,
6
+ ObjectSchema,
7
+ } from "valibot";
8
+
9
+ export type Db<
10
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
11
+ Schema extends Record<string, ObjectSchema<any, any>>,
12
+ R extends Relations<Schema> = Relations<Schema>,
13
+ F extends Flatten<Schema> = Flatten<Schema>,
14
+ > = {
15
+ [Table in keyof R]: Table extends keyof F & keyof Schema & string
16
+ ? {
17
+ delete(where: Where<Schema, Table>): void;
18
+
19
+ select<
20
+ W extends Partial<Join<R, Table>> &
21
+ Where<Schema, Table> & {
22
+ limit?: number;
23
+ offset?: number;
24
+ order?: Order<Schema, Table>;
25
+ /** @deprecated */ where?: Where<Schema, Table>;
26
+ },
27
+ >(
28
+ props?: W,
29
+ ): Select<Schema, R, F, Table, W>[];
30
+
31
+ update(
32
+ query: Where<Schema, Table> & {
33
+ set: Partial<InferOutput<Schema[Table]>>;
34
+ /** @deprecated */ where?: Where<Schema, Table>;
35
+ },
36
+ ): void;
37
+
38
+ upsert(
39
+ ...values: InferInput<Schema[Table]>[]
40
+ ): InferOutput<Schema[Table]>[];
41
+ }
42
+ : never;
43
+ } & {
44
+ schema: Schema;
45
+ subscribe(onMessage: ({ table }: { table: string }) => void): () => void;
46
+ };
47
+
48
+ export type Flatten<
49
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
50
+ Schema extends Record<string, ObjectSchema<any, any>>,
51
+ R extends Relations<Schema> = Relations<Schema>,
52
+ > = {
53
+ [Self in keyof R]: Self extends string
54
+ ? UnionToIntersection<
55
+ {
56
+ [Own in keyof R[Self]]: Own extends string
57
+ ? {
58
+ [Other in keyof R[Self][Own]]: Other extends string
59
+ ? R[Self][Own][Other] extends Record<string, { name: string }>
60
+ ? {
61
+ [
62
+ Its in keyof R[Self][Own][Other] as R[Self][Own][Other][Its]["name"]
63
+ ]: Its extends string
64
+ ? R[Self][Own][Other][Its]
65
+ : never;
66
+ }
67
+ : never
68
+ : never;
69
+ }[keyof R[Self][Own]]
70
+ : never;
71
+ }[keyof R[Self]]
72
+ >
73
+ : never;
74
+ };
75
+
76
+ export type Join<Relations, Self extends keyof Relations> = UnionToIntersection<
77
+ {
78
+ [Own in keyof Relations[Self]]: Own extends string
79
+ ? {
80
+ [Other in keyof Relations[Self][Own]]: Other extends keyof Relations
81
+ ? {
82
+ [Its in keyof Relations[Self][Own][Other]]: Its extends string
83
+ ? Relations[Self][Own][Other] extends Record<
84
+ string,
85
+ { name: string }
86
+ >
87
+ ? {
88
+ // Turning this into partial record would cause infinite depth.
89
+ // eslint-disable-next-line @typescript-eslint/consistent-indexed-object-style
90
+ with: {
91
+ [K in Relations[Self][Own][Other][Its]["name"]]?:
92
+ Join<Relations, Other> | true;
93
+ };
94
+ }
95
+ : never
96
+ : never;
97
+ }[keyof Relations[Self][Own][Other]]
98
+ : never;
99
+ }[keyof Relations[Self][Own]]
100
+ : never;
101
+ }[keyof Relations[Self]]
102
+ >;
103
+
104
+ export type Order<
105
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
106
+ Schema extends Record<string, ObjectSchema<any, any>>,
107
+ Table extends keyof Schema,
108
+ > =
109
+ | {
110
+ [K in keyof Schema[Table]["entries"]]?: null | true | { desc: true };
111
+ }
112
+ | {
113
+ [K in keyof Schema[Table]["entries"]]?: null | true | { desc: true };
114
+ }[];
115
+
116
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
117
+ export type Relations<Schema extends Record<string, ObjectSchema<any, any>>> = {
118
+ [Self in keyof Schema]: Self extends string
119
+ ? OmitExtendsRecordUndefined<{
120
+ [Own in keyof Schema[Self]["entries"]]: Own extends string
121
+ ? OmitExtendsRecordUndefined<{
122
+ [Other in keyof Schema]: Other extends string
123
+ ? OmitExtendsUndefined<{
124
+ [Its in keyof Schema[Other]["entries"]]: Its extends string
125
+ ? InferMetadata<Schema[Self]["entries"][Own]> extends {
126
+ key: Schema[Other]["entries"][Its];
127
+ references: Schema[Other];
128
+ }
129
+ ? Schema[Other] extends InferMetadata<
130
+ Schema[Self]["entries"][Own]
131
+ >["references"]
132
+ ? Schema[Other]["entries"][Its] extends InferMetadata<
133
+ Schema[Self]["entries"][Own]
134
+ >["key"]
135
+ ? {
136
+ Its: Its;
137
+ name: Rewrite<Self, Own, Other, Its>;
138
+ Other: Other;
139
+ Own: Own;
140
+ type: "references";
141
+ }
142
+ : undefined
143
+ : undefined
144
+ : InferMetadata<Schema[Other]["entries"][Its]> extends {
145
+ key: Schema[Self]["entries"][Own];
146
+ references: Schema[Self];
147
+ }
148
+ ? Schema[Self] extends InferMetadata<
149
+ Schema[Other]["entries"][Its]
150
+ >["references"]
151
+ ? Schema[Self]["entries"][Own] extends InferMetadata<
152
+ Schema[Other]["entries"][Its]
153
+ >["key"]
154
+ ? {
155
+ Its: Its;
156
+ name: Rewrite<Self, Own, Other, Its>;
157
+ Other: Other;
158
+ Own: Own;
159
+ type: "referencedBy";
160
+ }
161
+ : undefined
162
+ : undefined
163
+ : undefined
164
+ : undefined;
165
+ }>
166
+ : undefined;
167
+ }>
168
+ : undefined;
169
+ }>
170
+ : undefined;
171
+ };
172
+
173
+ export type Where<
174
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
175
+ Schema extends Record<string, ObjectSchema<any, any>>,
176
+ Table extends keyof Schema,
177
+ > = {
178
+ [K in keyof Schema[Table]["entries"]]?:
179
+ | InferOutput<Schema[Table]["entries"][K]>
180
+ | InferOutput<Schema[Table]["entries"][K]>[]
181
+ | Partial<
182
+ Record<
183
+ "gt" | "gte" | "lt" | "lte" | "not",
184
+ InferOutput<Schema[Table]["entries"][K]>
185
+ >
186
+ >;
187
+ };
188
+
189
+ type OmitExtends<T, U> = {
190
+ [K in keyof T as T[K] extends U ? never : K]: T[K];
191
+ };
192
+
193
+ type OmitExtendsRecordUndefined<T> = OmitExtends<T, Record<string, undefined>>;
194
+
195
+ type OmitExtendsUndefined<T> = OmitExtends<T, undefined>;
196
+
197
+ type Rewrite<
198
+ Self extends string,
199
+ Own extends string,
200
+ Other extends string,
201
+ Its extends string,
202
+ > =
203
+ // Image.id, Question.imageId -> Image.Question.
204
+ Its extends `${Uncapitalize<Self>}${Capitalize<Own>}`
205
+ ? Other
206
+ : // Quiz.coverId, Image.id -> Quiz.cover.
207
+ Own extends `${infer R}${Capitalize<Its>}`
208
+ ? R
209
+ : // Image.id, Quiz.coverId -> Image.CoverQuiz.
210
+ Its extends `${infer R}${Capitalize<Own>}`
211
+ ? `${Capitalize<R>}${Other}`
212
+ : never;
213
+
214
+ type Select<
215
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
216
+ Schema extends Record<string, ObjectSchema<any, any>>,
217
+ R extends Relations<Schema> = Relations<Schema>,
218
+ F extends Flatten<Schema> = Flatten<Schema>,
219
+ Table extends keyof F & keyof Schema = keyof F & keyof Schema,
220
+ W extends Join<R, Table> = Join<R, Table>,
221
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
222
+ > = W extends { with: any }
223
+ ? InferOutput<Schema[Table]> & {
224
+ [K in keyof W["with"]]: K extends keyof F[Table]
225
+ ? F[Table][K] extends {
226
+ Other: keyof F & keyof Schema;
227
+ type: "referencedBy";
228
+ }
229
+ ? // eslint-disable-next-line @typescript-eslint/no-explicit-any
230
+ (W["with"][K] extends { with: any }
231
+ ? Select<Schema, R, F, F[Table][K]["Other"], W["with"][K]>
232
+ : InferOutput<Schema[F[Table][K]["Other"]]>)[]
233
+ : F[Table][K] extends {
234
+ Other: keyof F & keyof Schema;
235
+ type: "references";
236
+ }
237
+ ? | undefined
238
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
239
+ | (W["with"][K] extends { with: any }
240
+ ? Select<Schema, R, F, F[Table][K]["Other"], W["with"][K]>
241
+ : InferOutput<Schema[F[Table][K]["Other"]]>)
242
+ : never
243
+ : never;
244
+ }
245
+ : InferOutput<Schema[Table]>;
246
+
247
+ // https://www.geeksforgeeks.org/typescript/union-type-to-intersection-type-in-typescript/
248
+ type UnionToIntersection<U> = (
249
+ U extends object ? (k: U) => void : never
250
+ ) extends (k: infer I) => void
251
+ ? I
252
+ : never;
253
+
254
+ export function flatten<
255
+ Schema extends Record<string, ObjectSchema<any, any>>, // eslint-disable-line @typescript-eslint/no-explicit-any
256
+ >(relations: Relations<Schema>): Flatten<Schema>;
257
+
258
+ export function primary<T>(): MetadataAction<T, { primary: true }>;
259
+
260
+ export function references<
261
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
262
+ R extends { entries: any },
263
+ K extends keyof R["entries"],
264
+ T,
265
+ >(
266
+ references: R,
267
+ key: K,
268
+ ): MetadataAction<T, { key: R["entries"][K]; references: R }>;
269
+
270
+ export function relations<
271
+ Schema extends Record<string, ObjectSchema<any, any>>, // eslint-disable-line @typescript-eslint/no-explicit-any
272
+ >(schema: Schema): Relations<Schema>;