Skip to content

Commit

Permalink
Merge branch 'master' into mssql-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
igalklebanov authored Dec 31, 2023
2 parents c38303d + b481619 commit c88e914
Show file tree
Hide file tree
Showing 24 changed files with 422 additions and 105 deletions.
14 changes: 4 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,10 @@
},
"scripts": {
"clean": "rm -rf dist & rm -rf test/node/dist & rm -rf test/browser/bundle.js & rm -rf helpers",
"test": "npm run build && npm run test:node:build && concurrently -n node,typings,esmimports \"npm run test:node\" \"npm run test:typings\" \"npm run test:esmimports\"",
"test": "npm run build && npm run test:node:build && npm run test:node:run && npm run test:typings && npm run test:esmimports",
"test:node:build": "tsc -p test/node",
"test:node": "concurrently -n postgres,mysql,mssql,sqlite \"npm run test:node:postgres\" \"npm run test:node:mysql\" \"npm run test:node:mssql\" \"npm run test:node:sqlite\"",
"test:node:postgres": "cross-env DIALECT=postgres npm run test:node:run",
"test:node:mysql": "cross-env DIALECT=mysql npm run test:node:run",
"test:node:mssql": "cross-env DIALECT=mssql npm run test:node:run",
"test:node:sqlite": "cross-env DIALECT=sqlite npm run test:node:run",
"test:node:run": "mocha --reporter min --timeout 15000 test/node/dist/**/*.test.js",
"test:node": "npm run build && npm run test:node:build && npm run test:node:run",
"test:node:run": "mocha --timeout 15000 test/node/dist/**/*.test.js",
"test:browser:build": "rm -rf test/browser/bundle.js && esbuild test/browser/main.ts --bundle --outfile=test/browser/bundle.js",
"test:browser": "npm run build && npm run test:browser:build && node test/browser/test.js",
"test:bun": "npm run build && cd test/bun && bun install && bun run test",
Expand All @@ -57,7 +53,7 @@
"test:esmimports": "node scripts/check-esm-imports.js",
"test:esbuild": "esbuild --bundle --platform=node --external:pg-native dist/esm/index.js --outfile=/dev/null",
"prettier": "prettier --write 'src/**/*.ts' 'test/**/*.ts'",
"build": "npm run clean && concurrently -n esm,cjs \"npm run build:esm\" \"npm run build:cjs\" && concurrently -n modulefix,docfix \"npm run script:module-fixup\" \"npm run script:copy-interface-doc\"",
"build": "npm run clean && npm run build:esm && npm run build:cjs && npm run script:module-fixup && npm run script:copy-interface-doc",
"build:esm": "tsc -p tsconfig.json && npm run script:add-deno-type-references",
"build:cjs": "tsc -p tsconfig-cjs.json",
"script:module-fixup": "node scripts/module-fixup.js",
Expand Down Expand Up @@ -87,8 +83,6 @@
"chai": "^4.3.7",
"chai-as-promised": "^7.1.1",
"chai-subset": "^1.6.0",
"concurrently": "^8.1.0",
"cross-env": "^7.0.3",
"esbuild": "^0.17.19",
"mocha": "^10.2.0",
"mysql2": "^3.3.3",
Expand Down
19 changes: 16 additions & 3 deletions src/expression/expression-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,20 @@ export class ExpressionWrapper<DB, TB extends keyof DB, T>
* This method call doesn't change the SQL in any way. This methods simply
* returns a copy of this `ExpressionWrapper` with a new output type.
*/
$castTo<T>(): ExpressionWrapper<DB, TB, T> {
$castTo<C>(): ExpressionWrapper<DB, TB, C> {
return new ExpressionWrapper(this.#node)
}

/**
* Omit null from the expression's type.
*
* This function can be useful in cases where you know an expression can't be
* null, but Kysely is unable to infer it.
*
* This method call doesn't change the SQL in any way. This methods simply
* returns a copy of `this` with a new output type.
*/
$notNull(): ExpressionWrapper<DB, TB, Exclude<T, null>> {
return new ExpressionWrapper(this.#node)
}

Expand Down Expand Up @@ -352,7 +365,7 @@ export class OrWrapper<DB, TB extends keyof DB, T extends SqlBool>
* This method call doesn't change the SQL in any way. This methods simply
* returns a copy of this `OrWrapper` with a new output type.
*/
$castTo<T extends SqlBool>(): OrWrapper<DB, TB, T> {
$castTo<C extends SqlBool>(): OrWrapper<DB, TB, C> {
return new OrWrapper(this.#node)
}

Expand Down Expand Up @@ -436,7 +449,7 @@ export class AndWrapper<DB, TB extends keyof DB, T extends SqlBool>
* This method call doesn't change the SQL in any way. This methods simply
* returns a copy of this `AndWrapper` with a new output type.
*/
$castTo<T extends SqlBool>(): AndWrapper<DB, TB, T> {
$castTo<C extends SqlBool>(): AndWrapper<DB, TB, C> {
return new AndWrapper(this.#node)
}

Expand Down
1 change: 1 addition & 0 deletions src/operation-node/insert-query-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface InsertQueryNode extends OperationNode {
readonly ignore?: boolean
readonly replace?: boolean
readonly explain?: ExplainNode
readonly defaultValues?: boolean
}

/**
Expand Down
7 changes: 3 additions & 4 deletions src/operation-node/limit-node.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { freeze } from '../util/object-utils.js'
import { OperationNode } from './operation-node.js'
import { ValueNode } from './value-node.js'

export interface LimitNode extends OperationNode {
readonly kind: 'LimitNode'
readonly limit: ValueNode
readonly limit: OperationNode
}

/**
Expand All @@ -15,10 +14,10 @@ export const LimitNode = freeze({
return node.kind === 'LimitNode'
},

create(limit: number): LimitNode {
create(limit: OperationNode): LimitNode {
return freeze({
kind: 'LimitNode',
limit: ValueNode.create(limit),
limit,
})
},
})
7 changes: 3 additions & 4 deletions src/operation-node/offset-node.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { freeze } from '../util/object-utils.js'
import { OperationNode } from './operation-node.js'
import { ValueNode } from './value-node.js'

export interface OffsetNode extends OperationNode {
readonly kind: 'OffsetNode'
readonly offset: ValueNode
readonly offset: OperationNode
}

/**
Expand All @@ -15,10 +14,10 @@ export const OffsetNode = freeze({
return node.kind === 'OffsetNode'
},

create(offset: number): OffsetNode {
create(offset: OperationNode): OffsetNode {
return freeze({
kind: 'OffsetNode',
offset: ValueNode.create(offset),
offset,
})
},
})
2 changes: 2 additions & 0 deletions src/operation-node/operation-node-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ export class OperationNodeTransformer {
ignore: node.ignore,
replace: node.replace,
explain: this.transformNode(node.explain),
defaultValues: node.defaultValues,
})
}

Expand Down Expand Up @@ -811,6 +812,7 @@ export class OperationNodeTransformer {
kind: 'SelectModifierNode',
modifier: node.modifier,
rawModifier: this.transformNode(node.rawModifier),
of: this.transformNodeList(node.of),
})
}

Expand Down
7 changes: 6 additions & 1 deletion src/operation-node/select-modifier-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface SelectModifierNode extends OperationNode {
readonly kind: 'SelectModifierNode'
readonly modifier?: SelectModifier
readonly rawModifier?: OperationNode
readonly of?: ReadonlyArray<OperationNode>
}

/**
Expand All @@ -24,10 +25,14 @@ export const SelectModifierNode = freeze({
return node.kind === 'SelectModifierNode'
},

create(modifier: SelectModifier): SelectModifierNode {
create(
modifier: SelectModifier,
of?: ReadonlyArray<OperationNode>
): SelectModifierNode {
return freeze({
kind: 'SelectModifierNode',
modifier,
of,
})
},

Expand Down
23 changes: 23 additions & 0 deletions src/query-builder/aggregate-function-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,29 @@ export class AggregateFunctionBuilder<DB, TB extends keyof DB, O = unknown>
return func(this)
}

/**
* Casts the expression to the given type.
*
* This method call doesn't change the SQL in any way. This methods simply
* returns a copy of this `AggregateFunctionBuilder` with a new output type.
*/
$castTo<C>(): AggregateFunctionBuilder<DB, TB, C> {
return new AggregateFunctionBuilder(this.#props)
}

/**
* Omit null from the expression's type.
*
* This function can be useful in cases where you know an expression can't be
* null, but Kysely is unable to infer it.
*
* This method call doesn't change the SQL in any way. This methods simply
* returns a copy of `this` with a new output type.
*/
$notNull(): AggregateFunctionBuilder<DB, TB, Exclude<O, null>> {
return new AggregateFunctionBuilder(this.#props)
}

toOperationNode(): AggregateFunctionNode {
return this.#props.aggregateFunctionNode
}
Expand Down
6 changes: 4 additions & 2 deletions src/query-builder/case-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class CaseThenBuilder<DB, TB extends keyof DB, W, O> {
* A `then` call can be followed by {@link Whenable.when}, {@link CaseWhenBuilder.else},
* {@link CaseWhenBuilder.end} or {@link CaseWhenBuilder.endCase} call.
*/
then<E extends Expression<any>>(
then<E extends Expression<unknown>>(
expression: E
): CaseWhenBuilder<DB, TB, W, O | ExtractTypeFromValueExpression<E>>

Expand Down Expand Up @@ -135,7 +135,9 @@ export class CaseWhenBuilder<DB, TB extends keyof DB, W, O>
*
* An `else` call must be followed by an {@link Endable.end} or {@link Endable.endCase} call.
*/
else<O2>(expression: Expression<O2>): CaseEndBuilder<DB, TB, O | O2>
else<E extends Expression<unknown>>(
expression: E
): CaseEndBuilder<DB, TB, O | ExtractTypeFromValueExpression<E>>

else<V>(value: V): CaseEndBuilder<DB, TB, O | V>

Expand Down
14 changes: 9 additions & 5 deletions src/query-builder/delete-query-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ import {
import { KyselyTypeError } from '../util/type-error.js'
import { Streamable } from '../util/streamable.js'
import { ExpressionOrFactory } from '../parser/expression-parser.js'
import {
ValueExpression,
parseValueExpression,
} from '../parser/value-parser.js'

export class DeleteQueryBuilder<DB, TB extends keyof DB, O>
implements
Expand Down Expand Up @@ -625,12 +629,12 @@ export class DeleteQueryBuilder<DB, TB extends keyof DB, O>
* .execute()
* ```
*/
limit(limit: number): DeleteQueryBuilder<DB, TB, O> {
limit(limit: ValueExpression<DB, TB, number>): DeleteQueryBuilder<DB, TB, O> {
return new DeleteQueryBuilder({
...this.#props,
queryNode: DeleteQueryNode.cloneWithLimit(
this.#props.queryNode,
LimitNode.create(limit)
LimitNode.create(parseValueExpression(limit))
),
})
}
Expand Down Expand Up @@ -716,10 +720,10 @@ export class DeleteQueryBuilder<DB, TB extends keyof DB, O>
/**
* Change the output type of the query.
*
* You should only use this method as the last resort if the types
* don't support your use case.
* This method call doesn't change the SQL in any way. This methods simply
* returns a copy of this `DeleteQueryBuilder` with a new output type.
*/
$castTo<T>(): DeleteQueryBuilder<DB, TB, T> {
$castTo<C>(): DeleteQueryBuilder<DB, TB, C> {
return new DeleteQueryBuilder(this.#props)
}

Expand Down
18 changes: 15 additions & 3 deletions src/query-builder/insert-query-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,18 @@ export class InsertQueryBuilder<DB, TB extends keyof DB, O>
})
}

/**
* Creates an `insert into "person" default values` query.
*/
defaultValues(): InsertQueryBuilder<DB, TB, O> {
return new InsertQueryBuilder({
...this.#props,
queryNode: InsertQueryNode.cloneWith(this.#props.queryNode, {
defaultValues: true,
}),
})
}

/**
* Changes an `insert into` query to an `insert ignore into` query.
*
Expand Down Expand Up @@ -677,10 +689,10 @@ export class InsertQueryBuilder<DB, TB extends keyof DB, O>
/**
* Change the output type of the query.
*
* You should only use this method as the last resort if the types
* don't support your use case.
* This method call doesn't change the SQL in any way. This methods simply
* returns a copy of this `InsertQueryBuilder` with a new output type.
*/
$castTo<T>(): InsertQueryBuilder<DB, TB, T> {
$castTo<C>(): InsertQueryBuilder<DB, TB, C> {
return new InsertQueryBuilder(this.#props)
}

Expand Down
6 changes: 5 additions & 1 deletion src/query-builder/json-path-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,11 @@ export class TraversedJSONPathBuilder<S, O>
* This method call doesn't change the SQL in any way. This methods simply
* returns a copy of this `JSONPathBuilder` with a new output type.
*/
$castTo<T>(): JSONPathBuilder<T> {
$castTo<C>(): JSONPathBuilder<C> {
return new JSONPathBuilder(this.#node)
}

$notNull(): JSONPathBuilder<Exclude<O, null>> {
return new JSONPathBuilder(this.#node)
}

Expand Down
Loading

0 comments on commit c88e914

Please sign in to comment.