@tanstack/db 0.0.14 → 0.0.16

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.
Files changed (197) hide show
  1. package/dist/cjs/collection.cjs +117 -104
  2. package/dist/cjs/collection.cjs.map +1 -1
  3. package/dist/cjs/collection.d.cts +18 -21
  4. package/dist/cjs/index.cjs +31 -13
  5. package/dist/cjs/index.cjs.map +1 -1
  6. package/dist/cjs/index.d.cts +0 -1
  7. package/dist/cjs/query/builder/functions.cjs +107 -0
  8. package/dist/cjs/query/builder/functions.cjs.map +1 -0
  9. package/dist/cjs/query/builder/functions.d.cts +38 -0
  10. package/dist/cjs/query/builder/index.cjs +499 -0
  11. package/dist/cjs/query/builder/index.cjs.map +1 -0
  12. package/dist/cjs/query/builder/index.d.cts +324 -0
  13. package/dist/cjs/query/builder/ref-proxy.cjs +92 -0
  14. package/dist/cjs/query/builder/ref-proxy.cjs.map +1 -0
  15. package/dist/cjs/query/builder/ref-proxy.d.cts +28 -0
  16. package/dist/cjs/query/builder/types.d.cts +81 -0
  17. package/dist/cjs/query/compiler/evaluators.cjs +261 -0
  18. package/dist/cjs/query/compiler/evaluators.cjs.map +1 -0
  19. package/dist/cjs/query/compiler/evaluators.d.cts +11 -0
  20. package/dist/cjs/query/compiler/group-by.cjs +271 -0
  21. package/dist/cjs/query/compiler/group-by.cjs.map +1 -0
  22. package/dist/cjs/query/compiler/group-by.d.cts +7 -0
  23. package/dist/cjs/query/compiler/index.cjs +181 -0
  24. package/dist/cjs/query/compiler/index.cjs.map +1 -0
  25. package/dist/cjs/query/compiler/index.d.cts +15 -0
  26. package/dist/cjs/query/compiler/joins.cjs +116 -0
  27. package/dist/cjs/query/compiler/joins.cjs.map +1 -0
  28. package/dist/cjs/query/compiler/joins.d.cts +11 -0
  29. package/dist/cjs/query/compiler/order-by.cjs +89 -0
  30. package/dist/cjs/query/compiler/order-by.cjs.map +1 -0
  31. package/dist/cjs/query/compiler/order-by.d.cts +9 -0
  32. package/dist/cjs/query/compiler/select.cjs +57 -0
  33. package/dist/cjs/query/compiler/select.cjs.map +1 -0
  34. package/dist/cjs/query/compiler/select.d.cts +15 -0
  35. package/dist/cjs/query/index.d.cts +5 -5
  36. package/dist/cjs/query/ir.cjs +57 -0
  37. package/dist/cjs/query/ir.cjs.map +1 -0
  38. package/dist/cjs/query/ir.d.cts +81 -0
  39. package/dist/cjs/query/live-query-collection.cjs +224 -0
  40. package/dist/cjs/query/live-query-collection.cjs.map +1 -0
  41. package/dist/cjs/query/live-query-collection.d.cts +124 -0
  42. package/dist/cjs/transactions.cjs +20 -13
  43. package/dist/cjs/transactions.cjs.map +1 -1
  44. package/dist/cjs/transactions.d.cts +10 -1
  45. package/dist/cjs/types.d.cts +13 -0
  46. package/dist/esm/collection.d.ts +18 -21
  47. package/dist/esm/collection.js +118 -105
  48. package/dist/esm/collection.js.map +1 -1
  49. package/dist/esm/index.d.ts +0 -1
  50. package/dist/esm/index.js +30 -12
  51. package/dist/esm/query/builder/functions.d.ts +38 -0
  52. package/dist/esm/query/builder/functions.js +107 -0
  53. package/dist/esm/query/builder/functions.js.map +1 -0
  54. package/dist/esm/query/builder/index.d.ts +324 -0
  55. package/dist/esm/query/builder/index.js +499 -0
  56. package/dist/esm/query/builder/index.js.map +1 -0
  57. package/dist/esm/query/builder/ref-proxy.d.ts +28 -0
  58. package/dist/esm/query/builder/ref-proxy.js +92 -0
  59. package/dist/esm/query/builder/ref-proxy.js.map +1 -0
  60. package/dist/esm/query/builder/types.d.ts +81 -0
  61. package/dist/esm/query/compiler/evaluators.d.ts +11 -0
  62. package/dist/esm/query/compiler/evaluators.js +261 -0
  63. package/dist/esm/query/compiler/evaluators.js.map +1 -0
  64. package/dist/esm/query/compiler/group-by.d.ts +7 -0
  65. package/dist/esm/query/compiler/group-by.js +271 -0
  66. package/dist/esm/query/compiler/group-by.js.map +1 -0
  67. package/dist/esm/query/compiler/index.d.ts +15 -0
  68. package/dist/esm/query/compiler/index.js +181 -0
  69. package/dist/esm/query/compiler/index.js.map +1 -0
  70. package/dist/esm/query/compiler/joins.d.ts +11 -0
  71. package/dist/esm/query/compiler/joins.js +116 -0
  72. package/dist/esm/query/compiler/joins.js.map +1 -0
  73. package/dist/esm/query/compiler/order-by.d.ts +9 -0
  74. package/dist/esm/query/compiler/order-by.js +89 -0
  75. package/dist/esm/query/compiler/order-by.js.map +1 -0
  76. package/dist/esm/query/compiler/select.d.ts +15 -0
  77. package/dist/esm/query/compiler/select.js +57 -0
  78. package/dist/esm/query/compiler/select.js.map +1 -0
  79. package/dist/esm/query/index.d.ts +5 -5
  80. package/dist/esm/query/ir.d.ts +81 -0
  81. package/dist/esm/query/ir.js +57 -0
  82. package/dist/esm/query/ir.js.map +1 -0
  83. package/dist/esm/query/live-query-collection.d.ts +124 -0
  84. package/dist/esm/query/live-query-collection.js +224 -0
  85. package/dist/esm/query/live-query-collection.js.map +1 -0
  86. package/dist/esm/transactions.d.ts +10 -1
  87. package/dist/esm/transactions.js +20 -13
  88. package/dist/esm/transactions.js.map +1 -1
  89. package/dist/esm/types.d.ts +13 -0
  90. package/package.json +3 -4
  91. package/src/collection.ts +152 -129
  92. package/src/index.ts +0 -1
  93. package/src/query/builder/functions.ts +267 -0
  94. package/src/query/builder/index.ts +648 -0
  95. package/src/query/builder/ref-proxy.ts +156 -0
  96. package/src/query/builder/types.ts +282 -0
  97. package/src/query/compiler/evaluators.ts +315 -0
  98. package/src/query/compiler/group-by.ts +428 -0
  99. package/src/query/compiler/index.ts +276 -0
  100. package/src/query/compiler/joins.ts +228 -0
  101. package/src/query/compiler/order-by.ts +139 -0
  102. package/src/query/compiler/select.ts +173 -0
  103. package/src/query/index.ts +54 -5
  104. package/src/query/ir.ts +128 -0
  105. package/src/query/live-query-collection.ts +512 -0
  106. package/src/transactions.ts +27 -16
  107. package/src/types.ts +15 -0
  108. package/dist/cjs/query/compiled-query.cjs +0 -160
  109. package/dist/cjs/query/compiled-query.cjs.map +0 -1
  110. package/dist/cjs/query/compiled-query.d.cts +0 -20
  111. package/dist/cjs/query/evaluators.cjs +0 -161
  112. package/dist/cjs/query/evaluators.cjs.map +0 -1
  113. package/dist/cjs/query/evaluators.d.cts +0 -14
  114. package/dist/cjs/query/extractors.cjs +0 -122
  115. package/dist/cjs/query/extractors.cjs.map +0 -1
  116. package/dist/cjs/query/extractors.d.cts +0 -22
  117. package/dist/cjs/query/functions.cjs +0 -152
  118. package/dist/cjs/query/functions.cjs.map +0 -1
  119. package/dist/cjs/query/functions.d.cts +0 -21
  120. package/dist/cjs/query/group-by.cjs +0 -88
  121. package/dist/cjs/query/group-by.cjs.map +0 -1
  122. package/dist/cjs/query/group-by.d.cts +0 -40
  123. package/dist/cjs/query/joins.cjs +0 -141
  124. package/dist/cjs/query/joins.cjs.map +0 -1
  125. package/dist/cjs/query/joins.d.cts +0 -14
  126. package/dist/cjs/query/order-by.cjs +0 -185
  127. package/dist/cjs/query/order-by.cjs.map +0 -1
  128. package/dist/cjs/query/order-by.d.cts +0 -3
  129. package/dist/cjs/query/pipeline-compiler.cjs +0 -89
  130. package/dist/cjs/query/pipeline-compiler.cjs.map +0 -1
  131. package/dist/cjs/query/pipeline-compiler.d.cts +0 -10
  132. package/dist/cjs/query/query-builder.cjs +0 -307
  133. package/dist/cjs/query/query-builder.cjs.map +0 -1
  134. package/dist/cjs/query/query-builder.d.cts +0 -225
  135. package/dist/cjs/query/schema.d.cts +0 -100
  136. package/dist/cjs/query/select.cjs +0 -130
  137. package/dist/cjs/query/select.cjs.map +0 -1
  138. package/dist/cjs/query/select.d.cts +0 -3
  139. package/dist/cjs/query/types.d.cts +0 -189
  140. package/dist/cjs/query/utils.cjs +0 -154
  141. package/dist/cjs/query/utils.cjs.map +0 -1
  142. package/dist/cjs/query/utils.d.cts +0 -37
  143. package/dist/cjs/utils.cjs +0 -17
  144. package/dist/cjs/utils.cjs.map +0 -1
  145. package/dist/cjs/utils.d.cts +0 -3
  146. package/dist/esm/query/compiled-query.d.ts +0 -20
  147. package/dist/esm/query/compiled-query.js +0 -160
  148. package/dist/esm/query/compiled-query.js.map +0 -1
  149. package/dist/esm/query/evaluators.d.ts +0 -14
  150. package/dist/esm/query/evaluators.js +0 -161
  151. package/dist/esm/query/evaluators.js.map +0 -1
  152. package/dist/esm/query/extractors.d.ts +0 -22
  153. package/dist/esm/query/extractors.js +0 -122
  154. package/dist/esm/query/extractors.js.map +0 -1
  155. package/dist/esm/query/functions.d.ts +0 -21
  156. package/dist/esm/query/functions.js +0 -152
  157. package/dist/esm/query/functions.js.map +0 -1
  158. package/dist/esm/query/group-by.d.ts +0 -40
  159. package/dist/esm/query/group-by.js +0 -88
  160. package/dist/esm/query/group-by.js.map +0 -1
  161. package/dist/esm/query/joins.d.ts +0 -14
  162. package/dist/esm/query/joins.js +0 -141
  163. package/dist/esm/query/joins.js.map +0 -1
  164. package/dist/esm/query/order-by.d.ts +0 -3
  165. package/dist/esm/query/order-by.js +0 -185
  166. package/dist/esm/query/order-by.js.map +0 -1
  167. package/dist/esm/query/pipeline-compiler.d.ts +0 -10
  168. package/dist/esm/query/pipeline-compiler.js +0 -89
  169. package/dist/esm/query/pipeline-compiler.js.map +0 -1
  170. package/dist/esm/query/query-builder.d.ts +0 -225
  171. package/dist/esm/query/query-builder.js +0 -307
  172. package/dist/esm/query/query-builder.js.map +0 -1
  173. package/dist/esm/query/schema.d.ts +0 -100
  174. package/dist/esm/query/select.d.ts +0 -3
  175. package/dist/esm/query/select.js +0 -130
  176. package/dist/esm/query/select.js.map +0 -1
  177. package/dist/esm/query/types.d.ts +0 -189
  178. package/dist/esm/query/utils.d.ts +0 -37
  179. package/dist/esm/query/utils.js +0 -154
  180. package/dist/esm/query/utils.js.map +0 -1
  181. package/dist/esm/utils.d.ts +0 -3
  182. package/dist/esm/utils.js +0 -17
  183. package/dist/esm/utils.js.map +0 -1
  184. package/src/query/compiled-query.ts +0 -234
  185. package/src/query/evaluators.ts +0 -250
  186. package/src/query/extractors.ts +0 -214
  187. package/src/query/functions.ts +0 -297
  188. package/src/query/group-by.ts +0 -139
  189. package/src/query/joins.ts +0 -260
  190. package/src/query/order-by.ts +0 -264
  191. package/src/query/pipeline-compiler.ts +0 -149
  192. package/src/query/query-builder.ts +0 -902
  193. package/src/query/schema.ts +0 -268
  194. package/src/query/select.ts +0 -208
  195. package/src/query/types.ts +0 -418
  196. package/src/query/utils.ts +0 -245
  197. package/src/utils.ts +0 -15
