kysely 0.11.2 → 0.11.3

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.
@@ -1,7 +1,5 @@
1
1
  import { DefaultQueryCompiler } from '../../query-compiler/default-query-compiler.js';
2
2
  export declare class MysqlQueryCompiler extends DefaultQueryCompiler {
3
- protected visitReturning(): void;
4
- protected getSql(): string;
5
3
  protected getCurrentParameterPlaceholder(): string;
6
4
  protected getLeftIdentifierWrapper(): string;
7
5
  protected getRightIdentifierWrapper(): string;
@@ -3,12 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MysqlQueryCompiler = void 0;
4
4
  const default_query_compiler_js_1 = require("../../query-compiler/default-query-compiler.js");
5
5
  class MysqlQueryCompiler extends default_query_compiler_js_1.DefaultQueryCompiler {
6
- visitReturning() {
7
- // Do nothing.
8
- }
9
- getSql() {
10
- return super.getSql().trimEnd();
11
- }
12
6
  getCurrentParameterPlaceholder() {
13
7
  return '?';
14
8
  }
@@ -1,3 +1,6 @@
1
1
  import { DefaultQueryCompiler } from '../../query-compiler/default-query-compiler.js';
2
2
  export declare class PostgresQueryCompiler extends DefaultQueryCompiler {
3
+ protected getCurrentParameterPlaceholder(): string;
4
+ protected getLeftIdentifierWrapper(): string;
5
+ protected getRightIdentifierWrapper(): string;
3
6
  }
@@ -2,7 +2,15 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PostgresQueryCompiler = void 0;
4
4
  const default_query_compiler_js_1 = require("../../query-compiler/default-query-compiler.js");
5
- // The default query compiler is for PostgreSQL.
6
5
  class PostgresQueryCompiler extends default_query_compiler_js_1.DefaultQueryCompiler {
6
+ getCurrentParameterPlaceholder() {
7
+ return '$' + this.numParameters;
8
+ }
9
+ getLeftIdentifierWrapper() {
10
+ return '"';
11
+ }
12
+ getRightIdentifierWrapper() {
13
+ return '"';
14
+ }
7
15
  }
8
16
  exports.PostgresQueryCompiler = PostgresQueryCompiler;
@@ -1,7 +1,7 @@
1
1
  import { DefaultQueryCompiler } from '../../query-compiler/default-query-compiler.js';
2
2
  export declare class SqliteQueryCompiler extends DefaultQueryCompiler {
3
- protected visitReturning(): void;
4
- protected getSql(): string;
5
3
  protected getCurrentParameterPlaceholder(): string;
4
+ protected getLeftIdentifierWrapper(): string;
5
+ protected getRightIdentifierWrapper(): string;
6
6
  protected getAutoIncrement(): string;
7
7
  }
@@ -3,15 +3,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SqliteQueryCompiler = void 0;
4
4
  const default_query_compiler_js_1 = require("../../query-compiler/default-query-compiler.js");
5
5
  class SqliteQueryCompiler extends default_query_compiler_js_1.DefaultQueryCompiler {
6
- visitReturning() {
7
- // Do nothing.
8
- }
9
- getSql() {
10
- return super.getSql().trimEnd();
11
- }
12
6
  getCurrentParameterPlaceholder() {
13
7
  return '?';
14
8
  }
9
+ getLeftIdentifierWrapper() {
10
+ return '"';
11
+ }
12
+ getRightIdentifierWrapper() {
13
+ return '"';
14
+ }
15
15
  getAutoIncrement() {
16
16
  return 'autoincrement';
17
17
  }
@@ -97,7 +97,7 @@ class RuntimeConnection {
97
97
  #logQuery(compiledQuery, startTime) {
98
98
  this.#log.query(() => ({
99
99
  level: 'query',
100
- sql: compiledQuery.sql,
100
+ query: compiledQuery,
101
101
  queryDurationMillis: this.#calculateDurationMillis(startTime),
102
102
  }));
103
103
  }
@@ -127,6 +127,7 @@ export * from './operation-node/where-node.js';
127
127
  export * from './operation-node/with-node.js';
