kysely 0.24.0 → 0.24.1

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.
@@ -0,0 +1,140 @@
1
+ import { Expression } from '../expression/expression.js';
2
+ import { SelectQueryBuilder } from '../query-builder/select-query-builder.js';
3
+ import { RawBuilder } from '../raw-builder/raw-builder.js';
4
+ import { Simplify } from '../util/type-utils.js';
5
+ /**
6
+ * A MySQL helper for aggregating a subquery (or other expression) into a JSON array.
7
+ *
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.
11
+ *
12
+ * ### Examples
13
+ *
14
+ * ```ts
15
+ * const result = await db
16
+ * .selectFrom('person')
17
+ * .select((eb) => [
18
+ * 'id',
19
+ * jsonArrayFrom(
20
+ * eb.selectFrom('pet')
21
+ * .select(['pet.id as pet_id', 'pet.name'])
22
+ * .where('pet.owner_id', '=', 'person.id')
23
+ * .orderBy('pet.name')
24
+ * ).as('pets')
25
+ * ])
26
+ * .execute()
27
+ *
28
+ * result[0].id
29
+ * result[0].pets[0].pet_id
30
+ * result[0].pets[0].name
31
+ * ```
32
+ *
33
+ * The generated SQL (MySQL):
34
+ *
35
+ * ```sql
36
+ * select `id`, (
37
+ * select cast(coalesce(json_arrayagg(json_object(
38
+ * 'pet_id', `agg`.`pet_id`,
39
+ * 'name', `agg`.`name`
40
+ * )), '[]') as json) from (
41
+ * select `pet`.`id` as `pet_id`, `pet`.`name`
42
+ * from `pet`
43
+ * where `pet`.`owner_id` = `person`.`id`
44
+ * order by `pet`.`name`
45
+ * ) as `agg`
46
+ * ) as `pets`
47
+ * from `person`
48
+ * ```
49
+ */
50
+ export declare function jsonArrayFrom<O>(expr: SelectQueryBuilder<any, any, O>): RawBuilder<Simplify<O>[]>;
51
+ /**
52
+ * A MySQL helper for turning a subquery (or other expression) into a JSON object.
53
+ *
54
+ * The subquery must only return one row.
55
+ *
56
+ * 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.
59
+ *
60
+ * ### Examples
61
+ *
62
+ * ```ts
63
+ * const result = await db
64
+ * .selectFrom('person')
65
+ * .select((eb) => [
66
+ * 'id',
67
+ * jsonObjectFrom(
68
+ * eb.selectFrom('pet')
69
+ * .select(['pet.id as pet_id', 'pet.name'])
70
+ * .where('pet.owner_id', '=', 'person.id')
71
+ * .where('pet.is_favorite', '=', true)
72
+ * ).as('favorite_pet')
73
+ * ])
74
+ * .execute()
75
+ *
76
+ * result[0].id
77
+ * result[0].favorite_pet.pet_id
78
+ * result[0].favorite_pet.name
79
+ * ```
80
+ *
81
+ * The generated SQL (MySQL):
82
+ *
83
+ * ```sql
84
+ * select `id`, (
85
+ * select json_object(
86
+ * 'pet_id', `obj`.`pet_id`,
87
+ * 'name', `obj`.`name`
88
+ * ) from (
89
+ * select `pet`.`id` as `pet_id`, `pet`.`name`
90
+ * from `pet`
91
+ * where `pet`.`owner_id` = `person`.`id`
92
+ * and `pet`.`is_favorite` = ?
93
+ * ) as obj
94
+ * ) as `favorite_pet`
95
+ * from `person`
96
+ * ```
97
+ */
98
+ export declare function jsonObjectFrom<O>(expr: SelectQueryBuilder<any, any, O>): RawBuilder<Simplify<O>>;
99
+ /**
100
+ * The MySQL `json_object` function.
101
+ *
102
+ * 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
+ *
106
+ * ### Examples
107
+ *
108
+ * ```ts
109
+ * const result = await db
110
+ * .selectFrom('person')
111
+ * .select((eb) => [
112
+ * 'id',
113
+ * jsonBuildObject({
114
+ * first: eb.ref('first_name'),
115
+ * last: eb.ref('last_name'),
116
+ * full: eb.fn('concat', ['first_name', eb.val(' '), 'last_name'])
117
+ * }).as('name')
118
+ * ])
119
+ * .execute()
120
+ *
121
+ * result[0].id
122
+ * result[0].name.first
123
+ * result[0].name.last
124
+ * result[0].name.full
125
+ * ```
126
+ *
127
+ * The generated SQL (MySQL):
128
+ *
129
+ * ```sql
130
+ * select "id", json_object(
131
+ * 'first', first_name,
132
+ * 'last', last_name,
133
+ * 'full', concat(`first_name`, ?, `last_name`)
134
+ * ) as "name"
135
+ * from "person"
136
+ * ```
137
+ */
138
+ export declare function jsonBuildObject<O extends Record<string, Expression<unknown>>>(obj: O): RawBuilder<Simplify<{
139
+ [K in keyof O]: O[K] extends Expression<infer V> ? V : never;
140
+ }>>;
@@ -0,0 +1,170 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.jsonBuildObject = exports.jsonObjectFrom = exports.jsonArrayFrom = void 0;
4
+ const alias_node_js_1 = require("../operation-node/alias-node.js");
5
+ const column_node_js_1 = require("../operation-node/column-node.js");
6
+ const identifier_node_js_1 = require("../operation-node/identifier-node.js");
7
+ const reference_node_js_1 = require("../operation-node/reference-node.js");
8
+ const sql_js_1 = require("../raw-builder/sql.js");
9
+ /**
10
+ * A MySQL helper for aggregating a subquery (or other expression) into a JSON array.
11
+ *
12
+ * NOTE: This helper is only guaranteed to fully work with the built-in `MysqlDialect`.
13
+ * While the produced SQL is compatibe with all MySQL databases, some 3rd party dialects
14
+ * may not parse the nested results into arrays.
15
+ *
16
+ * ### Examples
17
+ *
18
+ * ```ts
19
+ * const result = await db
20
+ * .selectFrom('person')
21
+ * .select((eb) => [
22
+ * 'id',
23
+ * jsonArrayFrom(
24
+ * eb.selectFrom('pet')
25
+ * .select(['pet.id as pet_id', 'pet.name'])
26
+ * .where('pet.owner_id', '=', 'person.id')
27
+ * .orderBy('pet.name')
28
+ * ).as('pets')
29
+ * ])
30
+ * .execute()
31
+ *
32
+ * result[0].id
33
+ * result[0].pets[0].pet_id
34
+ * result[0].pets[0].name
35
+ * ```
36
+ *
37
+ * The generated SQL (MySQL):
38
+ *
39
+ * ```sql
40
+ * select `id`, (
41
+ * select cast(coalesce(json_arrayagg(json_object(
42
+ * 'pet_id', `agg`.`pet_id`,
43
+ * 'name', `agg`.`name`
44
+ * )), '[]') as json) from (
45
+ * select `pet`.`id` as `pet_id`, `pet`.`name`
46
+ * from `pet`
47
+ * where `pet`.`owner_id` = `person`.`id`
48
+ * order by `pet`.`name`
49
+ * ) as `agg`
50
+ * ) as `pets`
51
+ * from `person`
52
+ * ```
53
+ */
54
+ function jsonArrayFrom(expr) {
55
+ return (0, sql_js_1.sql) `(select cast(coalesce(json_arrayagg(json_object(${sql_js_1.sql.join(getJsonObjectArgs(expr.toOperationNode(), 'agg'))})), '[]') as json) from ${expr} as agg)`;
56
+ }
57
+ exports.jsonArrayFrom = jsonArrayFrom;
58
+ /**
59
+ * A MySQL helper for turning a subquery (or other expression) into a JSON object.
60
+ *
61
+ * The subquery must only return one row.
62
+ *
63
+ * NOTE: This helper is only guaranteed to fully work with the built-in `MysqlDialect`.
64
+ * While the produced SQL is compatibe with all MySQL databases, some 3rd party dialects
65
+ * may not parse the nested results into objects.
66
+ *
67
+ * ### Examples
68
+ *
69
+ * ```ts
70
+ * const result = await db
71
+ * .selectFrom('person')
72
+ * .select((eb) => [
73
+ * 'id',
74
+ * jsonObjectFrom(
75
+ * eb.selectFrom('pet')
76
+ * .select(['pet.id as pet_id', 'pet.name'])
77
+ * .where('pet.owner_id', '=', 'person.id')
78
+ * .where('pet.is_favorite', '=', true)
79
+ * ).as('favorite_pet')
80
+ * ])
81
+ * .execute()
82
+ *
83
+ * result[0].id
84
+ * result[0].favorite_pet.pet_id
85
+ * result[0].favorite_pet.name
86
+ * ```
87
+ *
88
+ * The generated SQL (MySQL):
89
+ *
90
+ * ```sql
91
+ * select `id`, (
92
+ * select json_object(
93
+ * 'pet_id', `obj`.`pet_id`,
94
+ * 'name', `obj`.`name`
95
+ * ) from (
96
+ * select `pet`.`id` as `pet_id`, `pet`.`name`
97
+ * from `pet`
98
+ * where `pet`.`owner_id` = `person`.`id`
99
+ * and `pet`.`is_favorite` = ?
100
+ * ) as obj
101
+ * ) as `favorite_pet`
102
+ * from `person`
103
+ * ```
104
+ */
105
+ function jsonObjectFrom(expr) {
106
+ return (0, sql_js_1.sql) `(select json_object(${sql_js_1.sql.join(getJsonObjectArgs(expr.toOperationNode(), 'obj'))}) from ${expr} as obj)`;
107
+ }
108
+ exports.jsonObjectFrom = jsonObjectFrom;
109
+ /**
110
+ * The MySQL `json_object` function.
111
+ *
112
+ * NOTE: This helper is only guaranteed to fully work with the built-in `MysqlDialect`.
113
+ * While the produced SQL is compatibe with all MySQL databases, some 3rd party dialects
114
+ * may not parse the nested results into objects.
115
+ *
116
+ * ### Examples
117
+ *
118
+ * ```ts
119
+ * const result = await db
120
+ * .selectFrom('person')
121
+ * .select((eb) => [
122
+ * 'id',
123
+ * jsonBuildObject({
124
+ * first: eb.ref('first_name'),
125
+ * last: eb.ref('last_name'),
126
+ * full: eb.fn('concat', ['first_name', eb.val(' '), 'last_name'])
127
+ * }).as('name')
128
+ * ])
129
+ * .execute()
130
+ *
131
+ * result[0].id
132
+ * result[0].name.first
133
+ * result[0].name.last
134
+ * result[0].name.full
135
+ * ```
136
+ *
137
+ * The generated SQL (MySQL):
138
+ *
139
+ * ```sql
140
+ * select "id", json_object(
141
+ * 'first', first_name,
142
+ * 'last', last_name,
143
+ * 'full', concat(`first_name`, ?, `last_name`)
144
+ * ) as "name"
145
+ * from "person"
146
+ * ```
147
+ */
148
+ function jsonBuildObject(obj) {
149
+ return (0, sql_js_1.sql) `json_object(${sql_js_1.sql.join(Object.keys(obj).flatMap((k) => [sql_js_1.sql.lit(k), obj[k]]))})`;
150
+ }
151
+ exports.jsonBuildObject = jsonBuildObject;
152
+ function getJsonObjectArgs(node, table) {
153
+ return node.selections.flatMap(({ selection: s }) => {
154
+ if (reference_node_js_1.ReferenceNode.is(s) && column_node_js_1.ColumnNode.is(s.column)) {
155
+ return [
156
+ sql_js_1.sql.lit(s.column.column.name),
157
+ sql_js_1.sql.id(table, s.column.column.name),
158
+ ];
159
+ }
160
+ else if (column_node_js_1.ColumnNode.is(s)) {
161
+ return [sql_js_1.sql.lit(s.column.name), sql_js_1.sql.id(table, s.column.name)];
162
+ }
163
+ else if (alias_node_js_1.AliasNode.is(s) && identifier_node_js_1.IdentifierNode.is(s.alias)) {
164
+ return [sql_js_1.sql.lit(s.alias.name), sql_js_1.sql.id(table, s.alias.name)];
165
+ }
166
+ else {
167
+ 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.');
168
+ }
169
+ });
170
+ }
@@ -33,7 +33,7 @@ import { Simplify } from '../util/type-utils.js';
33
33
  *