@@ -1,418 +0,0 @@
1
- import type {
2
- ConditionOperand,
3
- ExplicitLiteral,
4
- FunctionCall,
5
- LiteralValue,
6
- Select,
7
- } from "./schema.js"
8
-
9
- // Input is analogous to a table in a SQL database
10
- // A Schema is a set of named Inputs
11
- export type Input = Record<string, unknown>
12
- export type Schema = Record<string, Input>
13
-
14
- // Context is a Schema with a default input
15
- export type Context<
16
- TBaseSchema extends Schema = Schema,
17
- TSchema extends Schema = Schema,
18
- > = {
19
- baseSchema: TBaseSchema
20
- schema: TSchema
21
- default?: keyof TSchema
22
- result?: Record<string, unknown>
23
- hasJoin?: boolean
24
- }
25
-
26
- // Helper types
27
-
28
- export type Flatten<T> = {
29
- [K in keyof T]: T[K]
30
- } & {}
31
-
32
- type UniqueSecondLevelKeys<T> = {
33
- [K in keyof T]: Exclude<
34
- keyof T[K],
35
- // all keys in every branch except K
36
- {
37
- [P in Exclude<keyof T, K>]: keyof T[P]
38
- }[Exclude<keyof T, K>]
39
- >
40
- }[keyof T]
41
-
42
- type InputNames<TSchema extends Schema> = RemoveIndexSignature<{
43
- [I in keyof TSchema]: I
44
- }>[keyof RemoveIndexSignature<{
45
- [I in keyof TSchema]: I
46
- }>]
47
-
48
- type UniquePropertyNames<TSchema extends Schema> = UniqueSecondLevelKeys<
49
- RemoveIndexSignature<TSchema>
50
- >
51
-
52
- export type RemoveIndexSignature<T> = {
53
- [K in keyof T as string extends K
54
- ? never
55
- : number extends K
56
- ? never
57
- : K]: T[K]
58
- }
59
-
60
- // Fully qualified references like "@employees.id"
61
- type QualifiedReferencesOfSchemaString<TSchema extends Schema> =
62
- RemoveIndexSignature<{
63
- [I in keyof TSchema]: {
64
- [P in keyof RemoveIndexSignature<
65
- TSchema[I]
66
- >]: `@${string & I}.${string & P}`
67
- }[keyof RemoveIndexSignature<TSchema[I]>]
68
- }>
69
-
70
- type QualifiedReferenceString<TContext extends Context<Schema>> =
71
- QualifiedReferencesOfSchemaString<
72
- TContext[`schema`]
73
- >[keyof QualifiedReferencesOfSchemaString<TContext[`schema`]>]
74
-
75
- // Fully qualified references like { col: '@employees.id' }
76
- type QualifiedReferencesOfSchemaObject<TSchema extends Schema> =
77
- RemoveIndexSignature<{
78
- [I in keyof TSchema]: {
79
- [P in keyof RemoveIndexSignature<TSchema[I]>]: {
80
- col: `${string & I}.${string & P}`
81
- }
82
- }[keyof RemoveIndexSignature<TSchema[I]>]
83
- }>
84
-
85
- type QualifiedReferenceObject<TContext extends Context<Schema>> =
86
- QualifiedReferencesOfSchemaObject<
87
- TContext[`schema`]
88
- >[keyof QualifiedReferencesOfSchemaObject<TContext[`schema`]>]
89
-
90
- type QualifiedReference<TContext extends Context<Schema>> =
91
- | QualifiedReferenceString<TContext>
92
- | QualifiedReferenceObject<TContext>
93
-
94
- type DefaultReferencesOfSchemaString<
95
- TSchema extends Schema,
96
- TDefault extends keyof TSchema,
97
- > = RemoveIndexSignature<{
98
- [P in keyof TSchema[TDefault]]: `@${string & P}`
99
- }>
100
-
101
- type DefaultReferenceString<TContext extends Context<Schema>> =
102
- TContext[`default`] extends undefined
103
- ? never
104
- : DefaultReferencesOfSchemaString<
105
- TContext[`schema`],
106
- Exclude<TContext[`default`], undefined>
107
- >[keyof DefaultReferencesOfSchemaString<
108
- TContext[`schema`],
109
- Exclude<TContext[`default`], undefined>
110
- >]
111
-
112
- type DefaultReferencesOfSchemaObject<
113
- TSchema extends Schema,
114
- TDefault extends keyof TSchema,
115
- > = RemoveIndexSignature<{
116
- [P in keyof TSchema[TDefault]]: { col: `${string & P}` }
117
- }>
118
-
119
- type DefaultReferenceObject<TContext extends Context<Schema>> =
120
- TContext[`default`] extends undefined
121
- ? never
122
- : DefaultReferencesOfSchemaObject<
123
- TContext[`schema`],
124
- Exclude<TContext[`default`], undefined>
125
- >[keyof DefaultReferencesOfSchemaObject<
126
- TContext[`schema`],
127
- Exclude<TContext[`default`], undefined>
128
- >]
129
-
130
- type DefaultReference<TContext extends Context<Schema>> =
131
- | DefaultReferenceString<TContext>
132
- | DefaultReferenceObject<TContext>
133
-
134
- type UniqueReferencesOfSchemaString<TSchema extends Schema> =
135
- RemoveIndexSignature<{
136
- [I in keyof TSchema]: {
137
- [P in keyof TSchema[I]]: P extends UniquePropertyNames<TSchema>
138
- ? `@${string & P}`
139
- : never
140
- }[keyof TSchema[I]]
141
- }>
142
-
143
- type UniqueReferenceString<TContext extends Context<Schema>> =
144
- UniqueReferencesOfSchemaString<
145
- TContext[`schema`]
146
- >[keyof UniqueReferencesOfSchemaString<TContext[`schema`]>]
147
-
148
- type UniqueReferencesOfSchemaObject<TSchema extends Schema> =
149
- RemoveIndexSignature<{
150
- [I in keyof TSchema]: {
151
- [P in keyof TSchema[I]]: P extends UniquePropertyNames<TSchema>
152
- ? { col: `${string & P}` }
153
- : never
154
- }[keyof TSchema[I]]
155
- }>
156
-
157
- type UniqueReferenceObject<TContext extends Context<Schema>> =
158
- UniqueReferencesOfSchemaObject<
159
- TContext[`schema`]
160
- >[keyof UniqueReferencesOfSchemaObject<TContext[`schema`]>]
161
-
162
- type UniqueReference<TContext extends Context<Schema>> =
163
- | UniqueReferenceString<TContext>
164
- | UniqueReferenceObject<TContext>
165
-
166
- type InputWildcardString<TContext extends Context<Schema>> = Flatten<
167
- {
168
- [I in InputNames<TContext[`schema`]>]: `@${I}.*`
169
- }[InputNames<TContext[`schema`]>]
170
- >
171
-
172
- type InputWildcardObject<TContext extends Context<Schema>> = Flatten<
173
- {
174
- [I in InputNames<TContext[`schema`]>]: { col: `${I}.*` }
175
- }[InputNames<TContext[`schema`]>]
176
- >
177
-
178
- type InputWildcard<TContext extends Context<Schema>> =
179
- | InputWildcardString<TContext>
180
- | InputWildcardObject<TContext>
181
-
182
- type AllWildcardString = `@*`
183
- type AllWildcardObject = { col: `*` }
184
- type AllWildcard = AllWildcardString | AllWildcardObject
185
-
186
- export type PropertyReferenceString<TContext extends Context<Schema>> =
187
- | DefaultReferenceString<TContext>
188
- | QualifiedReferenceString<TContext>
189
- | UniqueReferenceString<TContext>
190
-
191
- export type WildcardReferenceString<TContext extends Context<Schema>> =
192
- | InputWildcardString<TContext>
193
- | AllWildcardString
194
-
195
- export type PropertyReferenceObject<TContext extends Context<Schema>> =
196
- | DefaultReferenceObject<TContext>
197
- | QualifiedReferenceObject<TContext>
198
- | UniqueReferenceObject<TContext>
199
-
200
- export type WildcardReferenceObject<TContext extends Context<Schema>> =
201
- | InputWildcardObject<TContext>
202
- | AllWildcardObject
203
-
204
- export type PropertyReference<TContext extends Context<Schema>> =
205
- | DefaultReference<TContext>
206
- | QualifiedReference<TContext>
207
- | UniqueReference<TContext>
208
-
209
- export type WildcardReference<TContext extends Context<Schema>> =
210
- | InputWildcard<TContext>
211
- | AllWildcard
212
-
213
- type InputWithProperty<TSchema extends Schema, TProperty extends string> = {
214
- [I in keyof RemoveIndexSignature<TSchema>]: TProperty extends keyof TSchema[I]
215
- ? I
216
- : never
217
- }[keyof RemoveIndexSignature<TSchema>]
218
-
219
- export type TypeFromPropertyReference<
220
- TContext extends Context<Schema>,
221
- TReference extends PropertyReference<TContext>,
222
- > = TReference extends
223
- | `@${infer InputName}.${infer PropName}`
224
- | { col: `${infer InputName}.${infer PropName}` }
225
- ? InputName extends keyof TContext[`schema`]
226
- ? PropName extends keyof TContext[`schema`][InputName]
227
- ? TContext[`schema`][InputName][PropName]
228
- : never
229
- : never
230
- : TReference extends `@${infer PropName}` | { col: `${infer PropName}` }
231
- ? PropName extends keyof TContext[`schema`][Exclude<
232
- TContext[`default`],
233
- undefined
234
- >]
235
- ? TContext[`schema`][Exclude<TContext[`default`], undefined>][PropName]
236
- : TContext[`schema`][InputWithProperty<
237
- TContext[`schema`],
238
- PropName
239
- >][PropName]
240
- : never
241
-
242
- /**
243
- * Return the key that would be used in the result of the query for a given property
244
- * reference.
245
- * - `@id` -> `id`
246
- * - `@employees.id` -> `id`
247
- * - `{ col: 'id' }` -> `id`
248
- * - `{ col: 'employees.id' }` -> `id`
249
- */
250
- export type ResultKeyFromPropertyReference<
251
- TContext extends Context<Schema>,
252
- TReference extends PropertyReference<TContext>,
253
- > = TReference extends `@${infer _InputName}.${infer PropName}`
254
- ? PropName
255
- : TReference extends { col: `${infer _InputName}.${infer PropName}` }
256
- ? PropName
257
- : TReference extends `@${infer PropName}`
258
- ? PropName
259
- : TReference extends { col: `${infer PropName}` }
260
- ? PropName
261
- : never
262
-
263
- export type InputReference<TContext extends Context<Schema>> = {
264
- [I in InputNames<TContext[`schema`]>]: I
265
- }[InputNames<TContext[`schema`]>]
266
-
267
- export type RenameInput<
268
- TSchema extends Schema,
269
- TInput extends keyof TSchema,
270
- TNewName extends string,
271
- > = Flatten<
272
- {
273
- [K in Exclude<keyof TSchema, TInput>]: TSchema[K]
274
- } & {
275
- [P in TNewName]: TSchema[TInput]
276
- }
277
- >
278
-
279
- export type MaybeRenameInput<
280
- TSchema extends Schema,
281
- TInput extends keyof TSchema,
282
- TNewName extends string | undefined,
283
- > = TNewName extends undefined
284
- ? TSchema
285
- : RenameInput<TSchema, TInput, Exclude<TNewName, undefined>>
286
-
287
- /**
288
- * Helper type to combine result types from each select item in a tuple
289
- */
290
- export type InferResultTypeFromSelectTuple<
291
- TContext extends Context<Schema>,
292
- TSelects extends ReadonlyArray<Select<TContext>>,
293
- > = UnionToIntersection<
294
- {
295
- [K in keyof TSelects]: TSelects[K] extends Select<TContext>
296
- ? InferResultType<TContext, TSelects[K]>
297
- : never
298
- }[number]
299
- >
300
-
301
- /**
302
- * Convert a union type to an intersection type
303
- */
304
- type UnionToIntersection<TUnion> = (
305
- TUnion extends any ? (x: TUnion) => void : never
306
- ) extends (x: infer I) => void
307
- ? I
308
- : never
309
-
310
- /**
311
- * Infers the result type from a single select item
312
- */
313
- type InferResultType<
314
- TContext extends Context<Schema>,
315
- TSelect extends Select<TContext>,
316
- > =
317
- TSelect extends PropertyReferenceString<TContext>
318
- ? {
319
- [K in ResultKeyFromPropertyReference<
320
- TContext,
321
- TSelect
322
- >]: TypeFromPropertyReference<TContext, TSelect>
323
- }
324
- : TSelect extends WildcardReferenceString<TContext>
325
- ? TSelect extends `@*`
326
- ? InferAllColumnsType<TContext>
327
- : TSelect extends `@${infer TableName}.*`
328
- ? TableName extends keyof TContext[`schema`]
329
- ? InferTableColumnsType<TContext, TableName>
330
- : {}
331
- : {}
332
- : TSelect extends {
333
- [alias: string]:
334
- | PropertyReference<TContext>
335
- | FunctionCall<TContext>
336
- }
337
- ? {
338
- [K in keyof TSelect]: TSelect[K] extends PropertyReference<TContext>
339
- ? TypeFromPropertyReference<TContext, TSelect[K]>
340
- : TSelect[K] extends FunctionCall<TContext>
341
- ? InferFunctionCallResultType<TContext, TSelect[K]>
342
- : never
343
- }
344
- : {}
345
-
346
- /**
347
- * Infers the result type for all columns from all tables
348
- */
349
- type InferAllColumnsType<TContext extends Context<Schema>> = {
350
- [K in keyof TContext[`schema`]]: {
351
- [P in keyof TContext[`schema`][K]]: TContext[`schema`][K][P]
352
- }
353
- }[keyof TContext[`schema`]]
354
-
355
- /**
356
- * Infers the result type for all columns from a specific table
357
- */
358
- type InferTableColumnsType<
359
- TContext extends Context<Schema>,
360
- TTable extends keyof TContext[`schema`],
361
- > = {
362
- [P in keyof TContext[`schema`][TTable]]: TContext[`schema`][TTable][P]
363
- }
364
-
365
- /**
366
- * Infers the result type for a function call
367
- */
368
- type InferFunctionCallResultType<
369
- TContext extends Context<Schema>,
370
- TFunctionCall extends FunctionCall<TContext>,
371
- > = TFunctionCall extends { SUM: any }
372
- ? number
373
- : TFunctionCall extends { COUNT: any }
374
- ? number
375
- : TFunctionCall extends { AVG: any }
376
- ? number
377
- : TFunctionCall extends { MIN: any }
378
- ? InferOperandType<TContext, TFunctionCall[`MIN`]>
379
- : TFunctionCall extends { MAX: any }
380
- ? InferOperandType<TContext, TFunctionCall[`MAX`]>
381
- : TFunctionCall extends { DATE: any }
382
- ? string
383
- : TFunctionCall extends { JSON_EXTRACT: any }
384
- ? unknown
385
- : TFunctionCall extends { JSON_EXTRACT_PATH: any }
386
- ? unknown
387
- : TFunctionCall extends { UPPER: any }
388
- ? string
389
- : TFunctionCall extends { LOWER: any }
390
- ? string
391
- : TFunctionCall extends { COALESCE: any }
392
- ? InferOperandType<TContext, TFunctionCall[`COALESCE`]>
393
- : TFunctionCall extends { CONCAT: any }
394
- ? string
395
- : TFunctionCall extends { LENGTH: any }
396
- ? number
397
- : TFunctionCall extends { ORDER_INDEX: any }
398
- ? number
399
- : unknown
400
-
401
- /**
402
- * Infers the type of an operand
403
- */
404
- type InferOperandType<
405
- TContext extends Context<Schema>,
406
- TOperand extends ConditionOperand<TContext>,
407
- > =
408
- TOperand extends PropertyReference<TContext>
409
- ? TypeFromPropertyReference<TContext, TOperand>
410
- : TOperand extends LiteralValue
411
- ? TOperand
412
- : TOperand extends ExplicitLiteral
413
- ? TOperand[`value`]
414
- : TOperand extends FunctionCall<TContext>
415
- ? InferFunctionCallResultType<TContext, TOperand>
416
- : TOperand extends Array<ConditionOperand<TContext>>
417
- ? InferOperandType<TContext, TOperand[number]>
418
- : unknown
@@ -1,245 +0,0 @@
1
- /**
2
- * Helper function to determine if an object is a function call with an aggregate function
3
- */
4
- export function isAggregateFunctionCall(obj: any): boolean {
5
- if (!obj || typeof obj !== `object`) return false
6
-
7
- const aggregateFunctions = [
8
- `SUM`,
9
- `COUNT`,
10
- `AVG`,
11
- `MIN`,
12
- `MAX`,
13
- `MEDIAN`,
14
- `MODE`,
15
- ]
16
- const keys = Object.keys(obj)
17
-
18
- return keys.length === 1 && aggregateFunctions.includes(keys[0]!)
19
- }
20
-
21
- /**
22
- * Helper function to determine if an object is an ORDER_INDEX function call
23
- */
24
- export function isOrderIndexFunctionCall(obj: any): boolean {
25
- if (!obj || typeof obj !== `object`) return false
26
-
27
- const keys = Object.keys(obj)
28
- return keys.length === 1 && keys[0] === `ORDER_INDEX`
29
- }
30
-
31
- /**
32
- * Type guard to check if a value is comparable (can be used with <, >, <=, >= operators)
33
- * @param value The value to check
34
- * @returns True if the value is comparable
35
- */
36
- export function isComparable(
37
- value: unknown
38
- ): value is number | string | Date | boolean {
39
- return (
40
- typeof value === `number` ||
41
- typeof value === `string` ||
42
- typeof value === `boolean` ||
43
- value instanceof Date
44
- )
45
- }
46
-
47
- /**
48
- * Performs a comparison between two values, ensuring they are of compatible types
49
- * @param left The left operand
50
- * @param right The right operand
51
- * @param operator The comparison operator
52
- * @returns The result of the comparison
53
- * @throws Error if the values are not comparable
54
- */
55
- export function compareValues(
56
- left: unknown,
57
- right: unknown,
58
- operator: `<` | `<=` | `>` | `>=`
59
- ): boolean {
60
- // First check if both values are comparable
61
- if (!isComparable(left) || !isComparable(right)) {
62
- throw new Error(
63
- `Cannot compare non-comparable values: ${typeof left} and ${typeof right}`
64
- )
65
- }
66
-
67
- // If they're different types but both are strings or numbers, convert to strings
68
- if (
69
- typeof left !== typeof right &&
70
- (typeof left === `string` || typeof left === `number`) &&
71
- (typeof right === `string` || typeof right === `number`)
72
- ) {
73
- // Convert to strings for comparison (follows JavaScript's coercion rules)
74
- const leftStr = String(left)
75
- const rightStr = String(right)
76
-
77
- switch (operator) {
78
- case `<`:
79
- return leftStr < rightStr
80
- case `<=`:
81
- return leftStr <= rightStr
82
- case `>`:
83
- return leftStr > rightStr
84
- case `>=`:
85
- return leftStr >= rightStr
86
- }
87
- }
88
-
89
- // For Date objects, convert to timestamps
90
- if (left instanceof Date && right instanceof Date) {
91
- const leftTime = left.getTime()
92
- const rightTime = right.getTime()
93
-
94
- switch (operator) {
95
- case `<`:
96
- return leftTime < rightTime
97
- case `<=`:
98
- return leftTime <= rightTime
99
- case `>`:
100
- return leftTime > rightTime
101
- case `>=`:
102
- return leftTime >= rightTime
103
- }
104
- }
105
-
106
- // For other cases where types match
107
- if (typeof left === typeof right) {
108
- switch (operator) {
109
- case `<`:
110
- return left < right
111
- case `<=`:
112
- return left <= right
113
- case `>`:
114
- return left > right
115
- case `>=`:
116
- return left >= right
117
- }
118
- }
119
-
120
- // If we get here, it means the values are technically comparable but not compatible
121
- throw new Error(
122
- `Cannot compare incompatible types: ${typeof left} and ${typeof right}`
123
- )
124
- }
125
-
126
- /**
127
- * Converts a SQL LIKE pattern to a JavaScript regex pattern
128
- * @param pattern The SQL LIKE pattern to convert
129
- * @returns A regex-compatible pattern string
130
- */
131
- export function convertLikeToRegex(pattern: string): string {
132
- let finalPattern = ``
133
- let i = 0
134
-
135
- while (i < pattern.length) {
136
- const char = pattern[i]
137
-
138
- // Handle escape character
139
- if (char === `\\` && i + 1 < pattern.length) {
140
- // Add the next character as a literal (escaped)
141
- finalPattern += pattern[i + 1]
142
- i += 2 // Skip both the escape and the escaped character
143
- continue
144
- }
145
-
146
- // Handle SQL LIKE special characters
147
- switch (char) {
148
- case `%`:
149
- // % matches any sequence of characters (including empty)
150
- finalPattern += `.*`
151
- break
152
- case `_`:
153
- // _ matches any single character
154
- finalPattern += `.`
155
- break
156
- // Handle regex special characters
157
- case `.`:
158
- case `^`:
159
- case `$`:
160
- case `*`:
161
- case `+`:
162
- case `?`:
163
- case `(`:
164
- case `)`:
165
- case `[`:
166
- case `]`:
167
- case `{`:
168
- case `}`:
169
- case `|`:
170
- case `/`:
171
- // Escape regex special characters
172
- finalPattern += `\\` + char
173
- break
174
- default:
175
- // Regular character, just add it
176
- finalPattern += char
177
- }
178
-
179
- i++
180
- }
181
-
182
- return finalPattern
183
- }
184
-
185
- /**
186
- * Helper function to check if a value is in an array, with special handling for various types
187
- * @param value The value to check for
188
- * @param array The array to search in
189
- * @param caseInsensitive Optional flag to enable case-insensitive matching for strings (default: false)
190
- * @returns True if the value is found in the array
191
- */
192
- export function isValueInArray(
193
- value: unknown,
194
- array: Array<unknown>,
195
- caseInsensitive: boolean = false
196
- ): boolean {
197
- // Direct inclusion check first (fastest path)
198
- if (array.includes(value)) {
199
- return true
200
- }
201
-
202
- // Handle null/undefined
203
- if (value === null || value === undefined) {
204
- return array.some((item) => item === null || item === undefined)
205
- }
206
-
207
- // Handle numbers and strings with type coercion
208
- if (typeof value === `number` || typeof value === `string`) {
209
- return array.some((item) => {
210
- // Same type, direct comparison
211
- if (typeof item === typeof value) {
212
- if (typeof value === `string` && caseInsensitive) {
213
- // Case-insensitive comparison for strings (only if explicitly enabled)
214
- return value.toLowerCase() === (item as string).toLowerCase()
215
- }
216
- return item === value
217
- }
218
-
219
- // Different types, try coercion for number/string
220
- if (
221
- (typeof item === `number` || typeof item === `string`) &&
222
- (typeof value === `number` || typeof value === `string`)
223
- ) {
224
- // Convert both to strings for comparison
225
- return String(item) === String(value)
226
- }
227
-
228
- return false
229
- })
230
- }
231
-
232
- // Handle objects/arrays by comparing stringified versions
233
- if (typeof value === `object`) {
234
- const valueStr = JSON.stringify(value)
235
- return array.some((item) => {
236
- if (typeof item === `object` && item !== null) {
237
- return JSON.stringify(item) === valueStr
238
- }
239
- return false
240
- })
241
- }
242
-
243
- // Fallback
244
- return false
245
- }
package/src/utils.ts DELETED
@@ -1,15 +0,0 @@
1
- export function getLockedObjects(): Set<string> {
2
- // Stub implementation that returns an empty Set
3
- return new Set()
4
- }
5
-
6
- let globalVersion = 0
7
-
8
- export function getGlobalVersion(): number {
9
- return globalVersion
10
- }
11
-
12
- export function advanceGlobalVersion(): number {
13
- console.log(`==== advancing global version`, globalVersion + 1)
14
- return globalVersion++
15
- }