128
128
  export * from './introspection/database-introspector.js';
129
129
  export * from './util/compilable.js';
130
+ export * from './util/log.js';
130
131
  export { AnyColumn, UnknownRow, AnyQueryBuilder } from './util/type-utils.js';
131
132
  export { SelectExpression, SelectExpressionOrList, } from './parser/select-parser.js';
132
133
  export { ReferenceExpression, ReferenceExpressionOrList, } from './parser/reference-parser.js';
package/dist/cjs/index.js CHANGED
@@ -139,3 +139,4 @@ __exportStar(require("./operation-node/where-node.js"), exports);
139
139
  __exportStar(require("./operation-node/with-node.js"), exports);
140
140
  __exportStar(require("./introspection/database-introspector.js"), exports);
141
141
  __exportStar(require("./util/compilable.js"), exports);
142
+ __exportStar(require("./util/log.js"), exports);
@@ -10,7 +10,7 @@ import { DatabaseIntrospector } from './introspection/database-introspector.js';
10
10
  import { Driver, IsolationLevel } from './driver/driver.js';
11
11
  import { ParseContext } from './parser/parse-context.js';
12
12
  import { FunctionBuilder } from './query-builder/function-builder.js';
13
- import { LogLevel } from './util/log.js';
13
+ import { LogConfig } from './util/log.js';
14
14
  /**
15
15
  * The main Kysely class.
16
16
  *
@@ -235,17 +235,33 @@ export interface KyselyConfig {
235
235
  readonly dialect: Dialect;
236
236
  readonly plugins?: KyselyPlugin[];
237
237
  /**
238
- * A list of log levels to log.
238
+ * A list of log levels to log or a custom logger function.
239
239
  *
240
240
  * Currently there's only two levels: `query` and `error`.
241
241
  * This will be expanded based on user feedback later.
242
242
  *
243
- * Log levels:
243
+ * @example
244
+ * ```ts
245
+ * const db = new Kysely<Database>({
246
+ * dialect: new PostgresDialect(postgresConfig),
247
+ * log: ['query', 'error']
248
+ * })
249
+ * ```
244
250
  *
245
- * - query: Log each query's SQL and duration using console.log
246
- * - error: Log all query errors using console.error
251
+ * @example
252
+ * ```ts
253
+ * const db = new Kysely<Database>({
254
+ * dialect: new PostgresDialect(postgresConfig),
255
+ * log(event): void {
256
+ * if (event.level === 'query') {
257
+ * console.log(event.query.sql)
258
+ * console.log(event.query.parameters)
259
+ * }
260
+ * }
261
+ * })
262
+ * ```
247
263
  */
248
- readonly log?: ReadonlyArray<LogLevel>;
264
+ readonly log?: LogConfig;
249
265
  }
250
266
  export declare class ConnectionBuilder<DB> {
251
267
  #private;
@@ -1,19 +1,21 @@
1
+ import { CompiledQuery } from '../index.js';
1
2
  import { ArrayItemType } from './type-utils.js';
2
3
  export declare const LOG_LEVELS: readonly ["query", "error"];
3
4
  export declare type LogLevel = ArrayItemType<typeof LOG_LEVELS>;
4
- export declare class Log {
5
- #private;
6
- constructor(levels: ReadonlyArray<LogLevel>);
7
- query(getEvent: () => QueryLogEvent): void;
8
- error(getEvent: () => ErrorLogEvent): void;
9
- }
10
- export declare type LogEvent = QueryLogEvent | ErrorLogEvent;
11
5
  export interface QueryLogEvent {
12
6
  readonly level: 'query';
13
- readonly sql: string;
7
+ readonly query: CompiledQuery;
14
8
  readonly queryDurationMillis: number;
15
9
  }
16
10
  export interface ErrorLogEvent {
17
11
  readonly level: 'error';
18
12
  readonly error: unknown;
19
13
  }
14
+ export declare type LogEvent = QueryLogEvent | ErrorLogEvent;
15
+ export declare type LogConfig = ReadonlyArray<LogLevel> | ((event: LogEvent) => void);
16
+ export declare class Log {
17
+ #private;
18
+ constructor(config: LogConfig);
19
+ query(getEvent: () => QueryLogEvent): void;
20
+ error(getEvent: () => ErrorLogEvent): void;
21
+ }
@@ -2,27 +2,47 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Log = exports.LOG_LEVELS = void 0;
4
4
  const object_utils_js_1 = require("./object-utils.js");
5
- exports.LOG_LEVELS = ['query', 'error'];
5
+ exports.LOG_LEVELS = (0, object_utils_js_1.freeze)(['query', 'error']);
6
6
  class Log {
7
7
  #levels;
8
- constructor(levels) {
9
- this.#levels = (0, object_utils_js_1.freeze)({
10
- query: levels.includes('query'),
11
- error: levels.includes('error'),
12
- });
8
+ #logger;
9
+ constructor(config) {
10
+ if ((0, object_utils_js_1.isFunction)(config)) {
11
+ this.#logger = config;
12
+ this.#levels = (0, object_utils_js_1.freeze)({
13
+ query: true,
14
+ error: true,
15
+ });
16
+ }
17
+ else {
18
+ this.#levels = (0, object_utils_js_1.freeze)({
19
+ query: config.includes('query'),
20
+ error: config.includes('error'),
21
+ });
22
+ }
13
23
  }
