Skip to content

Commit

Permalink
add update from queries
Browse files Browse the repository at this point in the history
  • Loading branch information
koskimas committed Apr 16, 2022
1 parent a05a2b9 commit 17fb3a2
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 30 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kysely",
"version": "0.17.3",
"version": "0.18.0",
"description": "Type safe SQL query builder",
"repository": {
"type": "git",
Expand Down
1 change: 1 addition & 0 deletions src/operation-node/operation-node-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ export class OperationNodeTransformer {
return {
kind: 'UpdateQueryNode',
table: this.transformNode(node.table),
from: this.transformNode(node.from),
joins: this.transformNodeList(node.joins),
where: this.transformNode(node.where),
updates: this.transformNodeList(node.updates),
Expand Down
18 changes: 16 additions & 2 deletions src/operation-node/update-query-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import { ReturningNode } from './returning-node.js'
import { ValueListNode } from './value-list-node.js'
import { WhereNode } from './where-node.js'
import { WithNode } from './with-node.js'
import { FromNode } from './from-node.js'

export type UpdateValuesNode = ValueListNode | PrimitiveValueListNode

export interface UpdateQueryNode extends OperationNode {
readonly kind: 'UpdateQueryNode'
readonly table: TableExpressionNode
readonly from?: FromNode
readonly joins?: ReadonlyArray<JoinNode>
readonly where?: WhereNode
readonly updates?: ReadonlyArray<ColumnUpdateNode>
Expand All @@ -30,11 +32,23 @@ export const UpdateQueryNode = freeze({
},

create(table: TableExpressionNode, withNode?: WithNode): UpdateQueryNode {
return {
return freeze({
kind: 'UpdateQueryNode',
table,
...(withNode && { with: withNode }),
}
})
},

cloneWithFromItems(
updateQuery: UpdateQueryNode,
fromItems: ReadonlyArray<TableExpressionNode>
): UpdateQueryNode {
return freeze({
...updateQuery,
from: updateQuery.from
? FromNode.cloneWithFroms(updateQuery.from, fromItems)
: FromNode.create(fromItems),
})
},

cloneWithUpdates(
Expand Down
6 changes: 3 additions & 3 deletions src/parser/update-set-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { ColumnUpdateNode } from '../operation-node/column-update-node.js'
import { UpdateKeys, UpdateType } from '../util/column-type.js'
import { parseValueExpression, ValueExpression } from './value-parser.js'

export type MutationObject<DB, TB extends keyof DB> = {
[C in UpdateKeys<DB[TB]>]?: ValueExpression<DB, TB, UpdateType<DB[TB][C]>>
export type MutationObject<DB, TB extends keyof DB, TM extends keyof DB> = {
[C in UpdateKeys<DB[TM]>]?: ValueExpression<DB, TB, UpdateType<DB[TM][C]>>
}

export function parseUpdateObject(
row: MutationObject<any, any>
row: MutationObject<any, any, any>
): ReadonlyArray<ColumnUpdateNode> {
return Object.entries(row)
.filter(([_, value]) => value !== undefined)
Expand Down
4 changes: 2 additions & 2 deletions src/parser/with-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export type QueryCreatorWithCommonTableExpression<
type CommonTableExpressionOutput<DB, CN extends string> =
| SelectQueryBuilder<DB, any, ExtractRowFromCommonTableExpressionName<CN>>
| InsertQueryBuilder<DB, any, ExtractRowFromCommonTableExpressionName<CN>>
| UpdateQueryBuilder<DB, any, ExtractRowFromCommonTableExpressionName<CN>>
| UpdateQueryBuilder<DB, any, any, ExtractRowFromCommonTableExpressionName<CN>>
| DeleteQueryBuilder<DB, any, ExtractRowFromCommonTableExpressionName<CN>>
| RawBuilder<ExtractRowFromCommonTableExpressionName<CN>>

Expand All @@ -55,7 +55,7 @@ type ExtractRowFromCommonTableExpression<CTE> = CTE extends (
? QO
: Q extends InsertQueryBuilder<any, any, infer QO>
? QO
: Q extends UpdateQueryBuilder<any, any, infer QO>
: Q extends UpdateQueryBuilder<any, any, any, infer QO>
? QO
: Q extends DeleteQueryBuilder<any, any, infer QO>
? QO
Expand Down
2 changes: 1 addition & 1 deletion src/query-builder/insert-query-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ export class InsertQueryBuilder<DB, TB extends keyof DB, O>
* ```
*/
onDuplicateKeyUpdate(
updates: MutationObject<DB, TB>
updates: MutationObject<DB, TB, TB>
): InsertQueryBuilder<DB, TB, O> {
return new InsertQueryBuilder({
...this.#props,
Expand Down
6 changes: 5 additions & 1 deletion src/query-builder/on-conflict-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,11 @@ export class OnConflictBuilder<DB, TB extends keyof DB>
* ```
*/
doUpdateSet(
updates: MutationObject<OnConflictDatabase<DB, TB>, OnConflictTables<TB>>
updates: MutationObject<
OnConflictDatabase<DB, TB>,
OnConflictTables<TB>,
OnConflictTables<TB>
>
): OnConflictUpdateBuilder<OnConflictDatabase<DB, TB>, OnConflictTables<TB>> {
return new OnConflictUpdateBuilder({
...this.#props,
Expand Down
Loading

0 comments on commit 17fb3a2

Please sign in to comment.