kysely 0.27.3 → 0.27.4

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 (211) hide show
  1. package/CONTRIBUTING.md +1 -1
  2. package/README.md +7 -7
  3. package/dist/cjs/dialect/dialect-adapter-base.d.ts +2 -1
  4. package/dist/cjs/dialect/dialect-adapter-base.js +4 -1
  5. package/dist/cjs/dialect/dialect-adapter.d.ts +5 -0
  6. package/dist/cjs/dialect/mssql/mssql-adapter.d.ts +1 -5
  7. package/dist/cjs/dialect/mssql/mssql-adapter.js +2 -4
  8. package/dist/cjs/dialect/mssql/mssql-dialect-config.d.ts +46 -26
  9. package/dist/cjs/dialect/mssql/mssql-driver.d.ts +1 -0
  10. package/dist/cjs/dialect/mssql/mssql-driver.js +95 -36
  11. package/dist/cjs/dialect/postgres/postgres-introspector.js +1 -1
  12. package/dist/cjs/driver/database-connection.d.ts +3 -1
  13. package/dist/cjs/dynamic/dynamic-reference-builder.js +2 -2
  14. package/dist/cjs/dynamic/dynamic.d.ts +1 -1
  15. package/dist/cjs/dynamic/dynamic.js +1 -1
  16. package/dist/cjs/expression/expression-builder.d.ts +2 -2
  17. package/dist/cjs/expression/expression-builder.js +2 -3
  18. package/dist/cjs/expression/expression.js +2 -3
  19. package/dist/cjs/helpers/mssql.js +3 -4
  20. package/dist/cjs/helpers/mysql.d.ts +3 -3
  21. package/dist/cjs/helpers/mysql.js +6 -7
  22. package/dist/cjs/helpers/postgres.d.ts +4 -4
  23. package/dist/cjs/helpers/postgres.js +7 -8
  24. package/dist/cjs/helpers/sqlite.js +3 -4
  25. package/dist/cjs/index.d.ts +28 -21
  26. package/dist/cjs/index.js +28 -21
  27. package/dist/cjs/kysely.d.ts +1 -1
  28. package/dist/cjs/kysely.js +3 -3
  29. package/dist/cjs/operation-node/alter-column-node.d.ts +1 -1
  30. package/dist/cjs/operation-node/case-node.d.ts +1 -1
  31. package/dist/cjs/operation-node/column-definition-node.d.ts +1 -0
  32. package/dist/cjs/operation-node/data-type-node.d.ts +2 -2
  33. package/dist/cjs/operation-node/data-type-node.js +5 -3
  34. package/dist/cjs/operation-node/delete-query-node.d.ts +2 -0
  35. package/dist/cjs/operation-node/foreign-key-constraint-node.d.ts +4 -4
  36. package/dist/cjs/operation-node/having-node.d.ts +1 -1
  37. package/dist/cjs/operation-node/insert-query-node.d.ts +2 -0
  38. package/dist/cjs/operation-node/merge-query-node.d.ts +2 -0
  39. package/dist/cjs/operation-node/on-node.d.ts +1 -1
  40. package/dist/cjs/operation-node/operation-node-source.js +1 -2
  41. package/dist/cjs/operation-node/operation-node-transformer.d.ts +2 -0
  42. package/dist/cjs/operation-node/operation-node-transformer.js +12 -0
  43. package/dist/cjs/operation-node/operation-node-visitor.d.ts +2 -0
  44. package/dist/cjs/operation-node/operation-node-visitor.js +1 -0
  45. package/dist/cjs/operation-node/operation-node.d.ts +1 -1
  46. package/dist/cjs/operation-node/operator-node.d.ts +3 -3
  47. package/dist/cjs/operation-node/operator-node.js +8 -6
  48. package/dist/cjs/operation-node/output-node.d.ts +13 -0
  49. package/dist/cjs/operation-node/output-node.js +26 -0
  50. package/dist/cjs/operation-node/query-node.d.ts +11 -6
  51. package/dist/cjs/operation-node/query-node.js +9 -0
  52. package/dist/cjs/operation-node/select-query-node.d.ts +1 -0
  53. package/dist/cjs/operation-node/select-query-node.js +6 -0
  54. package/dist/cjs/operation-node/update-query-node.d.ts +2 -0
  55. package/dist/cjs/operation-node/where-node.d.ts +1 -1
  56. package/dist/cjs/parser/binary-operation-parser.js +5 -6
  57. package/dist/cjs/parser/data-type-parser.js +1 -2
  58. package/dist/cjs/parser/default-value-parser.js +1 -2
  59. package/dist/cjs/parser/expression-parser.js +3 -4
  60. package/dist/cjs/parser/fetch-parser.js +1 -2
  61. package/dist/cjs/parser/group-by-parser.js +1 -2
  62. package/dist/cjs/parser/identifier-parser.js +1 -2
  63. package/dist/cjs/parser/insert-values-parser.js +1 -2
  64. package/dist/cjs/parser/join-parser.js +1 -2
  65. package/dist/cjs/parser/merge-parser.js +2 -3
  66. package/dist/cjs/parser/on-commit-action-parse.js +1 -2
  67. package/dist/cjs/parser/on-modify-action-parser.js +1 -2
  68. package/dist/cjs/parser/order-by-parser.js +3 -4
  69. package/dist/cjs/parser/parse-utils.js +4 -5
  70. package/dist/cjs/parser/partition-by-parser.js +1 -2
  71. package/dist/cjs/parser/reference-parser.js +8 -9
  72. package/dist/cjs/parser/returning-parser.d.ts +4 -3
  73. package/dist/cjs/parser/select-parser.js +2 -3
  74. package/dist/cjs/parser/set-operation-parser.js +1 -2
  75. package/dist/cjs/parser/table-parser.js +4 -5
  76. package/dist/cjs/parser/top-parser.js +1 -2
  77. package/dist/cjs/parser/unary-operation-parser.js +3 -4
  78. package/dist/cjs/parser/update-set-parser.js +2 -3
  79. package/dist/cjs/parser/value-parser.js +4 -5
  80. package/dist/cjs/parser/with-parser.js +1 -2
  81. package/dist/cjs/plugin/camel-case/camel-case-plugin.d.ts +2 -2
  82. package/dist/cjs/plugin/camel-case/camel-case-plugin.js +2 -2
  83. package/dist/cjs/plugin/camel-case/camel-case.js +2 -3
  84. package/dist/cjs/plugin/parse-json-results/parse-json-results-plugin.d.ts +18 -0
  85. package/dist/cjs/plugin/parse-json-results/parse-json-results-plugin.js +19 -11
  86. package/dist/cjs/query-builder/delete-query-builder.d.ts +89 -5
  87. package/dist/cjs/query-builder/delete-query-builder.js +19 -5
  88. package/dist/cjs/query-builder/function-module.d.ts +5 -5
  89. package/dist/cjs/query-builder/function-module.js +1 -2
  90. package/dist/cjs/query-builder/insert-query-builder.d.ts +95 -10
  91. package/dist/cjs/query-builder/insert-query-builder.js +25 -10
  92. package/dist/cjs/query-builder/merge-query-builder.d.ts +167 -3
  93. package/dist/cjs/query-builder/merge-query-builder.js +30 -1
  94. package/dist/cjs/query-builder/no-result-error.js +2 -2
  95. package/dist/cjs/query-builder/on-conflict-builder.d.ts +2 -2
  96. package/dist/cjs/query-builder/output-interface.d.ts +95 -0
  97. package/dist/cjs/query-builder/output-interface.js +2 -0
  98. package/dist/cjs/query-builder/returning-interface.d.ts +2 -0
  99. package/dist/cjs/query-builder/select-query-builder.d.ts +76 -11
  100. package/dist/cjs/query-builder/select-query-builder.js +7 -2
  101. package/dist/cjs/query-builder/update-query-builder.d.ts +95 -10
  102. package/dist/cjs/query-builder/update-query-builder.js +21 -7
  103. package/dist/cjs/query-builder/update-result.d.ts +9 -0
  104. package/dist/cjs/query-builder/update-result.js +9 -0
  105. package/dist/cjs/query-builder/where-interface.d.ts +1 -1
  106. package/dist/cjs/query-compiler/default-query-compiler.d.ts +2 -0
  107. package/dist/cjs/query-compiler/default-query-compiler.js +25 -1
  108. package/dist/cjs/query-creator.d.ts +56 -3
  109. package/dist/cjs/query-creator.js +54 -1
  110. package/dist/cjs/raw-builder/raw-builder.js +1 -2
  111. package/dist/cjs/raw-builder/sql.d.ts +5 -5
  112. package/dist/cjs/schema/alter-column-builder.js +3 -0
  113. package/dist/cjs/schema/alter-table-add-index-builder.d.ts +1 -1
  114. package/dist/cjs/schema/alter-table-add-index-builder.js +1 -1
  115. package/dist/cjs/schema/alter-table-builder.js +1 -2
  116. package/dist/cjs/schema/column-definition-builder.d.ts +5 -0
  117. package/dist/cjs/schema/column-definition-builder.js +7 -0
  118. package/dist/cjs/util/assert.js +2 -3
  119. package/dist/cjs/util/compilable.js +1 -2
  120. package/dist/cjs/util/json-object-args.js +1 -2
  121. package/dist/cjs/util/log-once.js +1 -2
  122. package/dist/cjs/util/object-utils.js +38 -26
  123. package/dist/cjs/util/performance-now.js +1 -2
  124. package/dist/cjs/util/prevent-await.js +1 -2
  125. package/dist/cjs/util/query-id.js +1 -2
  126. package/dist/cjs/util/random-string.js +1 -2
  127. package/dist/cjs/util/require-all-props.js +1 -2
  128. package/dist/cjs/util/stack-trace-utils.js +1 -2
  129. package/dist/esm/dialect/dialect-adapter-base.d.ts +2 -1
  130. package/dist/esm/dialect/dialect-adapter-base.js +4 -1
  131. package/dist/esm/dialect/dialect-adapter.d.ts +5 -0
  132. package/dist/esm/dialect/mssql/mssql-adapter.d.ts +1 -5
  133. package/dist/esm/dialect/mssql/mssql-adapter.js +2 -4
  134. package/dist/esm/dialect/mssql/mssql-dialect-config.d.ts +46 -26
  135. package/dist/esm/dialect/mssql/mssql-driver.d.ts +1 -0
  136. package/dist/esm/dialect/mssql/mssql-driver.js +95 -36
  137. package/dist/esm/dialect/postgres/postgres-introspector.js +1 -1
  138. package/dist/esm/driver/database-connection.d.ts +3 -1
  139. package/dist/esm/dynamic/dynamic.d.ts +1 -1
  140. package/dist/esm/dynamic/dynamic.js +1 -1
  141. package/dist/esm/expression/expression-builder.d.ts +2 -2
  142. package/dist/esm/helpers/mysql.d.ts +3 -3
  143. package/dist/esm/helpers/mysql.js +3 -3
  144. package/dist/esm/helpers/postgres.d.ts +4 -4
  145. package/dist/esm/helpers/postgres.js +4 -4
  146. package/dist/esm/index.d.ts +28 -21
  147. package/dist/esm/index.js +28 -21
  148. package/dist/esm/kysely.d.ts +1 -1
  149. package/dist/esm/kysely.js +1 -1
  150. package/dist/esm/operation-node/alter-column-node.d.ts +1 -1
  151. package/dist/esm/operation-node/case-node.d.ts +1 -1
  152. package/dist/esm/operation-node/column-definition-node.d.ts +1 -0
  153. package/dist/esm/operation-node/data-type-node.d.ts +2 -2
  154. package/dist/esm/operation-node/data-type-node.js +3 -1
  155. package/dist/esm/operation-node/delete-query-node.d.ts +2 -0
  156. package/dist/esm/operation-node/foreign-key-constraint-node.d.ts +4 -4
  157. package/dist/esm/operation-node/having-node.d.ts +1 -1
  158. package/dist/esm/operation-node/insert-query-node.d.ts +2 -0
  159. package/dist/esm/operation-node/merge-query-node.d.ts +2 -0
  160. package/dist/esm/operation-node/on-node.d.ts +1 -1
  161. package/dist/esm/operation-node/operation-node-transformer.d.ts +2 -0
  162. package/dist/esm/operation-node/operation-node-transformer.js +12 -0
  163. package/dist/esm/operation-node/operation-node-visitor.d.ts +2 -0
  164. package/dist/esm/operation-node/operation-node-visitor.js +1 -0
  165. package/dist/esm/operation-node/operation-node.d.ts +1 -1
  166. package/dist/esm/operation-node/operator-node.d.ts +3 -3
  167. package/dist/esm/operation-node/operator-node.js +2 -0
  168. package/dist/esm/operation-node/output-node.d.ts +13 -0
  169. package/dist/esm/operation-node/output-node.js +24 -0
  170. package/dist/esm/operation-node/query-node.d.ts +11 -6
  171. package/dist/esm/operation-node/query-node.js +9 -0
  172. package/dist/esm/operation-node/select-query-node.d.ts +1 -0
  173. package/dist/esm/operation-node/select-query-node.js +6 -0
  174. package/dist/esm/operation-node/update-query-node.d.ts +2 -0
  175. package/dist/esm/operation-node/where-node.d.ts +1 -1
  176. package/dist/esm/parser/returning-parser.d.ts +4 -3
  177. package/dist/esm/plugin/camel-case/camel-case-plugin.d.ts +2 -2
  178. package/dist/esm/plugin/camel-case/camel-case-plugin.js +2 -2
  179. package/dist/esm/plugin/parse-json-results/parse-json-results-plugin.d.ts +18 -0
  180. package/dist/esm/plugin/parse-json-results/parse-json-results-plugin.js +19 -11
  181. package/dist/esm/query-builder/delete-query-builder.d.ts +89 -5
  182. package/dist/esm/query-builder/delete-query-builder.js +19 -5
  183. package/dist/esm/query-builder/function-module.d.ts +5 -5
  184. package/dist/esm/query-builder/insert-query-builder.d.ts +95 -10
  185. package/dist/esm/query-builder/insert-query-builder.js +25 -10
  186. package/dist/esm/query-builder/merge-query-builder.d.ts +167 -3
  187. package/dist/esm/query-builder/merge-query-builder.js +30 -1
  188. package/dist/esm/query-builder/on-conflict-builder.d.ts +2 -2
  189. package/dist/esm/query-builder/output-interface.d.ts +95 -0
  190. package/dist/esm/query-builder/output-interface.js +2 -0
  191. package/dist/esm/query-builder/returning-interface.d.ts +2 -0
  192. package/dist/esm/query-builder/select-query-builder.d.ts +76 -11
  193. package/dist/esm/query-builder/select-query-builder.js +6 -0
  194. package/dist/esm/query-builder/update-query-builder.d.ts +95 -10
  195. package/dist/esm/query-builder/update-query-builder.js +21 -7
  196. package/dist/esm/query-builder/update-result.d.ts +9 -0
  197. package/dist/esm/query-builder/update-result.js +9 -0
  198. package/dist/esm/query-builder/where-interface.d.ts +1 -1
  199. package/dist/esm/query-compiler/default-query-compiler.d.ts +2 -0
  200. package/dist/esm/query-compiler/default-query-compiler.js +25 -1
  201. package/dist/esm/query-creator.d.ts +56 -3
  202. package/dist/esm/query-creator.js +54 -1
  203. package/dist/esm/raw-builder/sql.d.ts +5 -5
  204. package/dist/esm/schema/alter-column-builder.js +3 -0
  205. package/dist/esm/schema/alter-table-add-index-builder.d.ts +1 -1
  206. package/dist/esm/schema/alter-table-add-index-builder.js +1 -1
  207. package/dist/esm/schema/alter-table-builder.js +1 -2
  208. package/dist/esm/schema/column-definition-builder.d.ts +5 -0
  209. package/dist/esm/schema/column-definition-builder.js +7 -0
  210. package/dist/esm/util/object-utils.js +18 -5
  211. package/package.json +7 -6