14
24
  query(getEvent) {
15
25
  if (this.#levels.query) {
16
- this.#defaultQuery(getEvent());
26
+ if (this.#logger) {
27
+ this.#logger(getEvent());
28
+ }
29
+ else {
30
+ this.#defaultQuery(getEvent());
31
+ }
17
32
  }
18
33
  }
19
34
  error(getEvent) {
20
35
  if (this.#levels.error) {
21
- this.#defaultError(getEvent());
36
+ if (this.#logger) {
37
+ this.#logger(getEvent());
38
+ }
39
+ else {
40
+ this.#defaultError(getEvent());
41
+ }
22
42
  }
23
43
  }
24
44
  #defaultQuery(event) {
25
- console.log(`kysely:query: ${event.sql}`);
45
+ console.log(`kysely:query: ${event.query.sql}`);
26
46
  console.log(`kysely:query: duration: ${event.queryDurationMillis.toFixed(1)}ms`);
27
47
  }
28
48
  #defaultError(event) {
@@ -1,7 +1,5 @@
1
1
  import { DefaultQueryCompiler } from '../../query-compiler/default-query-compiler.js';
2
2
  export declare class MysqlQueryCompiler extends DefaultQueryCompiler {
3
- protected visitReturning(): void;
4
- protected getSql(): string;
5
3
  protected getCurrentParameterPlaceholder(): string;
6
4
  protected getLeftIdentifierWrapper(): string;
7
5
  protected getRightIdentifierWrapper(): string;
@@ -1,11 +1,5 @@
1
1
  import { DefaultQueryCompiler } from '../../query-compiler/default-query-compiler.js';
2
2
  export class MysqlQueryCompiler extends DefaultQueryCompiler {
3
- visitReturning() {
4
- // Do nothing.
5
- }
6
- getSql() {
7
- return super.getSql().trimEnd();
8
- }
9
3
  getCurrentParameterPlaceholder() {
10
4
  return '?';
11
5
  }
@@ -1,3 +1,6 @@
1
1
  import { DefaultQueryCompiler } from '../../query-compiler/default-query-compiler.js';
2
2
  export declare class PostgresQueryCompiler extends DefaultQueryCompiler {
3
+ protected getCurrentParameterPlaceholder(): string;
4
+ protected getLeftIdentifierWrapper(): string;
5
+ protected getRightIdentifierWrapper(): string;
3
6
  }
@@ -1,4 +1,12 @@
1
1
  import { DefaultQueryCompiler } from '../../query-compiler/default-query-compiler.js';
2
- // The default query compiler is for PostgreSQL.
3
2
  export class PostgresQueryCompiler extends DefaultQueryCompiler {
3
+ getCurrentParameterPlaceholder() {
4
+ return '$' + this.numParameters;
5
+ }
6
+ getLeftIdentifierWrapper() {
7
+ return '"';
8
+ }
9
+ getRightIdentifierWrapper() {
10
+ return '"';
11
+ }
4
12
  }
@@ -1,7 +1,7 @@
1
1
  import { DefaultQueryCompiler } from '../../query-compiler/default-query-compiler.js';
2
2
  export declare class SqliteQueryCompiler extends DefaultQueryCompiler {
3
- protected visitReturning(): void;
4
- protected getSql(): string;
5
3
  protected getCurrentParameterPlaceholder(): string;
4
+ protected getLeftIdentifierWrapper(): string;
5
+ protected getRightIdentifierWrapper(): string;
6
6
  protected getAutoIncrement(): string;
7
7
  }
@@ -1,14 +1,14 @@
1
1
  import { DefaultQueryCompiler } from '../../query-compiler/default-query-compiler.js';
2
2
  export class SqliteQueryCompiler extends DefaultQueryCompiler {
3
- visitReturning() {
4
- // Do nothing.
5
- }
6
- getSql() {
7
- return super.getSql().trimEnd();
8
- }
9
3
  getCurrentParameterPlaceholder() {
10
4
  return '?';
11
5
  }
6
+ getLeftIdentifierWrapper() {
7
+ return '"';
8
+ }
9
+ getRightIdentifierWrapper() {
10
+ return '"';
11
+ }
12
12
  getAutoIncrement() {
13
13
  return 'autoincrement';
14
14
  }
@@ -93,7 +93,7 @@ class RuntimeConnection {
93
93
  #logQuery(compiledQuery, startTime) {
94
94
  this.#log.query(() => ({
95
95
  level: 'query',
96
- sql: compiledQuery.sql,
96
+ query: compiledQuery,
97
97
  queryDurationMillis: this.#calculateDurationMillis(startTime),
98
98
  }));
99
99
  }