34
34
  * ```sql
35
35
  * select "id", (
36
- * select coalesce(jsonb_agg(agg), '[]') from (
36
+ * select coalesce(json_agg(agg), '[]') from (
37
37
  * select "pet"."id" as "pet_id", "pet"."name"
38
38
  * from "pet"
39
39
  * where "pet"."owner_id" = "person"."id"
@@ -45,7 +45,7 @@ import { Simplify } from '../util/type-utils.js';
45
45
  */
46
46
  export declare function jsonArrayFrom<O>(expr: Expression<O>): RawBuilder<Simplify<O>[]>;
47
47
  /**
48
- * A postgres helper for turning a subquery (or other expression) into a JSONB object.
48
+ * A postgres helper for turning a subquery (or other expression) into a JSON object.
49
49
  *
50
50
  * The subquery must only return one row.
51
51
  *
@@ -78,7 +78,7 @@ export declare function jsonArrayFrom<O>(expr: Expression<O>): RawBuilder<Simpli
78
78
  *
79
79
  * ```sql
80
80
  * select "id", (
81
- * select to_jsonb(obj) from (
81
+ * select to_json(obj) from (
82
82
  * select "pet"."id" as "pet_id", "pet"."name"
83
83
  * from "pet"
84
84
  * where "pet"."owner_id" = "person"."id"
@@ -90,7 +90,7 @@ export declare function jsonArrayFrom<O>(expr: Expression<O>): RawBuilder<Simpli
90
90
  */
91
91
  export declare function jsonObjectFrom<O>(expr: Expression<O>): RawBuilder<Simplify<O>>;
92
92
  /**
93
- * The PostgreSQL `jsonb_build_object` function.
93
+ * The PostgreSQL `json_build_object` function.
94
94
  *
95
95
  * NOTE: This helper is only guaranteed to fully work with the built-in `PostgresDialect`.
96
96
  * While the produced SQL is compatibe with all PostgreSQL databases, some 3rd party dialects
@@ -103,7 +103,7 @@ export declare function jsonObjectFrom<O>(expr: Expression<O>): RawBuilder<Simpl
103
103
  * .selectFrom('person')
104
104
  * .select((eb) => [
105
105
  * 'id',
106
- * jsonbBuildObject({
106
+ * jsonBuildObject({
107
107
  * first: eb.ref('first_name'),
108
108
  * last: eb.ref('last_name'),
109
109
  * full: sql<string>`first_name || ' ' || last_name`
@@ -120,7 +120,7 @@ export declare function jsonObjectFrom<O>(expr: Expression<O>): RawBuilder<Simpl
120
120
  * The generated SQL (PostgreSQL):
121
121
  *
122
122
  * ```sql
123
- * select "id", jsonb_build_object(
123
+ * select "id", json_build_object(
124
124
  * 'first', first_name,
125
125
  * 'last', last_name,
126
126
  * 'full', first_name || ' ' || last_name
@@ -128,6 +128,6 @@ export declare function jsonObjectFrom<O>(expr: Expression<O>): RawBuilder<Simpl
128
128
  * from "person"
129
129
  * ```
130
130
  */
131
- export declare function jsonbBuildObject<O extends Record<string, Expression<unknown>>>(obj: O): RawBuilder<Simplify<{
131
+ export declare function jsonBuildObject<O extends Record<string, Expression<unknown>>>(obj: O): RawBuilder<Simplify<{
132
132
  [K in keyof O]: O[K] extends Expression<infer V> ? V : never;
133
133
  }>>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.jsonbBuildObject = exports.jsonObjectFrom = exports.jsonArrayFrom = void 0;
3
+ exports.jsonBuildObject = exports.jsonObjectFrom = exports.jsonArrayFrom = void 0;
4
4
  const sql_js_1 = require("../raw-builder/sql.js");
5
5
  /**
6
6
  * A postgres helper for aggregating a subquery (or other expression) into a JSONB array.
@@ -34,7 +34,7 @@ const sql_js_1 = require("../raw-builder/sql.js");
34
34
  *
35
35
  * ```sql
36
36
  * select "id", (
37
- * select coalesce(jsonb_agg(agg), '[]') from (
37
+ * select coalesce(json_agg(agg), '[]') from (
38
38
  * select "pet"."id" as "pet_id", "pet"."name"
39
39
  * from "pet"
40
40
  * where "pet"."owner_id" = "person"."id"
@@ -45,11 +45,11 @@ const sql_js_1 = require("../raw-builder/sql.js");
45
45
  * ```
46
46
  */
47
47
  function jsonArrayFrom(expr) {
48
- return (0, sql_js_1.sql) `(select coalesce(jsonb_agg(agg), '[]') from ${expr} as agg)`;
48
+ return (0, sql_js_1.sql) `(select coalesce(json_agg(agg), '[]') from ${expr} as agg)`;
49
49
  }
50
50
  exports.jsonArrayFrom = jsonArrayFrom;
51
51
  /**
52
- * A postgres helper for turning a subquery (or other expression) into a JSONB object.
52
+ * A postgres helper for turning a subquery (or other expression) into a JSON object.
53
53
  *
54
54
  * The subquery must only return one row.
55
55
  *
@@ -82,7 +82,7 @@ exports.jsonArrayFrom = jsonArrayFrom;
82
82
  *
83
83
  * ```sql
84
84
  * select "id", (
85
- * select to_jsonb(obj) from (
85
+ * select to_json(obj) from (
86
86
  * select "pet"."id" as "pet_id", "pet"."name"
87
87
  * from "pet"
88
88
  * where "pet"."owner_id" = "person"."id"
@@ -93,11 +93,11 @@ exports.jsonArrayFrom = jsonArrayFrom;
93
93
  * ```
94
94
  */
95
95
  function jsonObjectFrom(expr) {
96
- return (0, sql_js_1.sql) `(select to_jsonb(obj) from ${expr} as obj)`;
96
+ return (0, sql_js_1.sql) `(select to_json(obj) from ${expr} as obj)`;
97
97
  }
98
98
  exports.jsonObjectFrom = jsonObjectFrom;
99
99
  /**
100
- * The PostgreSQL `jsonb_build_object` function.
100
+ * The PostgreSQL `json_build_object` function.
101
101
  *
102
102
  * NOTE: This helper is only guaranteed to fully work with the built-in `PostgresDialect`.
103
103
  * While the produced SQL is compatibe with all PostgreSQL databases, some 3rd party dialects
@@ -110,7 +110,7 @@ exports.jsonObjectFrom = jsonObjectFrom;
110
110
  * .selectFrom('person')
111
111
  * .select((eb) => [
112
112
  * 'id',
113
- * jsonbBuildObject({
113
+ * jsonBuildObject({
114
114
  * first: eb.ref('first_name'),
115
115
  * last: eb.ref('last_name'),
116
116
  * full: sql<string>`first_name || ' ' || last_name`
@@ -127,7 +127,7 @@ exports.jsonObjectFrom = jsonObjectFrom;
127
127
  * The generated SQL (PostgreSQL):
128
128
  *
129
129
  * ```sql
130
- * select "id", jsonb_build_object(
130
+ * select "id", json_build_object(
131
131
  * 'first', first_name,
132
132
  * 'last', last_name,
133
133
  * 'full', first_name || ' ' || last_name
@@ -135,7 +135,7 @@ exports.jsonObjectFrom = jsonObjectFrom;
135
135
  * from "person"
136
136
  * ```
137
137
  */
138
- function jsonbBuildObject(obj) {
139
- return (0, sql_js_1.sql) `jsonb_build_object(${sql_js_1.sql.join(Object.keys(obj).flatMap((k) => [sql_js_1.sql.lit(k), obj[k]]))})`;
138
+ function jsonBuildObject(obj) {
139
+ return (0, sql_js_1.sql) `json_build_object(${sql_js_1.sql.join(Object.keys(obj).flatMap((k) => [sql_js_1.sql.lit(k), obj[k]]))})`;
140
140
  }
141
- exports.jsonbBuildObject = jsonbBuildObject;
141
+ exports.jsonBuildObject = jsonBuildObject;
@@ -471,7 +471,7 @@ export declare class UpdateQueryBuilder<DB, UT extends keyof DB, TB extends keyo
471
471
  * any expressions such as raw {@link sql} snippets or select queries.
472
472
  *
473
473
  * This method also accepts a callback that returns the update object. The
474
- * callback takes an instance of `{@link ExpressionBuilder} as its only argument.
474
+ * callback takes an instance of {@link ExpressionBuilder} as its only argument.
475
475
  * The expression builder can be used to create arbitrary update expressions.
476
476
  *
477
477
  * The return value of an update query is an instance of {@link UpdateResult}.
@@ -0,0 +1,140 @@
1
+ import { Expression } from '../expression/expression.js';
2
+ import { SelectQueryBuilder } from '../query-builder/select-query-builder.js';
3
+ import { RawBuilder } from '../raw-builder/raw-builder.js';
4
+ import { Simplify } from '../util/type-utils.js';
5
+ /**
6
+ * A MySQL helper for aggregating a subquery (or other expression) into a JSON array.
7
+ *
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.
11
+ *
12
+ * ### Examples
13
+ *
14
+ * ```ts
15
+ * const result = await db
16
+ * .selectFrom('person')
17
+ * .select((eb) => [
18
+ * 'id',
19
+ * jsonArrayFrom(
20
+ * eb.selectFrom('pet')
21
+ * .select(['pet.id as pet_id', 'pet.name'])
22
+ * .where('pet.owner_id', '=', 'person.id')
23
+ * .orderBy('pet.name')
24
+ * ).as('pets')
25
+ * ])
26
+ * .execute()
27
+ *
28
+ * result[0].id
29
+ * result[0].pets[0].pet_id
30
+ * result[0].pets[0].name
31
+ * ```
32
+ *
33
+ * The generated SQL (MySQL):
34
+ *
35
+ * ```sql
36
+ * select `id`, (
37
+ * select cast(coalesce(json_arrayagg(json_object(
38
+ * 'pet_id', `agg`.`pet_id`,
39
+ * 'name', `agg`.`name`
40
+ * )), '[]') as json) from (
41
+ * select `pet`.`id` as `pet_id`, `pet`.`name`
42
+ * from `pet`
43
+ * where `pet`.`owner_id` = `person`.`id`
44
+ * order by `pet`.`name`
45
+ * ) as `agg`
46
+ * ) as `pets`
47
+ * from `person`
48
+ * ```
49
+ */
50
+ export declare function jsonArrayFrom<O>(expr: SelectQueryBuilder<any, any, O>): RawBuilder<Simplify<O>[]>;
51
+ /**
52
+ * A MySQL helper for turning a subquery (or other expression) into a JSON object.
53
+ *
54
+ * The subquery must only return one row.
55
+ *
56
+ * 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.
59
+ *
60
+ * ### Examples
61
+ *
62
+ * ```ts
63
+ * const result = await db
64
+ * .selectFrom('person')
65
+ * .select((eb) => [
66
+ * 'id',
67
+ * jsonObjectFrom(
68
+ * eb.selectFrom('pet')
69
+ * .select(['pet.id as pet_id', 'pet.name'])
70
+ * .where('pet.owner_id', '=', 'person.id')
71
+ * .where('pet.is_favorite', '=', true)
72
+ * ).as('favorite_pet')
73
+ * ])
74
+ * .execute()
75
+ *
76
+ * result[0].id
77
+ * result[0].favorite_pet.pet_id
78
+ * result[0].favorite_pet.name
79
+ * ```
80
+ *
81
+ * The generated SQL (MySQL):
82
+ *
83
+ * ```sql
84
+ * select `id`, (
85
+ * select json_object(
86
+ * 'pet_id', `obj`.`pet_id`,
87
+ * 'name', `obj`.`name`
88
+ * ) from (
89
+ * select `pet`.`id` as `pet_id`, `pet`.`name`
90
+ * from `pet`
91
+ * where `pet`.`owner_id` = `person`.`id`
92
+ * and `pet`.`is_favorite` = ?
93
+ * ) as obj
94
+ * ) as `favorite_pet`
95
+ * from `person`
96
+ * ```
97
+ */
98
+ export declare function jsonObjectFrom<O>(expr: SelectQueryBuilder<any, any, O>): RawBuilder<Simplify<O>>;
99
+ /**
100
+ * The MySQL `json_object` function.
101
+ *
102
+ * 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
+ *
106
+ * ### Examples
107
+ *
108
+ * ```ts
109
+ * const result = await db
110
+ * .selectFrom('person')
111
+ * .select((eb) => [
112
+ * 'id',
113
+ * jsonBuildObject({
114
+ * first: eb.ref('first_name'),
115
+ * last: eb.ref('last_name'),
116
+ * full: eb.fn('concat', ['first_name', eb.val(' '), 'last_name'])
117
+ * }).as('name')
118
+ * ])
119
+ * .execute()
120
+ *
121
+ * result[0].id
122
+ * result[0].name.first
123
+ * result[0].name.last
124
+ * result[0].name.full
125
+ * ```
126
+ *
127
+ * The generated SQL (MySQL):
128
+ *
129
+ * ```sql
130
+ * select "id", json_object(
131
+ * 'first', first_name,
132
+ * 'last', last_name,
133
+ * 'full', concat(`first_name`, ?, `last_name`)
134
+ * ) as "name"
135
+ * from "person"
136
+ * ```
137
+ */
138
+ export declare function jsonBuildObject<O extends Record<string, Expression<unknown>>>(obj: O): RawBuilder<Simplify<{
139
+ [K in keyof O]: O[K] extends Expression<infer V> ? V : never;
140
+ }>>;
@@ -0,0 +1,165 @@
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
+ import { sql } from '../raw-builder/sql.js';
7
+ /**
8
+ * A MySQL helper for aggregating a subquery (or other expression) into a JSON array.
9
+ *
10
+ * 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.
13
+ *
14
+ * ### Examples
15
+ *
16
+ * ```ts
17
+ * const result = await db
18
+ * .selectFrom('person')
19
+ * .select((eb) => [
20
+ * 'id',
21
+ * jsonArrayFrom(
22
+ * eb.selectFrom('pet')
23
+ * .select(['pet.id as pet_id', 'pet.name'])
24
+ * .where('pet.owner_id', '=', 'person.id')
25
+ * .orderBy('pet.name')
26
+ * ).as('pets')
27
+ * ])
28
+ * .execute()
29
+ *
30
+ * result[0].id
31
+ * result[0].pets[0].pet_id
32
+ * result[0].pets[0].name
33
+ * ```
34
+ *
35
+ * The generated SQL (MySQL):
36
+ *
37
+ * ```sql
38
+ * select `id`, (
39
+ * select cast(coalesce(json_arrayagg(json_object(
40
+ * 'pet_id', `agg`.`pet_id`,
41
+ * 'name', `agg`.`name`
42
+ * )), '[]') as json) from (
43
+ * select `pet`.`id` as `pet_id`, `pet`.`name`
44
+ * from `pet`
45
+ * where `pet`.`owner_id` = `person`.`id`
46
+ * order by `pet`.`name`
47
+ * ) as `agg`
48
+ * ) as `pets`
49
+ * from `person`
50
+ * ```
51
+ */
52
+ 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)`;
54
+ }
55
+ /**
56
+ * A MySQL helper for turning a subquery (or other expression) into a JSON object.
57
+ *
58
+ * The subquery must only return one row.
59
+ *
60
+ * 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.
63
+ *
64
+ * ### Examples
65
+ *
66
+ * ```ts
67
+ * const result = await db
68
+ * .selectFrom('person')
69
+ * .select((eb) => [
70
+ * 'id',
71
+ * jsonObjectFrom(
72
+ * eb.selectFrom('pet')
73
+ * .select(['pet.id as pet_id', 'pet.name'])
74
+ * .where('pet.owner_id', '=', 'person.id')
75
+ * .where('pet.is_favorite', '=', true)
76
+ * ).as('favorite_pet')
77
+ * ])
78
+ * .execute()
79
+ *
80
+ * result[0].id
81
+ * result[0].favorite_pet.pet_id
82
+ * result[0].favorite_pet.name
83
+ * ```
84
+ *
85
+ * The generated SQL (MySQL):
86
+ *
87
+ * ```sql
88
+ * select `id`, (
89
+ * select json_object(
90
+ * 'pet_id', `obj`.`pet_id`,
91
+ * 'name', `obj`.`name`
92
+ * ) from (
93
+ * select `pet`.`id` as `pet_id`, `pet`.`name`
94
+ * from `pet`
95
+ * where `pet`.`owner_id` = `person`.`id`
96
+ * and `pet`.`is_favorite` = ?
97
+ * ) as obj
98
+ * ) as `favorite_pet`
99
+ * from `person`
100
+ * ```
101
+ */
102
+ export function jsonObjectFrom(expr) {
103
+ return sql `(select json_object(${sql.join(getJsonObjectArgs(expr.toOperationNode(), 'obj'))}) from ${expr} as obj)`;
104
+ }
105
+ /**
106
+ * The MySQL `json_object` function.
107
+ *
108
+ * 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.
111
+ *
112
+ * ### Examples
113
+ *
114
+ * ```ts
115
+ * const result = await db
116
+ * .selectFrom('person')
117
+ * .select((eb) => [
118
+ * 'id',
119
+ * jsonBuildObject({
120
+ * first: eb.ref('first_name'),
121
+ * last: eb.ref('last_name'),
122
+ * full: eb.fn('concat', ['first_name', eb.val(' '), 'last_name'])
123
+ * }).as('name')
124
+ * ])
125
+ * .execute()
126
+ *
127
+ * result[0].id
128
+ * result[0].name.first
129
+ * result[0].name.last
130
+ * result[0].name.full
131
+ * ```
132
+ *
133
+ * The generated SQL (MySQL):
134
+ *
135
+ * ```sql
136
+ * select "id", json_object(
137
+ * 'first', first_name,
138
+ * 'last', last_name,
139
+ * 'full', concat(`first_name`, ?, `last_name`)
140
+ * ) as "name"
141
+ * from "person"
142
+ * ```
143
+ */
144
+ export function jsonBuildObject(obj) {
145
+ return sql `json_object(${sql.join(Object.keys(obj).flatMap((k) => [sql.lit(k), obj[k]]))})`;
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
+ });
165
+ }
@@ -33,7 +33,7 @@ import { Simplify } from '../util/type-utils.js';
33
33
  *
34
34
  * ```sql
35
35
  * select "id", (
36
- * select coalesce(jsonb_agg(agg), '[]') from (
36
+ * select coalesce(json_agg(agg), '[]') from (
37
37
  * select "pet"."id" as "pet_id", "pet"."name"
38
38
  * from "pet"
39
39
  * where "pet"."owner_id" = "person"."id"
@@ -45,7 +45,7 @@ import { Simplify } from '../util/type-utils.js';
45
45
  */
46
46
  export declare function jsonArrayFrom<O>(expr: Expression<O>): RawBuilder<Simplify<O>[]>;
47
47
  /**
48
- * A postgres helper for turning a subquery (or other expression) into a JSONB object.
48
+ * A postgres helper for turning a subquery (or other expression) into a JSON object.
49
49
  *
50
50
  * The subquery must only return one row.
51
51
  *
@@ -78,7 +78,7 @@ export declare function jsonArrayFrom<O>(expr: Expression<O>): RawBuilder<Simpli
78
78
  *
79
79
  * ```sql
80
80
  * select "id", (
81
- * select to_jsonb(obj) from (
81
+ * select to_json(obj) from (
82
82
  * select "pet"."id" as "pet_id", "pet"."name"
83
83
  * from "pet"
84
84
  * where "pet"."owner_id" = "person"."id"
@@ -90,7 +90,7 @@ export declare function jsonArrayFrom<O>(expr: Expression<O>): RawBuilder<Simpli
90
90
  */
91
91
  export declare function jsonObjectFrom<O>(expr: Expression<O>): RawBuilder<Simplify<O>>;
92
92
  /**
93
- * The PostgreSQL `jsonb_build_object` function.
93
+ * The PostgreSQL `json_build_object` function.
94
94
  *
95
95
  * NOTE: This helper is only guaranteed to fully work with the built-in `PostgresDialect`.
96
96
  * While the produced SQL is compatibe with all PostgreSQL databases, some 3rd party dialects
@@ -103,7 +103,7 @@ export declare function jsonObjectFrom<O>(expr: Expression<O>): RawBuilder<Simpl
103
103
  * .selectFrom('person')
104
104
  * .select((eb) => [
105
105
  * 'id',
106
- * jsonbBuildObject({
106
+ * jsonBuildObject({
107
107
  * first: eb.ref('first_name'),
108
108
  * last: eb.ref('last_name'),
109
109
  * full: sql<string>`first_name || ' ' || last_name`
@@ -120,7 +120,7 @@ export declare function jsonObjectFrom<O>(expr: Expression<O>): RawBuilder<Simpl
120
120
  * The generated SQL (PostgreSQL):
121
121
  *
122
122
  * ```sql
123
- * select "id", jsonb_build_object(
123
+ * select "id", json_build_object(
124
124
  * 'first', first_name,
125
125
  * 'last', last_name,
126
126
  * 'full', first_name || ' ' || last_name
@@ -128,6 +128,6 @@ export declare function jsonObjectFrom<O>(expr: Expression<O>): RawBuilder<Simpl
128
128
  * from "person"
129
129
  * ```
130
130
  */
131
- export declare function jsonbBuildObject<O extends Record<string, Expression<unknown>>>(obj: O): RawBuilder<Simplify<{
131
+ export declare function jsonBuildObject<O extends Record<string, Expression<unknown>>>(obj: O): RawBuilder<Simplify<{
132
132
  [K in keyof O]: O[K] extends Expression<infer V> ? V : never;
133
133
  }>>;
@@ -32,7 +32,7 @@ import { sql } from '../raw-builder/sql.js';
32
32
  *
33
33
  * ```sql
34
34
  * select "id", (
35
- * select coalesce(jsonb_agg(agg), '[]') from (
35
+ * select coalesce(json_agg(agg), '[]') from (
36
36
  * select "pet"."id" as "pet_id", "pet"."name"
37
37
  * from "pet"
38
38
  * where "pet"."owner_id" = "person"."id"
@@ -43,10 +43,10 @@ import { sql } from '../raw-builder/sql.js';
43
43
  * ```
44
44
  */
45
45
  export function jsonArrayFrom(expr) {
46
- return sql `(select coalesce(jsonb_agg(agg), '[]') from ${expr} as agg)`;
46
+ return sql `(select coalesce(json_agg(agg), '[]') from ${expr} as agg)`;
47
47
  }
48
48
  /**
49
- * A postgres helper for turning a subquery (or other expression) into a JSONB object.
49
+ * A postgres helper for turning a subquery (or other expression) into a JSON object.
50
50
  *
51
51
  * The subquery must only return one row.
52
52
  *
@@ -79,7 +79,7 @@ export function jsonArrayFrom(expr) {
79
79
  *
80
80
  * ```sql
81
81
  * select "id", (
82
- * select to_jsonb(obj) from (
82
+ * select to_json(obj) from (
83
83
  * select "pet"."id" as "pet_id", "pet"."name"
84
84
  * from "pet"
85
85
  * where "pet"."owner_id" = "person"."id"
@@ -90,10 +90,10 @@ export function jsonArrayFrom(expr) {
90
90
  * ```
91
91
  */
92
92
  export function jsonObjectFrom(expr) {
93
- return sql `(select to_jsonb(obj) from ${expr} as obj)`;
93
+ return sql `(select to_json(obj) from ${expr} as obj)`;
94
94
  }
95
95
  /**
96
- * The PostgreSQL `jsonb_build_object` function.
96
+ * The PostgreSQL `json_build_object` function.
97
97
  *
98
98
  * NOTE: This helper is only guaranteed to fully work with the built-in `PostgresDialect`.
99
99
  * While the produced SQL is compatibe with all PostgreSQL databases, some 3rd party dialects
@@ -106,7 +106,7 @@ export function jsonObjectFrom(expr) {
106
106
  * .selectFrom('person')
107
107
  * .select((eb) => [
108
108
  * 'id',
109
- * jsonbBuildObject({
109
+ * jsonBuildObject({
110
110
  * first: eb.ref('first_name'),
111
111
  * last: eb.ref('last_name'),
112
112
  * full: sql<string>`first_name || ' ' || last_name`
@@ -123,7 +123,7 @@ export function jsonObjectFrom(expr) {
123
123
  * The generated SQL (PostgreSQL):
124
124
  *
125
125
  * ```sql
126
- * select "id", jsonb_build_object(
126
+ * select "id", json_build_object(
127
127
  * 'first', first_name,
128
128
  * 'last', last_name,
129
129
  * 'full', first_name || ' ' || last_name
@@ -131,6 +131,6 @@ export function jsonObjectFrom(expr) {
131
131
  * from "person"
132
132
  * ```
133
133
  */
134
- export function jsonbBuildObject(obj) {
135
- return sql `jsonb_build_object(${sql.join(Object.keys(obj).flatMap((k) => [sql.lit(k), obj[k]]))})`;
134
+ export function jsonBuildObject(obj) {
135
+ return sql `json_build_object(${sql.join(Object.keys(obj).flatMap((k) => [sql.lit(k), obj[k]]))})`;
136
136
  }
@@ -471,7 +471,7 @@ export declare class UpdateQueryBuilder<DB, UT extends keyof DB, TB extends keyo
471
471
  * any expressions such as raw {@link sql} snippets or select queries.
472
472
  *
473
473
  * This method also accepts a callback that returns the update object. The
474
- * callback takes an instance of `{@link ExpressionBuilder} as its only argument.
474
+ * callback takes an instance of {@link ExpressionBuilder} as its only argument.
475
475
  * The expression builder can be used to create arbitrary update expressions.
476
476
  *
477
477
  * The return value of an update query is an instance of {@link UpdateResult}.
@@ -0,0 +1 @@
1
+ export * from '../dist/cjs/helpers/mysql.js'
@@ -0,0 +1 @@
1
+ module.exports = require('../dist/cjs/helpers/mysql.js')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kysely",
3
- "version": "0.24.0",
3
+ "version": "0.24.1",
4
4
  "description": "Type safe SQL query builder",
5
5
  "repository": {
6
6
  "type": "git",
@@ -21,6 +21,11 @@
21
21
  "import": "./dist/esm/helpers/postgres.js",
22
22
  "require": "./dist/cjs/helpers/postgres.js",
23
23
  "default": "./dist/cjs/helpers/postgres.js"
24
+ },
25
+ "./helpers/mysql": {
26
+ "import": "./dist/esm/helpers/mysql.js",
27
+ "require": "./dist/cjs/helpers/mysql.js",
28
+ "default": "./dist/cjs/helpers/mysql.js"
24
29
  }
25
30
  },
26
31
  "scripts": {
@@ -60,6 +65,7 @@
60
65
  "@types/chai-as-promised": "^7.1.5",
61
66
  "@types/chai-subset": "^1.3.3",
62
67
  "@types/mocha": "^9.1.1",
68
+ "@types/node": "^18.15.11",
63
69
  "@types/pg": "^8.6.5",
64
70
  "@types/pg-cursor": "^2.7.0",
65
71
  "@types/sinon": "^10.0.13",