kysely 0.26.1 → 0.26.2

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 (210) hide show
  1. package/README.md +6 -6
  2. package/dist/cjs/dialect/dialect-adapter-base.d.ts +3 -3
  3. package/dist/cjs/dialect/mysql/mysql-adapter.d.ts +3 -2
  4. package/dist/cjs/dialect/mysql/mysql-adapter.js +2 -2
  5. package/dist/cjs/dialect/postgres/postgres-adapter.d.ts +3 -2
  6. package/dist/cjs/dialect/postgres/postgres-adapter.js +2 -2
  7. package/dist/cjs/dialect/postgres/postgres-dialect-config.d.ts +2 -2
  8. package/dist/cjs/dialect/sqlite/sqlite-adapter.d.ts +4 -2
  9. package/dist/cjs/dialect/sqlite/sqlite-adapter.js +2 -2
  10. package/dist/cjs/driver/runtime-driver.js +17 -2
  11. package/dist/cjs/expression/expression-builder.d.ts +298 -27
  12. package/dist/cjs/expression/expression-builder.js +36 -26
  13. package/dist/cjs/expression/expression-wrapper.d.ts +177 -8
  14. package/dist/cjs/expression/expression-wrapper.js +4 -4
  15. package/dist/cjs/expression/expression.d.ts +64 -1
  16. package/dist/cjs/helpers/mysql.d.ts +11 -8
  17. package/dist/cjs/helpers/mysql.js +21 -32
  18. package/dist/cjs/helpers/postgres.d.ts +8 -5
  19. package/dist/cjs/helpers/postgres.js +8 -5
  20. package/dist/cjs/helpers/sqlite.d.ts +164 -0
  21. package/dist/cjs/helpers/sqlite.js +180 -0
  22. package/dist/cjs/index.d.ts +3 -1
  23. package/dist/cjs/index.js +1 -0
  24. package/dist/cjs/kysely.d.ts +4 -3
  25. package/dist/cjs/kysely.js +1 -1
  26. package/dist/cjs/operation-node/common-table-expression-node.d.ts +4 -0
  27. package/dist/cjs/operation-node/common-table-expression-node.js +6 -0
  28. package/dist/cjs/operation-node/delete-query-node.d.ts +1 -1
  29. package/dist/cjs/operation-node/delete-query-node.js +3 -3
  30. package/dist/cjs/operation-node/operation-node-transformer.d.ts +2 -0
  31. package/dist/cjs/operation-node/operation-node-transformer.js +8 -0
  32. package/dist/cjs/operation-node/operation-node-visitor.d.ts +2 -0
  33. package/dist/cjs/operation-node/operation-node-visitor.js +1 -0
  34. package/dist/cjs/operation-node/operation-node.d.ts +1 -1
  35. package/dist/cjs/operation-node/operator-node.d.ts +3 -3
  36. package/dist/cjs/operation-node/operator-node.js +3 -0
  37. package/dist/cjs/operation-node/order-by-node.d.ts +2 -2
  38. package/dist/cjs/operation-node/order-by-node.js +4 -4
  39. package/dist/cjs/operation-node/over-node.d.ts +1 -1
  40. package/dist/cjs/operation-node/over-node.js +3 -3
  41. package/dist/cjs/operation-node/select-query-node.d.ts +5 -4
  42. package/dist/cjs/operation-node/select-query-node.js +13 -7
  43. package/dist/cjs/operation-node/tuple-node.d.ts +12 -0
  44. package/dist/cjs/operation-node/tuple-node.js +18 -0
  45. package/dist/cjs/parser/binary-operation-parser.d.ts +11 -7
  46. package/dist/cjs/parser/binary-operation-parser.js +35 -46
  47. package/dist/cjs/parser/expression-parser.d.ts +9 -4
  48. package/dist/cjs/parser/join-parser.d.ts +2 -2
  49. package/dist/cjs/parser/join-parser.js +2 -3
  50. package/dist/cjs/parser/order-by-parser.d.ts +6 -2
  51. package/dist/cjs/parser/order-by-parser.js +41 -7
  52. package/dist/cjs/parser/parse-utils.js +2 -2
  53. package/dist/cjs/parser/reference-parser.js +1 -1
  54. package/dist/cjs/parser/select-parser.d.ts +5 -5
  55. package/dist/cjs/parser/set-operation-parser.d.ts +3 -1
  56. package/dist/cjs/parser/set-operation-parser.js +13 -4
  57. package/dist/cjs/parser/tuple-parser.d.ts +47 -0
  58. package/dist/cjs/parser/tuple-parser.js +2 -0
  59. package/dist/cjs/parser/value-parser.d.ts +4 -2
  60. package/dist/cjs/parser/value-parser.js +12 -1
  61. package/dist/cjs/parser/with-parser.d.ts +3 -2
  62. package/dist/cjs/parser/with-parser.js +16 -4
  63. package/dist/cjs/plugin/camel-case/camel-case-plugin.d.ts +3 -3
  64. package/dist/cjs/plugin/camel-case/camel-case-plugin.js +4 -8
  65. package/dist/cjs/plugin/parse-json-results/parse-json-results-plugin.d.ts +13 -0
  66. package/dist/cjs/plugin/parse-json-results/parse-json-results-plugin.js +50 -14
  67. package/dist/cjs/query-builder/aggregate-function-builder.d.ts +5 -5
  68. package/dist/cjs/query-builder/aggregate-function-builder.js +2 -2
  69. package/dist/cjs/query-builder/case-builder.js +8 -4
  70. package/dist/cjs/query-builder/cte-builder.d.ts +20 -0
  71. package/dist/cjs/query-builder/cte-builder.js +39 -0
  72. package/dist/cjs/query-builder/delete-query-builder.d.ts +34 -7
  73. package/dist/cjs/query-builder/delete-query-builder.js +3 -3
  74. package/dist/cjs/query-builder/function-module.d.ts +41 -6
  75. package/dist/cjs/query-builder/function-module.js +3 -0
  76. package/dist/cjs/query-builder/having-interface.d.ts +2 -5
  77. package/dist/cjs/query-builder/join-builder.d.ts +2 -6
  78. package/dist/cjs/query-builder/join-builder.js +2 -2
  79. package/dist/cjs/query-builder/json-path-builder.d.ts +59 -3
  80. package/dist/cjs/query-builder/on-conflict-builder.d.ts +65 -12
  81. package/dist/cjs/query-builder/on-conflict-builder.js +4 -4
  82. package/dist/cjs/query-builder/over-builder.js +1 -1
  83. package/dist/cjs/query-builder/select-query-builder.d.ts +201 -65
  84. package/dist/cjs/query-builder/select-query-builder.js +72 -924
  85. package/dist/cjs/query-builder/update-query-builder.d.ts +34 -7
  86. package/dist/cjs/query-builder/update-query-builder.js +2 -2
  87. package/dist/cjs/query-builder/where-interface.d.ts +32 -8
  88. package/dist/cjs/query-compiler/default-query-compiler.d.ts +3 -0
  89. package/dist/cjs/query-compiler/default-query-compiler.js +39 -7
  90. package/dist/cjs/query-creator.d.ts +74 -2
  91. package/dist/cjs/query-creator.js +83 -6
  92. package/dist/cjs/raw-builder/raw-builder.d.ts +88 -49
  93. package/dist/cjs/raw-builder/raw-builder.js +18 -45
  94. package/dist/cjs/raw-builder/sql.js +8 -8
  95. package/dist/cjs/schema/alter-table-builder.d.ts +4 -0
  96. package/dist/cjs/schema/alter-table-builder.js +12 -0
  97. package/dist/cjs/schema/create-index-builder.d.ts +1 -1
  98. package/dist/cjs/schema/create-index-builder.js +1 -1
  99. package/dist/cjs/util/column-type.d.ts +7 -6
  100. package/dist/cjs/util/json-object-args.d.ts +3 -0
  101. package/dist/cjs/util/json-object-args.js +35 -0
  102. package/dist/cjs/util/object-utils.d.ts +1 -0
  103. package/dist/cjs/util/object-utils.js +9 -1
  104. package/dist/cjs/util/type-utils.d.ts +10 -10
  105. package/dist/esm/dialect/dialect-adapter-base.d.ts +3 -3
  106. package/dist/esm/dialect/mysql/mysql-adapter.d.ts +3 -2
  107. package/dist/esm/dialect/mysql/mysql-adapter.js +2 -2
  108. package/dist/esm/dialect/postgres/postgres-adapter.d.ts +3 -2
  109. package/dist/esm/dialect/postgres/postgres-adapter.js +2 -2
  110. package/dist/esm/dialect/postgres/postgres-dialect-config.d.ts +2 -2
  111. package/dist/esm/dialect/sqlite/sqlite-adapter.d.ts +4 -2
  112. package/dist/esm/dialect/sqlite/sqlite-adapter.js +2 -2
  113. package/dist/esm/driver/runtime-driver.js +17 -2
  114. package/dist/esm/expression/expression-builder.d.ts +298 -27
  115. package/dist/esm/expression/expression-builder.js +40 -30
  116. package/dist/esm/expression/expression-wrapper.d.ts +177 -8
  117. package/dist/esm/expression/expression-wrapper.js +5 -5
  118. package/dist/esm/expression/expression.d.ts +64 -1
  119. package/dist/esm/helpers/mysql.d.ts +11 -8
  120. package/dist/esm/helpers/mysql.js +21 -32
  121. package/dist/esm/helpers/postgres.d.ts +8 -5
  122. package/dist/esm/helpers/postgres.js +8 -5
  123. package/dist/esm/helpers/sqlite.d.ts +164 -0
  124. package/dist/esm/helpers/sqlite.js +175 -0
  125. package/dist/esm/index.d.ts +3 -1
  126. package/dist/esm/index.js +1 -0
  127. package/dist/esm/kysely.d.ts +4 -3
  128. package/dist/esm/kysely.js +1 -1
  129. package/dist/esm/operation-node/common-table-expression-node.d.ts +4 -0
  130. package/dist/esm/operation-node/common-table-expression-node.js +6 -0
  131. package/dist/esm/operation-node/delete-query-node.d.ts +1 -1
  132. package/dist/esm/operation-node/delete-query-node.js +3 -3
  133. package/dist/esm/operation-node/operation-node-transformer.d.ts +2 -0
  134. package/dist/esm/operation-node/operation-node-transformer.js +8 -0
  135. package/dist/esm/operation-node/operation-node-visitor.d.ts +2 -0
  136. package/dist/esm/operation-node/operation-node-visitor.js +1 -0
  137. package/dist/esm/operation-node/operation-node.d.ts +1 -1
  138. package/dist/esm/operation-node/operator-node.d.ts +3 -3
  139. package/dist/esm/operation-node/operator-node.js +3 -0
  140. package/dist/esm/operation-node/order-by-node.d.ts +2 -2
  141. package/dist/esm/operation-node/order-by-node.js +4 -4
  142. package/dist/esm/operation-node/over-node.d.ts +1 -1
  143. package/dist/esm/operation-node/over-node.js +3 -3
  144. package/dist/esm/operation-node/select-query-node.d.ts +5 -4
  145. package/dist/esm/operation-node/select-query-node.js +13 -7
  146. package/dist/esm/operation-node/tuple-node.d.ts +12 -0
  147. package/dist/esm/operation-node/tuple-node.js +16 -0
  148. package/dist/esm/parser/binary-operation-parser.d.ts +11 -7
  149. package/dist/esm/parser/binary-operation-parser.js +36 -47
  150. package/dist/esm/parser/expression-parser.d.ts +9 -4
  151. package/dist/esm/parser/join-parser.d.ts +2 -2
  152. package/dist/esm/parser/join-parser.js +3 -4
  153. package/dist/esm/parser/order-by-parser.d.ts +6 -2
  154. package/dist/esm/parser/order-by-parser.js +40 -7
  155. package/dist/esm/parser/parse-utils.js +3 -3
  156. package/dist/esm/parser/reference-parser.js +1 -1
  157. package/dist/esm/parser/select-parser.d.ts +5 -5
  158. package/dist/esm/parser/set-operation-parser.d.ts +3 -1
  159. package/dist/esm/parser/set-operation-parser.js +11 -2
  160. package/dist/esm/parser/tuple-parser.d.ts +47 -0
  161. package/dist/esm/parser/tuple-parser.js +2 -0
  162. package/dist/esm/parser/value-parser.d.ts +4 -2
  163. package/dist/esm/parser/value-parser.js +10 -1
  164. package/dist/esm/parser/with-parser.d.ts +3 -2
  165. package/dist/esm/parser/with-parser.js +16 -4
  166. package/dist/esm/plugin/camel-case/camel-case-plugin.d.ts +3 -3
  167. package/dist/esm/plugin/camel-case/camel-case-plugin.js +5 -9
  168. package/dist/esm/plugin/parse-json-results/parse-json-results-plugin.d.ts +13 -0
  169. package/dist/esm/plugin/parse-json-results/parse-json-results-plugin.js +51 -15
  170. package/dist/esm/query-builder/aggregate-function-builder.d.ts +5 -5
  171. package/dist/esm/query-builder/aggregate-function-builder.js +3 -3
  172. package/dist/esm/query-builder/case-builder.js +10 -6
  173. package/dist/esm/query-builder/cte-builder.d.ts +20 -0
  174. package/dist/esm/query-builder/cte-builder.js +36 -0
  175. package/dist/esm/query-builder/delete-query-builder.d.ts +34 -7
  176. package/dist/esm/query-builder/delete-query-builder.js +4 -4
  177. package/dist/esm/query-builder/function-module.d.ts +41 -6
  178. package/dist/esm/query-builder/function-module.js +3 -0
  179. package/dist/esm/query-builder/having-interface.d.ts +2 -5
  180. package/dist/esm/query-builder/join-builder.d.ts +2 -6
  181. package/dist/esm/query-builder/join-builder.js +3 -3
  182. package/dist/esm/query-builder/json-path-builder.d.ts +59 -3
  183. package/dist/esm/query-builder/on-conflict-builder.d.ts +65 -12
  184. package/dist/esm/query-builder/on-conflict-builder.js +5 -5
  185. package/dist/esm/query-builder/over-builder.js +1 -1
  186. package/dist/esm/query-builder/select-query-builder.d.ts +201 -65
  187. package/dist/esm/query-builder/select-query-builder.js +72 -923
  188. package/dist/esm/query-builder/update-query-builder.d.ts +34 -7
  189. package/dist/esm/query-builder/update-query-builder.js +3 -3
  190. package/dist/esm/query-builder/where-interface.d.ts +32 -8
  191. package/dist/esm/query-compiler/default-query-compiler.d.ts +3 -0
  192. package/dist/esm/query-compiler/default-query-compiler.js +39 -7
  193. package/dist/esm/query-creator.d.ts +74 -2
  194. package/dist/esm/query-creator.js +84 -7
  195. package/dist/esm/raw-builder/raw-builder.d.ts +88 -49
  196. package/dist/esm/raw-builder/raw-builder.js +17 -42
  197. package/dist/esm/raw-builder/sql.js +9 -9
  198. package/dist/esm/schema/alter-table-builder.d.ts +4 -0
  199. package/dist/esm/schema/alter-table-builder.js +12 -0
  200. package/dist/esm/schema/create-index-builder.d.ts +1 -1
  201. package/dist/esm/schema/create-index-builder.js +2 -2
  202. package/dist/esm/util/column-type.d.ts +7 -6
  203. package/dist/esm/util/json-object-args.d.ts +3 -0
  204. package/dist/esm/util/json-object-args.js +32 -0
  205. package/dist/esm/util/object-utils.d.ts +1 -0
  206. package/dist/esm/util/object-utils.js +7 -0
  207. package/dist/esm/util/type-utils.d.ts +10 -10
  208. package/helpers/sqlite.d.ts +1 -0
  209. package/helpers/sqlite.js +1 -0
  210. package/package.json +7 -4