@@ -127,6 +127,7 @@ export * from './operation-node/where-node.js';
127
127
  export * from './operation-node/with-node.js';
128
128
  export * from './introspection/database-introspector.js';
129
129
  export * from './util/compilable.js';
130
+ export * from './util/log.js';
130
131
  export { AnyColumn, UnknownRow, AnyQueryBuilder } from './util/type-utils.js';
131
132
  export { SelectExpression, SelectExpressionOrList, } from './parser/select-parser.js';
132
133
  export { ReferenceExpression, ReferenceExpressionOrList, } from './parser/reference-parser.js';
package/dist/esm/index.js CHANGED
@@ -127,3 +127,4 @@ export * from './operation-node/where-node.js';
127
127
  export * from './operation-node/with-node.js';
128
128
  export * from './introspection/database-introspector.js';
129
129
  export * from './util/compilable.js';
130
+ export * from './util/log.js';
@@ -10,7 +10,7 @@ import { DatabaseIntrospector } from './introspection/database-introspector.js';
10
10
  import { Driver, IsolationLevel } from './driver/driver.js';
11
11
  import { ParseContext } from './parser/parse-context.js';
12
12
  import { FunctionBuilder } from './query-builder/function-builder.js';
13
- import { LogLevel } from './util/log.js';
13
+ import { LogConfig } from './util/log.js';
14
14
  /**
15
15
  * The main Kysely class.
16
16
  *
@@ -235,17 +235,33 @@ export interface KyselyConfig {
235
235
  readonly dialect: Dialect;
236
236
  readonly plugins?: KyselyPlugin[];
237
237
  /**
238
- * A list of log levels to log.
238
+ * A list of log levels to log or a custom logger function.
239
239
  *
240
240
  * Currently there's only two levels: `query` and `error`.
241
241
  * This will be expanded based on user feedback later.
242
242
  *
243
- * Log levels:
243
+ * @example
244
+ * ```ts
245
+ * const db = new Kysely<Database>({
246
+ * dialect: new PostgresDialect(postgresConfig),
247
+ * log: ['query', 'error']
248
+ * })
249
+ * ```
244
250
  *
245
- * - query: Log each query's SQL and duration using console.log
246
- * - error: Log all query errors using console.error
251
+ * @example
252
+ * ```ts
253
+ * const db = new Kysely<Database>({
254
+ * dialect: new PostgresDialect(postgresConfig),
255
+ * log(event): void {
256
+ * if (event.level === 'query') {
257
+ * console.log(event.query.sql)
258
+ * console.log(event.query.parameters)
259
+ * }
260
+ * }
261
+ * })
262
+ * ```
247
263
  */