@@ -9,10 +9,8 @@ export class MssqlAdapter extends DialectAdapterBase {
9
9
  get supportsTransactionalDdl() {
10
10
  return true;
11
11
  }
12
- get supportsReturning() {
13
- // mssql should support returning with the `output` clause.
14
- // we need to figure this out when we'll introduce support for it.
15
- return false;
12
+ get supportsOutput() {
13
+ return true;
16
14
  }
17
15
  async acquireMigrationLock(db) {
18
16
  // Acquire a transaction-level exclusive lock on the migrations table.
@@ -50,39 +50,59 @@ export interface MssqlDialectConfig {
50
50
  export interface Tedious {
51
51
  connectionFactory: () => TediousConnection | Promise<TediousConnection>;
52
52
  ISOLATION_LEVEL: TediousIsolationLevel;
53
- Request: typeof TediousRequest;
53
+ Request: TediousRequestClass;
54
54
  TYPES: TediousTypes;
55
55
  }
56
- export type TediousIsolationLevel = Record<'NO_CHANGE' | 'READ_UNCOMMITTED' | 'READ_COMMITTED' | 'REPEATABLE_READ' | 'SERIALIZABLE' | 'SNAPSHOT', number>;
57
- export type TediousTypes = Record<'BigInt' | 'Binary' | 'Bit' | 'Char' | 'Date' | 'DateTime' | 'DateTime2' | 'DateTimeOffset' | 'Decimal' | 'Float' | 'Image' | 'Int' | 'Money' | 'NChar' | 'NText' | 'Null' | 'Numeric' | 'NVarChar' | 'Real' | 'SmallDateTime' | 'SmallInt' | 'SmallMoney' | 'Text' | 'Time' | 'TinyInt' | 'TVP' | 'UDT' | 'UniqueIdentifier' | 'VarBinary' | 'VarChar' | 'Xml', {
58
- name: string;
59
- type: string;
60
- }>;
61
- export interface TediousType {
62
- name: string;
63
- type: string;
64
- }
65
56
  export interface TediousConnection {
66
- beginTransaction(callback: (error?: Error) => void, transactionId?: string | undefined, isolationLevel?: number | undefined): void;
67
- cancel(): void;
57
+ beginTransaction(callback: (error?: Error | null, transactionDescriptor?: any) => void, name?: string, isolationLevel?: number): void;
58
+ cancel(): boolean;
68
59
  close(): void;
69
- commitTransaction(callback: (error?: Error) => void): void;
70
- connect(callback: (error?: Error) => void): void;
60
+ commitTransaction(callback: (error?: Error | null) => void, name?: string): void;
61
+ connect(callback?: (error?: Error) => void): void;
71
62
  execSql(request: TediousRequest): void;
72
- reset(callback: (error?: Error) => void): void;
73
- rollbackTransaction(callback: (error?: Error) => void): void;
74
- once(event: 'end', listener: () => void): void;
63
+ off(event: 'error', listener: (error: unknown) => void): this;
64
+ off(event: string, listener: (...args: any[]) => void): this;
65
+ on(event: 'error', listener: (error: unknown) => void): this;
66
+ on(event: string, listener: (...args: any[]) => void): this;
67
+ once(event: 'end', listener: () => void): this;
68
+ once(event: string, listener: (...args: any[]) => void): this;
69
+ reset(callback: (error?: Error | null) => void): void;
70
+ rollbackTransaction(callback: (error?: Error | null) => void, name?: string): void;
71
+ saveTransaction(callback: (error?: Error | null) => void, name: string): void;
72
+ }
73
+ export type TediousIsolationLevel = Record<string, number>;
74
+ export interface TediousRequestClass {
75
+ new (sqlTextOrProcedure: string | undefined, callback: (error?: Error | null, rowCount?: number, rows?: any) => void, options?: {
76
+ statementColumnEncryptionSetting?: any;
77
+ }): TediousRequest;
75
78
  }
76
79
  export declare class TediousRequest {
77
- constructor(sql: string, callback: (error: Error, rowCount: number, rows: any[]) => void);
78
- addParameter(name: string, type: TediousType, value: any, options?: {
79
- length?: number | 'max' | undefined;
80
- precision?: number | undefined;
81
- scale?: number | undefined;
82
- }): void;
83
- off(event: 'row', listener: (...args: any[]) => void): void;
84
- on(event: 'row', listener: (columns: TediousColumnValue[]) => void): void;
85
- once(event: 'requestCompleted', listener: (...args: any[]) => void): void;
80
+ addParameter(name: string, dataType: TediousDataType, value?: unknown, options?: Readonly<{
81
+ output?: boolean;
82
+ length?: number;
83
+ precision?: number;
84
+ scale?: number;
85
+ }> | null): void;
86
+ off(event: 'row', listener: (columns: any) => void): this;
87
+ off(event: string, listener: (...args: any[]) => void): this;
88
+ on(event: 'row', listener: (columns: any) => void): this;
89
+ on(event: string, listener: (...args: any[]) => void): this;
90
+ once(event: 'requestCompleted', listener: () => void): this;
91
+ once(event: string, listener: (...args: any[]) => void): this;
92
+ pause(): void;
93
+ resume(): void;
94
+ }
95
+ export interface TediousTypes {
96
+ NVarChar: TediousDataType;
97
+ BigInt: TediousDataType;
98
+ Int: TediousDataType;
99
+ Float: TediousDataType;
100
+ Bit: TediousDataType;
101
+ DateTime: TediousDataType;
102
+ VarBinary: TediousDataType;
103
+ [x: string]: TediousDataType;
104
+ }
105
+ export interface TediousDataType {
86
106
  }
87
107
  export interface TediousColumnValue {
88
108
  metadata: {
@@ -44,6 +44,7 @@ declare class MssqlConnection implements DatabaseConnection {
44
44
  constructor(connection: TediousConnection, tedious: Tedious);
45
45
  beginTransaction(settings: TransactionSettings): Promise<void>;
46
46
  commitTransaction(): Promise<void>;
47
+ connect(): Promise<this>;
47
48
  executeQuery<O>(compiledQuery: CompiledQuery): Promise<QueryResult<O>>;
48
49
  rollbackTransaction(): Promise<void>;
49
50
  streamQuery<O>(compiledQuery: CompiledQuery, chunkSize: number): AsyncIterableIterator<QueryResult<O>>;
@@ -15,13 +15,7 @@ export class MssqlDriver {
15
15
  ...this.#config.tarn.options,
16
16
  create: async () => {
17
17
  const connection = await this.#config.tedious.connectionFactory();
18
- await new Promise((resolve, reject) => connection.connect((error) => {
19
- if (error)
20
- reject(error);
21
- else
22
- resolve(undefined);
23
- }));
24
- return new MssqlConnection(connection, this.#config.tedious);
18
+ return await new MssqlConnection(connection, this.#config.tedious).connect();
25
19
  },
26
20
  destroy: async (connection) => {
27
21
  await connection[PRIVATE_DESTROY_METHOD]();
@@ -60,6 +54,10 @@ class MssqlConnection {
60
54
  constructor(connection, tedious) {
61
55
  this.#connection = connection;
62
56
  this.#tedious = tedious;
57
+ this.#connection.on('error', console.error);
58
+ this.#connection.once('end', () => {
59
+ this.#connection.off('error', console.error);
60
+ });
63
61
  }
64
62
  async beginTransaction(settings) {
65
63
  const { isolationLevel } = settings;
@@ -80,10 +78,28 @@ class MssqlConnection {
80
78
  resolve(undefined);
81
79
  }));
82
80
  }
81
+ async connect() {
82
+ await new Promise((resolve, reject) => {
83
+ this.#connection.connect((error) => {
84
+ if (error) {
85
+ console.error(error);
86
+ reject(error);
87
+ }
88
+ else {
89
+ resolve(undefined);
90
+ }
91
+ });
92
+ });
93
+ return this;
94
+ }
83
95
  async executeQuery(compiledQuery) {
84
96
  try {
85
97
  const deferred = new Deferred();
86
- const request = new MssqlRequest(this.#tedious, compiledQuery, deferred);
98
+ const request = new MssqlRequest({
99
+ compiledQuery,
100
+ tedious: this.#tedious,
101
+ onDone: deferred,
102
+ });
87
103
  this.#connection.execSql(request.request);
88
104
  const { rowCount, rows } = await deferred.promise;
89
105
  return {
@@ -107,11 +123,15 @@ class MssqlConnection {
107
123
  if (!Number.isInteger(chunkSize) || chunkSize <= 0) {
108
124
  throw new Error('chunkSize must be a positive integer');
109
125
  }
110
- const request = new MssqlRequest(this.#tedious, compiledQuery);
126
+ const request = new MssqlRequest({
127
+ compiledQuery,
128
+ streamChunkSize: chunkSize,
129
+ tedious: this.#tedious,
130
+ });
111
131
  this.#connection.execSql(request.request);
112
132
  try {
113
133
  while (true) {
114
- const rows = await request.readChunk(chunkSize);
134
+ const rows = await request.readChunk();
115
135
  if (rows.length === 0) {
116
136
  break;
117
137
  }
@@ -122,13 +142,17 @@ class MssqlConnection {
122
142
  }
123
143
  }
124
144
  finally {
125
- this.#connection.cancel();
145
+ await this.#cancelRequest(request);
126
146
  }
127
147
  }
128
148
  async validate() {
129
149
  try {
130
150
  const deferred = new Deferred();
131
- const request = new MssqlRequest(this.#tedious, CompiledQuery.raw('select 1'), deferred);
151
+ const request = new MssqlRequest({
152
+ compiledQuery: CompiledQuery.raw('select 1'),
153
+ onDone: deferred,
154
+ tedious: this.#tedious,
155
+ });
132
156
  this.#connection.execSql(request.request);
133
157
  await deferred.promise;
134
158
  return true;
@@ -152,6 +176,16 @@ class MssqlConnection {
152
176
  }
153
177
  return tediousIsolationLevel;
154
178
  }
179
+ #cancelRequest(request) {
180
+ return new Promise((resolve) => {
181
+ request.request.once('requestCompleted', resolve);
182
+ const wasCanceled = this.#connection.cancel();
183
+ if (!wasCanceled) {
184
+ request.request.off('requestCompleted', resolve);
185
+ resolve(undefined);
186
+ }
187
+ });
188
+ }
155
189
  [PRIVATE_RELEASE_METHOD]() {
156
190
  return new Promise((resolve, reject) => {
157
191
  this.#connection.reset((error) => {
@@ -174,42 +208,62 @@ class MssqlConnection {
174
208
  class MssqlRequest {
175
209
  #request;
176
210
  #rows;
211
+ #streamChunkSize;
212
+ #subscribers;
177
213
  #tedious;
178
- #completed;
179
214
  #error;
180
215
  #rowCount;
181
- constructor(tedious, compiledQuery, onDone) {
182
- this.#completed = false;
216
+ constructor(props) {
217
+ const { compiledQuery, onDone, streamChunkSize, tedious } = props;
183
218
  this.#rows = [];
219
+ this.#streamChunkSize = streamChunkSize;
220
+ this.#subscribers = {};
184
221
  this.#tedious = tedious;
185
- const { parameters, sql } = compiledQuery;
186
- this.#request = new this.#tedious.Request(sql, (err, rowCount) => {
222
+ if (onDone) {
223
+ const subscriptionKey = 'onDone';
224
+ this.#subscribers[subscriptionKey] = (event, error) => {
225
+ if (event === 'chunkReady') {
226
+ return;
227
+ }
228
+ delete this.#subscribers[subscriptionKey];
229
+ if (event === 'error') {
230
+ onDone.reject(error);
231
+ }
232
+ else {
233
+ onDone.resolve({
234
+ rowCount: this.#rowCount,
235
+ rows: this.#rows,
236
+ });
237
+ }
238
+ };
239
+ }
240
+ this.#request = new this.#tedious.Request(compiledQuery.sql, (err, rowCount) => {
187
241
  if (err) {
188
- this.#error = err instanceof AggregateError ? err.errors : err;
189
- onDone?.reject(this.#error);
242
+ Object.values(this.#subscribers).forEach((subscriber) => subscriber('error', err instanceof AggregateError ? err.errors : err));
190
243
  }
191
244
  else {
192
245
  this.#rowCount = rowCount;
193
246
  }
194
247
  });
195
- this.#addParametersToRequest(parameters);
196
- this.#attachListeners(onDone);
248
+ this.#addParametersToRequest(compiledQuery.parameters);
249
+ this.#attachListeners();
197
250
  }
198
251
  get request() {
199
252
  return this.#request;
200
253
  }
201
- readChunk(chunkSize) {
254
+ readChunk() {
255
+ const subscriptionKey = this.readChunk.name;
202
256
  return new Promise((resolve, reject) => {
203
- const interval = setInterval(() => {
204
- if (this.#error) {
205
- clearInterval(interval);
206
- reject(this.#error);
257
+ this.#subscribers[subscriptionKey] = (event, error) => {
258
+ delete this.#subscribers[subscriptionKey];
259
+ if (event === 'error') {
260
+ reject(error);
207
261
  }
208
- else if (this.#completed || this.#rows.length >= chunkSize) {
209
- clearInterval(interval);
210
- resolve(this.#rows.splice(0, chunkSize));
262
+ else {
263
+ resolve(this.#rows.splice(0, this.#streamChunkSize));
211
264
  }
212
- }, 0);
265
+ };
266
+ this.#request.resume();
213
267
  });
214
268
  }
215
269
  #addParametersToRequest(parameters) {
@@ -218,22 +272,27 @@ class MssqlRequest {
218
272
  this.#request.addParameter(String(i + 1), this.#getTediousDataType(parameter), parameter);
219
273
  }
220
274
  }
221
- #attachListeners(onDone) {
275
+ #attachListeners() {
276
+ const pauseAndEmitChunkReady = this.#streamChunkSize
277
+ ? () => {
278
+ if (this.#streamChunkSize <= this.#rows.length) {
279
+ this.#request.pause();
280
+ Object.values(this.#subscribers).forEach((subscriber) => subscriber('chunkReady'));
281
+ }
282
+ }
283
+ : () => { };
222
284
  const rowListener = (columns) => {
223
285
  const row = {};
224
286
  for (const column of columns) {
225
287
  row[column.metadata.colName] = column.value;
226
288
  }
227
289
  this.#rows.push(row);
290
+ pauseAndEmitChunkReady();
228
291
  };
229
292
  this.#request.on('row', rowListener);
230
293
  this.#request.once('requestCompleted', () => {
231
- this.#completed = true;
294
+ Object.values(this.#subscribers).forEach((subscriber) => subscriber('completed'));
232
295
  this.#request.off('row', rowListener);
233
- onDone?.resolve({
234
- rowCount: this.#rowCount,
235
- rows: this.#rows,
236
- });
237
296
  });
238
297
  }
239
298
  #getTediousDataType(value) {
@@ -49,7 +49,7 @@ export class PostgresIntrospector {
49
49
  .as('auto_incrementing'),
50
50
  ])
51
51
  // r == normal table
52
- .where((eb) => eb.or([eb('c.relkind', '=', 'r'), eb('c.relkind', '=', 'v')]))
52
+ .where((eb) => eb.or([eb('c.relkind', '=', 'r'), eb('c.relkind', '=', 'v'), eb('c.relkind', '=', 'p')]))
53
53
  .where('ns.nspname', '!~', '^pg_')
54
54
  .where('ns.nspname', '!=', 'information_schema')
55
55
  // No system columns
@@ -21,7 +21,9 @@ export interface QueryResult<O> {
21
21
  /**
22
22
  * This is defined for update queries and contains the number of rows
23
23
  * the query changed.
24
- * This is optional and only provided by some drivers like node-mysql2.
24
+ *
25
+ * This is **optional** and only provided in dialects such as MySQL.
26
+ * You would probably use {@link numAffectedRows} in most cases.
25
27
  */
26
28
  readonly numChangedRows?: bigint;
27
29
  /**
@@ -5,7 +5,7 @@ export declare class DynamicModule {
5
5
  *
6
6
  * Kysely is built in a way that by default you can't refer to tables or columns
7
7
  * that are not actually visible in the current query and context. This is all
8
- * done by typescript at compile time, which means that you need to know the
8
+ * done by TypeScript at compile time, which means that you need to know the
9
9
  * columns and tables at compile time. This is not always the case of course.
10
10
  *
11
11
  * This method is meant to be used in those cases where the column names
@@ -6,7 +6,7 @@ export class DynamicModule {
6
6
  *
7
7
  * Kysely is built in a way that by default you can't refer to tables or columns
8
8
  * that are not actually visible in the current query and context. This is all
9
- * done by typescript at compile time, which means that you need to know the
9
+ * done by TypeScript at compile time, which means that you need to know the
10
10
  * columns and tables at compile time. This is not always the case of course.
11
11
  *
12
12
  * This method is meant to be used in those cases where the column names
@@ -293,7 +293,7 @@ export interface ExpressionBuilder<DB, TB extends keyof DB> {
293
293
  *
294
294
  * ```ts
295
295
  * db.update('pet').set((eb) => ({
296
- * name: sql`concat(${eb.ref('pet.name')}, ${suffix})`
296
+ * name: sql<string>`concat(${eb.ref('pet.name')}, ${suffix})`
297
297
  * }))
298
298
  * ```
299
299
  *
@@ -796,7 +796,7 @@ export interface ExpressionBuilder<DB, TB extends keyof DB> {
796
796
  /**
797
797
  * Creates a `cast(expr as dataType)` expression.
798
798
  *
799
- * Since Kysely can't know the mapping between javascript and database types,
799
+ * Since Kysely can't know the mapping between JavaScript and database types,
800
800
  * you need to provide both explicitly.
801
801
  *
802
802
  * ### Examples
@@ -6,7 +6,7 @@ import { Simplify } from '../util/type-utils.js';
6
6
  * A MySQL helper for aggregating a subquery into a JSON array.
7
7
  *
8
8
  * NOTE: This helper is only guaranteed to fully work with the built-in `MysqlDialect`.
9
- * While the produced SQL is compatible with all MySQL databases, some 3rd party dialects
9
+ * While the produced SQL is compatible with all MySQL databases, some third-party dialects
10
10
  * may not parse the nested JSON into arrays. In these cases you can use the built in
11
11
  * `ParseJSONResultsPlugin` to parse the results.
12
12
  *
@@ -55,7 +55,7 @@ export declare function jsonArrayFrom<O>(expr: SelectQueryBuilderExpression<O>):
55
55
  * The subquery must only return one row.
56
56
  *
57
57
  * NOTE: This helper is only guaranteed to fully work with the built-in `MysqlDialect`.
58
- * While the produced SQL is compatible with all MySQL databases, some 3rd party dialects
58
+ * While the produced SQL is compatible with all MySQL databases, some third-party dialects
59
59
  * may not parse the nested JSON into objects. In these cases you can use the built in
60
60
  * `ParseJSONResultsPlugin` to parse the results.
61
61
  *
@@ -102,7 +102,7 @@ export declare function jsonObjectFrom<O>(expr: SelectQueryBuilderExpression<O>)
102
102
  * The MySQL `json_object` function.
103
103
  *
104
104
  * NOTE: This helper is only guaranteed to fully work with the built-in `MysqlDialect`.
105
- * While the produced SQL is compatible with all MySQL databases, some 3rd party dialects
105
+ * While the produced SQL is compatible with all MySQL databases, some third-party dialects
106
106
  * may not parse the nested JSON into objects. In these cases you can use the built in
107
107
  * `ParseJSONResultsPlugin` to parse the results.
108
108
  *
@@ -5,7 +5,7 @@ import { getJsonObjectArgs } from '../util/json-object-args.js';
5
5
  * A MySQL helper for aggregating a subquery into a JSON array.
6
6
  *
7
7
  * NOTE: This helper is only guaranteed to fully work with the built-in `MysqlDialect`.
8
- * While the produced SQL is compatible with all MySQL databases, some 3rd party dialects
8
+ * While the produced SQL is compatible with all MySQL databases, some third-party dialects
9
9
  * may not parse the nested JSON into arrays. In these cases you can use the built in
10
10
  * `ParseJSONResultsPlugin` to parse the results.
11
11
  *
@@ -56,7 +56,7 @@ export function jsonArrayFrom(expr) {
56
56
  * The subquery must only return one row.
57
57
  *
58
58
  * NOTE: This helper is only guaranteed to fully work with the built-in `MysqlDialect`.
59
- * While the produced SQL is compatible with all MySQL databases, some 3rd party dialects
59
+ * While the produced SQL is compatible with all MySQL databases, some third-party dialects
60
60
  * may not parse the nested JSON into objects. In these cases you can use the built in
61
61
  * `ParseJSONResultsPlugin` to parse the results.
62
62
  *
@@ -105,7 +105,7 @@ export function jsonObjectFrom(expr) {
105
105
  * The MySQL `json_object` function.
106
106
  *
107
107
  * NOTE: This helper is only guaranteed to fully work with the built-in `MysqlDialect`.
108
- * While the produced SQL is compatible with all MySQL databases, some 3rd party dialects
108
+ * While the produced SQL is compatible with all MySQL databases, some third-party dialects
109
109
  * may not parse the nested JSON into objects. In these cases you can use the built in
110
110
  * `ParseJSONResultsPlugin` to parse the results.
111
111
  *
@@ -15,7 +15,7 @@ import { Simplify } from '../util/type-utils.js';
15
15
  * Please keep in mind that the helpers under the `kysely/helpers` folder, including
16
16
  * `jsonArrayFrom`, are not guaranteed to work with third party dialects. In order for
17
17
  * them to work, the dialect must automatically parse the `json` data type into
18
- * javascript JSON values like objects and arrays. Some dialects might simply return
18
+ * JavaScript JSON values like objects and arrays. Some dialects might simply return
19
19
  * the data as a JSON string. In these cases you can use the built in `ParseJSONResultsPlugin`
20
20
  * to parse the results.
21
21
  *
@@ -65,9 +65,9 @@ export declare function jsonArrayFrom<O>(expr: Expression<O>): RawBuilder<Simpli
65
65
  * use the `jsonObjectFrom` helper to fetch person's favorite pet along with the person's id.
66
66
  *
67
67
  * Please keep in mind that the helpers under the `kysely/helpers` folder, including
68
- * `jsonObjectFrom`, are not guaranteed to work with 3rd party dialects. In order for
68
+ * `jsonObjectFrom`, are not guaranteed to work with third-party dialects. In order for
69
69
  * them to work, the dialect must automatically parse the `json` data type into
70
- * javascript JSON values like objects and arrays. Some dialects might simply return
70
+ * JavaScript JSON values like objects and arrays. Some dialects might simply return
71
71
  * the data as a JSON string. In these cases you can use the built in `ParseJSONResultsPlugin`
72
72
  * to parse the results.
73
73
  *
@@ -107,7 +107,7 @@ export declare function jsonObjectFrom<O>(expr: Expression<O>): RawBuilder<Simpl
107
107
  * The PostgreSQL `json_build_object` function.
108
108
  *
109
109
  * NOTE: This helper is only guaranteed to fully work with the built-in `PostgresDialect`.
110
- * While the produced SQL is compatible with all PostgreSQL databases, some 3rd party dialects
110
+ * While the produced SQL is compatible with all PostgreSQL databases, some third-party dialects
111
111
  * may not parse the nested JSON into objects. In these cases you can use the built in
112
112
  * `ParseJSONResultsPlugin` to parse the results.
113
113
  *
@@ -14,7 +14,7 @@ import { sql } from '../raw-builder/sql.js';
14
14
  * Please keep in mind that the helpers under the `kysely/helpers` folder, including
15
15
  * `jsonArrayFrom`, are not guaranteed to work with third party dialects. In order for
16
16
  * them to work, the dialect must automatically parse the `json` data type into
17
- * javascript JSON values like objects and arrays. Some dialects might simply return
17
+ * JavaScript JSON values like objects and arrays. Some dialects might simply return
18
18
  * the data as a JSON string. In these cases you can use the built in `ParseJSONResultsPlugin`
19
19
  * to parse the results.
20
20
  *
@@ -66,9 +66,9 @@ export function jsonArrayFrom(expr) {
66
66
  * use the `jsonObjectFrom` helper to fetch person's favorite pet along with the person's id.
67
67
  *
68
68
  * Please keep in mind that the helpers under the `kysely/helpers` folder, including
69
- * `jsonObjectFrom`, are not guaranteed to work with 3rd party dialects. In order for
69
+ * `jsonObjectFrom`, are not guaranteed to work with third-party dialects. In order for
70
70
  * them to work, the dialect must automatically parse the `json` data type into
71
- * javascript JSON values like objects and arrays. Some dialects might simply return
71
+ * JavaScript JSON values like objects and arrays. Some dialects might simply return
72
72
  * the data as a JSON string. In these cases you can use the built in `ParseJSONResultsPlugin`
73
73
  * to parse the results.
74
74
  *
@@ -110,7 +110,7 @@ export function jsonObjectFrom(expr) {
110
110
  * The PostgreSQL `json_build_object` function.
111
111
  *
112
112
  * NOTE: This helper is only guaranteed to fully work with the built-in `PostgresDialect`.
113
- * While the produced SQL is compatible with all PostgreSQL databases, some 3rd party dialects
113
+ * While the produced SQL is compatible with all PostgreSQL databases, some third-party dialects
114
114
  * may not parse the nested JSON into objects. In these cases you can use the built in
115
115
  * `ParseJSONResultsPlugin` to parse the results.
116
116
  *
@@ -5,6 +5,7 @@ export { ExpressionBuilder, expressionBuilder, } from './expression/expression-b
5
5
  export * from './expression/expression-wrapper.js';
6
6
  export * from './query-builder/where-interface.js';
7
7
  export * from './query-builder/returning-interface.js';
8
+ export * from './query-builder/output-interface.js';
8
9
  export * from './query-builder/having-interface.js';
9
10
  export * from './query-builder/select-query-builder.js';
10
11
  export * from './query-builder/insert-query-builder.js';
@@ -91,17 +92,21 @@ export * from './plugin/with-schema/with-schema-plugin.js';
91
92
  export * from './plugin/parse-json-results/parse-json-results-plugin.js';
92
93
  export * from './operation-node/add-column-node.js';
93
94
  export * from './operation-node/add-constraint-node.js';
95
+ export * from './operation-node/add-index-node.js';
96
+ export * from './operation-node/aggregate-function-node.js';
94
97
  export * from './operation-node/alias-node.js';
95
98
  export * from './operation-node/alter-column-node.js';
96
99
  export * from './operation-node/alter-table-node.js';
97
100
  export * from './operation-node/and-node.js';
101
+ export * from './operation-node/binary-operation-node.js';
98
102
  export * from './operation-node/case-node.js';
103
+ export * from './operation-node/cast-node.js';
99
104
  export * from './operation-node/check-constraint-node.js';
100
105
  export * from './operation-node/column-definition-node.js';
101
106
  export * from './operation-node/column-node.js';
102
107
  export * from './operation-node/column-update-node.js';
103
- export * from './operation-node/common-table-expression-node.js';
104
108
  export * from './operation-node/common-table-expression-name-node.js';
109
+ export * from './operation-node/common-table-expression-node.js';
105
110
  export * from './operation-node/constraint-node.js';
106
111
  export * from './operation-node/create-index-node.js';
107
112
  export * from './operation-node/create-schema-node.js';
@@ -109,6 +114,7 @@ export * from './operation-node/create-table-node.js';
109
114
  export * from './operation-node/create-type-node.js';
110
115
  export * from './operation-node/create-view-node.js';
111
116
  export * from './operation-node/data-type-node.js';
117
+ export * from './operation-node/default-insert-value-node.js';
112
118
  export * from './operation-node/default-value-node.js';
113
119
  export * from './operation-node/delete-query-node.js';
114
120
  export * from './operation-node/drop-column-node.js';
@@ -118,8 +124,11 @@ export * from './operation-node/drop-schema-node.js';
118
124
  export * from './operation-node/drop-table-node.js';
119
125
  export * from './operation-node/drop-type-node.js';
120
126
  export * from './operation-node/drop-view-node.js';
127
+ export * from './operation-node/explain-node.js';
128
+ export * from './operation-node/fetch-node.js';
121
129
  export * from './operation-node/foreign-key-constraint-node.js';
122
130
  export * from './operation-node/from-node.js';
131
+ export * from './operation-node/function-node.js';
123
132
  export * from './operation-node/generated-node.js';
124
133
  export * from './operation-node/group-by-item-node.js';
125
134
  export * from './operation-node/group-by-node.js';
@@ -127,8 +136,14 @@ export * from './operation-node/having-node.js';
127
136
  export * from './operation-node/identifier-node.js';
128
137
  export * from './operation-node/insert-query-node.js';
129
138
  export * from './operation-node/join-node.js';
139
+ export * from './operation-node/json-operator-chain-node.js';
140
+ export * from './operation-node/json-path-leg-node.js';
141
+ export * from './operation-node/json-path-node.js';
142
+ export * from './operation-node/json-reference-node.js';
130
143
  export * from './operation-node/limit-node.js';
131
144
  export * from './operation-node/list-node.js';
145
+ export * from './operation-node/matched-node.js';
146
+ export * from './operation-node/merge-query-node.js';
132
147
  export * from './operation-node/modify-column-node.js';
133
148
  export * from './operation-node/offset-node.js';
134
149
  export * from './operation-node/on-conflict-node.js';
@@ -142,7 +157,11 @@ export * from './operation-node/operator-node.js';
142
157
  export * from './operation-node/or-node.js';
143
158
  export * from './operation-node/order-by-item-node.js';
144
159
  export * from './operation-node/order-by-node.js';
160
+ export * from './operation-node/output-node.js';
161
+ export * from './operation-node/over-node.js';
145
162
  export * from './operation-node/parens-node.js';
163
+ export * from './operation-node/partition-by-item-node.js';
164
+ export * from './operation-node/partition-by-node.js';
146
165
  export * from './operation-node/primary-constraint-node.js';
147
166
  export * from './operation-node/primitive-value-list-node.js';
148
167
  export * from './operation-node/query-node.js';
@@ -151,38 +170,26 @@ export * from './operation-node/reference-node.js';
151
170
  export * from './operation-node/references-node.js';
152
171
  export * from './operation-node/rename-column-node.js';
153
172
  export * from './operation-node/returning-node.js';
173
+ export * from './operation-node/schemable-identifier-node.js';
154
174
  export * from './operation-node/select-all-node.js';
155
- export * from './operation-node/select-query-node.js';
175
+ export * from './operation-node/select-modifier-node.js';
156
176
  export * from './operation-node/select-query-node.js';
157
177
  export * from './operation-node/selection-node.js';
178
+ export * from './operation-node/set-operation-node.js';
179
+ export * from './operation-node/simple-reference-expression-node.js';
158
180
  export * from './operation-node/table-node.js';
181
+ export * from './operation-node/top-node.js';
182
+ export * from './operation-node/tuple-node.js';
183
+ export * from './operation-node/unary-operation-node.js';
159
184
  export * from './operation-node/unique-constraint-node.js';
160
185
  export * from './operation-node/update-query-node.js';
186
+ export * from './operation-node/using-node.js';
161
187
  export * from './operation-node/value-list-node.js';
162
188
  export * from './operation-node/value-node.js';
163
189
  export * from './operation-node/values-node.js';
164
190
  export * from './operation-node/when-node.js';
165
191
  export * from './operation-node/where-node.js';
166
192
  export * from './operation-node/with-node.js';
167
- export * from './operation-node/explain-node.js';
168
- export * from './operation-node/default-insert-value-node.js';
169
- export * from './operation-node/aggregate-function-node.js';
170
- export * from './operation-node/over-node.js';
171
- export * from './operation-node/partition-by-node.js';
172
- export * from './operation-node/partition-by-item-node.js';
173
- export * from './operation-node/set-operation-node.js';
174
- export * from './operation-node/binary-operation-node.js';
175
- export * from './operation-node/unary-operation-node.js';
176
- export * from './operation-node/using-node.js';
177
- export * from './operation-node/json-reference-node.js';
178
- export * from './operation-node/json-path-leg-node.js';
179
- export * from './operation-node/json-path-node.js';
180
- export * from './operation-node/json-operator-chain-node.js';
181
- export * from './operation-node/tuple-node.js';
182
- export * from './operation-node/merge-query-node.js';
183
- export * from './operation-node/matched-node.js';
184
- export * from './operation-node/fetch-node.js';
185
- export * from './operation-node/top-node.js';
186
193
  export * from './util/column-type.js';
187
194
  export * from './util/compilable.js';
188
195
  export * from './util/explainable.js';