kysely 0.16.4 → 0.16.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +118 -5
- package/dist/cjs/dialect/mysql/mysql-dialect.js +2 -1
- package/dist/cjs/dialect/mysql/mysql-driver.js +1 -1
- package/dist/cjs/dialect/postgres/postgres-dialect.js +2 -1
- package/dist/cjs/dialect/postgres/postgres-driver.js +1 -1
- package/dist/cjs/dialect/sqlite/sqlite-dialect.js +2 -1
- package/dist/cjs/dialect/sqlite/sqlite-driver.js +1 -1
- package/dist/cjs/driver/dummy-driver.d.ts +68 -0
- package/dist/cjs/driver/dummy-driver.js +62 -0
- package/dist/cjs/driver/runtime-driver.d.ts +1 -1
- package/dist/cjs/driver/runtime-driver.js +30 -33
- package/dist/cjs/index-nodeless.d.ts +2 -0
- package/dist/cjs/index-nodeless.js +2 -0
- package/dist/cjs/kysely.d.ts +8 -6
- package/dist/cjs/kysely.js +8 -6
- package/dist/cjs/migration/migrator.d.ts +2 -2
- package/dist/cjs/operation-node/primitive-value-list-node.d.ts +2 -3
- package/dist/cjs/operation-node/value-node.d.ts +3 -4
- package/dist/cjs/parser/complex-expression-parser.d.ts +1 -0
- package/dist/cjs/parser/complex-expression-parser.js +5 -1
- package/dist/cjs/parser/default-value-parser.d.ts +2 -3
- package/dist/cjs/parser/default-value-parser.js +2 -2
- package/dist/cjs/parser/insert-values-parser.js +4 -3
- package/dist/cjs/parser/reference-parser.d.ts +2 -3
- package/dist/cjs/parser/value-parser.d.ts +2 -3
- package/dist/cjs/parser/value-parser.js +6 -7
- package/dist/cjs/plugin/deduplicate-joins/deduplicate-joins-plugin.d.ts +48 -0
- package/dist/cjs/plugin/deduplicate-joins/deduplicate-joins-plugin.js +19 -0
- package/dist/cjs/plugin/deduplicate-joins/deduplicate-joins-transformer.d.ts +9 -0
- package/dist/cjs/plugin/deduplicate-joins/deduplicate-joins-transformer.js +42 -0
- package/dist/cjs/plugin/{immediate-value.ts → immediate-value}/immediate-value-plugin.d.ts +0 -0
- package/dist/cjs/plugin/{immediate-value.ts → immediate-value}/immediate-value-plugin.js +0 -0
- package/dist/cjs/plugin/{immediate-value.ts → immediate-value}/immediate-value-transformer.d.ts +0 -0
- package/dist/cjs/plugin/{immediate-value.ts → immediate-value}/immediate-value-transformer.js +0 -0
- package/dist/cjs/query-builder/delete-query-builder.d.ts +51 -9
- package/dist/cjs/query-builder/delete-query-builder.js +56 -8
- package/dist/cjs/query-builder/expression-builder.d.ts +1 -1
- package/dist/cjs/query-builder/expression-builder.js +2 -2
- package/dist/cjs/query-builder/function-builder.d.ts +29 -5
- package/dist/cjs/query-builder/function-builder.js +31 -7
- package/dist/cjs/query-builder/insert-query-builder.d.ts +53 -11
- package/dist/cjs/query-builder/insert-query-builder.js +58 -10
- package/dist/cjs/query-builder/select-query-builder.d.ts +58 -16
- package/dist/cjs/query-builder/select-query-builder.js +64 -15
- package/dist/cjs/query-builder/update-query-builder.d.ts +52 -9
- package/dist/cjs/query-builder/update-query-builder.js +57 -8
- package/dist/cjs/query-compiler/default-query-compiler.d.ts +1 -2
- package/dist/cjs/query-creator.d.ts +2 -2
- package/dist/cjs/query-creator.js +2 -2
- package/dist/cjs/query-executor/query-executor.js +3 -2
- package/dist/cjs/raw-builder/raw-builder.d.ts +2 -2
- package/dist/cjs/raw-builder/raw-builder.js +8 -1
- package/dist/cjs/schema/alter-table-builder.d.ts +30 -4
- package/dist/cjs/schema/alter-table-builder.js +2 -5
- package/dist/cjs/schema/column-definition-builder.d.ts +27 -2
- package/dist/cjs/schema/create-view-builder.js +1 -1
- package/dist/cjs/util/log.d.ts +1 -0
- package/dist/cjs/util/log.js +3 -0
- package/dist/cjs/util/object-utils.d.ts +5 -5
- package/dist/cjs/util/object-utils.js +52 -11
- package/dist/cjs/util/type-utils.d.ts +21 -0
- package/dist/esm/dialect/mysql/mysql-dialect.js +2 -1
- package/dist/esm/dialect/mysql/mysql-driver.js +2 -2
- package/dist/esm/dialect/postgres/postgres-dialect.js +2 -1
- package/dist/esm/dialect/postgres/postgres-driver.js +2 -2
- package/dist/esm/dialect/sqlite/sqlite-dialect.js +2 -1
- package/dist/esm/dialect/sqlite/sqlite-driver.js +2 -2
- package/dist/esm/driver/dummy-driver.d.ts +68 -0
- package/dist/esm/driver/dummy-driver.js +59 -0
- package/dist/esm/driver/runtime-driver.d.ts +1 -1
- package/dist/esm/driver/runtime-driver.js +29 -32
- package/dist/esm/index-nodeless.d.ts +2 -0
- package/dist/esm/index-nodeless.js +2 -0
- package/dist/esm/kysely.d.ts +8 -6
- package/dist/esm/kysely.js +8 -6
- package/dist/esm/migration/migrator.d.ts +2 -2
- package/dist/esm/operation-node/primitive-value-list-node.d.ts +2 -3
- package/dist/esm/operation-node/value-node.d.ts +3 -4
- package/dist/esm/parser/complex-expression-parser.d.ts +1 -0
- package/dist/esm/parser/complex-expression-parser.js +3 -0
- package/dist/esm/parser/default-value-parser.d.ts +2 -3
- package/dist/esm/parser/default-value-parser.js +2 -2
- package/dist/esm/parser/insert-values-parser.js +5 -4
- package/dist/esm/parser/reference-parser.d.ts +2 -3
- package/dist/esm/parser/reference-parser.js +1 -1
- package/dist/esm/parser/value-parser.d.ts +2 -3
- package/dist/esm/parser/value-parser.js +8 -9
- package/dist/esm/plugin/deduplicate-joins/deduplicate-joins-plugin.d.ts +48 -0
- package/dist/esm/plugin/deduplicate-joins/deduplicate-joins-plugin.js +16 -0
- package/dist/esm/plugin/deduplicate-joins/deduplicate-joins-transformer.d.ts +9 -0
- package/dist/esm/plugin/deduplicate-joins/deduplicate-joins-transformer.js +39 -0
- package/dist/esm/plugin/{immediate-value.ts → immediate-value}/immediate-value-plugin.d.ts +0 -0
- package/dist/esm/plugin/{immediate-value.ts → immediate-value}/immediate-value-plugin.js +0 -0
- package/dist/esm/plugin/{immediate-value.ts → immediate-value}/immediate-value-transformer.d.ts +0 -0
- package/dist/esm/plugin/{immediate-value.ts → immediate-value}/immediate-value-transformer.js +0 -0
- package/dist/esm/query-builder/delete-query-builder.d.ts +51 -9
- package/dist/esm/query-builder/delete-query-builder.js +56 -8
- package/dist/esm/query-builder/expression-builder.d.ts +1 -1
- package/dist/esm/query-builder/expression-builder.js +1 -1
- package/dist/esm/query-builder/function-builder.d.ts +29 -5
- package/dist/esm/query-builder/function-builder.js +31 -7
- package/dist/esm/query-builder/insert-query-builder.d.ts +53 -11
- package/dist/esm/query-builder/insert-query-builder.js +58 -10
- package/dist/esm/query-builder/select-query-builder.d.ts +58 -16
- package/dist/esm/query-builder/select-query-builder.js +64 -15
- package/dist/esm/query-builder/update-query-builder.d.ts +52 -9
- package/dist/esm/query-builder/update-query-builder.js +57 -8
- package/dist/esm/query-compiler/default-query-compiler.d.ts +1 -2
- package/dist/esm/query-creator.d.ts +2 -2
- package/dist/esm/query-creator.js +2 -2
- package/dist/esm/query-executor/query-executor.js +3 -2
- package/dist/esm/raw-builder/raw-builder.d.ts +2 -2
- package/dist/esm/raw-builder/raw-builder.js +8 -1
- package/dist/esm/schema/alter-table-builder.d.ts +30 -4
- package/dist/esm/schema/alter-table-builder.js +2 -5
- package/dist/esm/schema/column-definition-builder.d.ts +27 -2
- package/dist/esm/schema/create-view-builder.js +1 -1
- package/dist/esm/util/log.d.ts +1 -0
- package/dist/esm/util/log.js +3 -0
- package/dist/esm/util/object-utils.d.ts +5 -5
- package/dist/esm/util/object-utils.js +50 -9
- package/dist/esm/util/type-utils.d.ts +21 -0
- package/package.json +11 -5
|
@@ -192,26 +192,72 @@ export class UpdateQueryBuilder {
|
|
|
192
192
|
/**
|
|
193
193
|
* Simply calls the given function passing `this` as the only argument.
|
|
194
194
|
*
|
|
195
|
-
*
|
|
195
|
+
* If you want to conditionally call a method on `this`, see
|
|
196
|
+
* the {@link if} method.
|
|
196
197
|
*
|
|
197
198
|
* ### Examples
|
|
198
199
|
*
|
|
200
|
+
* The next example uses a helper funtion `log` to log a query:
|
|
201
|
+
*
|
|
199
202
|
* ```ts
|
|
203
|
+
* function log<T extends Compilable>(qb: T): T {
|
|
204
|
+
* console.log(qb.compile())
|
|
205
|
+
* return qb
|
|
206
|
+
* }
|
|
207
|
+
*
|
|
200
208
|
* db.updateTable('person')
|
|
201
209
|
* .set(values)
|
|
202
|
-
* .call(
|
|
203
|
-
* if (something) {
|
|
204
|
-
* return qb.where('something', '=', something)
|
|
205
|
-
* } else {
|
|
206
|
-
* return qb.where('somethingElse', '=', somethingElse)
|
|
207
|
-
* }
|
|
208
|
-
* })
|
|
210
|
+
* .call(log)
|
|
209
211
|
* .execute()
|
|
210
212
|
* ```
|
|
211
213
|
*/
|
|
212
214
|
call(func) {
|
|
213
215
|
return func(this);
|
|
214
216
|
}
|
|
217
|
+
/**
|
|
218
|
+
* Call `func(this)` if `condition` is true.
|
|
219
|
+
*
|
|
220
|
+
* This method is especially handy with optional selects. Any `returning` or `returningAll`
|
|
221
|
+
* method calls add columns as optional fields to the output type when called inside
|
|
222
|
+
* the `func` callback. This is because we can't know if those selections were actually
|
|
223
|
+
* made before running the code.
|
|
224
|
+
*
|
|
225
|
+
* You can also call any other methods inside the callback.
|
|
226
|
+
*
|
|
227
|
+
* ### Examples
|
|
228
|
+
*
|
|
229
|
+
* ```ts
|
|
230
|
+
* async function updatePerson(id: number, updates: UpdateablePerson, returnLastName: boolean) {
|
|
231
|
+
* return await db
|
|
232
|
+
* .updateTable('person')
|
|
233
|
+
* .set(updates)
|
|
234
|
+
* .where('id', '=', id)
|
|
235
|
+
* .returning(['id', 'first_name'])
|
|
236
|
+
* .if(returnLastName, (qb) => qb.returning('last_name'))
|
|
237
|
+
* .executeTakeFirstOrThrow()
|
|
238
|
+
* }
|
|
239
|
+
* ```
|
|
240
|
+
*
|
|
241
|
+
* Any selections added inside the `if` callback will be added as optional fields to the
|
|
242
|
+
* output type since we can't know if the selections were actually made before running
|
|
243
|
+
* the code. In the example above the return type of the `updatePerson` function is:
|
|
244
|
+
*
|
|
245
|
+
* ```ts
|
|
246
|
+
* {
|
|
247
|
+
* id: number
|
|
248
|
+
* first_name: string
|
|
249
|
+
* last_name?: string
|
|
250
|
+
* }
|
|
251
|
+
* ```
|
|
252
|
+
*/
|
|
253
|
+
if(condition, func) {
|
|
254
|
+
if (condition) {
|
|
255
|
+
return func(this);
|
|
256
|
+
}
|
|
257
|
+
return new UpdateQueryBuilder({
|
|
258
|
+
...this.#props,
|
|
259
|
+
});
|
|
260
|
+
}
|
|
215
261
|
/**
|
|
216
262
|
* Change the output type of the query.
|
|
217
263
|
*
|
|
@@ -221,6 +267,9 @@ export class UpdateQueryBuilder {
|
|
|
221
267
|
castTo() {
|
|
222
268
|
return new UpdateQueryBuilder(this.#props);
|
|
223
269
|
}
|
|
270
|
+
/**
|
|
271
|
+
* Returns a copy of this UpdateQueryBuilder instance with the given plugin installed.
|
|
272
|
+
*/
|
|
224
273
|
withPlugin(plugin) {
|
|
225
274
|
return new UpdateQueryBuilder({
|
|
226
275
|
...this.#props,
|
|
@@ -44,7 +44,6 @@ import { ValueNode } from '../operation-node/value-node.js';
|
|
|
44
44
|
import { WhereNode } from '../operation-node/where-node.js';
|
|
45
45
|
import { CommonTableExpressionNode } from '../operation-node/common-table-expression-node.js';
|
|
46
46
|
import { WithNode } from '../operation-node/with-node.js';
|
|
47
|
-
import { PrimitiveValue } from '../util/object-utils.js';
|
|
48
47
|
import { CompiledQuery } from './compiled-query.js';
|
|
49
48
|
import { RootOperationNode, QueryCompiler } from './query-compiler.js';
|
|
50
49
|
import { HavingNode } from '../operation-node/having-node.js';
|
|
@@ -150,5 +149,5 @@ export declare class DefaultQueryCompiler extends OperationNodeVisitor implement
|
|
|
150
149
|
protected getRightIdentifierWrapper(): string;
|
|
151
150
|
protected getCurrentParameterPlaceholder(): string;
|
|
152
151
|
protected addParameter(parameter: unknown): void;
|
|
153
|
-
protected appendImmediateValue(value:
|
|
152
|
+
protected appendImmediateValue(value: unknown): void;
|
|
154
153
|
}
|
|
@@ -162,7 +162,7 @@ export declare class QueryCreator<DB> {
|
|
|
162
162
|
/**
|
|
163
163
|
* Creates a delete query.
|
|
164
164
|
*
|
|
165
|
-
* See the {@link
|
|
165
|
+
* See the {@link DeleteQueryBuilder.where} method for examples on how to specify
|
|
166
166
|
* a where clause for the delete operation.
|
|
167
167
|
*
|
|
168
168
|
* The return value of the query is an instance of {@link DeleteResult}.
|
|
@@ -182,7 +182,7 @@ export declare class QueryCreator<DB> {
|
|
|
182
182
|
/**
|
|
183
183
|
* Creates an update query.
|
|
184
184
|
*
|
|
185
|
-
* See the {@link
|
|
185
|
+
* See the {@link UpdateQueryBuilder.where} method for examples on how to specify
|
|
186
186
|
* a where clause for the update operation.
|
|
187
187
|
*
|
|
188
188
|
* See the {@link UpdateQueryBuilder.set} method for examples on how to
|
|
@@ -76,7 +76,7 @@ export class QueryCreator {
|
|
|
76
76
|
/**
|
|
77
77
|
* Creates a delete query.
|
|
78
78
|
*
|
|
79
|
-
* See the {@link
|
|
79
|
+
* See the {@link DeleteQueryBuilder.where} method for examples on how to specify
|
|
80
80
|
* a where clause for the delete operation.
|
|
81
81
|
*
|
|
82
82
|
* The return value of the query is an instance of {@link DeleteResult}.
|
|
@@ -103,7 +103,7 @@ export class QueryCreator {
|
|
|
103
103
|
/**
|
|
104
104
|
* Creates an update query.
|
|
105
105
|
*
|
|
106
|
-
* See the {@link
|
|
106
|
+
* See the {@link UpdateQueryBuilder.where} method for examples on how to specify
|
|
107
107
|
* a where clause for the update operation.
|
|
108
108
|
*
|
|
109
109
|
* See the {@link UpdateQueryBuilder.set} method for examples on how to
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/// <reference types="./query-executor.d.ts" />
|
|
2
2
|
import { freeze } from '../util/object-utils.js';
|
|
3
|
+
const NO_PLUGINS = freeze([]);
|
|
3
4
|
/**
|
|
4
5
|
* This class abstracts away the details of how to compile a query into SQL
|
|
5
6
|
* and execute it. Instead of passing around all those details, {@link SelectQueryBuilder}
|
|
@@ -8,8 +9,8 @@ import { freeze } from '../util/object-utils.js';
|
|
|
8
9
|
*/
|
|
9
10
|
export class QueryExecutor {
|
|
10
11
|
#plugins;
|
|
11
|
-
constructor(plugins
|
|
12
|
-
this.#plugins =
|
|
12
|
+
constructor(plugins) {
|
|
13
|
+
this.#plugins = plugins ?? NO_PLUGINS;
|
|
13
14
|
}
|
|
14
15
|
get plugins() {
|
|
15
16
|
return this.#plugins;
|
|
@@ -40,8 +40,8 @@ export declare class AliasedRawBuilder<O = unknown, A extends string = never> im
|
|
|
40
40
|
constructor(rawBuilder: RawBuilder<O>, alias: A);
|
|
41
41
|
}
|
|
42
42
|
export interface RawBuilderProps {
|
|
43
|
-
readonly queryId
|
|
44
|
-
readonly executor
|
|
43
|
+
readonly queryId?: QueryId;
|
|
44
|
+
readonly executor?: QueryExecutor;
|
|
45
45
|
readonly sql: string;
|
|
46
46
|
readonly parameters?: ReadonlyArray<unknown>;
|
|
47
47
|
}
|
|
@@ -5,11 +5,18 @@ import { RawNode } from '../operation-node/raw-node.js';
|
|
|
5
5
|
import { ValueNode } from '../operation-node/value-node.js';
|
|
6
6
|
import { parseStringReference } from '../parser/reference-parser.js';
|
|
7
7
|
import { preventAwait } from '../util/prevent-await.js';
|
|
8
|
+
import { createQueryId } from '../util/query-id.js';
|
|
8
9
|
import { freeze } from '../util/object-utils.js';
|
|
10
|
+
import { NoopQueryExecutor } from '../query-executor/noop-query-executor.js';
|
|
9
11
|
export class RawBuilder {
|
|
10
12
|
#props;
|
|
11
13
|
constructor(props) {
|
|
12
|
-
this.#props = freeze(
|
|
14
|
+
this.#props = freeze({
|
|
15
|
+
queryId: props.queryId ?? createQueryId(),
|
|
16
|
+
executor: props.executor ?? new NoopQueryExecutor(),
|
|
17
|
+
sql: props.sql,
|
|
18
|
+
parameters: props.parameters,
|
|
19
|
+
});
|
|
13
20
|
}
|
|
14
21
|
as(alias) {
|
|
15
22
|
return new AliasedRawBuilder(this, alias);
|
|
@@ -5,13 +5,13 @@ import { OperationNodeSource } from '../operation-node/operation-node-source.js'
|
|
|
5
5
|
import { OnModifyForeignAction } from '../operation-node/references-node.js';
|
|
6
6
|
import { CompiledQuery } from '../query-compiler/compiled-query.js';
|
|
7
7
|
import { Compilable } from '../util/compilable.js';
|
|
8
|
-
import { PrimitiveValue } from '../util/object-utils.js';
|
|
9
8
|
import { ColumnDefinitionBuilder, ColumnDefinitionBuilderInterface } from './column-definition-builder.js';
|
|
10
9
|
import { AnyRawBuilder } from '../util/type-utils.js';
|
|
11
10
|
import { QueryId } from '../util/query-id.js';
|
|
12
11
|
import { QueryExecutor } from '../query-executor/query-executor.js';
|
|
13
12
|
import { DataTypeExpression } from '../parser/data-type-parser.js';
|
|
14
13
|
import { ForeignKeyConstraintBuilder, ForeignKeyConstraintBuilderInterface } from './foreign-key-constraint-builder.js';
|
|
14
|
+
import { DefaultValueExpression } from '../parser/default-value-parser.js';
|
|
15
15
|
/**
|
|
16
16
|
* This builder can be used to create a `alter table` query.
|
|
17
17
|
*/
|
|
@@ -43,7 +43,7 @@ export declare class AlterColumnBuilder {
|
|
|
43
43
|
#private;
|
|
44
44
|
constructor(props: AlterColumnBuilderProps);
|
|
45
45
|
setDataType(dataType: ColumnDataType): AlterTableExecutor;
|
|
46
|
-
setDefault(value:
|
|
46
|
+
setDefault(value: DefaultValueExpression): AlterTableExecutor;
|
|
47
47
|
dropDefault(): AlterTableExecutor;
|
|
48
48
|
setNotNull(): AlterTableExecutor;
|
|
49
49
|
dropNotNull(): AlterTableExecutor;
|
|
@@ -141,8 +141,21 @@ export declare class AlterTableAddColumnBuilder implements ColumnDefinitionBuild
|
|
|
141
141
|
* .addColumn('number_of_legs', 'integer', (col) => col.defaultTo(4))
|
|
142
142
|
* .execute()
|
|
143
143
|
* ```
|
|
144
|
+
*
|
|
145
|
+
* Raw expressions are also supported:
|
|
146
|
+
*
|
|
147
|
+
* ```ts
|
|
148
|
+
* db.schema
|
|
149
|
+
* .createTable('pet')
|
|
150
|
+
* .addColumn(
|
|
151
|
+
* 'number_of_legs',
|
|
152
|
+
* 'integer',
|
|
153
|
+
* (col) => col.defaultTo(db.raw('any SQL here'))
|
|
154
|
+
* )
|
|
155
|
+
* .execute()
|
|
156
|
+
* ```
|
|
144
157
|
*/
|
|
145
|
-
defaultTo(value:
|
|
158
|
+
defaultTo(value: DefaultValueExpression): AlterTableAddColumnBuilder;
|
|
146
159
|
/**
|
|
147
160
|
* Adds a check constraint for the column.
|
|
148
161
|
*
|
|
@@ -286,8 +299,21 @@ export declare class AlterTableModifyColumnBuilder implements ColumnDefinitionBu
|
|
|
286
299
|
* .addColumn('number_of_legs', 'integer', (col) => col.defaultTo(4))
|
|
287
300
|
* .execute()
|
|
288
301
|
* ```
|
|
302
|
+
*
|
|
303
|
+
* Raw expressions are also supported:
|
|
304
|
+
*
|
|
305
|
+
* ```ts
|
|
306
|
+
* db.schema
|
|
307
|
+
* .createTable('pet')
|
|
308
|
+
* .addColumn(
|
|
309
|
+
* 'number_of_legs',
|
|
310
|
+
* 'integer',
|
|
311
|
+
* (col) => col.defaultTo(db.raw('any SQL here'))
|
|
312
|
+
* )
|
|
313
|
+
* .execute()
|
|
314
|
+
* ```
|
|
289
315
|
*/
|
|
290
|
-
defaultTo(value:
|
|
316
|
+
defaultTo(value: DefaultValueExpression): AlterTableModifyColumnBuilder;
|
|
291
317
|
/**
|
|
292
318
|
* Adds a check constraint for the column.
|
|
293
319
|
*
|
|
@@ -6,10 +6,8 @@ import { ColumnDefinitionNode } from '../operation-node/column-definition-node.j
|
|
|
6
6
|
import { DataTypeNode, } from '../operation-node/data-type-node.js';
|
|
7
7
|
import { DropColumnNode } from '../operation-node/drop-column-node.js';
|
|
8
8
|
import { IdentifierNode } from '../operation-node/identifier-node.js';
|
|
9
|
-
import { isOperationNodeSource, } from '../operation-node/operation-node-source.js';
|
|
10
9
|
import { RenameColumnNode } from '../operation-node/rename-column-node.js';
|
|
11
10
|
import { TableNode } from '../operation-node/table-node.js';
|
|
12
|
-
import { ValueNode } from '../operation-node/value-node.js';
|
|
13
11
|
import { freeze } from '../util/object-utils.js';
|
|
14
12
|
import { preventAwait } from '../util/prevent-await.js';
|
|
15
13
|
import { ColumnDefinitionBuilder, } from './column-definition-builder.js';
|
|
@@ -21,6 +19,7 @@ import { UniqueConstraintNode } from '../operation-node/unique-constraint-node.j
|
|
|
21
19
|
import { CheckConstraintNode } from '../operation-node/check-constraint-node.js';
|
|
22
20
|
import { ForeignKeyConstraintNode } from '../operation-node/foreign-key-constraint-node.js';
|
|
23
21
|
import { ColumnNode } from '../operation-node/column-node.js';
|
|
22
|
+
import { parseDefaultValueExpression, } from '../parser/default-value-parser.js';
|
|
24
23
|
/**
|
|
25
24
|
* This builder can be used to create a `alter table` query.
|
|
26
25
|
*/
|
|
@@ -127,9 +126,7 @@ export class AlterColumnBuilder {
|
|
|
127
126
|
...this.#props,
|
|
128
127
|
alterTableNode: AlterTableNode.cloneWith(this.#props.alterTableNode, {
|
|
129
128
|
alterColumn: AlterColumnNode.cloneWith(this.#props.alterColumnNode, {
|
|
130
|
-
setDefault:
|
|
131
|
-
? value.toOperationNode()
|
|
132
|
-
: ValueNode.createImmediate(value),
|
|
129
|
+
setDefault: parseDefaultValueExpression(value),
|
|
133
130
|
}),
|
|
134
131
|
}),
|
|
135
132
|
});
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { OperationNodeSource } from '../operation-node/operation-node-source.js';
|
|
2
2
|
import { OnModifyForeignAction } from '../operation-node/references-node.js';
|
|
3
|
-
import { PrimitiveValue } from '../util/object-utils.js';
|
|
4
3
|
import { ColumnDefinitionNode } from '../operation-node/column-definition-node.js';
|
|
5
4
|
import { AnyRawBuilder } from '../util/type-utils.js';
|
|
6
5
|
import { DefaultValueExpression } from '../parser/default-value-parser.js';
|
|
@@ -83,8 +82,21 @@ export interface ColumnDefinitionBuilderInterface {
|
|
|
83
82
|
* .addColumn('number_of_legs', 'integer', (col) => col.defaultTo(4))
|
|
84
83
|
* .execute()
|
|
85
84
|
* ```
|
|
85
|
+
*
|
|
86
|
+
* Raw expressions are also supported:
|
|
87
|
+
*
|
|
88
|
+
* ```ts
|
|
89
|
+
* db.schema
|
|
90
|
+
* .createTable('pet')
|
|
91
|
+
* .addColumn(
|
|
92
|
+
* 'number_of_legs',
|
|
93
|
+
* 'integer',
|
|
94
|
+
* (col) => col.defaultTo(db.raw('any SQL here'))
|
|
95
|
+
* )
|
|
96
|
+
* .execute()
|
|
97
|
+
* ```
|
|
86
98
|
*/
|
|
87
|
-
defaultTo(value:
|
|
99
|
+
defaultTo(value: DefaultValueExpression): ColumnDefinitionBuilderInterface;
|
|
88
100
|
/**
|
|
89
101
|
* Adds a check constraint for the column.
|
|
90
102
|
*
|
|
@@ -222,6 +234,19 @@ export declare class ColumnDefinitionBuilder implements ColumnDefinitionBuilderI
|
|
|
222
234
|
* .addColumn('number_of_legs', 'integer', (col) => col.defaultTo(4))
|
|
223
235
|
* .execute()
|
|
224
236
|
* ```
|
|
237
|
+
*
|
|
238
|
+
* Raw expressions are also supported:
|
|
239
|
+
*
|
|
240
|
+
* ```ts
|
|
241
|
+
* db.schema
|
|
242
|
+
* .createTable('pet')
|
|
243
|
+
* .addColumn(
|
|
244
|
+
* 'number_of_legs',
|
|
245
|
+
* 'integer',
|
|
246
|
+
* (col) => col.defaultTo(db.raw('any SQL here'))
|
|
247
|
+
* )
|
|
248
|
+
* .execute()
|
|
249
|
+
* ```
|
|
225
250
|
*/
|
|
226
251
|
defaultTo(value: DefaultValueExpression): ColumnDefinitionBuilder;
|
|
227
252
|
/**
|
|
@@ -3,7 +3,7 @@ import { preventAwait } from '../util/prevent-await.js';
|
|
|
3
3
|
import { freeze } from '../util/object-utils.js';
|
|
4
4
|
import { CreateViewNode } from '../operation-node/create-view-node.js';
|
|
5
5
|
import { parseColumnName } from '../parser/reference-parser.js';
|
|
6
|
-
import { ImmediateValuePlugin } from '../plugin/immediate-value
|
|
6
|
+
import { ImmediateValuePlugin } from '../plugin/immediate-value/immediate-value-plugin.js';
|
|
7
7
|
export class CreateViewBuilder {
|
|
8
8
|
#props;
|
|
9
9
|
constructor(props) {
|
package/dist/esm/util/log.d.ts
CHANGED
|
@@ -17,6 +17,7 @@ export declare type LogConfig = ReadonlyArray<LogLevel> | Logger;
|
|
|
17
17
|
export declare class Log {
|
|
18
18
|
#private;
|
|
19
19
|
constructor(config: LogConfig);
|
|
20
|
+
isLevelEnabled(level: LogLevel): boolean;
|
|
20
21
|
query(getEvent: () => QueryLogEvent): void;
|
|
21
22
|
error(getEvent: () => ErrorLogEvent): void;
|
|
22
23
|
}
|
package/dist/esm/util/log.js
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
export declare type PrimitiveValue = string | number | boolean | null | Date | Buffer | BigInt;
|
|
3
|
-
export declare function isEmpty(obj: ArrayLike<unknown> | string | object | Buffer): boolean;
|
|
1
|
+
export declare function isEmpty(obj: ArrayLike<unknown> | string | object): boolean;
|
|
4
2
|
export declare function isString(obj: unknown): obj is string;
|
|
5
3
|
export declare function isNumber(obj: unknown): obj is number;
|
|
6
4
|
export declare function isBoolean(obj: unknown): obj is boolean;
|
|
7
5
|
export declare function isNull(obj: unknown): obj is null;
|
|
8
6
|
export declare function isDate(obj: unknown): obj is Date;
|
|
9
7
|
export declare function isBigInt(obj: unknown): obj is BigInt;
|
|
10
|
-
export declare function isBuffer(obj: unknown): obj is
|
|
11
|
-
|
|
8
|
+
export declare function isBuffer(obj: unknown): obj is {
|
|
9
|
+
length: number;
|
|
10
|
+
};
|
|
12
11
|
export declare function isFunction(obj: unknown): obj is Function;
|
|
13
12
|
export declare function isObject(obj: unknown): obj is Record<string, unknown>;
|
|
14
13
|
export declare function getLast<T>(arr: ArrayLike<T>): T | undefined;
|
|
@@ -17,3 +16,4 @@ export declare function asArray<T>(arg: T | T[]): T[];
|
|
|
17
16
|
export declare function asReadonlyArray<T>(arg: T | ReadonlyArray<T>): ReadonlyArray<T>;
|
|
18
17
|
export declare function isReadonlyArray(arg: unknown): arg is ReadonlyArray<unknown>;
|
|
19
18
|
export declare function noop<T>(obj: T): T;
|
|
19
|
+
export declare function compare(obj1: unknown, obj2: unknown): boolean;
|
|
@@ -26,18 +26,11 @@ export function isDate(obj) {
|
|
|
26
26
|
export function isBigInt(obj) {
|
|
27
27
|
return typeof obj === 'bigint';
|
|
28
28
|
}
|
|
29
|
+
// Don't change the returnd type to `obj is Buffer` to not create a
|
|
30
|
+
// hard dependency to node.
|
|
29
31
|
export function isBuffer(obj) {
|
|
30
32
|
return typeof Buffer !== 'undefined' && Buffer.isBuffer(obj);
|
|
31
33
|
}
|
|
32
|
-
export function isPrimitive(obj) {
|
|
33
|
-
return (isString(obj) ||
|
|
34
|
-
isNumber(obj) ||
|
|
35
|
-
isBoolean(obj) ||
|
|
36
|
-
isNull(obj) ||
|
|
37
|
-
isDate(obj) ||
|
|
38
|
-
isBigInt(obj) ||
|
|
39
|
-
isBuffer(obj));
|
|
40
|
-
}
|
|
41
34
|
export function isFunction(obj) {
|
|
42
35
|
return typeof obj === 'function';
|
|
43
36
|
}
|
|
@@ -72,3 +65,51 @@ export function isReadonlyArray(arg) {
|
|
|
72
65
|
export function noop(obj) {
|
|
73
66
|
return obj;
|
|
74
67
|
}
|
|
68
|
+
export function compare(obj1, obj2) {
|
|
69
|
+
if (isReadonlyArray(obj1) && isReadonlyArray(obj2)) {
|
|
70
|
+
return compareArrays(obj1, obj2);
|
|
71
|
+
}
|
|
72
|
+
else if (isObject(obj1) && isObject(obj2)) {
|
|
73
|
+
return compareObjects(obj1, obj2);
|
|
74
|
+
}
|
|
75
|
+
return obj1 === obj2;
|
|
76
|
+
}
|
|
77
|
+
function compareArrays(arr1, arr2) {
|
|
78
|
+
if (arr1.length !== arr2.length) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
for (let i = 0; i < arr1.length; ++i) {
|
|
82
|
+
if (!compare(arr1[i], arr2[i])) {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
function compareObjects(obj1, obj2) {
|
|
89
|
+
if (isBuffer(obj1) && isBuffer(obj2)) {
|
|
90
|
+
return compareBuffers(obj1, obj2);
|
|
91
|
+
}
|
|
92
|
+
else if (isDate(obj1) && isDate(obj2)) {
|
|
93
|
+
return compareDates(obj1, obj2);
|
|
94
|
+
}
|
|
95
|
+
return compareGenericObjects(obj1, obj2);
|
|
96
|
+
}
|
|
97
|
+
function compareBuffers(buf1, buf2) {
|
|
98
|
+
return Buffer.compare(buf1, buf2) === 0;
|
|
99
|
+
}
|
|
100
|
+
function compareDates(date1, date2) {
|
|
101
|
+
return date1.getTime() === date2.getTime();
|
|
102
|
+
}
|
|
103
|
+
function compareGenericObjects(obj1, obj2) {
|
|
104
|
+
const keys1 = Object.keys(obj1);
|
|
105
|
+
const keys2 = Object.keys(obj2);
|
|
106
|
+
if (keys1.length !== keys2.length) {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
for (const key of keys1) {
|
|
110
|
+
if (!compare(obj1[key], obj2[key])) {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
@@ -111,3 +111,24 @@ export declare type UnknownRow = Record<string, unknown>;
|
|
|
111
111
|
export declare type Nullable<T> = {
|
|
112
112
|
[P in keyof T]: T[P] | null;
|
|
113
113
|
};
|
|
114
|
+
/**
|
|
115
|
+
* Takes all properties from T1 and merges all properties from T2
|
|
116
|
+
* that don't exist in T1 as optional properties.
|
|
117
|
+
*
|
|
118
|
+
* Example:
|
|
119
|
+
*
|
|
120
|
+
* interface Person {
|
|
121
|
+
* name: string
|
|
122
|
+
* age: number
|
|
123
|
+
* }
|
|
124
|
+
*
|
|
125
|
+
* interface Pet {
|
|
126
|
+
* name: string
|
|
127
|
+
* species: 'cat' | 'dog'
|
|
128
|
+
* }
|
|
129
|
+
*
|
|
130
|
+
* type Merged = MergePartial<Person, Pet>
|
|
131
|
+
*
|
|
132
|
+
* // { name: string, age: number, species?: 'cat' | 'dog' }
|
|
133
|
+
*/
|
|
134
|
+
export declare type MergePartial<T1, T2> = T1 & Partial<Omit<T2, keyof T1>>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kysely",
|
|
3
|
-
"version": "0.16.
|
|
3
|
+
"version": "0.16.8",
|
|
4
4
|
"description": "Type safe SQL query builder",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -20,12 +20,16 @@
|
|
|
20
20
|
"./dist/esm/index-nodeless.js": "./dist/esm/index-nodeless.js"
|
|
21
21
|
},
|
|
22
22
|
"scripts": {
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"test": "
|
|
26
|
-
"test:
|
|
23
|
+
"clean": "rm -rf dist && rm -rf test/node/dist && rm -rf test/browser/bundle.js",
|
|
24
|
+
"test": "npm run clean && npm run build:cjs && npm run script:copy-interface-doc && npm run test:node:build && npm run test:node && npm run test:typings && npm run test:esmimports",
|
|
25
|
+
"test:node:build": "rm -rf test/node/dist && tsc -p test/node",
|
|
26
|
+
"test:node": "mocha --timeout 15000 test/node/dist/**/*.test.js",
|
|
27
|
+
"test:browser:build": "rm -rf test/browser/bundle.js && esbuild test/browser/main.ts --bundle --outfile=test/browser/bundle.js",
|
|
28
|
+
"test:browser": "npm run build && npm run test:browser:build && node test/browser/test.js",
|
|
29
|
+
"test:deno": "npm run build && deno run test/deno/local.test.ts && deno run test/deno/cdn.test.ts",
|
|
27
30
|
"test:typings": "tsd test/typings",
|
|
28
31
|
"test:esmimports": "node scripts/check-esm-imports.js",
|
|
32
|
+
"test:esbuild": "esbuild --bundle --platform=node --external:pg-native dist/esm/index.js --outfile=/dev/null",
|
|
29
33
|
"prettier": "prettier --write src/**/*.ts test/**/*.ts",
|
|
30
34
|
"build": "npm run clean && npm run build:esm && npm run build:cjs && npm run script:module-fixup && npm run script:copy-interface-doc",
|
|
31
35
|
"build:esm": "tsc -p tsconfig.json && npm run script:add-deno-type-references",
|
|
@@ -51,9 +55,11 @@
|
|
|
51
55
|
"better-sqlite3": "^7.4.5",
|
|
52
56
|
"chai": "^4.3.4",
|
|
53
57
|
"chai-subset": "^1.6.0",
|
|
58
|
+
"esbuild": "^0.14.11",
|
|
54
59
|
"mocha": "^9.1.3",
|
|
55
60
|
"mysql2": "^2.3.3",
|
|
56
61
|
"pg": "^8.7.1",
|
|
62
|
+
"playwright": "^1.17.2",
|
|
57
63
|
"prettier": "^2.5.1",
|
|
58
64
|
"tsd": "^0.19.0",
|
|
59
65
|
"typedoc": "^0.22.10",
|