@@ -4,11 +4,12 @@ import { OperationNode } from '../operation-node/operation-node.js';
4
4
  import { OrNode } from '../operation-node/or-node.js';
5
5
  import { ParensNode } from '../operation-node/parens-node.js';
6
6
  import { ComparisonOperatorExpression, OperandValueExpressionOrList } from '../parser/binary-operation-parser.js';
7
+ import { OperandExpression } from '../parser/expression-parser.js';
7
8
  import { ReferenceExpression } from '../parser/reference-parser.js';
8
9
  import { KyselyTypeError } from '../util/type-error.js';
9
10
  import { SqlBool } from '../util/type-utils.js';
10
- import { AliasedExpression, Expression } from './expression.js';
11
- export declare class ExpressionWrapper<DB, TB extends keyof DB, T> implements Expression<T> {
11
+ import { AliasableExpression, AliasedExpression, Expression } from './expression.js';
12
+ export declare class ExpressionWrapper<DB, TB extends keyof DB, T> implements AliasableExpression<T> {
12
13
  #private;
13
14
  constructor(node: OperationNode);
14
15
  /** @private */
@@ -55,6 +56,62 @@ export declare class ExpressionWrapper<DB, TB extends keyof DB, T> implements Ex
55
56
  * ```
56
57
  */
57
58
  as<A extends string>(alias: A): AliasedExpression<T, A>;
59
+ /**
60
+ * Returns an aliased version of the expression.
61
+ *
62
+ * In addition to slapping `as "the_alias"` at the end of the expression,
63
+ * this method also provides strict typing:
64
+ *
65
+ * ```ts
66
+ * const result = await db
67
+ * .selectFrom('person')
68
+ * .select((eb) =>
69
+ * // `eb.fn<string>` returns an AliasableExpression<string>
70
+ * eb.fn<string>('concat', ['first_name' eb.val(' '), 'last_name']).as('full_name')
71
+ * )
72
+ * .executeTakeFirstOrThrow()
73
+ *
74
+ * // `full_name: string` field exists in the result type.
75
+ * console.log(result.full_name)
76
+ * ```
77
+ *
78
+ * The generated SQL (PostgreSQL):
79
+ *
80
+ * ```ts
81
+ * select
82
+ * concat("first_name", $1, "last_name") as "full_name"
83
+ * from
84
+ * "person"
85
+ * ```
86
+ *
87
+ * You can also pass in a raw SQL snippet (or any expression) but in that case you must
88
+ * provide the alias as the only type argument:
89
+ *
90
+ * ```ts
91
+ * const values = sql<{ a: number, b: string }>`(values (1, 'foo'))`
92
+ *
93
+ * // The alias is `t(a, b)` which specifies the column names
94
+ * // in addition to the table name. We must tell kysely that
95
+ * // columns of the table can be referenced through `t`
96
+ * // by providing an explicit type argument.
97
+ * const aliasedValues = values.as<'t'>(sql`t(a, b)`)
98
+ *
99
+ * await db
100
+ * .insertInto('person')
101
+ * .columns(['first_name', 'last_name'])
102
+ * .expression(
103
+ * db.selectFrom(aliasedValues).select(['t.a', 't.b'])
104
+ * )
105
+ * ```
106
+ *
107
+ * The generated SQL (PostgreSQL):
108
+ *
109
+ * ```ts
110
+ * insert into "person" ("first_name", "last_name")
111
+ * from (values (1, 'foo')) as t(a, b)
112
+ * select "t"."a", "t"."b"
113
+ * ```
114
+ */
58
115
  as<A extends string>(alias: Expression<unknown>): AliasedExpression<T, A>;
59
116
  /**
60
117
  * Combines `this` and another expression using `OR`.
@@ -117,7 +174,7 @@ export declare class ExpressionWrapper<DB, TB extends keyof DB, T> implements Ex
117
174
  * ```
118
175
  */
119
176
  or<RE extends ReferenceExpression<DB, TB>>(lhs: RE, op: ComparisonOperatorExpression, rhs: OperandValueExpressionOrList<DB, TB, RE>): T extends SqlBool ? OrWrapper<DB, TB, SqlBool> : KyselyTypeError<'or() method can only be called on boolean expressions'>;
120
- or(expression: Expression<SqlBool>): T extends SqlBool ? OrWrapper<DB, TB, SqlBool> : KyselyTypeError<'or() method can only be called on boolean expressions'>;
177
+ or(expression: OperandExpression<SqlBool>): T extends SqlBool ? OrWrapper<DB, TB, SqlBool> : KyselyTypeError<'or() method can only be called on boolean expressions'>;
121
178
  /**
122
179
  * Combines `this` and another expression using `AND`.
123
180
  *
@@ -179,7 +236,7 @@ export declare class ExpressionWrapper<DB, TB extends keyof DB, T> implements Ex
179
236
  * ```
180
237
  */
181
238
  and<RE extends ReferenceExpression<DB, TB>>(lhs: RE, op: ComparisonOperatorExpression, rhs: OperandValueExpressionOrList<DB, TB, RE>): T extends SqlBool ? AndWrapper<DB, TB, SqlBool> : KyselyTypeError<'and() method can only be called on boolean expressions'>;
182
- and(expression: Expression<SqlBool>): T extends SqlBool ? AndWrapper<DB, TB, SqlBool> : KyselyTypeError<'and() method can only be called on boolean expressions'>;
239
+ and(expression: OperandExpression<SqlBool>): T extends SqlBool ? AndWrapper<DB, TB, SqlBool> : KyselyTypeError<'and() method can only be called on boolean expressions'>;
183
240
  /**
184
241
  * Change the output type of the expression.
185
242
  *
@@ -221,7 +278,7 @@ export declare class AliasedExpressionWrapper<T, A extends string> implements Al
221
278
  */
222
279
  toOperationNode(): AliasNode;
223
280
  }
224
- export declare class OrWrapper<DB, TB extends keyof DB, T extends SqlBool> implements Expression<T> {
281
+ export declare class OrWrapper<DB, TB extends keyof DB, T extends SqlBool> implements AliasableExpression<T> {
225
282
  #private;
226
283
  constructor(node: OrNode);
227
284
  /** @private */
@@ -270,6 +327,62 @@ export declare class OrWrapper<DB, TB extends keyof DB, T extends SqlBool> imple
270
327
  * ```
271
328
  */
272
329
  as<A extends string>(alias: A): AliasedExpression<T, A>;
330
+ /**
331
+ * Returns an aliased version of the expression.
332
+ *
333
+ * In addition to slapping `as "the_alias"` at the end of the expression,
334
+ * this method also provides strict typing:
335
+ *
336
+ * ```ts
337
+ * const result = await db
338
+ * .selectFrom('person')
339
+ * .select((eb) =>
340
+ * // `eb.fn<string>` returns an AliasableExpression<string>
341
+ * eb.fn<string>('concat', ['first_name' eb.val(' '), 'last_name']).as('full_name')
342
+ * )
343
+ * .executeTakeFirstOrThrow()
344
+ *
345
+ * // `full_name: string` field exists in the result type.
346
+ * console.log(result.full_name)
347
+ * ```
348
+ *
349
+ * The generated SQL (PostgreSQL):
350
+ *
351
+ * ```ts
352
+ * select
353
+ * concat("first_name", $1, "last_name") as "full_name"
354
+ * from
355
+ * "person"
356
+ * ```
357
+ *
358
+ * You can also pass in a raw SQL snippet (or any expression) but in that case you must
359
+ * provide the alias as the only type argument:
360
+ *
361
+ * ```ts
362
+ * const values = sql<{ a: number, b: string }>`(values (1, 'foo'))`
363
+ *
364
+ * // The alias is `t(a, b)` which specifies the column names
365
+ * // in addition to the table name. We must tell kysely that
366
+ * // columns of the table can be referenced through `t`
367
+ * // by providing an explicit type argument.
368
+ * const aliasedValues = values.as<'t'>(sql`t(a, b)`)
369
+ *
370
+ * await db
371
+ * .insertInto('person')
372
+ * .columns(['first_name', 'last_name'])
373
+ * .expression(
374
+ * db.selectFrom(aliasedValues).select(['t.a', 't.b'])
375
+ * )
376
+ * ```
377
+ *
378
+ * The generated SQL (PostgreSQL):
379
+ *
380
+ * ```ts
381
+ * insert into "person" ("first_name", "last_name")
382
+ * from (values (1, 'foo')) as t(a, b)
383
+ * select "t"."a", "t"."b"
384
+ * ```
385
+ */
273
386
  as<A extends string>(alias: Expression<unknown>): AliasedExpression<T, A>;
274
387
  /**
275
388
  * Combines `this` and another expression using `OR`.
@@ -277,7 +390,7 @@ export declare class OrWrapper<DB, TB extends keyof DB, T extends SqlBool> imple
277
390
  * See {@link ExpressionWrapper.or} for examples.
278
391
  */
279
392
  or<RE extends ReferenceExpression<DB, TB>>(lhs: RE, op: ComparisonOperatorExpression, rhs: OperandValueExpressionOrList<DB, TB, RE>): OrWrapper<DB, TB, T>;
280
- or(expression: Expression<SqlBool>): OrWrapper<DB, TB, T>;
393
+ or(expression: OperandExpression<SqlBool>): OrWrapper<DB, TB, T>;
281
394
  /**
282
395
  * Change the output type of the expression.
283
396
  *
@@ -301,7 +414,7 @@ export declare class OrWrapper<DB, TB extends keyof DB, T extends SqlBool> imple
301
414
  */
302
415
  toOperationNode(): ParensNode;
303
416
  }
304
- export declare class AndWrapper<DB, TB extends keyof DB, T extends SqlBool> implements Expression<T> {
417
+ export declare class AndWrapper<DB, TB extends keyof DB, T extends SqlBool> implements AliasableExpression<T> {
305
418
  #private;
306
419
  constructor(node: AndNode);
307
420
  /** @private */
@@ -350,6 +463,62 @@ export declare class AndWrapper<DB, TB extends keyof DB, T extends SqlBool> impl
350
463
  * ```
351
464
  */
352
465
  as<A extends string>(alias: A): AliasedExpression<T, A>;
466
+ /**
467
+ * Returns an aliased version of the expression.
468
+ *
469
+ * In addition to slapping `as "the_alias"` at the end of the expression,
470
+ * this method also provides strict typing:
471
+ *
472
+ * ```ts
473
+ * const result = await db
474
+ * .selectFrom('person')
475
+ * .select((eb) =>
476
+ * // `eb.fn<string>` returns an AliasableExpression<string>
477
+ * eb.fn<string>('concat', ['first_name' eb.val(' '), 'last_name']).as('full_name')
478
+ * )
479
+ * .executeTakeFirstOrThrow()
480
+ *
481
+ * // `full_name: string` field exists in the result type.
482
+ * console.log(result.full_name)
483
+ * ```
484
+ *
485
+ * The generated SQL (PostgreSQL):
486
+ *
487
+ * ```ts
488
+ * select
489
+ * concat("first_name", $1, "last_name") as "full_name"
490
+ * from
491
+ * "person"
492
+ * ```
493
+ *
494
+ * You can also pass in a raw SQL snippet (or any expression) but in that case you must
495
+ * provide the alias as the only type argument:
496
+ *
497
+ * ```ts
498
+ * const values = sql<{ a: number, b: string }>`(values (1, 'foo'))`
499
+ *
500
+ * // The alias is `t(a, b)` which specifies the column names
501
+ * // in addition to the table name. We must tell kysely that
502
+ * // columns of the table can be referenced through `t`
503
+ * // by providing an explicit type argument.
504
+ * const aliasedValues = values.as<'t'>(sql`t(a, b)`)
505
+ *
506
+ * await db
507
+ * .insertInto('person')
508
+ * .columns(['first_name', 'last_name'])
509
+ * .expression(
510
+ * db.selectFrom(aliasedValues).select(['t.a', 't.b'])
511
+ * )
512
+ * ```
513
+ *
514
+ * The generated SQL (PostgreSQL):
515
+ *
516
+ * ```ts
517
+ * insert into "person" ("first_name", "last_name")
518
+ * from (values (1, 'foo')) as t(a, b)
519
+ * select "t"."a", "t"."b"
520
+ * ```
521
+ */
353
522
  as<A extends string>(alias: Expression<unknown>): AliasedExpression<T, A>;
354
523
  /**
355
524
  * Combines `this` and another expression using `AND`.
@@ -357,7 +526,7 @@ export declare class AndWrapper<DB, TB extends keyof DB, T extends SqlBool> impl
357
526
  * See {@link ExpressionWrapper.and} for examples.
358
527
  */
359
528
  and<RE extends ReferenceExpression<DB, TB>>(lhs: RE, op: ComparisonOperatorExpression, rhs: OperandValueExpressionOrList<DB, TB, RE>): AndWrapper<DB, TB, T>;
360
- and(expression: Expression<SqlBool>): AndWrapper<DB, TB, T>;
529
+ and(expression: OperandExpression<SqlBool>): AndWrapper<DB, TB, T>;
361
530
  /**
362
531
  * Change the output type of the expression.
363
532
  *
@@ -5,7 +5,7 @@ import { IdentifierNode } from '../operation-node/identifier-node.js';
5
5
  import { isOperationNodeSource } from '../operation-node/operation-node-source.js';
6
6
  import { OrNode } from '../operation-node/or-node.js';
7
7
  import { ParensNode } from '../operation-node/parens-node.js';
8
- import { parseFilter, } from '../parser/binary-operation-parser.js';
8
+ import { parseValueBinaryOperationOrExpression, } from '../parser/binary-operation-parser.js';
9
9
  export class ExpressionWrapper {
10
10
  #node;
11
11
  constructor(node) {
@@ -19,10 +19,10 @@ export class ExpressionWrapper {
19
19
  return new AliasedExpressionWrapper(this, alias);
20
20
  }
21
21
  or(...args) {
22
- return new OrWrapper(OrNode.create(this.#node, parseFilter(args)));
22
+ return new OrWrapper(OrNode.create(this.#node, parseValueBinaryOperationOrExpression(args)));
23
23
  }
24
24
  and(...args) {
25
- return new AndWrapper(AndNode.create(this.#node, parseFilter(args)));
25
+ return new AndWrapper(AndNode.create(this.#node, parseValueBinaryOperationOrExpression(args)));
26
26
  }
27
27
  /**
28
28
  * Change the output type of the expression.
@@ -71,7 +71,7 @@ export class OrWrapper {
71
71
  return new AliasedExpressionWrapper(this, alias);
72
72
  }
73
73
  or(...args) {
74
- return new OrWrapper(OrNode.create(this.#node, parseFilter(args)));
74
+ return new OrWrapper(OrNode.create(this.#node, parseValueBinaryOperationOrExpression(args)));
75
75
  }
76
76
  /**
77
77
  * Change the output type of the expression.
@@ -99,7 +99,7 @@ export class AndWrapper {
99
99
  return new AliasedExpressionWrapper(this, alias);
100
100
  }
101
101
  and(...args) {
102
- return new AndWrapper(AndNode.create(this.#node, parseFilter(args)));
102
+ return new AndWrapper(AndNode.create(this.#node, parseValueBinaryOperationOrExpression(args)));
103
103
  }
104
104
  /**
105
105
  * Change the output type of the expression.
@@ -49,7 +49,70 @@ export interface Expression<T> extends OperationNodeSource {
49
49
  toOperationNode(): OperationNode;
50
50
  }
51
51
  /**
52
- * Just like `Expression<T>` but also holds an alias type `A`.
52
+ * An expression with an `as` method.
53
+ */
54
+ export interface AliasableExpression<T> extends Expression<T> {
55
+ /**
56
+ * Returns an aliased version of the expression.
57
+ *
58
+ * In addition to slapping `as "the_alias"` at the end of the expression,
59
+ * this method also provides strict typing:
60
+ *
61
+ * ```ts
62
+ * const result = await db
63
+ * .selectFrom('person')
64
+ * .select((eb) =>
65
+ * // `eb.fn<string>` returns an AliasableExpression<string>
66
+ * eb.fn<string>('concat', ['first_name' eb.val(' '), 'last_name']).as('full_name')
67
+ * )
68
+ * .executeTakeFirstOrThrow()
69
+ *
70
+ * // `full_name: string` field exists in the result type.
71
+ * console.log(result.full_name)
72
+ * ```
73
+ *
74
+ * The generated SQL (PostgreSQL):
75
+ *
76
+ * ```ts
77
+ * select
78
+ * concat("first_name", $1, "last_name") as "full_name"
79
+ * from
80
+ * "person"
81
+ * ```
82
+ *
83
+ * You can also pass in a raw SQL snippet (or any expression) but in that case you must
84
+ * provide the alias as the only type argument:
85
+ *
86
+ * ```ts
87
+ * const values = sql<{ a: number, b: string }>`(values (1, 'foo'))`
88
+ *
89
+ * // The alias is `t(a, b)` which specifies the column names
90
+ * // in addition to the table name. We must tell kysely that
91
+ * // columns of the table can be referenced through `t`
92
+ * // by providing an explicit type argument.
93
+ * const aliasedValues = values.as<'t'>(sql`t(a, b)`)
94
+ *
95
+ * await db
96
+ * .insertInto('person')
97
+ * .columns(['first_name', 'last_name'])
98
+ * .expression(
99
+ * db.selectFrom(aliasedValues).select(['t.a', 't.b'])
100
+ * )
101
+ * ```
102
+ *
103
+ * The generated SQL (PostgreSQL):
104
+ *
105
+ * ```ts
106
+ * insert into "person" ("first_name", "last_name")
107
+ * from (values (1, 'foo')) as t(a, b)
108
+ * select "t"."a", "t"."b"
109
+ * ```
110
+ */
111
+ as<A extends string>(alias: A): AliasedExpression<T, A>;
112
+ as<A extends string>(alias: Expression<any>): AliasedExpression<T, A>;
113
+ }
114
+ /**
115
+ * A type that holds an expression and an alias for it.
53
116
  *
54
117
  * `AliasedExpression<T, A>` can be used in places where, in addition to the value type `T`, you
55
118
  * also need a name `A` for that value. For example anything you can pass into the `select` method
@@ -6,8 +6,9 @@ import { Simplify } from '../util/type-utils.js';
6
6
  * A MySQL helper for aggregating a subquery into a JSON array.
7
7
  *
8
8
  * NOTE: This helper is only guaranteed to fully work with the built-in `MysqlDialect`.
9
- * While the produced SQL is compatibe with all MySQL databases, some 3rd party dialects
10
- * may not parse the nested results into arrays.
9
+ * While the produced SQL is compatible with all MySQL databases, some 3rd party dialects
10
+ * may not parse the nested JSON into arrays. In these cases you can use the built in
11
+ * `ParseJSONResultsPlugin` to parse the results.
11
12
  *
12
13
  * ### Examples
13
14
  *
@@ -19,7 +20,7 @@ import { Simplify } from '../util/type-utils.js';
19
20
  * jsonArrayFrom(
20
21
  * eb.selectFrom('pet')
21
22
  * .select(['pet.id as pet_id', 'pet.name'])
22
- * .where('pet.owner_id', '=', 'person.id')
23
+ * .whereRef('pet.owner_id', '=', 'person.id')
23
24
  * .orderBy('pet.name')
24
25
  * ).as('pets')
25
26
  * ])
@@ -54,8 +55,9 @@ export declare function jsonArrayFrom<O>(expr: SelectQueryBuilder<any, any, O>):
54
55
  * The subquery must only return one row.
55
56
  *
56
57
  * NOTE: This helper is only guaranteed to fully work with the built-in `MysqlDialect`.
57
- * While the produced SQL is compatibe with all MySQL databases, some 3rd party dialects
58
- * may not parse the nested results into objects.
58
+ * While the produced SQL is compatible with all MySQL databases, some 3rd party dialects
59
+ * may not parse the nested JSON into objects. In these cases you can use the built in
60
+ * `ParseJSONResultsPlugin` to parse the results.
59
61
  *
60
62
  * ### Examples
61
63
  *
@@ -67,7 +69,7 @@ export declare function jsonArrayFrom<O>(expr: SelectQueryBuilder<any, any, O>):
67
69
  * jsonObjectFrom(
68
70
  * eb.selectFrom('pet')
69
71
  * .select(['pet.id as pet_id', 'pet.name'])
70
- * .where('pet.owner_id', '=', 'person.id')
72
+ * .whereRef('pet.owner_id', '=', 'person.id')
71
73
  * .where('pet.is_favorite', '=', true)
72
74
  * ).as('favorite_pet')
73
75
  * ])
@@ -100,8 +102,9 @@ export declare function jsonObjectFrom<O>(expr: SelectQueryBuilder<any, any, O>)
100
102
  * The MySQL `json_object` function.
101
103
  *
102
104
  * NOTE: This helper is only guaranteed to fully work with the built-in `MysqlDialect`.
103
- * While the produced SQL is compatibe with all MySQL databases, some 3rd party dialects
104
- * may not parse the nested results into objects.
105
+ * While the produced SQL is compatible with all MySQL databases, some 3rd party dialects
106
+ * may not parse the nested JSON into objects. In these cases you can use the built in
107
+ * `ParseJSONResultsPlugin` to parse the results.
105
108
  *
106
109
  * ### Examples
107
110
  *
@@ -1,15 +1,13 @@
1
1
  /// <reference types="./mysql.d.ts" />
2
- import { AliasNode } from '../operation-node/alias-node.js';
3
- import { ColumnNode } from '../operation-node/column-node.js';
4
- import { IdentifierNode } from '../operation-node/identifier-node.js';
5
- import { ReferenceNode } from '../operation-node/reference-node.js';
6
2
  import { sql } from '../raw-builder/sql.js';
3
+ import { getJsonObjectArgs } from '../util/json-object-args.js';
7
4
  /**
8
5
  * A MySQL helper for aggregating a subquery into a JSON array.
9
6
  *
10
7
  * NOTE: This helper is only guaranteed to fully work with the built-in `MysqlDialect`.
11
- * While the produced SQL is compatibe with all MySQL databases, some 3rd party dialects
12
- * may not parse the nested results into arrays.
8
+ * While the produced SQL is compatible with all MySQL databases, some 3rd party dialects
9
+ * may not parse the nested JSON into arrays. In these cases you can use the built in
10
+ * `ParseJSONResultsPlugin` to parse the results.
13
11
  *
14
12
  * ### Examples
15
13
  *
@@ -21,7 +19,7 @@ import { sql } from '../raw-builder/sql.js';
21
19
  * jsonArrayFrom(
22
20
  * eb.selectFrom('pet')
23
21
  * .select(['pet.id as pet_id', 'pet.name'])
24
- * .where('pet.owner_id', '=', 'person.id')
22
+ * .whereRef('pet.owner_id', '=', 'person.id')
25
23
  * .orderBy('pet.name')
26
24
  * ).as('pets')
27
25
  * ])
@@ -50,7 +48,7 @@ import { sql } from '../raw-builder/sql.js';
50
48
  * ```
51
49
  */
52
50
  export function jsonArrayFrom(expr) {
53
- return sql `(select cast(coalesce(json_arrayagg(json_object(${sql.join(getJsonObjectArgs(expr.toOperationNode(), 'agg'))})), '[]') as json) from ${expr} as agg)`;
51
+ return sql `(select cast(coalesce(json_arrayagg(json_object(${sql.join(getMysqlJsonObjectArgs(expr.toOperationNode(), 'agg'))})), '[]') as json) from ${expr} as agg)`;
54
52
  }
55
53
  /**
56
54
  * A MySQL helper for turning a subquery into a JSON object.
@@ -58,8 +56,9 @@ export function jsonArrayFrom(expr) {
58
56
  * The subquery must only return one row.
59
57
  *
60
58
  * NOTE: This helper is only guaranteed to fully work with the built-in `MysqlDialect`.
61
- * While the produced SQL is compatibe with all MySQL databases, some 3rd party dialects
62
- * may not parse the nested results into objects.
59
+ * While the produced SQL is compatible with all MySQL databases, some 3rd party dialects
60
+ * may not parse the nested JSON into objects. In these cases you can use the built in
61
+ * `ParseJSONResultsPlugin` to parse the results.
63
62
  *
64
63
  * ### Examples
65
64
  *
@@ -71,7 +70,7 @@ export function jsonArrayFrom(expr) {
71
70
  * jsonObjectFrom(
72
71
  * eb.selectFrom('pet')
73
72
  * .select(['pet.id as pet_id', 'pet.name'])
74
- * .where('pet.owner_id', '=', 'person.id')
73
+ * .whereRef('pet.owner_id', '=', 'person.id')
75
74
  * .where('pet.is_favorite', '=', true)
76
75
  * ).as('favorite_pet')
77
76
  * ])
@@ -100,14 +99,15 @@ export function jsonArrayFrom(expr) {
100
99
  * ```
101
100
  */
102
101
  export function jsonObjectFrom(expr) {
103
- return sql `(select json_object(${sql.join(getJsonObjectArgs(expr.toOperationNode(), 'obj'))}) from ${expr} as obj)`;
102
+ return sql `(select json_object(${sql.join(getMysqlJsonObjectArgs(expr.toOperationNode(), 'obj'))}) from ${expr} as obj)`;
104
103
  }
105
104
  /**
106
105
  * The MySQL `json_object` function.
107
106
  *
108
107
  * NOTE: This helper is only guaranteed to fully work with the built-in `MysqlDialect`.
109
- * While the produced SQL is compatibe with all MySQL databases, some 3rd party dialects
110
- * may not parse the nested results into objects.
108
+ * While the produced SQL is compatible with all MySQL databases, some 3rd party dialects
109
+ * may not parse the nested JSON into objects. In these cases you can use the built in
110
+ * `ParseJSONResultsPlugin` to parse the results.
111
111
  *
112
112
  * ### Examples
113
113
  *
@@ -144,22 +144,11 @@ export function jsonObjectFrom(expr) {
144
144
  export function jsonBuildObject(obj) {
145
145
  return sql `json_object(${sql.join(Object.keys(obj).flatMap((k) => [sql.lit(k), obj[k]]))})`;
146
146
  }
147
- function getJsonObjectArgs(node, table) {
148
- return node.selections.flatMap(({ selection: s }) => {
149
- if (ReferenceNode.is(s) && ColumnNode.is(s.column)) {
150
- return [
151
- sql.lit(s.column.column.name),
152
- sql.id(table, s.column.column.name),
153
- ];
154
- }
155
- else if (ColumnNode.is(s)) {
156
- return [sql.lit(s.column.name), sql.id(table, s.column.name)];
157
- }
158
- else if (AliasNode.is(s) && IdentifierNode.is(s.alias)) {
159
- return [sql.lit(s.alias.name), sql.id(table, s.alias.name)];
160
- }
161
- else {
162
- throw new Error('MySQL jsonArrayFrom and jsonObjectFrom functions can only handle explicit selections due to limitations of the json_object function. selectAll() is not allowed in the subquery.');
163
- }
164
- });
147
+ function getMysqlJsonObjectArgs(node, table) {
148
+ try {
149
+ return getJsonObjectArgs(node, table);
150
+ }
151
+ catch {
152
+ throw new Error('MySQL jsonArrayFrom and jsonObjectFrom functions can only handle explicit selections due to limitations of the json_object function. selectAll() is not allowed in the subquery.');
153
+ }
165
154
  }
@@ -16,7 +16,8 @@ import { Simplify } from '../util/type-utils.js';
16
16
  * `jsonArrayFrom`, are not guaranteed to work with third party dialects. In order for
17
17
  * them to work, the dialect must automatically parse the `json` data type into
18
18
  * javascript JSON values like objects and arrays. Some dialects might simply return
19
- * the data as a JSON string.
19
+ * the data as a JSON string. In these cases you can use the built in `ParseJSONResultsPlugin`
20
+ * to parse the results.
20
21
  *
21
22
  * ```ts
22
23
  * import { jsonArrayFrom } from 'kysely/helpers/postgres'
@@ -64,10 +65,11 @@ export declare function jsonArrayFrom<O>(expr: Expression<O>): RawBuilder<Simpli
64
65
  * use the `jsonObjectFrom` helper to fetch person's favorite pet along with the person's id.
65
66
  *
66
67
  * Please keep in mind that the helpers under the `kysely/helpers` folder, including
67
- * `jsonObjectFrom`, are not guaranteed to work with third party dialects. In order for
68
+ * `jsonObjectFrom`, are not guaranteed to work with 3rd party dialects. In order for
68
69
  * them to work, the dialect must automatically parse the `json` data type into
69
70
  * javascript JSON values like objects and arrays. Some dialects might simply return
70
- * the data as a JSON string.
71
+ * the data as a JSON string. In these cases you can use the built in `ParseJSONResultsPlugin`
72
+ * to parse the results.
71
73
  *
72
74
  * ```ts
73
75
  * import { jsonObjectFrom } from 'kysely/helpers/postgres'
@@ -105,8 +107,9 @@ export declare function jsonObjectFrom<O>(expr: Expression<O>): RawBuilder<Simpl
105
107
  * The PostgreSQL `json_build_object` function.
106
108
  *
107
109
  * NOTE: This helper is only guaranteed to fully work with the built-in `PostgresDialect`.
108
- * While the produced SQL is compatibe with all PostgreSQL databases, some 3rd party dialects
109
- * may not parse the nested results into objects.
110
+ * While the produced SQL is compatible with all PostgreSQL databases, some 3rd party dialects
111
+ * may not parse the nested JSON into objects. In these cases you can use the built in
112
+ * `ParseJSONResultsPlugin` to parse the results.
110
113
  *
111
114
  * ### Examples
112
115
  *
@@ -15,7 +15,8 @@ import { sql } from '../raw-builder/sql.js';
15
15
  * `jsonArrayFrom`, are not guaranteed to work with third party dialects. In order for
16
16
  * them to work, the dialect must automatically parse the `json` data type into
17
17
  * javascript JSON values like objects and arrays. Some dialects might simply return
18
- * the data as a JSON string.
18
+ * the data as a JSON string. In these cases you can use the built in `ParseJSONResultsPlugin`
19
+ * to parse the results.
19
20
  *
20
21
  * ```ts
21
22
  * import { jsonArrayFrom } from 'kysely/helpers/postgres'
@@ -65,10 +66,11 @@ export function jsonArrayFrom(expr) {
65
66
  * use the `jsonObjectFrom` helper to fetch person's favorite pet along with the person's id.
66
67
  *
67
68
  * Please keep in mind that the helpers under the `kysely/helpers` folder, including
68
- * `jsonObjectFrom`, are not guaranteed to work with third party dialects. In order for
69
+ * `jsonObjectFrom`, are not guaranteed to work with 3rd party dialects. In order for
69
70
  * them to work, the dialect must automatically parse the `json` data type into
70
71
  * javascript JSON values like objects and arrays. Some dialects might simply return
71
- * the data as a JSON string.
72
+ * the data as a JSON string. In these cases you can use the built in `ParseJSONResultsPlugin`
73
+ * to parse the results.
72
74
  *
73
75
  * ```ts
74
76
  * import { jsonObjectFrom } from 'kysely/helpers/postgres'
@@ -108,8 +110,9 @@ export function jsonObjectFrom(expr) {
108
110
  * The PostgreSQL `json_build_object` function.
109
111
  *
110
112
  * NOTE: This helper is only guaranteed to fully work with the built-in `PostgresDialect`.
111
- * While the produced SQL is compatibe with all PostgreSQL databases, some 3rd party dialects
112
- * may not parse the nested results into objects.
113
+ * While the produced SQL is compatible with all PostgreSQL databases, some 3rd party dialects
114
+ * may not parse the nested JSON into objects. In these cases you can use the built in
115
+ * `ParseJSONResultsPlugin` to parse the results.
113
116
  *
114
117
  * ### Examples
115
118
  *