Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
wirekang committed Dec 29, 2022
2 parents 71419b5 + 71507c4 commit b2951f1
Show file tree
Hide file tree
Showing 13 changed files with 473 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ You can find a more thorough introduction [here](https://www.jakso.me/blog/kysel
- [Installation](#installation)
- [3rd party dialects](#3rd-party-dialects)
- [Minimal example](#minimal-example)
- [Playground](#playground)
- [Generating types](#generating-types)
- [Query examples](#query-examples)
- [Select queries](#select-queries)
Expand Down Expand Up @@ -180,6 +181,10 @@ type InsertablePerson = Insertable<PersonTable>
type UpdateablePerson = Updateable<PersonTable>
```
# Playground
[@wirekang](https://github.com/wirekang) has created a [playground for Kysely](https://wirekang.github.io/kysely-playground/#eyJkaWFsZWN0IjoibXlzcWwiLCJ0cyI6ImludGVyZmFjZSBEQiB7XG4gIHVzZXI6IFVzZXJUYWJsZVxufVxuXG5pbnRlcmZhY2UgVXNlclRhYmxlIHtcbiAgaWQ6IEdlbmVyYXRlZDxzdHJpbmc+XG4gIGZpcnN0X25hbWU6IHN0cmluZyB8IG51bGxcbiAgbGFzdF9uYW1lOiBzdHJpbmcgfCBudWxsXG4gIGNyZWF0ZWRfYXQ6IEdlbmVyYXRlZDxEYXRlPlxufVxuXG5yZXN1bHQgPSBreXNlbHlcbiAgLnNlbGVjdEZyb20oXCJ1c2VyXCIpXG4gIC5zZWxlY3RBbGwoKVxuICAub3JkZXJCeShcImNyZWF0ZWRfYXRcIikifQ==). You can use to quickly test stuff out and for creating code examples for your issues, PRs and discord messages.
# Generating types
If you want to generate the table types automatically from the database schema please
Expand Down
14 changes: 14 additions & 0 deletions src/operation-node/on-conflict-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,18 @@ export const OnConflictNode = freeze({
: WhereNode.create(operation),
})
},

cloneWithoutIndexWhere(node: OnConflictNode): OnConflictNode {
return freeze({
...node,
indexWhere: undefined,
})
},

cloneWithoutUpdateWhere(node: OnConflictNode): OnConflictNode {
return freeze({
...node,
updateWhere: undefined,
})
},
})
7 changes: 7 additions & 0 deletions src/operation-node/query-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,11 @@ export const QueryNode = freeze({
: ReturningNode.create(selections),
})
},

cloneWithoutWhere<T extends FilterableQueryNode>(node: T): T {
return freeze({
...node,
where: undefined,
})
},
})
28 changes: 28 additions & 0 deletions src/operation-node/select-query-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,32 @@ export const SelectQueryNode = freeze({
explain,
})
},

cloneWithoutSelections(select: SelectQueryNode): SelectQueryNode {
return freeze({
...select,
selections: [],
})
},

cloneWithoutLimit(select: SelectQueryNode): SelectQueryNode {
return freeze({
...select,
limit: undefined,
})
},

cloneWithoutOffset(select: SelectQueryNode): SelectQueryNode {
return freeze({
...select,
offset: undefined,
})
},

cloneWithoutOrderBy(select: SelectQueryNode): SelectQueryNode {
return freeze({
...select,
orderBy: undefined,
})
},
})
4 changes: 3 additions & 1 deletion src/parser/select-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ type ExtractTypeFromSelectExpression<
? QA extends A
? ValueType<O>
: never
: SE extends (qb: any) => AliasedSelectQueryBuilder<any, any, infer O, infer QA>
: SE extends (
qb: any
) => AliasedSelectQueryBuilder<any, any, infer O, infer QA>
? QA extends A
? ValueType<O>
: never
Expand Down
7 changes: 7 additions & 0 deletions src/query-builder/delete-query-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ export class DeleteQueryBuilder<DB, TB extends keyof DB, O>
})
}

clearWhere(): DeleteQueryBuilder<DB, TB, O> {
return new DeleteQueryBuilder<DB, TB, O>({
...this.#props,
queryNode: QueryNode.cloneWithoutWhere(this.#props.queryNode),
})
}

/**
* Joins another table to the query using an inner join.
*
Expand Down
18 changes: 18 additions & 0 deletions src/query-builder/on-conflict-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,15 @@ export class OnConflictBuilder<DB, TB extends keyof DB>
})
}

clearWhere(): OnConflictBuilder<DB, TB> {
return new OnConflictBuilder<DB, TB>({
...this.#props,
onConflictNode: OnConflictNode.cloneWithoutIndexWhere(
this.#props.onConflictNode
),
})
}

/**
* Adds the "do nothing" conflict action.
*
Expand Down Expand Up @@ -524,6 +533,15 @@ export class OnConflictUpdateBuilder<DB, TB extends keyof DB>
})
}

clearWhere(): OnConflictUpdateBuilder<DB, TB> {
return new OnConflictUpdateBuilder({
...this.#props,
onConflictNode: OnConflictNode.cloneWithoutUpdateWhere(
this.#props.onConflictNode
),
})
}

toOperationNode(): OnConflictNode {
return this.#props.onConflictNode
}
Expand Down
108 changes: 108 additions & 0 deletions src/query-builder/select-query-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1465,6 +1465,114 @@ export class SelectQueryBuilder<DB, TB extends keyof DB, O>
})
}

/**
* Clears all select clauses from the query.
*
* ### Examples
*
* ```ts
* db.selectFrom('person')
* .select(['id', 'first_name'])
* .clearSelect()
* .select(['id','gender'])
* ```
*
* The generated SQL(PostgreSQL):
*
* ```sql
* select "id", "gender" from "person"
* ```
*/
clearSelect(): SelectQueryBuilder<DB, TB, {}> {
return new SelectQueryBuilder<DB, TB, {}>({
...this.#props,
queryNode: SelectQueryNode.cloneWithoutSelections(this.#props.queryNode),
})
}

clearWhere(): SelectQueryBuilder<DB, TB, O> {
return new SelectQueryBuilder<DB, TB, O>({
...this.#props,
queryNode: QueryNode.cloneWithoutWhere(this.#props.queryNode),
})
}

/**
* Clears limit clause from the query.
*
* ### Examples
*
* ```ts
* db.selectFrom('person')
* .selectAll()
* .limit(10)
* .clearLimit()
* ```
*
* The generated SQL(PostgreSQL):
*
* ```sql
* select * from "person"
* ```
*/
clearLimit(): SelectQueryBuilder<DB, TB, O> {
return new SelectQueryBuilder<DB, TB, O>({
...this.#props,
queryNode: SelectQueryNode.cloneWithoutLimit(this.#props.queryNode),
})
}

/**
* Clears offset clause from the query.
*
* ### Examples
*
* ```ts
* db.selectFrom('person')
* .selectAll()
* .limit(10)
* .offset(20)
* .clearOffset()
* ```
*
* The generated SQL(PostgreSQL):
*
* ```sql
* select * from "person" limit 10
* ```
*/
clearOffset(): SelectQueryBuilder<DB, TB, O> {
return new SelectQueryBuilder<DB, TB, O>({
...this.#props,
queryNode: SelectQueryNode.cloneWithoutOffset(this.#props.queryNode),
})
}

/**
* Clears all `order by` clauses from the query.
*
* ### Examples
*
* ```ts
* db.selectFrom('person')
* .selectAll()
* .orderBy('id')
* .clearOrderBy()
* ```
*
* The generated SQL(PostgreSQL):
*
* ```sql
* select * from "person"
* ```
*/
clearOrderBy(): SelectQueryBuilder<DB, TB, O> {
return new SelectQueryBuilder<DB, TB, O>({
...this.#props,
queryNode: SelectQueryNode.cloneWithoutOrderBy(this.#props.queryNode),
})
}

/**
* Simply calls the given function passing `this` as the only argument.
*
Expand Down
7 changes: 7 additions & 0 deletions src/query-builder/update-query-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,13 @@ export class UpdateQueryBuilder<DB, UT extends keyof DB, TB extends keyof DB, O>
})
}

clearWhere(): UpdateQueryBuilder<DB, UT, TB, O> {
return new UpdateQueryBuilder({
...this.#props,
queryNode: QueryNode.cloneWithoutWhere(this.#props.queryNode),
})
}

/**
* Adds a from clause to the update query.
*
Expand Down
20 changes: 20 additions & 0 deletions src/query-builder/where-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,4 +419,24 @@ export interface WhereInterface<DB, TB extends keyof DB> {
* Just like {@link whereExists} but creates an `or not exists` clause.
*/
orWhereNotExists(arg: ExistsExpression<DB, TB>): WhereInterface<DB, TB>

/**
* Clears all where clauses from the query.
*
* ### Examples
*
* ```ts
* db.selectFrom('person')
* .selectAll()
* .where('id','=',42)
* .clearWhere()
* ```
*
* The generated SQL(PostgreSQL):
*
* ```sql
* select * from "person"
* ```
*/
clearWhere(): WhereInterface<DB, TB>
}
5 changes: 4 additions & 1 deletion src/raw-builder/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,10 @@ export interface Sql {
* BEFORE $1::varchar, (1 == 1)::varchar, (select * from "person")::varchar, false::varchar, "first_name" AFTER
* ```
*/
join(array: readonly unknown[], separator?: RawBuilder<any>): RawBuilder<unknown>
join(
array: readonly unknown[],
separator?: RawBuilder<any>
): RawBuilder<unknown>
}

export const sql: Sql = Object.assign(
Expand Down
Loading

0 comments on commit b2951f1

Please sign in to comment.