kysely 0.7.7 → 0.8.1

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.
Files changed (98) hide show
  1. package/README.md +29 -2
  2. package/dist/cjs/dialect/dialect-adapter.d.ts +19 -12
  3. package/dist/cjs/dialect/dialect.d.ts +1 -1
  4. package/dist/cjs/dialect/mysql/mysql-adapter.js +6 -0
  5. package/dist/cjs/dialect/mysql/mysql-driver.js +5 -5
  6. package/dist/cjs/dialect/postgres/postgres-driver.js +5 -5
  7. package/dist/cjs/driver/database-connection.d.ts +5 -0
  8. package/dist/cjs/driver/driver.d.ts +3 -5
  9. package/dist/cjs/index.d.ts +1 -1
  10. package/dist/cjs/kysely.d.ts +31 -1
  11. package/dist/cjs/kysely.js +33 -1
  12. package/dist/cjs/operation-node/operator-node.d.ts +1 -1
  13. package/dist/cjs/parser/data-type-parser.d.ts +1 -1
  14. package/dist/cjs/parser/default-value-parser.d.ts +1 -1
  15. package/dist/cjs/parser/filter-parser.d.ts +1 -1
  16. package/dist/cjs/parser/insert-values-parser.d.ts +2 -2
  17. package/dist/cjs/parser/join-parser.d.ts +1 -1
  18. package/dist/cjs/parser/on-conflict-parser.d.ts +1 -1
  19. package/dist/cjs/parser/order-by-parser.d.ts +1 -1
  20. package/dist/cjs/parser/reference-parser.d.ts +3 -2
  21. package/dist/cjs/parser/returning-parser.d.ts +1 -1
  22. package/dist/cjs/parser/select-parser.d.ts +1 -1
  23. package/dist/cjs/parser/table-parser.d.ts +17 -6
  24. package/dist/cjs/parser/update-set-parser.d.ts +2 -2
  25. package/dist/cjs/parser/value-parser.d.ts +1 -1
  26. package/dist/cjs/parser/with-parser.d.ts +1 -1
  27. package/dist/cjs/plugin/camel-case/camel-case-plugin.d.ts +4 -3
  28. package/dist/cjs/plugin/kysely-plugin.d.ts +9 -5
  29. package/dist/cjs/plugin/with-schema/with-schema-plugin.d.ts +3 -2
  30. package/dist/cjs/query-builder/expression-builder.d.ts +33 -0
  31. package/dist/cjs/query-builder/expression-builder.js +35 -0
  32. package/dist/cjs/query-builder/function-builder.d.ts +112 -0
  33. package/dist/cjs/query-builder/function-builder.js +138 -0
  34. package/dist/cjs/query-builder/query-builder.d.ts +8 -8
  35. package/dist/cjs/query-compiler/compiled-query.d.ts +1 -1
  36. package/dist/cjs/query-compiler/default-query-compiler.d.ts +2 -2
  37. package/dist/cjs/query-compiler/default-query-compiler.js +9 -9
  38. package/dist/cjs/query-compiler/query-compiler.d.ts +3 -0
  39. package/dist/cjs/query-creator.d.ts +9 -9
  40. package/dist/cjs/query-creator.js +11 -10
  41. package/dist/cjs/raw-builder/raw-builder.d.ts +14 -5
  42. package/dist/cjs/raw-builder/raw-builder.js +25 -20
  43. package/dist/cjs/schema/alter-table-builder.d.ts +1 -1
  44. package/dist/cjs/schema/column-definition-builder.d.ts +1 -1
  45. package/dist/cjs/util/generated-placeholder.d.ts +2 -2
  46. package/dist/cjs/util/generated-placeholder.js +2 -2
  47. package/dist/cjs/util/random-string.js +3 -3
  48. package/dist/cjs/{query-builder → util}/type-utils.d.ts +11 -3
  49. package/dist/cjs/{query-builder → util}/type-utils.js +0 -0
  50. package/dist/esm/dialect/dialect-adapter.d.ts +19 -12
  51. package/dist/esm/dialect/dialect.d.ts +1 -1
  52. package/dist/esm/dialect/mysql/mysql-adapter.js +6 -0
  53. package/dist/esm/dialect/mysql/mysql-driver.js +5 -5
  54. package/dist/esm/dialect/postgres/postgres-driver.js +5 -5
  55. package/dist/esm/driver/database-connection.d.ts +5 -0
  56. package/dist/esm/driver/driver.d.ts +3 -5
  57. package/dist/esm/index.d.ts +1 -1
  58. package/dist/esm/kysely.d.ts +31 -1
  59. package/dist/esm/kysely.js +34 -2
  60. package/dist/esm/operation-node/operator-node.d.ts +1 -1
  61. package/dist/esm/parser/data-type-parser.d.ts +1 -1
  62. package/dist/esm/parser/default-value-parser.d.ts +1 -1
  63. package/dist/esm/parser/filter-parser.d.ts +1 -1
  64. package/dist/esm/parser/insert-values-parser.d.ts +2 -2
  65. package/dist/esm/parser/join-parser.d.ts +1 -1
  66. package/dist/esm/parser/on-conflict-parser.d.ts +1 -1
  67. package/dist/esm/parser/order-by-parser.d.ts +1 -1
  68. package/dist/esm/parser/reference-parser.d.ts +3 -2
  69. package/dist/esm/parser/returning-parser.d.ts +1 -1
  70. package/dist/esm/parser/select-parser.d.ts +1 -1
  71. package/dist/esm/parser/table-parser.d.ts +17 -6
  72. package/dist/esm/parser/update-set-parser.d.ts +2 -2
  73. package/dist/esm/parser/value-parser.d.ts +1 -1
  74. package/dist/esm/parser/with-parser.d.ts +1 -1
  75. package/dist/esm/plugin/camel-case/camel-case-plugin.d.ts +4 -3
  76. package/dist/esm/plugin/kysely-plugin.d.ts +9 -5
  77. package/dist/esm/plugin/with-schema/with-schema-plugin.d.ts +3 -2
  78. package/dist/esm/query-builder/expression-builder.d.ts +33 -0
  79. package/dist/esm/query-builder/expression-builder.js +35 -0
  80. package/dist/esm/query-builder/function-builder.d.ts +112 -0
  81. package/dist/esm/query-builder/function-builder.js +134 -0
  82. package/dist/esm/query-builder/query-builder.d.ts +8 -8
  83. package/dist/esm/query-compiler/compiled-query.d.ts +1 -1
  84. package/dist/esm/query-compiler/default-query-compiler.d.ts +2 -2
  85. package/dist/esm/query-compiler/default-query-compiler.js +9 -9
  86. package/dist/esm/query-compiler/query-compiler.d.ts +3 -0
  87. package/dist/esm/query-creator.d.ts +9 -9
  88. package/dist/esm/query-creator.js +11 -10
  89. package/dist/esm/raw-builder/raw-builder.d.ts +14 -5
  90. package/dist/esm/raw-builder/raw-builder.js +25 -20
  91. package/dist/esm/schema/alter-table-builder.d.ts +1 -1
  92. package/dist/esm/schema/column-definition-builder.d.ts +1 -1
  93. package/dist/esm/util/generated-placeholder.d.ts +2 -2
  94. package/dist/esm/util/generated-placeholder.js +1 -1
  95. package/dist/esm/util/random-string.js +3 -3
  96. package/dist/esm/{query-builder → util}/type-utils.d.ts +11 -3
  97. package/dist/esm/{query-builder → util}/type-utils.js +0 -0
  98. package/package.json +8 -8
