kysely 0.21.6 → 0.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CONTRIBUTING.md +70 -0
- package/README.md +27 -4
- package/dist/cjs/dialect/database-introspector.d.ts +4 -0
- package/dist/cjs/dialect/mysql/mysql-driver.js +6 -3
- package/dist/cjs/dialect/postgres/postgres-adapter.js +1 -1
- package/dist/cjs/dialect/postgres/postgres-driver.js +7 -2
- package/dist/cjs/dialect/postgres/postgres-introspector.js +8 -0
- package/dist/cjs/dialect/sqlite/sqlite-driver.js +4 -3
- package/dist/cjs/dialect/sqlite/sqlite-introspector.js +1 -0
- package/dist/cjs/driver/database-connection.d.ts +6 -2
- package/dist/cjs/dynamic/dynamic-reference-builder.d.ts +2 -3
- package/dist/cjs/dynamic/dynamic-reference-builder.js +1 -1
- package/dist/cjs/expression/expression.d.ts +100 -0
- package/dist/cjs/expression/expression.js +16 -0
- package/dist/cjs/index.d.ts +17 -8
- package/dist/cjs/index.js +15 -4
- package/dist/cjs/kysely.d.ts +3 -3
- package/dist/cjs/kysely.js +3 -3
- package/dist/cjs/migration/file-migration-provider.js +11 -4
- package/dist/cjs/operation-node/aggregate-function-node.d.ts +25 -0
- package/dist/cjs/operation-node/aggregate-function-node.js +48 -0
- package/dist/cjs/operation-node/alias-node.d.ts +3 -10
- package/dist/cjs/operation-node/alter-column-node.d.ts +1 -2
- package/dist/cjs/operation-node/alter-table-node.d.ts +5 -7
- package/dist/cjs/operation-node/alter-table-node.js +9 -1
- package/dist/cjs/operation-node/and-node.d.ts +3 -4
- package/dist/cjs/operation-node/binary-operation-node.d.ts +14 -0
- package/dist/cjs/operation-node/binary-operation-node.js +20 -0
- package/dist/cjs/operation-node/check-constraint-node.d.ts +2 -3
- package/dist/cjs/operation-node/column-definition-node.d.ts +6 -5
- package/dist/cjs/operation-node/column-definition-node.js +16 -0
- package/dist/cjs/operation-node/column-update-node.d.ts +2 -3
- package/dist/cjs/operation-node/common-table-expression-node.d.ts +2 -4
- package/dist/cjs/operation-node/create-index-node.d.ts +1 -3
- package/dist/cjs/operation-node/create-table-node.d.ts +4 -5
- package/dist/cjs/operation-node/create-type-node.d.ts +1 -1
- package/dist/cjs/operation-node/create-type-node.js +1 -2
- package/dist/cjs/operation-node/default-value-node.d.ts +2 -4
- package/dist/cjs/operation-node/delete-query-node.d.ts +1 -2
- package/dist/cjs/operation-node/drop-type-node.d.ts +1 -1
- package/dist/cjs/operation-node/drop-type-node.js +1 -2
- package/dist/cjs/operation-node/explain-node.d.ts +2 -4
- package/dist/cjs/operation-node/explain-node.js +1 -1
- package/dist/cjs/operation-node/from-node.d.ts +3 -4
- package/dist/cjs/operation-node/generated-node.d.ts +2 -3
- package/dist/cjs/operation-node/group-by-item-node.d.ts +2 -3
- package/dist/cjs/operation-node/group-by-node.d.ts +1 -1
- package/dist/cjs/operation-node/group-by-node.js +3 -3
- package/dist/cjs/operation-node/having-node.d.ts +3 -4
- package/dist/cjs/operation-node/having-node.js +4 -4
- package/dist/cjs/operation-node/insert-query-node.d.ts +1 -4
- package/dist/cjs/operation-node/join-node.d.ts +5 -9
- package/dist/cjs/operation-node/join-node.js +6 -6
- package/dist/cjs/operation-node/on-conflict-node.d.ts +5 -7
- package/dist/cjs/operation-node/on-conflict-node.js +12 -12
- package/dist/cjs/operation-node/on-node.d.ts +3 -4
- package/dist/cjs/operation-node/on-node.js +4 -4
- package/dist/cjs/operation-node/operation-node-transformer.d.ts +14 -4
- package/dist/cjs/operation-node/operation-node-transformer.js +60 -20
- package/dist/cjs/operation-node/operation-node-visitor.d.ts +14 -4
- package/dist/cjs/operation-node/operation-node-visitor.js +7 -2
- package/dist/cjs/operation-node/operation-node.d.ts +1 -1
- package/dist/cjs/operation-node/operator-node.d.ts +5 -3
- package/dist/cjs/operation-node/operator-node.js +3 -5
- package/dist/cjs/operation-node/or-node.d.ts +3 -4
- package/dist/cjs/operation-node/order-by-item-node.d.ts +3 -5
- package/dist/cjs/operation-node/over-node.d.ts +19 -0
- package/dist/cjs/operation-node/over-node.js +35 -0
- package/dist/cjs/operation-node/parens-node.d.ts +2 -3
- package/dist/cjs/operation-node/partition-by-item-node.d.ts +13 -0
- package/dist/cjs/operation-node/{filter-node.js → partition-by-item-node.js} +6 -8
- package/dist/cjs/operation-node/partition-by-node.d.ts +14 -0
- package/dist/cjs/operation-node/partition-by-node.js +24 -0
- package/dist/cjs/operation-node/query-node.d.ts +2 -3
- package/dist/cjs/operation-node/query-node.js +6 -6
- package/dist/cjs/operation-node/select-modifier-node.d.ts +2 -3
- package/dist/cjs/operation-node/select-modifier-node.js +1 -1
- package/dist/cjs/operation-node/select-query-node.d.ts +8 -9
- package/dist/cjs/operation-node/select-query-node.js +14 -14
- package/dist/cjs/operation-node/selection-node.d.ts +2 -3
- package/dist/cjs/operation-node/set-operation-node.d.ts +15 -0
- package/dist/cjs/operation-node/{union-node.js → set-operation-node.js} +7 -6
- package/dist/cjs/operation-node/simple-reference-expression-node.d.ts +3 -0
- package/dist/cjs/operation-node/{operation-node-utils.js → simple-reference-expression-node.js} +0 -0
- package/dist/cjs/operation-node/unary-operation-node.d.ts +13 -0
- package/dist/cjs/operation-node/unary-operation-node.js +19 -0
- package/dist/cjs/operation-node/update-query-node.d.ts +3 -4
- package/dist/cjs/operation-node/value-list-node.d.ts +2 -8
- package/dist/cjs/operation-node/where-node.d.ts +3 -4
- package/dist/cjs/operation-node/where-node.js +4 -4
- package/dist/cjs/parser/binary-operation-parser.d.ts +20 -0
- package/dist/cjs/parser/binary-operation-parser.js +99 -0
- package/dist/cjs/parser/coalesce-parser.d.ts +4 -0
- package/dist/cjs/parser/coalesce-parser.js +11 -0
- package/dist/cjs/parser/data-type-parser.d.ts +5 -5
- package/dist/cjs/parser/default-value-parser.d.ts +4 -5
- package/dist/cjs/parser/default-value-parser.js +2 -2
- package/dist/cjs/parser/expression-parser.d.ts +10 -0
- package/dist/cjs/parser/{complex-expression-parser.js → expression-parser.js} +9 -8
- package/dist/cjs/parser/group-by-parser.d.ts +1 -1
- package/dist/cjs/parser/group-by-parser.js +2 -2
- package/dist/cjs/parser/identifier-parser.d.ts +2 -0
- package/dist/cjs/parser/identifier-parser.js +23 -0
- package/dist/cjs/parser/insert-values-parser.d.ts +1 -1
- package/dist/cjs/parser/insert-values-parser.js +2 -2
- package/dist/cjs/parser/join-parser.js +2 -2
- package/dist/cjs/parser/order-by-parser.d.ts +2 -2
- package/dist/cjs/parser/parse-utils.d.ts +2 -0
- package/dist/cjs/parser/parse-utils.js +9 -1
- package/dist/cjs/parser/partition-by-parser.d.ts +6 -0
- package/dist/cjs/parser/partition-by-parser.js +9 -0
- package/dist/cjs/parser/reference-parser.d.ts +14 -13
- package/dist/cjs/parser/reference-parser.js +12 -9
- package/dist/cjs/parser/select-parser.d.ts +6 -6
- package/dist/cjs/parser/select-parser.js +2 -2
- package/dist/cjs/parser/set-operation-parser.d.ts +3 -0
- package/dist/cjs/parser/set-operation-parser.js +8 -0
- package/dist/cjs/parser/table-parser.d.ts +11 -12
- package/dist/cjs/parser/table-parser.js +2 -2
- package/dist/cjs/parser/unary-operation-parser.d.ts +5 -0
- package/dist/cjs/parser/unary-operation-parser.js +17 -0
- package/dist/cjs/parser/update-set-parser.d.ts +1 -1
- package/dist/cjs/parser/value-parser.d.ts +5 -8
- package/dist/cjs/parser/value-parser.js +4 -4
- package/dist/cjs/parser/with-parser.d.ts +3 -4
- package/dist/cjs/plugin/camel-case/camel-case-plugin.js +1 -1
- package/dist/cjs/query-builder/aggregate-function-builder.d.ts +348 -0
- package/dist/cjs/query-builder/aggregate-function-builder.js +298 -0
- package/dist/cjs/query-builder/delete-query-builder.d.ts +20 -20
- package/dist/cjs/query-builder/delete-query-builder.js +19 -16
- package/dist/cjs/query-builder/expression-builder.d.ts +3 -3
- package/dist/cjs/query-builder/expression-builder.js +3 -3
- package/dist/cjs/query-builder/function-module.d.ts +241 -0
- package/dist/cjs/query-builder/function-module.js +214 -0
- package/dist/cjs/query-builder/having-interface.d.ts +9 -8
- package/dist/cjs/query-builder/insert-query-builder.d.ts +7 -6
- package/dist/cjs/query-builder/insert-query-builder.js +11 -9
- package/dist/cjs/query-builder/insert-result.d.ts +8 -1
- package/dist/cjs/query-builder/insert-result.js +12 -1
- package/dist/cjs/query-builder/join-builder.d.ts +9 -8
- package/dist/cjs/query-builder/join-builder.js +10 -9
- package/dist/cjs/query-builder/on-conflict-builder.d.ts +25 -24
- package/dist/cjs/query-builder/on-conflict-builder.js +18 -17
- package/dist/cjs/query-builder/over-builder.d.ts +59 -0
- package/dist/cjs/query-builder/over-builder.js +50 -0
- package/dist/cjs/query-builder/select-query-builder.d.ts +138 -47
- package/dist/cjs/query-builder/select-query-builder.js +122 -40
- package/dist/cjs/query-builder/update-query-builder.d.ts +20 -20
- package/dist/cjs/query-builder/update-query-builder.js +19 -16
- package/dist/cjs/query-builder/where-interface.d.ts +12 -11
- package/dist/cjs/query-compiler/compiled-query.d.ts +1 -1
- package/dist/cjs/query-compiler/default-query-compiler.d.ts +15 -5
- package/dist/cjs/query-compiler/default-query-compiler.js +78 -47
- package/dist/cjs/query-executor/query-executor-base.js +16 -1
- package/dist/cjs/raw-builder/raw-builder.d.ts +49 -13
- package/dist/cjs/raw-builder/raw-builder.js +24 -14
- package/dist/cjs/raw-builder/sql.d.ts +15 -7
- package/dist/cjs/raw-builder/sql.js +6 -0
- package/dist/cjs/schema/alter-column-builder.d.ts +28 -0
- package/dist/cjs/schema/alter-column-builder.js +55 -0
- package/dist/cjs/schema/alter-table-builder.d.ts +39 -341
- package/dist/cjs/schema/alter-table-builder.js +65 -280
- package/dist/cjs/schema/column-definition-builder.d.ts +43 -155
- package/dist/cjs/schema/column-definition-builder.js +189 -0
- package/dist/cjs/schema/create-index-builder.d.ts +2 -2
- package/dist/cjs/schema/create-table-builder.d.ts +38 -5
- package/dist/cjs/schema/create-table-builder.js +36 -1
- package/dist/cjs/schema/create-view-builder.d.ts +3 -2
- package/dist/cjs/schema/schema.d.ts +2 -2
- package/dist/cjs/schema/schema.js +6 -5
- package/dist/cjs/util/compilable.d.ts +2 -2
- package/dist/cjs/util/explainable.d.ts +2 -2
- package/dist/cjs/util/infer-result.d.ts +46 -0
- package/dist/cjs/util/infer-result.js +2 -0
- package/dist/cjs/util/log-once.d.ts +5 -0
- package/dist/cjs/util/log-once.js +16 -0
- package/dist/cjs/util/object-utils.d.ts +1 -0
- package/dist/cjs/util/object-utils.js +5 -1
- package/dist/cjs/util/type-utils.d.ts +3 -13
- package/dist/esm/dialect/database-introspector.d.ts +4 -0
- package/dist/esm/dialect/mysql/mysql-driver.js +6 -3
- package/dist/esm/dialect/postgres/postgres-adapter.js +1 -1
- package/dist/esm/dialect/postgres/postgres-driver.js +7 -2
- package/dist/esm/dialect/postgres/postgres-introspector.js +8 -0
- package/dist/esm/dialect/sqlite/sqlite-driver.js +4 -3
- package/dist/esm/dialect/sqlite/sqlite-introspector.js +1 -0
- package/dist/esm/driver/database-connection.d.ts +6 -2
- package/dist/esm/dynamic/dynamic-reference-builder.d.ts +2 -3
- package/dist/esm/dynamic/dynamic-reference-builder.js +2 -2
- package/dist/esm/expression/expression.d.ts +100 -0
- package/dist/esm/expression/expression.js +12 -0
- package/dist/esm/index.d.ts +17 -8
- package/dist/esm/index.js +13 -4
- package/dist/esm/kysely.d.ts +3 -3
- package/dist/esm/kysely.js +3 -3
- package/dist/esm/migration/file-migration-provider.js +11 -4
- package/dist/esm/operation-node/aggregate-function-node.d.ts +25 -0
- package/dist/esm/operation-node/aggregate-function-node.js +46 -0
- package/dist/esm/operation-node/alias-node.d.ts +3 -10
- package/dist/esm/operation-node/alter-column-node.d.ts +1 -2
- package/dist/esm/operation-node/alter-table-node.d.ts +5 -7
- package/dist/esm/operation-node/alter-table-node.js +9 -1
- package/dist/esm/operation-node/and-node.d.ts +3 -4
- package/dist/esm/operation-node/binary-operation-node.d.ts +14 -0
- package/dist/esm/operation-node/binary-operation-node.js +18 -0
- package/dist/esm/operation-node/check-constraint-node.d.ts +2 -3
- package/dist/esm/operation-node/column-definition-node.d.ts +6 -5
- package/dist/esm/operation-node/column-definition-node.js +16 -0
- package/dist/esm/operation-node/column-update-node.d.ts +2 -3
- package/dist/esm/operation-node/common-table-expression-node.d.ts +2 -4
- package/dist/esm/operation-node/create-index-node.d.ts +1 -3
- package/dist/esm/operation-node/create-table-node.d.ts +4 -5
- package/dist/esm/operation-node/create-type-node.d.ts +1 -1
- package/dist/esm/operation-node/create-type-node.js +1 -2
- package/dist/esm/operation-node/default-value-node.d.ts +2 -4
- package/dist/esm/operation-node/delete-query-node.d.ts +1 -2
- package/dist/esm/operation-node/drop-type-node.d.ts +1 -1
- package/dist/esm/operation-node/drop-type-node.js +1 -2
- package/dist/esm/operation-node/explain-node.d.ts +2 -4
- package/dist/esm/operation-node/explain-node.js +1 -1
- package/dist/esm/operation-node/from-node.d.ts +3 -4
- package/dist/esm/operation-node/generated-node.d.ts +2 -3
- package/dist/esm/operation-node/group-by-item-node.d.ts +2 -3
- package/dist/esm/operation-node/group-by-node.d.ts +1 -1
- package/dist/esm/operation-node/group-by-node.js +3 -3
- package/dist/esm/operation-node/having-node.d.ts +3 -4
- package/dist/esm/operation-node/having-node.js +4 -4
- package/dist/esm/operation-node/insert-query-node.d.ts +1 -4
- package/dist/esm/operation-node/join-node.d.ts +5 -9
- package/dist/esm/operation-node/join-node.js +6 -6
- package/dist/esm/operation-node/on-conflict-node.d.ts +5 -7
- package/dist/esm/operation-node/on-conflict-node.js +12 -12
- package/dist/esm/operation-node/on-node.d.ts +3 -4
- package/dist/esm/operation-node/on-node.js +4 -4
- package/dist/esm/operation-node/operation-node-transformer.d.ts +14 -4
- package/dist/esm/operation-node/operation-node-transformer.js +60 -20
- package/dist/esm/operation-node/operation-node-visitor.d.ts +14 -4
- package/dist/esm/operation-node/operation-node-visitor.js +7 -2
- package/dist/esm/operation-node/operation-node.d.ts +1 -1
- package/dist/esm/operation-node/operator-node.d.ts +5 -3
- package/dist/esm/operation-node/operator-node.js +2 -4
- package/dist/esm/operation-node/or-node.d.ts +3 -4
- package/dist/esm/operation-node/order-by-item-node.d.ts +3 -5
- package/dist/esm/operation-node/over-node.d.ts +19 -0
- package/dist/esm/operation-node/over-node.js +33 -0
- package/dist/esm/operation-node/parens-node.d.ts +2 -3
- package/dist/esm/operation-node/partition-by-item-node.d.ts +13 -0
- package/dist/esm/operation-node/partition-by-item-node.js +16 -0
- package/dist/esm/operation-node/partition-by-node.d.ts +14 -0
- package/dist/esm/operation-node/partition-by-node.js +22 -0
- package/dist/esm/operation-node/query-node.d.ts +2 -3
- package/dist/esm/operation-node/query-node.js +6 -6
- package/dist/esm/operation-node/select-modifier-node.d.ts +2 -3
- package/dist/esm/operation-node/select-modifier-node.js +1 -1
- package/dist/esm/operation-node/select-query-node.d.ts +8 -9
- package/dist/esm/operation-node/select-query-node.js +14 -14
- package/dist/esm/operation-node/selection-node.d.ts +2 -3
- package/dist/esm/operation-node/set-operation-node.d.ts +15 -0
- package/dist/esm/operation-node/set-operation-node.js +18 -0
- package/dist/esm/operation-node/simple-reference-expression-node.d.ts +3 -0
- package/dist/esm/operation-node/simple-reference-expression-node.js +2 -0
- package/dist/esm/operation-node/unary-operation-node.d.ts +13 -0
- package/dist/esm/operation-node/unary-operation-node.js +17 -0
- package/dist/esm/operation-node/update-query-node.d.ts +3 -4
- package/dist/esm/operation-node/value-list-node.d.ts +2 -8
- package/dist/esm/operation-node/where-node.d.ts +3 -4
- package/dist/esm/operation-node/where-node.js +4 -4
- package/dist/esm/parser/binary-operation-parser.d.ts +20 -0
- package/dist/esm/parser/binary-operation-parser.js +92 -0
- package/dist/esm/parser/coalesce-parser.d.ts +4 -0
- package/dist/esm/parser/coalesce-parser.js +8 -0
- package/dist/esm/parser/data-type-parser.d.ts +5 -5
- package/dist/esm/parser/default-value-parser.d.ts +4 -5
- package/dist/esm/parser/default-value-parser.js +2 -2
- package/dist/esm/parser/expression-parser.d.ts +10 -0
- package/dist/esm/parser/{complex-expression-parser.js → expression-parser.js} +6 -5
- package/dist/esm/parser/group-by-parser.d.ts +1 -1
- package/dist/esm/parser/group-by-parser.js +2 -2
- package/dist/esm/parser/identifier-parser.d.ts +2 -0
- package/dist/esm/parser/identifier-parser.js +20 -0
- package/dist/esm/parser/insert-values-parser.d.ts +1 -1
- package/dist/esm/parser/insert-values-parser.js +2 -2
- package/dist/esm/parser/join-parser.js +2 -2
- package/dist/esm/parser/order-by-parser.d.ts +2 -2
- package/dist/esm/parser/parse-utils.d.ts +2 -0
- package/dist/esm/parser/parse-utils.js +7 -0
- package/dist/esm/parser/partition-by-parser.d.ts +6 -0
- package/dist/esm/parser/partition-by-parser.js +6 -0
- package/dist/esm/parser/reference-parser.d.ts +14 -13
- package/dist/esm/parser/reference-parser.js +10 -8
- package/dist/esm/parser/select-parser.d.ts +6 -6
- package/dist/esm/parser/select-parser.js +2 -2
- package/dist/esm/parser/set-operation-parser.d.ts +3 -0
- package/dist/esm/parser/set-operation-parser.js +5 -0
- package/dist/esm/parser/table-parser.d.ts +11 -12
- package/dist/esm/parser/table-parser.js +2 -2
- package/dist/esm/parser/unary-operation-parser.d.ts +5 -0
- package/dist/esm/parser/unary-operation-parser.js +13 -0
- package/dist/esm/parser/update-set-parser.d.ts +1 -1
- package/dist/esm/parser/value-parser.d.ts +5 -8
- package/dist/esm/parser/value-parser.js +4 -4
- package/dist/esm/parser/with-parser.d.ts +3 -4
- package/dist/esm/plugin/camel-case/camel-case-plugin.js +2 -2
- package/dist/esm/query-builder/aggregate-function-builder.d.ts +348 -0
- package/dist/esm/query-builder/aggregate-function-builder.js +294 -0
- package/dist/esm/query-builder/delete-query-builder.d.ts +20 -20
- package/dist/esm/query-builder/delete-query-builder.js +19 -16
- package/dist/esm/query-builder/expression-builder.d.ts +3 -3
- package/dist/esm/query-builder/expression-builder.js +3 -3
- package/dist/esm/query-builder/function-module.d.ts +241 -0
- package/dist/esm/query-builder/function-module.js +211 -0
- package/dist/esm/query-builder/having-interface.d.ts +9 -8
- package/dist/esm/query-builder/insert-query-builder.d.ts +7 -6
- package/dist/esm/query-builder/insert-query-builder.js +11 -9
- package/dist/esm/query-builder/insert-result.d.ts +8 -1
- package/dist/esm/query-builder/insert-result.js +12 -1
- package/dist/esm/query-builder/join-builder.d.ts +9 -8
- package/dist/esm/query-builder/join-builder.js +10 -9
- package/dist/esm/query-builder/on-conflict-builder.d.ts +25 -24
- package/dist/esm/query-builder/on-conflict-builder.js +18 -17
- package/dist/esm/query-builder/over-builder.d.ts +59 -0
- package/dist/esm/query-builder/over-builder.js +47 -0
- package/dist/esm/query-builder/select-query-builder.d.ts +138 -47
- package/dist/esm/query-builder/select-query-builder.js +120 -38
- package/dist/esm/query-builder/update-query-builder.d.ts +20 -20
- package/dist/esm/query-builder/update-query-builder.js +19 -16
- package/dist/esm/query-builder/where-interface.d.ts +12 -11
- package/dist/esm/query-compiler/compiled-query.d.ts +1 -1
- package/dist/esm/query-compiler/default-query-compiler.d.ts +15 -5
- package/dist/esm/query-compiler/default-query-compiler.js +79 -48
- package/dist/esm/query-executor/query-executor-base.js +16 -1
- package/dist/esm/raw-builder/raw-builder.d.ts +49 -13
- package/dist/esm/raw-builder/raw-builder.js +23 -14
- package/dist/esm/raw-builder/sql.d.ts +15 -7
- package/dist/esm/raw-builder/sql.js +6 -0
- package/dist/esm/schema/alter-column-builder.d.ts +28 -0
- package/dist/esm/schema/alter-column-builder.js +51 -0
- package/dist/esm/schema/alter-table-builder.d.ts +39 -341
- package/dist/esm/schema/alter-table-builder.js +61 -274
- package/dist/esm/schema/column-definition-builder.d.ts +43 -155
- package/dist/esm/schema/column-definition-builder.js +189 -0
- package/dist/esm/schema/create-index-builder.d.ts +2 -2
- package/dist/esm/schema/create-table-builder.d.ts +38 -5
- package/dist/esm/schema/create-table-builder.js +36 -1
- package/dist/esm/schema/create-view-builder.d.ts +3 -2
- package/dist/esm/schema/schema.d.ts +2 -2
- package/dist/esm/schema/schema.js +6 -5
- package/dist/esm/util/compilable.d.ts +2 -2
- package/dist/esm/util/explainable.d.ts +2 -2
- package/dist/esm/util/infer-result.d.ts +46 -0
- package/dist/esm/util/infer-result.js +2 -0
- package/dist/esm/util/log-once.d.ts +5 -0
- package/dist/esm/util/log-once.js +13 -0
- package/dist/esm/util/object-utils.d.ts +1 -0
- package/dist/esm/util/object-utils.js +3 -0
- package/dist/esm/util/type-utils.d.ts +3 -13
- package/package.json +4 -1
- package/dist/cjs/operation-node/filter-node.d.ts +0 -18
- package/dist/cjs/operation-node/operation-node-utils.d.ts +0 -17
- package/dist/cjs/operation-node/union-node.d.ts +0 -16
- package/dist/cjs/parser/complex-expression-parser.d.ts +0 -10
- package/dist/cjs/parser/filter-parser.d.ts +0 -24
- package/dist/cjs/parser/filter-parser.js +0 -109
- package/dist/cjs/parser/union-parser.d.ts +0 -5
- package/dist/cjs/parser/union-parser.js +0 -8
- package/dist/cjs/query-builder/function-builder.d.ts +0 -133
- package/dist/cjs/query-builder/function-builder.js +0 -160
- package/dist/esm/operation-node/filter-node.d.ts +0 -18
- package/dist/esm/operation-node/filter-node.js +0 -18
- package/dist/esm/operation-node/operation-node-utils.d.ts +0 -17
- package/dist/esm/operation-node/operation-node-utils.js +0 -2
- package/dist/esm/operation-node/union-node.d.ts +0 -16
- package/dist/esm/operation-node/union-node.js +0 -17
- package/dist/esm/parser/complex-expression-parser.d.ts +0 -10
- package/dist/esm/parser/filter-parser.d.ts +0 -24
- package/dist/esm/parser/filter-parser.js +0 -100
- package/dist/esm/parser/union-parser.d.ts +0 -5
- package/dist/esm/parser/union-parser.js +0 -5
- package/dist/esm/query-builder/function-builder.d.ts +0 -133
- package/dist/esm/query-builder/function-builder.js +0 -157
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { DynamicReferenceBuilder } from '../dynamic/dynamic-reference-builder.js';
|
|
2
|
+
import { CoalesceReferenceExpressionList } from '../parser/coalesce-parser.js';
|
|
3
|
+
import { ExtractTypeFromReferenceExpression, SimpleReferenceExpression, ReferenceExpression, StringReference } from '../parser/reference-parser.js';
|
|
4
|
+
import { RawBuilder } from '../raw-builder/raw-builder.js';
|
|
5
|
+
import { Equals, IsNever } from '../util/type-utils.js';
|
|
6
|
+
import { AggregateFunctionBuilder } from './aggregate-function-builder.js';
|
|
7
|
+
/**
|
|
8
|
+
* Helpers for type safe SQL function calls.
|
|
9
|
+
*
|
|
10
|
+
* You can always use the {@link sql} tag to call functions and build arbitrary
|
|
11
|
+
* expressions. This module simply has shortcuts for most common function calls.
|
|
12
|
+
*
|
|
13
|
+
* ### Examples
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* const { count } = db.fn
|
|
17
|
+
*
|
|
18
|
+
* await db.selectFrom('person')
|
|
19
|
+
* .innerJoin('pet', 'pet.owner_id', 'person.id')
|
|
20
|
+
* .select([
|
|
21
|
+
* 'person.id',
|
|
22
|
+
* count('pet.id').as('pet_count')
|
|
23
|
+
* ])
|
|
24
|
+
* .groupBy('person.id')
|
|
25
|
+
* .having(count('pet.id'), '>', 10)
|
|
26
|
+
* .execute()
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* The generated SQL (PostgreSQL):
|
|
30
|
+
*
|
|
31
|
+
* ```sql
|
|
32
|
+
* select "person"."id", count("pet"."id") as "pet_count"
|
|
33
|
+
* from "person"
|
|
34
|
+
* inner join "pet" on "pet"."owner_id" = "person"."id"
|
|
35
|
+
* group by "person"."id"
|
|
36
|
+
* having count("pet"."id") > $1
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export declare class FunctionModule<DB, TB extends keyof DB> {
|
|
40
|
+
constructor();
|
|
41
|
+
/**
|
|
42
|
+
* Calls the `avg` function for the column given as the argument.
|
|
43
|
+
*
|
|
44
|
+
* If this is used in a `select` statement the type of the selected expression
|
|
45
|
+
* will be `number | string` by default. This is because Kysely can't know the
|
|
46
|
+
* type the db driver outputs. Sometimes the output can be larger than the largest
|
|
47
|
+
* javascript number and a string is returned instead. Most drivers allow you
|
|
48
|
+
* to configure the output type of large numbers and Kysely can't know if you've
|
|
49
|
+
* done so.
|
|
50
|
+
*
|
|
51
|
+
* You can specify the output type of the expression by providing the type as
|
|
52
|
+
* the first type argument:
|
|
53
|
+
*
|
|
54
|
+
* ```ts
|
|
55
|
+
* const { avg } = db.fn
|
|
56
|
+
*
|
|
57
|
+
* db.selectFrom('toy')
|
|
58
|
+
* .select(avg<number>('price').as('avg_price'))
|
|
59
|
+
* .execute()
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* Sometimes a null is returned, e.g. when row count is 0, and no `group by`
|
|
63
|
+
* was used. It is highly recommended to include null in the output type union
|
|
64
|
+
* and handle null values in post-execute code, or wrap the function with a `coalesce`
|
|
65
|
+
* function.
|
|
66
|
+
*
|
|
67
|
+
* ```ts
|
|
68
|
+
* const { avg } = db.fn
|
|
69
|
+
*
|
|
70
|
+
* db.selectFrom('toy')
|
|
71
|
+
* .select(avg<number | null>('price').as('avg_price'))
|
|
72
|
+
* .execute()
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
avg<O extends number | string | null = number | string, C extends SimpleReferenceExpression<DB, TB> = SimpleReferenceExpression<DB, TB>>(column: C): AggregateFunctionBuilder<DB, TB, O>;
|
|
76
|
+
/**
|
|
77
|
+
* Calls the `coalesce` function for given arguments.
|
|
78
|
+
*
|
|
79
|
+
* This function returns the first non-null value from left to right, commonly
|
|
80
|
+
* used to provide a default scalar for nullable columns or functions.
|
|
81
|
+
*
|
|
82
|
+
* ```ts
|
|
83
|
+
* const { coalesce, max } = db.fn
|
|
84
|
+
*
|
|
85
|
+
* db.selectFrom('person')
|
|
86
|
+
* .select(coalesce(max('age'), sql<number>`0`).as('max_age'))
|
|
87
|
+
* .where('first_name', '=', 'Jennifer')
|
|
88
|
+
* .execute()
|
|
89
|
+
* ```
|
|
90
|
+
*
|
|
91
|
+
* The generated SQL (postgres):
|
|
92
|
+
*
|
|
93
|
+
* ```sql
|
|
94
|
+
* select coalesce(max("age"), 0) as "max_age" from "person" where "first_name" = $1
|
|
95
|
+
* ```
|
|
96
|
+
*
|
|
97
|
+
* If this is used in a `select` statement the type of the selected expression
|
|
98
|
+
* is inferred in the same manner that the function computes. A union of arguments'
|
|
99
|
+
* types - if a non-nullable argument exists, it stops there (ignoring any further
|
|
100
|
+
* arguments' types) and exludes null from the final union type.
|
|
101
|
+
*
|
|
102
|
+
* Examples:
|
|
103
|
+
*
|
|
104
|
+
* `(string | null, number | null)` is inferred as `string | number | null`.
|
|
105
|
+
*
|
|
106
|
+
* `(string | null, number, Date | null)` is inferred as `string | number`.
|
|
107
|
+
*
|
|
108
|
+
* `(number, string | null)` is inferred as `number`.
|
|
109
|
+
*/
|
|
110
|
+
coalesce<V extends ReferenceExpression<DB, TB>, OV extends ReferenceExpression<DB, TB>[]>(value: V, ...otherValues: OV): RawBuilder<CoalesceReferenceExpressionList<DB, TB, [V, ...OV]>>;
|
|
111
|
+
/**
|
|
112
|
+
* Calls the `count` function for the column given as the argument.
|
|
113
|
+
*
|
|
114
|
+
* If this is used in a `select` statement the type of the selected expression
|
|
115
|
+
* will be `number | string | bigint` by default. This is because Kysely
|
|
116
|
+
* can't know the type the db driver outputs. Sometimes the output can be larger
|
|
117
|
+
* than the largest javascript number and a string is returned instead. Most
|
|
118
|
+
* drivers allow you to configure the output type of large numbers and Kysely
|
|
119
|
+
* can't know if you've done so.
|
|
120
|
+
*
|
|
121
|
+
* You can specify the output type of the expression by providing
|
|
122
|
+
* the type as the first type argument:
|
|
123
|
+
*
|
|
124
|
+
* ```ts
|
|
125
|
+
* const { count } = db.fn
|
|
126
|
+
*
|
|
127
|
+
* db.selectFrom('toy')
|
|
128
|
+
* .select(count<number>('id').as('num_toys'))
|
|
129
|
+
* .execute()
|
|
130
|
+
* ```
|
|
131
|
+
*
|
|
132
|
+
* You can limit column range:
|
|
133
|
+
*
|
|
134
|
+
* ```ts
|
|
135
|
+
* db.selectFrom('toy')
|
|
136
|
+
* .select(qb => qb.fn.count<number>('id').as('num_toys'))
|
|
137
|
+
* .execute()
|
|
138
|
+
* ```
|
|
139
|
+
*/
|
|
140
|
+
count<O extends number | string | bigint, C extends SimpleReferenceExpression<DB, TB> = SimpleReferenceExpression<DB, TB>>(column: C): AggregateFunctionBuilder<DB, TB, O>;
|
|
141
|
+
/**
|
|
142
|
+
* Calls the `max` function for the column given as the argument.
|
|
143
|
+
*
|
|
144
|
+
* If this is used in a `select` statement the type of the selected expression
|
|
145
|
+
* will be the referenced column's type.
|
|
146
|
+
*
|
|
147
|
+
* ### Examples
|
|
148
|
+
*
|
|
149
|
+
* ```ts
|
|
150
|
+
* const { max } = db.fn
|
|
151
|
+
*
|
|
152
|
+
* db.selectFrom('toy')
|
|
153
|
+
* .select(max('price').as('max_price'))
|
|
154
|
+
* .execute()
|
|
155
|
+
* ```
|
|
156
|
+
*
|
|
157
|
+
* Sometimes a null is returned, e.g. when row count is 0, and no `group by`
|
|
158
|
+
* was used. It is highly recommended to include null in the output type union
|
|
159
|
+
* and handle null values in post-execute code, or wrap the function with a `coalesce`
|
|
160
|
+
* function.
|
|
161
|
+
*
|
|
162
|
+
* ```ts
|
|
163
|
+
* const { max } = db.fn
|
|
164
|
+
*
|
|
165
|
+
* db.selectFrom('toy')
|
|
166
|
+
* .select(max<number | null, 'price'>('price').as('max_price'))
|
|
167
|
+
* .execute()
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
max<O extends number | string | bigint | null = never, C extends StringReference<DB, TB> = StringReference<DB, TB>>(column: OutputBoundStringReference<DB, TB, C, O>): StringReferenceBoundAggregateFunctionBuilder<DB, TB, C, O>;
|
|
171
|
+
max<O extends number | string | bigint | null = number | string | bigint>(column: DynamicReferenceBuilder): AggregateFunctionBuilder<DB, TB, O>;
|
|
172
|
+
/**
|
|
173
|
+
* Calls the `min` function for the column given as the argument.
|
|
174
|
+
*
|
|
175
|
+
* If this is used in a `select` statement the type of the selected expression
|
|
176
|
+
* will be the referenced column's type.
|
|
177
|
+
*
|
|
178
|
+
* ### Examples
|
|
179
|
+
*
|
|
180
|
+
* ```ts
|
|
181
|
+
* const { min } = db.fn
|
|
182
|
+
*
|
|
183
|
+
* db.selectFrom('toy')
|
|
184
|
+
* .select(min('price').as('min_price'))
|
|
185
|
+
* .execute()
|
|
186
|
+
* ```
|
|
187
|
+
*
|
|
188
|
+
* Sometimes a null is returned, e.g. when row count is 0, and no `group by`
|
|
189
|
+
* was used. It is highly recommended to include null in the output type union
|
|
190
|
+
* and handle null values in post-execute code, or wrap the function with a `coalesce`
|
|
191
|
+
* function.
|
|
192
|
+
*
|
|
193
|
+
* ```ts
|
|
194
|
+
* const { min } = db.fn
|
|
195
|
+
*
|
|
196
|
+
* db.selectFrom('toy')
|
|
197
|
+
* .select(min<number | null, 'price'>('price').as('min_price'))
|
|
198
|
+
* .execute()
|
|
199
|
+
* ```
|
|
200
|
+
*/
|
|
201
|
+
min<O extends number | string | bigint | null = never, C extends StringReference<DB, TB> = StringReference<DB, TB>>(column: OutputBoundStringReference<DB, TB, C, O>): StringReferenceBoundAggregateFunctionBuilder<DB, TB, C, O>;
|
|
202
|
+
min<O extends number | string | bigint | null = number | string | bigint>(column: DynamicReferenceBuilder): AggregateFunctionBuilder<DB, TB, O>;
|
|
203
|
+
/**
|
|
204
|
+
* Calls the `sum` function for the column given as the argument.
|
|
205
|
+
*
|
|
206
|
+
* If this is used in a `select` statement the type of the selected expression
|
|
207
|
+
* will be `number | string | bigint` by default. This is because Kysely
|
|
208
|
+
* can't know the type the db driver outputs. Sometimes the output can be larger
|
|
209
|
+
* than the largest javascript number and a string is returned instead. Most
|
|
210
|
+
* drivers allow you to configure the output type of large numbers and Kysely
|
|
211
|
+
* can't know if you've done so.
|
|
212
|
+
*
|
|
213
|
+
* You can specify the output type of the expression by providing
|
|
214
|
+
* the type as the first type argument:
|
|
215
|
+
*
|
|
216
|
+
* ```ts
|
|
217
|
+
* const { sum } = db.fn
|
|
218
|
+
*
|
|
219
|
+
* db.selectFrom('toy')
|
|
220
|
+
* .select(sum<number>('price').as('total_price'))
|
|
221
|
+
* .execute()
|
|
222
|
+
* ```
|
|
223
|
+
*
|
|
224
|
+
* Sometimes a null is returned, e.g. when row count is 0, and no `group by`
|
|
225
|
+
* was used. It is highly recommended to include null in the output type union
|
|
226
|
+
* and handle null values in post-execute code, or wrap the function with a `coalesce`
|
|
227
|
+
* function.
|
|
228
|
+
*
|
|
229
|
+
* ```ts
|
|
230
|
+
* const { sum } = db.fn
|
|
231
|
+
*
|
|
232
|
+
* db.selectFrom('toy')
|
|
233
|
+
* .select(sum<number | null>('price').as('total_price'))
|
|
234
|
+
* .execute()
|
|
235
|
+
* ```
|
|
236
|
+
*/
|
|
237
|
+
sum<O extends number | string | bigint | null = number | string | bigint, C extends SimpleReferenceExpression<DB, TB> = SimpleReferenceExpression<DB, TB>>(column: C): AggregateFunctionBuilder<DB, TB, O>;
|
|
238
|
+
}
|
|
239
|
+
declare type OutputBoundStringReference<DB, TB extends keyof DB, C extends StringReference<DB, TB>, O> = IsNever<O> extends true ? C : Equals<ExtractTypeFromReferenceExpression<DB, TB, C> | null, O | null> extends true ? C : never;
|
|
240
|
+
declare type StringReferenceBoundAggregateFunctionBuilder<DB, TB extends keyof DB, C extends StringReference<DB, TB>, O> = AggregateFunctionBuilder<DB, TB, ExtractTypeFromReferenceExpression<DB, TB, C> | (null extends O ? null : never)>;
|
|
241
|
+
export {};
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
/// <reference types="./function-module.d.ts" />
|
|
2
|
+
import { AggregateFunctionNode } from '../operation-node/aggregate-function-node.js';
|
|
3
|
+
import { parseCoalesce, } from '../parser/coalesce-parser.js';
|
|
4
|
+
import { parseSimpleReferenceExpression, } from '../parser/reference-parser.js';
|
|
5
|
+
import { RawBuilder } from '../raw-builder/raw-builder.js';
|
|
6
|
+
import { createQueryId } from '../util/query-id.js';
|
|
7
|
+
import { AggregateFunctionBuilder } from './aggregate-function-builder.js';
|
|
8
|
+
/**
|
|
9
|
+
* Helpers for type safe SQL function calls.
|
|
10
|
+
*
|
|
11
|
+
* You can always use the {@link sql} tag to call functions and build arbitrary
|
|
12
|
+
* expressions. This module simply has shortcuts for most common function calls.
|
|
13
|
+
*
|
|
14
|
+
* ### Examples
|
|
15
|
+
*
|
|
16
|
+
* ```ts
|
|
17
|
+
* const { count } = db.fn
|
|
18
|
+
*
|
|
19
|
+
* await db.selectFrom('person')
|
|
20
|
+
* .innerJoin('pet', 'pet.owner_id', 'person.id')
|
|
21
|
+
* .select([
|
|
22
|
+
* 'person.id',
|
|
23
|
+
* count('pet.id').as('pet_count')
|
|
24
|
+
* ])
|
|
25
|
+
* .groupBy('person.id')
|
|
26
|
+
* .having(count('pet.id'), '>', 10)
|
|
27
|
+
* .execute()
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* The generated SQL (PostgreSQL):
|
|
31
|
+
*
|
|
32
|
+
* ```sql
|
|
33
|
+
* select "person"."id", count("pet"."id") as "pet_count"
|
|
34
|
+
* from "person"
|
|
35
|
+
* inner join "pet" on "pet"."owner_id" = "person"."id"
|
|
36
|
+
* group by "person"."id"
|
|
37
|
+
* having count("pet"."id") > $1
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export class FunctionModule {
|
|
41
|
+
constructor() {
|
|
42
|
+
this.avg = this.avg.bind(this);
|
|
43
|
+
this.coalesce = this.coalesce.bind(this);
|
|
44
|
+
this.count = this.count.bind(this);
|
|
45
|
+
this.max = this.max.bind(this);
|
|
46
|
+
this.min = this.min.bind(this);
|
|
47
|
+
this.sum = this.sum.bind(this);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Calls the `avg` function for the column given as the argument.
|
|
51
|
+
*
|
|
52
|
+
* If this is used in a `select` statement the type of the selected expression
|
|
53
|
+
* will be `number | string` by default. This is because Kysely can't know the
|
|
54
|
+
* type the db driver outputs. Sometimes the output can be larger than the largest
|
|
55
|
+
* javascript number and a string is returned instead. Most drivers allow you
|
|
56
|
+
* to configure the output type of large numbers and Kysely can't know if you've
|
|
57
|
+
* done so.
|
|
58
|
+
*
|
|
59
|
+
* You can specify the output type of the expression by providing the type as
|
|
60
|
+
* the first type argument:
|
|
61
|
+
*
|
|
62
|
+
* ```ts
|
|
63
|
+
* const { avg } = db.fn
|
|
64
|
+
*
|
|
65
|
+
* db.selectFrom('toy')
|
|
66
|
+
* .select(avg<number>('price').as('avg_price'))
|
|
67
|
+
* .execute()
|
|
68
|
+
* ```
|
|
69
|
+
*
|
|
70
|
+
* Sometimes a null is returned, e.g. when row count is 0, and no `group by`
|
|
71
|
+
* was used. It is highly recommended to include null in the output type union
|
|
72
|
+
* and handle null values in post-execute code, or wrap the function with a `coalesce`
|
|
73
|
+
* function.
|
|
74
|
+
*
|
|
75
|
+
* ```ts
|
|
76
|
+
* const { avg } = db.fn
|
|
77
|
+
*
|
|
78
|
+
* db.selectFrom('toy')
|
|
79
|
+
* .select(avg<number | null>('price').as('avg_price'))
|
|
80
|
+
* .execute()
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
avg(column) {
|
|
84
|
+
return new AggregateFunctionBuilder({
|
|
85
|
+
aggregateFunctionNode: AggregateFunctionNode.create('avg', parseSimpleReferenceExpression(column)),
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Calls the `coalesce` function for given arguments.
|
|
90
|
+
*
|
|
91
|
+
* This function returns the first non-null value from left to right, commonly
|
|
92
|
+
* used to provide a default scalar for nullable columns or functions.
|
|
93
|
+
*
|
|
94
|
+
* ```ts
|
|
95
|
+
* const { coalesce, max } = db.fn
|
|
96
|
+
*
|
|
97
|
+
* db.selectFrom('person')
|
|
98
|
+
* .select(coalesce(max('age'), sql<number>`0`).as('max_age'))
|
|
99
|
+
* .where('first_name', '=', 'Jennifer')
|
|
100
|
+
* .execute()
|
|
101
|
+
* ```
|
|
102
|
+
*
|
|
103
|
+
* The generated SQL (postgres):
|
|
104
|
+
*
|
|
105
|
+
* ```sql
|
|
106
|
+
* select coalesce(max("age"), 0) as "max_age" from "person" where "first_name" = $1
|
|
107
|
+
* ```
|
|
108
|
+
*
|
|
109
|
+
* If this is used in a `select` statement the type of the selected expression
|
|
110
|
+
* is inferred in the same manner that the function computes. A union of arguments'
|
|
111
|
+
* types - if a non-nullable argument exists, it stops there (ignoring any further
|
|
112
|
+
* arguments' types) and exludes null from the final union type.
|
|
113
|
+
*
|
|
114
|
+
* Examples:
|
|
115
|
+
*
|
|
116
|
+
* `(string | null, number | null)` is inferred as `string | number | null`.
|
|
117
|
+
*
|
|
118
|
+
* `(string | null, number, Date | null)` is inferred as `string | number`.
|
|
119
|
+
*
|
|
120
|
+
* `(number, string | null)` is inferred as `number`.
|
|
121
|
+
*/
|
|
122
|
+
coalesce(value, ...otherValues) {
|
|
123
|
+
return new RawBuilder({
|
|
124
|
+
queryId: createQueryId(),
|
|
125
|
+
rawNode: parseCoalesce([value, ...otherValues]),
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Calls the `count` function for the column given as the argument.
|
|
130
|
+
*
|
|
131
|
+
* If this is used in a `select` statement the type of the selected expression
|
|
132
|
+
* will be `number | string | bigint` by default. This is because Kysely
|
|
133
|
+
* can't know the type the db driver outputs. Sometimes the output can be larger
|
|
134
|
+
* than the largest javascript number and a string is returned instead. Most
|
|
135
|
+
* drivers allow you to configure the output type of large numbers and Kysely
|
|
136
|
+
* can't know if you've done so.
|
|
137
|
+
*
|
|
138
|
+
* You can specify the output type of the expression by providing
|
|
139
|
+
* the type as the first type argument:
|
|
140
|
+
*
|
|
141
|
+
* ```ts
|
|
142
|
+
* const { count } = db.fn
|
|
143
|
+
*
|
|
144
|
+
* db.selectFrom('toy')
|
|
145
|
+
* .select(count<number>('id').as('num_toys'))
|
|
146
|
+
* .execute()
|
|
147
|
+
* ```
|
|
148
|
+
*
|
|
149
|
+
* You can limit column range:
|
|
150
|
+
*
|
|
151
|
+
* ```ts
|
|
152
|
+
* db.selectFrom('toy')
|
|
153
|
+
* .select(qb => qb.fn.count<number>('id').as('num_toys'))
|
|
154
|
+
* .execute()
|
|
155
|
+
* ```
|
|
156
|
+
*/
|
|
157
|
+
count(column) {
|
|
158
|
+
return new AggregateFunctionBuilder({
|
|
159
|
+
aggregateFunctionNode: AggregateFunctionNode.create('count', parseSimpleReferenceExpression(column)),
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
max(column) {
|
|
163
|
+
return new AggregateFunctionBuilder({
|
|
164
|
+
aggregateFunctionNode: AggregateFunctionNode.create('max', parseSimpleReferenceExpression(column)),
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
min(column) {
|
|
168
|
+
return new AggregateFunctionBuilder({
|
|
169
|
+
aggregateFunctionNode: AggregateFunctionNode.create('min', parseSimpleReferenceExpression(column)),
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Calls the `sum` function for the column given as the argument.
|
|
174
|
+
*
|
|
175
|
+
* If this is used in a `select` statement the type of the selected expression
|
|
176
|
+
* will be `number | string | bigint` by default. This is because Kysely
|
|
177
|
+
* can't know the type the db driver outputs. Sometimes the output can be larger
|
|
178
|
+
* than the largest javascript number and a string is returned instead. Most
|
|
179
|
+
* drivers allow you to configure the output type of large numbers and Kysely
|
|
180
|
+
* can't know if you've done so.
|
|
181
|
+
*
|
|
182
|
+
* You can specify the output type of the expression by providing
|
|
183
|
+
* the type as the first type argument:
|
|
184
|
+
*
|
|
185
|
+
* ```ts
|
|
186
|
+
* const { sum } = db.fn
|
|
187
|
+
*
|
|
188
|
+
* db.selectFrom('toy')
|
|
189
|
+
* .select(sum<number>('price').as('total_price'))
|
|
190
|
+
* .execute()
|
|
191
|
+
* ```
|
|
192
|
+
*
|
|
193
|
+
* Sometimes a null is returned, e.g. when row count is 0, and no `group by`
|
|
194
|
+
* was used. It is highly recommended to include null in the output type union
|
|
195
|
+
* and handle null values in post-execute code, or wrap the function with a `coalesce`
|
|
196
|
+
* function.
|
|
197
|
+
*
|
|
198
|
+
* ```ts
|
|
199
|
+
* const { sum } = db.fn
|
|
200
|
+
*
|
|
201
|
+
* db.selectFrom('toy')
|
|
202
|
+
* .select(sum<number | null>('price').as('total_price'))
|
|
203
|
+
* .execute()
|
|
204
|
+
* ```
|
|
205
|
+
*/
|
|
206
|
+
sum(column) {
|
|
207
|
+
return new AggregateFunctionBuilder({
|
|
208
|
+
aggregateFunctionNode: AggregateFunctionNode.create('sum', parseSimpleReferenceExpression(column)),
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
}
|
|
@@ -1,31 +1,32 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Expression } from '../expression/expression.js';
|
|
2
|
+
import { ComparisonOperatorExpression, HavingGrouper, OperandValueExpressionOrList } from '../parser/binary-operation-parser.js';
|
|
2
3
|
import { ReferenceExpression } from '../parser/reference-parser.js';
|
|
3
|
-
import {
|
|
4
|
+
import { ExistsExpression } from '../parser/unary-operation-parser.js';
|
|
4
5
|
export interface HavingInterface<DB, TB extends keyof DB> {
|
|
5
6
|
/**
|
|
6
7
|
* Just like {@link WhereInterface.where | where} but adds a `having` statement
|
|
7
8
|
* instead of a `where` statement.
|
|
8
9
|
*/
|
|
9
|
-
having<RE extends ReferenceExpression<DB, TB>>(lhs: RE, op:
|
|
10
|
+
having<RE extends ReferenceExpression<DB, TB>>(lhs: RE, op: ComparisonOperatorExpression, rhs: OperandValueExpressionOrList<DB, TB, RE>): HavingInterface<DB, TB>;
|
|
10
11
|
having(grouper: HavingGrouper<DB, TB>): HavingInterface<DB, TB>;
|
|
11
|
-
having(
|
|
12
|
+
having(expression: Expression<any>): HavingInterface<DB, TB>;
|
|
12
13
|
/**
|
|
13
14
|
* Just like {@link WhereInterface.whereRef | whereRef} but adds a `having` statement
|
|
14
15
|
* instead of a `where` statement.
|
|
15
16
|
*/
|
|
16
|
-
havingRef(lhs: ReferenceExpression<DB, TB>, op:
|
|
17
|
+
havingRef(lhs: ReferenceExpression<DB, TB>, op: ComparisonOperatorExpression, rhs: ReferenceExpression<DB, TB>): HavingInterface<DB, TB>;
|
|
17
18
|
/**
|
|
18
19
|
* Just like {@link WhereInterface.orWhere | orWhere} but adds a `having` statement
|
|
19
20
|
* instead of a `where` statement.
|
|
20
21
|
*/
|
|
21
|
-
orHaving<RE extends ReferenceExpression<DB, TB>>(lhs: RE, op:
|
|
22
|
+
orHaving<RE extends ReferenceExpression<DB, TB>>(lhs: RE, op: ComparisonOperatorExpression, rhs: OperandValueExpressionOrList<DB, TB, RE>): HavingInterface<DB, TB>;
|
|
22
23
|
orHaving(grouper: HavingGrouper<DB, TB>): HavingInterface<DB, TB>;
|
|
23
|
-
orHaving(
|
|
24
|
+
orHaving(expression: Expression<any>): HavingInterface<DB, TB>;
|
|
24
25
|
/**
|
|
25
26
|
* Just like {@link WhereInterface.orWhereRef | orWhereRef} but adds a `having` statement
|
|
26
27
|
* instead of a `where` statement.
|
|
27
28
|
*/
|
|
28
|
-
orHavingRef(lhs: ReferenceExpression<DB, TB>, op:
|
|
29
|
+
orHavingRef(lhs: ReferenceExpression<DB, TB>, op: ComparisonOperatorExpression, rhs: ReferenceExpression<DB, TB>): HavingInterface<DB, TB>;
|
|
29
30
|
/**
|
|
30
31
|
* Just like {@link WhereInterface.whereExists | whereExists} but adds a `having` statement
|
|
31
32
|
* instead of a `where` statement.
|
|
@@ -3,7 +3,7 @@ import { CompiledQuery } from '../query-compiler/compiled-query.js';
|
|
|
3
3
|
import { SelectExpression } from '../parser/select-parser.js';
|
|
4
4
|
import { InsertObject } from '../parser/insert-values-parser.js';
|
|
5
5
|
import { InsertQueryNode } from '../operation-node/insert-query-node.js';
|
|
6
|
-
import {
|
|
6
|
+
import { MergePartial, SingleResultType } from '../util/type-utils.js';
|
|
7
7
|
import { MutationObject } from '../parser/update-set-parser.js';
|
|
8
8
|
import { Compilable } from '../util/compilable.js';
|
|
9
9
|
import { QueryExecutor } from '../query-executor/query-executor.js';
|
|
@@ -12,12 +12,13 @@ import { InsertResult } from './insert-result.js';
|
|
|
12
12
|
import { KyselyPlugin } from '../plugin/kysely-plugin.js';
|
|
13
13
|
import { ReturningRow } from '../parser/returning-parser.js';
|
|
14
14
|
import { NoResultErrorConstructor } from './no-result-error.js';
|
|
15
|
-
import {
|
|
15
|
+
import { ExpressionOrFactory } from '../parser/expression-parser.js';
|
|
16
16
|
import { ReturningInterface } from './returning-interface.js';
|
|
17
17
|
import { OnConflictBuilder, OnConflictDoNothingBuilder, OnConflictUpdateBuilder } from './on-conflict-builder.js';
|
|
18
18
|
import { Selectable } from '../util/column-type.js';
|
|
19
19
|
import { Explainable, ExplainFormat } from '../util/explainable.js';
|
|
20
|
-
|
|
20
|
+
import { Expression } from '../expression/expression.js';
|
|
21
|
+
export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements ReturningInterface<DB, TB, O>, OperationNodeSource, Compilable<O>, Explainable {
|
|
21
22
|
#private;
|
|
22
23
|
constructor(props: InsertQueryBuilderProps);
|
|
23
24
|
/**
|
|
@@ -189,7 +190,7 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
189
190
|
* select "pet"."name" from "pet"
|
|
190
191
|
* ```
|
|
191
192
|
*/
|
|
192
|
-
expression(expression:
|
|
193
|
+
expression(expression: ExpressionOrFactory<DB, TB, any>): InsertQueryBuilder<DB, TB, O>;
|
|
193
194
|
/**
|
|
194
195
|
* Changes an `insert into` query to an `insert ignore into` query.
|
|
195
196
|
*
|
|
@@ -521,7 +522,7 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
521
522
|
*/
|
|
522
523
|
withPlugin(plugin: KyselyPlugin): InsertQueryBuilder<DB, TB, O>;
|
|
523
524
|
toOperationNode(): InsertQueryNode;
|
|
524
|
-
compile(): CompiledQuery
|
|
525
|
+
compile(): CompiledQuery<O>;
|
|
525
526
|
/**
|
|
526
527
|
* Executes the query and returns an array of rows.
|
|
527
528
|
*
|
|
@@ -558,7 +559,7 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
558
559
|
* explain format=json insert into `person` (`id`, `first_name`, `last_name`) values (?, ?, ?) (?, ?, ?)
|
|
559
560
|
* ```
|
|
560
561
|
*/
|
|
561
|
-
explain<ER extends Record<string, any> = Record<string, any>>(format?: ExplainFormat, options?:
|
|
562
|
+
explain<ER extends Record<string, any> = Record<string, any>>(format?: ExplainFormat, options?: Expression<any>): Promise<ER[]>;
|
|
562
563
|
}
|
|
563
564
|
export interface InsertQueryBuilderProps {
|
|
564
565
|
readonly queryId: QueryId;
|
|
@@ -9,7 +9,7 @@ import { freeze } from '../util/object-utils.js';
|
|
|
9
9
|
import { OnDuplicateKeyNode } from '../operation-node/on-duplicate-key-node.js';
|
|
10
10
|
import { InsertResult } from './insert-result.js';
|
|
11
11
|
import { NoResultError } from './no-result-error.js';
|
|
12
|
-
import {
|
|
12
|
+
import { parseExpression, } from '../parser/expression-parser.js';
|
|
13
13
|
import { ColumnNode } from '../operation-node/column-node.js';
|
|
14
14
|
import { OnConflictBuilder, } from './on-conflict-builder.js';
|
|
15
15
|
import { OnConflictNode } from '../operation-node/on-conflict-node.js';
|
|
@@ -81,7 +81,7 @@ export class InsertQueryBuilder {
|
|
|
81
81
|
return new InsertQueryBuilder({
|
|
82
82
|
...this.#props,
|
|
83
83
|
queryNode: InsertQueryNode.cloneWith(this.#props.queryNode, {
|
|
84
|
-
values:
|
|
84
|
+
values: parseExpression(expression),
|
|
85
85
|
}),
|
|
86
86
|
});
|
|
87
87
|
}
|
|
@@ -412,15 +412,17 @@ export class InsertQueryBuilder {
|
|
|
412
412
|
* Also see the {@link executeTakeFirst} and {@link executeTakeFirstOrThrow} methods.
|
|
413
413
|
*/
|
|
414
414
|
async execute() {
|
|
415
|
-
const
|
|
416
|
-
const query =
|
|
417
|
-
const result = await this.#props.executor.executeQuery(
|
|
415
|
+
const compiledQuery = this.compile();
|
|
416
|
+
const query = compiledQuery.query;
|
|
417
|
+
const result = await this.#props.executor.executeQuery(compiledQuery, this.#props.queryId);
|
|
418
418
|
if (this.#props.executor.adapter.supportsReturning && query.returning) {
|
|
419
419
|
return result.rows;
|
|
420
420
|
}
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
421
|
+
return [
|
|
422
|
+
new InsertResult(result.insertId,
|
|
423
|
+
// TODO: remove numUpdatedOrDeletedRows.
|
|
424
|
+
result.numAffectedRows ?? result.numUpdatedOrDeletedRows),
|
|
425
|
+
];
|
|
424
426
|
}
|
|
425
427
|
/**
|
|
426
428
|
* Executes the query and returns the first result or undefined if
|
|
@@ -465,7 +467,7 @@ export class InsertQueryBuilder {
|
|
|
465
467
|
const builder = new InsertQueryBuilder({
|
|
466
468
|
...this.#props,
|
|
467
469
|
queryNode: InsertQueryNode.cloneWith(this.#props.queryNode, {
|
|
468
|
-
explain: ExplainNode.create(format, options),
|
|
470
|
+
explain: ExplainNode.create(format, options?.toOperationNode()),
|
|
469
471
|
}),
|
|
470
472
|
});
|
|
471
473
|
return await builder.execute();
|
|
@@ -7,6 +7,9 @@
|
|
|
7
7
|
* need to use {@link ReturningInterface.returning} or {@link ReturningInterface.returningAll}
|
|
8
8
|
* to get out the inserted id.
|
|
9
9
|
*
|
|
10
|
+
* {@link numInsertedOrUpdatedRows} holds the number of (actually) inserted rows.
|
|
11
|
+
* On MySQL, updated rows are counted twice when using `on duplicate key update`.
|
|
12
|
+
*
|
|
10
13
|
* ### Examples
|
|
11
14
|
*
|
|
12
15
|
* ```ts
|
|
@@ -20,9 +23,13 @@
|
|
|
20
23
|
*/
|
|
21
24
|
export declare class InsertResult {
|
|
22
25
|
#private;
|
|
23
|
-
constructor(insertId: bigint | undefined);
|
|
26
|
+
constructor(insertId: bigint | undefined, numInsertedOrUpdatedRows: bigint | undefined);
|
|
24
27
|
/**
|
|
25
28
|
* The auto incrementing primary key
|
|
26
29
|
*/
|
|
27
30
|
get insertId(): bigint | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* Affected rows count.
|
|
33
|
+
*/
|
|
34
|
+
get numInsertedOrUpdatedRows(): bigint | undefined;
|
|
28
35
|
}
|
|
@@ -8,6 +8,9 @@
|
|
|
8
8
|
* need to use {@link ReturningInterface.returning} or {@link ReturningInterface.returningAll}
|
|
9
9
|
* to get out the inserted id.
|
|
10
10
|
*
|
|
11
|
+
* {@link numInsertedOrUpdatedRows} holds the number of (actually) inserted rows.
|
|
12
|
+
* On MySQL, updated rows are counted twice when using `on duplicate key update`.
|
|
13
|
+
*
|
|
11
14
|
* ### Examples
|
|
12
15
|
*
|
|
13
16
|
* ```ts
|
|
@@ -21,8 +24,10 @@
|
|
|
21
24
|
*/
|
|
22
25
|
export class InsertResult {
|
|
23
26
|
#insertId;
|
|
24
|
-
|
|
27
|
+
#numInsertedOrUpdatedRows;
|
|
28
|
+
constructor(insertId, numInsertedOrUpdatedRows) {
|
|
25
29
|
this.#insertId = insertId;
|
|
30
|
+
this.#numInsertedOrUpdatedRows = numInsertedOrUpdatedRows;
|
|
26
31
|
}
|
|
27
32
|
/**
|
|
28
33
|
* The auto incrementing primary key
|
|
@@ -30,4 +35,10 @@ export class InsertResult {
|
|
|
30
35
|
get insertId() {
|
|
31
36
|
return this.#insertId;
|
|
32
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* Affected rows count.
|
|
40
|
+
*/
|
|
41
|
+
get numInsertedOrUpdatedRows() {
|
|
42
|
+
return this.#numInsertedOrUpdatedRows;
|
|
43
|
+
}
|
|
33
44
|
}
|