248
- readonly log?: ReadonlyArray<LogLevel>;
264
+ readonly log?: LogConfig;
249
265
  }
250
266
  export declare class ConnectionBuilder<DB> {
251
267
  #private;
@@ -1,19 +1,21 @@
1
+ import { CompiledQuery } from '../index.js';
1
2
  import { ArrayItemType } from './type-utils.js';
2
3
  export declare const LOG_LEVELS: readonly ["query", "error"];
3
4
  export declare type LogLevel = ArrayItemType<typeof LOG_LEVELS>;
4
- export declare class Log {
5
- #private;
6
- constructor(levels: ReadonlyArray<LogLevel>);
7
- query(getEvent: () => QueryLogEvent): void;
8
- error(getEvent: () => ErrorLogEvent): void;
9
- }
10
- export declare type LogEvent = QueryLogEvent | ErrorLogEvent;
11
5
  export interface QueryLogEvent {
12
6
  readonly level: 'query';
13
- readonly sql: string;
7
+ readonly query: CompiledQuery;
14
8
  readonly queryDurationMillis: number;
15
9
  }
16
10
  export interface ErrorLogEvent {
17
11
  readonly level: 'error';
18
12
  readonly error: unknown;
19
13
  }
14
+ export declare type LogEvent = QueryLogEvent | ErrorLogEvent;
15
+ export declare type LogConfig = ReadonlyArray<LogLevel> | ((event: LogEvent) => void);
16
+ export declare class Log {
17
+ #private;
18
+ constructor(config: LogConfig);
19
+ query(getEvent: () => QueryLogEvent): void;
20
+ error(getEvent: () => ErrorLogEvent): void;
21
+ }
@@ -1,25 +1,45 @@
1
- import { freeze } from './object-utils.js';
2
- export const LOG_LEVELS = ['query', 'error'];
1
+ import { freeze, isFunction } from './object-utils.js';
2
+ export const LOG_LEVELS = freeze(['query', 'error']);
3
3
  export class Log {
4
4
  #levels;
5
- constructor(levels) {
6
- this.#levels = freeze({
7
- query: levels.includes('query'),
8
- error: levels.includes('error'),
9
- });
5
+ #logger;
6
+ constructor(config) {
7
+ if (isFunction(config)) {
8
+ this.#logger = config;
9
+ this.#levels = freeze({
10
+ query: true,
11
+ error: true,
12
+ });
13
+ }
14
+ else {
15
+ this.#levels = freeze({
16
+ query: config.includes('query'),
17
+ error: config.includes('error'),
18
+ });
19
+ }
10
20
  }
11
21
  query(getEvent) {
12
22
  if (this.#levels.query) {
13
- this.#defaultQuery(getEvent());
23
+ if (this.#logger) {
24
+ this.#logger(getEvent());
25
+ }
26
+ else {
27
+ this.#defaultQuery(getEvent());
28
+ }
14
29
  }
15
30
  }
16
31
  error(getEvent) {
17
32
  if (this.#levels.error) {
18
- this.#defaultError(getEvent());
33
+ if (this.#logger) {
34
+ this.#logger(getEvent());
35
+ }
36
+ else {
37
+ this.#defaultError(getEvent());
38
+ }
19
39
  }
20
40
  }
21
41
  #defaultQuery(event) {
22
- console.log(`kysely:query: ${event.sql}`);
42
+ console.log(`kysely:query: ${event.query.sql}`);
23
43
  console.log(`kysely:query: duration: ${event.queryDurationMillis.toFixed(1)}ms`);
24
44
  }
25
45
  #defaultError(event) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kysely",
3
- "version": "0.11.2",
3
+ "version": "0.11.3",
4
4
  "description": "Type safe SQL query builder",
5
5
  "repository": {
6
6
  "type": "git",