@@ -215,11 +215,11 @@ export class QueryCreator {
215
215
  * Raw builder instances can be passed to pretty much anywhere: `select`, `where`,
216
216
  * `*Join`, `groupBy`, `orderBy` etc. Just try it. If the method accepts it, it works.
217
217
  *
218
- * @param sql - The raw SQL. Special strings `?` and `??` can be used to provide
219
- * parameter bindings. `?` for values and `??` for identifiers such as column names
218
+ * @param sql - The raw SQL. Special strings `?` and `??` can be used as parameter
219
+ * placeholders. `?` for values and `??` for identifiers such as column names
220
220
  * or `column.table` references.
221
221
  *
222
- * @param params - The parameters that will be bound to the `?` and `??` bindings in
222
+ * @param params - The parameters that will be bound to the `?` and `??` placeholders in
223
223
  * the sql string.
224
224
  *
225
225
  * @example
@@ -257,7 +257,7 @@ export class QueryCreator {
257
257
  * the output type. In this case you need to use the `castTo` method on the query
258
258
  * to specify a return type for the query.
259
259
  *
260
- * We could've also used `??` bindings to provide `first_name` and `last_name` like
260
+ * We could've also used a `??` placeholder to provide `first_name` and `last_name` like
261
261
  * this:
262
262
  *
263
263
  * ```ts
@@ -272,9 +272,9 @@ export class QueryCreator {
272
272
  *
273
273
  * But it's often cleaner to just write the column names in the SQL. Again remember to
274
274
  * never concatenate column names or any other untrusted user input to the SQL string or you
275
- * are going to create an injection vulnerability. All user input should go to the bindings
275
+ * are going to create an injection vulnerability. All user input should go to the parameters
276
276
  * array, never to the SQL string directly. But if the column names or values are trusted
277
- * and known at compile time, there is no reason to use bindings.
277
+ * and known at compile time, there is no reason to use parameters.
278
278
  *
279
279
  * @example
280
280
  * Example of using `raw` in `where`:
@@ -300,7 +300,7 @@ export class QueryCreator {
300
300
  *
301
301
  * The function in the above example returns people that are older than the given number of
302
302
  * years. The number of years in this example is an untrusted user input, and therefore we use
303
- * a `?` binding for it.
303
+ * a `?` placeholder for it.
304
304
  *
305
305
  * @example
306
306
  * Example of creating a completely raw query from scratch:
@@ -314,11 +314,12 @@ export class QueryCreator {
314
314
  * The result of `execute()` method is always an array. In this case the type of
315
315
  * the `persons` variable is `Person[]`.
316
316
  */
317
- raw(sql, params) {
317
+ raw(sql, parameters) {
318
318
  return new RawBuilder({
319
- sql,
320
- params,
319
+ queryId: createQueryId(),
321
320
  executor: this.#props.executor,
321
+ sql,
322
+ parameters,
322
323
  });
323
324
  }
324
325
  }
@@ -4,10 +4,18 @@ import { OperationNodeSource } from '../operation-node/operation-node-source.js'
4
4
  import { RawNode } from '../operation-node/raw-node.js';
5
5
  import { CompiledQuery } from '../query-compiler/compiled-query.js';
6
6
  import { QueryExecutor } from '../query-executor/query-executor.js';
7
+ import { QueryId } from '../util/query-id.js';
7
8
  export declare class RawBuilder<O = unknown> implements OperationNodeSource {
8
9
  #private;
9
- constructor(args: RawBuilderConstructorArgs);
10
+ constructor(props: RawBuilderProps);
10
11
  as<A extends string>(alias: A): AliasedRawBuilder<O, A>;
12
+ /**
13
+ * Change the output type of the raw expression.
14
+ *
15
+ * This doesn't produce any SQL. This methods simply returns a copy
16
+ * of this `RawBuilder` with a new output type.
17
+ */
18
+ castTo<T>(): RawBuilder<T>;
11
19
  toOperationNode(): RawNode;
12
20
  compile(): CompiledQuery;
13
21
  execute(): Promise<QueryResult<O>>;
@@ -29,8 +37,9 @@ export declare class AliasedRawBuilder<O = unknown, A extends string = never> im
29
37
  toOperationNode(): AliasNode;
30
38
  constructor(rawBuilder: RawBuilder<O>, alias: A);
31
39
  }
32
- export interface RawBuilderConstructorArgs {
33
- sql: string;
34
- params?: any;
35
- executor: QueryExecutor;
40
+ export interface RawBuilderProps {
41
+ readonly queryId: QueryId;
42
+ readonly executor: QueryExecutor;
43
+ readonly sql: string;
44
+ readonly parameters?: any;
36
45
  }
@@ -4,52 +4,57 @@ import { RawNode } from '../operation-node/raw-node.js';
4
4
  import { ValueNode } from '../operation-node/value-node.js';
5
5
  import { parseStringReference } from '../parser/reference-parser.js';
6
6
  import { preventAwait } from '../util/prevent-await.js';
7
- import { createQueryId } from '../util/query-id.js';
7
+ import { freeze } from '../util/object-utils.js';
8
8
  export class RawBuilder {
9
- #queryId;
10
- #sql;
11
- #params;
12
- #executor;
13
- constructor(args) {
14
- this.#queryId = createQueryId();
15
- this.#sql = args.sql;
16
- this.#params = args.params;
17
- this.#executor = args.executor;
9
+ #props;
10
+ constructor(props) {
11
+ this.#props = freeze(props);
18
12
  }
19
13
  as(alias) {
20
14
  return new AliasedRawBuilder(this, alias);
21
15
  }
16
+ /**
17
+ * Change the output type of the raw expression.
18
+ *
19
+ * This doesn't produce any SQL. This methods simply returns a copy
20
+ * of this `RawBuilder` with a new output type.
21
+ */
22
+ castTo() {
23
+ return new RawBuilder({
24
+ ...this.#props,
25
+ });
26
+ }
22
27
  toOperationNode() {
23
- const bindingRegex = /(\?\??)/g;
24
- const sql = this.#sql;
25
- const params = this.#params ?? [];
28
+ const parameterPlaceholderRegex = /(\?\??)/g;
29
+ const sql = this.#props.sql;
30
+ const parameters = this.#props.parameters ?? [];
26
31
  const sqlFragments = [];
27
32
  const argNodes = [];
28
33
  let paramIdx = 0;
29
34
  let sqlIdx = 0;
30
35
  let match = null;
31
- while ((match = bindingRegex.exec(sql))) {
36
+ while ((match = parameterPlaceholderRegex.exec(sql))) {
32
37
  const str = match[1];
33
- if (paramIdx >= params.length) {
34
- throw new Error(`value not provided for all bindings in string ${sql}`);
38
+ if (paramIdx >= parameters.length) {
39
+ throw new Error(`value not provided for all placeholders in string ${sql}`);
35
40
  }
36
41
  if (match.index > 0 && sql[match.index - 1] === '\\') {
37
42
  continue;
38
43
  }
39
44
  sqlFragments.push(sql.slice(sqlIdx, match.index).replaceAll('\\?', '?'));
40
- argNodes.push(parseRawArg(str, params[paramIdx]));
45
+ argNodes.push(parseRawArg(str, parameters[paramIdx]));
41
46
  sqlIdx = match.index + str.length;
42
47
  ++paramIdx;
43
48
  }
44
49
  sqlFragments.push(sql.slice(sqlIdx));
45
50
  const rawNode = RawNode.create(sqlFragments, argNodes);
46
- return this.#executor.transformQuery(rawNode, this.#queryId);
51
+ return this.#props.executor.transformQuery(rawNode, this.#props.queryId);
47
52
  }
48
53
  compile() {
49
- return this.#executor.compileQuery(this.toOperationNode(), this.#queryId);
54
+ return this.#props.executor.compileQuery(this.toOperationNode(), this.#props.queryId);
50
55
  }
51
56
  async execute() {
52
- return this.#executor.executeQuery(this.compile(), this.#queryId);
57
+ return this.#props.executor.executeQuery(this.compile(), this.#props.queryId);
53
58
  }
54
59
  }
55
60
  preventAwait(RawBuilder, "don't await RawBuilder instances directly. To execute the query you need to call `execute`");
@@ -7,7 +7,7 @@ import { CompiledQuery } from '../query-compiler/compiled-query.js';
7
7
  import { Compilable } from '../util/compilable.js';
8
8
  import { PrimitiveValue } from '../util/object-utils.js';
9
9
  import { ColumnDefinitionBuilder, ColumnDefinitionBuilderInterface } from './column-definition-builder.js';
10
- import { AnyRawBuilder } from '../query-builder/type-utils.js';
10
+ import { AnyRawBuilder } from '../util/type-utils.js';
11
11
  import { QueryId } from '../util/query-id.js';
12
12
  import { QueryExecutor } from '../query-executor/query-executor.js';
13
13
  import { DataTypeExpression } from '../parser/data-type-parser.js';
@@ -2,7 +2,7 @@ import { OperationNodeSource } from '../operation-node/operation-node-source.js'
2
2
  import { OnModifyForeignAction } from '../operation-node/references-node.js';
3
3
  import { PrimitiveValue } from '../util/object-utils.js';
4
4
  import { ColumnDefinitionNode } from '../operation-node/column-definition-node.js';
5
- import { AnyRawBuilder } from '../query-builder/type-utils.js';
5
+ import { AnyRawBuilder } from '../util/type-utils.js';
6
6
  import { DefaultValueExpression } from '../parser/default-value-parser.js';
7
7
  export interface ColumnDefinitionBuilderInterface<R> {
8
8
  /**
@@ -1,3 +1,3 @@
1
- import { GeneratedPlaceholder } from '../query-builder/type-utils.js';
2
- export declare const generatedPlaceholder: GeneratedPlaceholder;
1
+ import { GeneratedPlaceholder } from './type-utils.js';
2
+ export declare const GENERATED_PLACEHOLDER: GeneratedPlaceholder;
3
3
  export declare function isGeneratedPlaceholder(obj: unknown): obj is GeneratedPlaceholder;
@@ -1,5 +1,5 @@
1
1
  import { freeze, isObject } from './object-utils.js';
2
- export const generatedPlaceholder = freeze({
2
+ export const GENERATED_PLACEHOLDER = freeze({
3
3
  __isGeneratedPlaceholder__: true,
4
4
  });
5
5
  export function isGeneratedPlaceholder(obj) {
@@ -1,8 +1,8 @@
1
1
  const CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
2
2
  export function randomString(length) {
3
- let str = '';
3
+ let chars = new Array(length);
4
4
  for (let i = 0; i < length; ++i) {
5
- str += CHARS[Math.floor(Math.random() * CHARS.length)];
5
+ chars[i] = CHARS[Math.floor(Math.random() * CHARS.length)];
6
6
  }
7
- return str;
7
+ return chars.join('');
8
8
  }
@@ -1,6 +1,9 @@
1
1
  import { AliasedRawBuilder, RawBuilder } from '../raw-builder/raw-builder.js';
2
- import { AliasedQueryBuilder, QueryBuilder } from './query-builder.js';
3
- import { ExpressionBuilder } from './expression-builder.js';
2
+ import { AliasedQueryBuilder, QueryBuilder } from '../query-builder/query-builder.js';
3
+ import { ExpressionBuilder } from '../query-builder/expression-builder.js';
4
+ /**
5
+ * Given an object type, extracts the union of all value types.
6
+ */
4
7
  export declare type ValueType<T> = T[keyof T];
5
8
  /**
6
9
  * Given a database type and a union of table names returns the row type
@@ -38,7 +41,7 @@ export declare type RowType<DB, TB extends keyof DB> = UnionToIntersection<DB[TB
38
41
  * Evil typescript magic to convert a union type `A | B | C` into an
39
42
  * intersection type `A & B & C`.
40
43
  */
41
- export declare type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
44
+ declare type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
42
45
  /**
43
46
  * Given a database type and a union of table names in that db, returns
44
47
  * a union type with all possible column names.
@@ -150,3 +153,8 @@ export interface GeneratedPlaceholder {
150
153
  export declare type ManyResultRowType<O> = O extends InsertResultTypeTag ? number | undefined : O extends DeleteResultTypeTag ? number : O extends UpdateResultTypeTag ? number : O;
151
154
  export declare type SingleResultRowType<O> = O extends InsertResultTypeTag ? number | undefined : O extends DeleteResultTypeTag ? number : O extends UpdateResultTypeTag ? number : O | undefined;
152
155
  export declare type NonEmptySingleResultRowType<O> = O extends InsertResultTypeTag ? number : O extends DeleteResultTypeTag ? number : O extends UpdateResultTypeTag ? number : O;
156
+ export declare type UnknownRow = Record<string, unknown>;
157
+ export declare type Nullable<T> = {
158
+ [P in keyof T]: T[P] | null;
159
+ };
160
+ export {};
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kysely",
3
- "version": "0.7.7",
3
+ "version": "0.8.1",
4
4
  "description": "Type safe SQL query builder",
5
5
  "repository": {
6
6
  "type": "git",
@@ -15,7 +15,7 @@
15
15
  }
16
16
  },
17
17
  "scripts": {
18
- "test:build": "rm -rf test/lib && tsc -p test",
18
+ "test:build": "rm -rf test/dist && tsc -p test",
19
19
  "clean": "rm -rf dist && rm -rf test/dist",
20
20
  "test": "npm run clean && npm run build:cjs && npm run test:build && npm run test:integration && npm run test:typings && npm run test:esmimports",
21
21
  "test:integration": "mocha --timeout 15000 test/dist/**/*.test.js",
@@ -32,7 +32,7 @@
32
32
  "author": "Sami Koskimäki <sami@jakso.me>",
33
33
  "license": "MIT",
34
34
  "dependencies": {
35
- "@types/node": "^16.10.9"
35
+ "@types/node": "^16.11.9"
36
36
  },
37
37
  "devDependencies": {
38
38
  "@types/chai": "^4.2.22",
@@ -41,12 +41,12 @@
41
41
  "@types/pg": "^8.6.1",
42
42
  "chai": "^4.3.4",
43
43
  "chai-subset": "^1.6.0",
44
- "mocha": "^9.1.2",
45
- "mysql2": "^2.3.2",
44
+ "mocha": "^9.1.3",
45
+ "mysql2": "^2.3.3",
46
46
  "pg": "^8.7.1",
47
47
  "prettier": "^2.4.1",
48
- "tsd": "^0.18.0",
49
- "typedoc": "^0.22.5",
50
- "typescript": "4.4.4"
48
+ "tsd": "^0.19.0",
49
+ "typedoc": "^0.22.9",
50
+ "typescript": "4.5.2"
51
51
  }
52
52
  }