kysely 0.9.3 → 0.9.7

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 (66) hide show
  1. package/dist/cjs/dialect/mysql/mysql-dialect.d.ts +11 -0
  2. package/dist/cjs/dialect/mysql/mysql-dialect.js +7 -0
  3. package/dist/cjs/dialect/postgres/postgres-dialect.d.ts +13 -1
  4. package/dist/cjs/dialect/postgres/postgres-dialect.js +10 -0
  5. package/dist/cjs/dialect/postgres/postgres-driver.js +2 -2
  6. package/dist/cjs/dialect/postgres/postgres-query-compiler.js +1 -1
  7. package/dist/cjs/driver/runtime-driver.d.ts +3 -2
  8. package/dist/cjs/driver/runtime-driver.js +57 -4
  9. package/dist/cjs/index.d.ts +2 -1
  10. package/dist/cjs/kysely.d.ts +31 -9
  11. package/dist/cjs/kysely.js +25 -9
  12. package/dist/cjs/migration/migration.d.ts +32 -0
  13. package/dist/cjs/operation-node/operator-node.d.ts +1 -1
  14. package/dist/cjs/operation-node/operator-node.js +1 -0
  15. package/dist/cjs/parser/join-parser.d.ts +4 -4
  16. package/dist/cjs/plugin/camel-case/camel-case-plugin.js +5 -116
  17. package/dist/cjs/plugin/camel-case/camel-case-transformer.d.ts +8 -0
  18. package/dist/cjs/plugin/camel-case/camel-case-transformer.js +19 -0
  19. package/dist/cjs/plugin/camel-case/camel-case.d.ts +15 -0
  20. package/dist/cjs/plugin/camel-case/camel-case.js +109 -0
  21. package/dist/cjs/plugin/kysely-plugin.d.ts +1 -1
  22. package/dist/cjs/query-builder/query-builder.d.ts +35 -0
  23. package/dist/cjs/query-builder/query-builder.js +37 -0
  24. package/dist/cjs/query-executor/default-query-executor.d.ts +1 -0
  25. package/dist/cjs/query-executor/default-query-executor.js +6 -0
  26. package/dist/cjs/query-executor/noop-query-executor.d.ts +1 -0
  27. package/dist/cjs/query-executor/noop-query-executor.js +3 -0
  28. package/dist/cjs/query-executor/query-executor.d.ts +9 -4
  29. package/dist/cjs/util/log.d.ts +19 -0
  30. package/dist/cjs/util/log.js +37 -0
  31. package/dist/cjs/util/random-string.js +6 -3
  32. package/dist/cjs/util/type-utils.d.ts +4 -4
  33. package/dist/esm/dialect/mysql/mysql-dialect.d.ts +11 -0
  34. package/dist/esm/dialect/mysql/mysql-dialect.js +7 -0
  35. package/dist/esm/dialect/mysql/mysql-driver.js +1 -1
  36. package/dist/esm/dialect/postgres/postgres-dialect.d.ts +13 -1
  37. package/dist/esm/dialect/postgres/postgres-dialect.js +10 -0
  38. package/dist/esm/dialect/postgres/postgres-driver.js +2 -2
  39. package/dist/esm/dialect/postgres/postgres-query-compiler.js +1 -1
  40. package/dist/esm/driver/runtime-driver.d.ts +3 -2
  41. package/dist/esm/driver/runtime-driver.js +57 -4
  42. package/dist/esm/index.d.ts +2 -1
  43. package/dist/esm/kysely.d.ts +31 -9
  44. package/dist/esm/kysely.js +25 -9
  45. package/dist/esm/migration/migration.d.ts +32 -0
  46. package/dist/esm/operation-node/operator-node.d.ts +1 -1
  47. package/dist/esm/operation-node/operator-node.js +1 -0
  48. package/dist/esm/parser/join-parser.d.ts +4 -4
  49. package/dist/esm/plugin/camel-case/camel-case-plugin.js +4 -115
  50. package/dist/esm/plugin/camel-case/camel-case-transformer.d.ts +8 -0
  51. package/dist/esm/plugin/camel-case/camel-case-transformer.js +15 -0
  52. package/dist/esm/plugin/camel-case/camel-case.d.ts +15 -0
  53. package/dist/esm/plugin/camel-case/camel-case.js +104 -0
  54. package/dist/esm/plugin/kysely-plugin.d.ts +1 -1
  55. package/dist/esm/query-builder/query-builder.d.ts +35 -0
  56. package/dist/esm/query-builder/query-builder.js +37 -0
  57. package/dist/esm/query-executor/default-query-executor.d.ts +1 -0
  58. package/dist/esm/query-executor/default-query-executor.js +6 -0
  59. package/dist/esm/query-executor/noop-query-executor.d.ts +1 -0
  60. package/dist/esm/query-executor/noop-query-executor.js +3 -0
  61. package/dist/esm/query-executor/query-executor.d.ts +9 -4
  62. package/dist/esm/util/log.d.ts +19 -0
  63. package/dist/esm/util/log.js +33 -0
  64. package/dist/esm/util/random-string.js +6 -3
  65. package/dist/esm/util/type-utils.d.ts +4 -4
  66. package/package.json +4 -1
@@ -5,6 +5,13 @@ import { Dialect } from '../dialect.js';
5
5
  import { DatabaseIntrospector } from '../../introspection/database-introspector.js';
6
6
  import { DialectAdapter } from '../dialect-adapter.js';
7
7
  import { DatabaseConnection } from '../../driver/database-connection.js';
8
+ /**
9
+ * MySQL dialect that uses the [mysql2](https://github.com/sidorares/node-mysql2#readme) library.
10
+ *
11
+ * The {@link MysqlDialectConfig | configuration} passed to the constructor
12
+ * is given as-is to the mysql2 library's [createPool](https://github.com/sidorares/node-mysql2#using-connection-pools)
13
+ * method.
14
+ */
8
15
  export declare class MysqlDialect implements Dialect {
9
16
  #private;
10
17
  constructor(config: MysqlDialectConfig);
@@ -17,6 +24,8 @@ export declare class MysqlDialect implements Dialect {
17
24
  * Config for the mysql dialect.
18
25
  *
19
26
  * This interface is equal to `mysql2` library's pool config.
27
+ *
28
+ * https://github.com/sidorares/node-mysql2#using-connection-pools
20
29
  */
21
30
  export interface MysqlDialectConfig {
22
31
  /**
@@ -176,6 +185,8 @@ export interface MysqlDialectConfig {
176
185
  keepAliveInitialDelay?: number;
177
186
  /**
178
187
  * Called once for each created connection.
188
+ *
189
+ * This is a Kysely specific feature and does not come from the `pg` module.
179
190
  */
180
191
  onCreateConnection?: (connection: DatabaseConnection) => Promise<void>;
181
192
  }
@@ -5,6 +5,13 @@ const mysql_driver_js_1 = require("./mysql-driver.js");
5
5
  const mysql_query_compiler_js_1 = require("./mysql-query-compiler.js");
6
6
  const mysql_introspector_js_1 = require("./mysql-introspector.js");
7
7
  const mysql_adapter_js_1 = require("./mysql-adapter.js");
8
+ /**
9
+ * MySQL dialect that uses the [mysql2](https://github.com/sidorares/node-mysql2#readme) library.
10
+ *
11
+ * The {@link MysqlDialectConfig | configuration} passed to the constructor
12
+ * is given as-is to the mysql2 library's [createPool](https://github.com/sidorares/node-mysql2#using-connection-pools)
13
+ * method.
14
+ */
8
15
  class MysqlDialect {
9
16
  #config;
10
17
  constructor(config) {
@@ -8,6 +8,16 @@ import { Dialect } from '../dialect.js';
8
8
  import { DatabaseIntrospector } from '../../introspection/database-introspector.js';
9
9
  import { DialectAdapter } from '../dialect-adapter.js';
10
10
  import { DatabaseConnection } from '../../driver/database-connection.js';
11
+ /**
12
+ * PostgreSQL dialect that uses the [pg](https://node-postgres.com/) library.
13
+ *
14
+ * The {@link PostgresDialectConfig | configuration} passed to the constructor
15
+ * is given as-is to the pg library's [Pool](https://node-postgres.com/api/pool)
16
+ * constructor. See the following two links for more documentation:
17
+ *
18
+ * https://node-postgres.com/api/pool
19
+ * https://node-postgres.com/api/client
20
+ */
11
21
  export declare class PostgresDialect implements Dialect {
12
22
  #private;
13
23
  constructor(config: PostgresDialectConfig);
@@ -19,7 +29,7 @@ export declare class PostgresDialect implements Dialect {
19
29
  /**
20
30
  * Config for the postgres dialect.
21
31
  *
22
- * This interface is equal to `pg` library's pool config:
32
+ * This interface is equal to `pg` library's `Pool` config:
23
33
  *
24
34
  * https://node-postgres.com/api/pool
25
35
  * https://node-postgres.com/api/client
@@ -105,6 +115,8 @@ export interface PostgresDialectConfig {
105
115
  application_name?: string;
106
116
  /**
107
117
  * Called once for each created connection.
118
+ *
119
+ * This is a Kysely specific feature and does not come from the `pg` module.
108
120
  */
109
121
  onCreateConnection?: (connection: DatabaseConnection) => Promise<void>;
110
122
  keepAlive?: boolean;
@@ -5,6 +5,16 @@ const postgres_driver_js_1 = require("./postgres-driver.js");
5
5
  const postgres_introspector_js_1 = require("./postgres-introspector.js");
6
6
  const postgres_query_compiler_js_1 = require("./postgres-query-compiler.js");
7
7
  const postgres_adapter_js_1 = require("./postgres-adapter.js");
8
+ /**
9
+ * PostgreSQL dialect that uses the [pg](https://node-postgres.com/) library.
10
+ *
11
+ * The {@link PostgresDialectConfig | configuration} passed to the constructor
12
+ * is given as-is to the pg library's [Pool](https://node-postgres.com/api/pool)
13
+ * constructor. See the following two links for more documentation:
14
+ *
15
+ * https://node-postgres.com/api/pool
16
+ * https://node-postgres.com/api/client
17
+ */
8
18
  class PostgresDialect {
9
19
  #config;
10
20
  constructor(config) {
@@ -16,10 +16,10 @@ class PostgresDriver {
16
16
  // installed. As you can see, there IS an import from `pg` at the
17
17
  // top level too, but that's only for types. It doesn't get compiled
18
18
  // into javascript. You can check the built javascript code.
19
- const PoolConstrucor = await importPgPool();
19
+ const PoolConstructor = await importPgPool();
20
20
  // Use the `pg` module's own pool. All drivers should use the
21
21
  // pool provided by the database library if possible.
22
- this.#pool = new PoolConstrucor(this.#config);
22
+ this.#pool = new PoolConstructor(this.#config);
23
23
  }
24
24
  async acquireConnection() {
25
25
  const client = await this.#pool.connect();
@@ -2,7 +2,7 @@
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 postgres dialect.
5
+ // The default query compiler is for PostgreSQL.
6
6
  class PostgresQueryCompiler extends default_query_compiler_js_1.DefaultQueryCompiler {
7
7
  }
8
8
  exports.PostgresQueryCompiler = PostgresQueryCompiler;
@@ -1,3 +1,4 @@
1
+ import { Log } from '../util/log.js';
1
2
  import { DatabaseConnection } from './database-connection.js';
2
3
  import { Driver, TransactionSettings } from './driver.js';
3
4
  /**
@@ -7,10 +8,10 @@ import { Driver, TransactionSettings } from './driver.js';
7
8
  */
8
9
  export declare class RuntimeDriver implements Driver {
9
10
  #private;
10
- constructor(driver: Driver);
11
+ constructor(driver: Driver, log: Log);
11
12
  init(): Promise<void>;
12
13
  acquireConnection(): Promise<DatabaseConnection>;
13
- releaseConnection(connection: DatabaseConnection): Promise<void>;
14
+ releaseConnection(runtimeConnection: DatabaseConnection): Promise<void>;
14
15
  beginTransaction(connection: DatabaseConnection, settings: TransactionSettings): Promise<void>;
15
16
  commitTransaction(connection: DatabaseConnection): Promise<void>;
16
17
  rollbackTransaction(connection: DatabaseConnection): Promise<void>;
@@ -8,10 +8,13 @@ exports.RuntimeDriver = void 0;
8
8
  */
9
9
  class RuntimeDriver {
10
10
  #driver;
11
+ #log;
11
12
  #initPromise;
12
13
  #destroyPromise;
13
- constructor(driver) {
14
+ #connections = new WeakMap();
15
+ constructor(driver, log) {
14
16
  this.#driver = driver;
17
+ this.#log = log;
15
18
  }
16
19
  async init() {
17
20
  if (!this.#initPromise) {
@@ -24,10 +27,18 @@ class RuntimeDriver {
24
27
  }
25
28
  async acquireConnection() {
26
29
  await this.init();
27
- return this.#driver.acquireConnection();
30
+ const connection = await this.#driver.acquireConnection();
31
+ let runtimeConnection = this.#connections.get(connection);
32
+ if (!runtimeConnection) {
33
+ runtimeConnection = new RuntimeConnection(connection, this.#log);
34
+ this.#connections.set(connection, runtimeConnection);
35
+ }
36
+ return runtimeConnection;
28
37
  }
29
- releaseConnection(connection) {
30
- return this.#driver.releaseConnection(connection);
38
+ async releaseConnection(runtimeConnection) {
39
+ if (runtimeConnection instanceof RuntimeConnection) {
40
+ await this.#driver.releaseConnection(runtimeConnection.connection);
41
+ }
31
42
  }
32
43
  beginTransaction(connection, settings) {
33
44
  return this.#driver.beginTransaction(connection, settings);
@@ -53,3 +64,45 @@ class RuntimeDriver {
53
64
  }
54
65
  }
55
66
  exports.RuntimeDriver = RuntimeDriver;
67
+ class RuntimeConnection {
68
+ #connection;
69
+ #log;
70
+ get connection() {
71
+ return this.#connection;
72
+ }
73
+ constructor(connection, log) {
74
+ this.#connection = connection;
75
+ this.#log = log;
76
+ }
77
+ async executeQuery(compiledQuery) {
78
+ const startTime = process.hrtime.bigint();
79
+ try {
80
+ return await this.#connection.executeQuery(compiledQuery);
81
+ }
82
+ catch (error) {
83
+ this.#logError(error);
84
+ throw error;
85
+ }
86
+ finally {
87
+ this.#logQuery(compiledQuery, startTime);
88
+ }
89
+ }
90
+ #logError(error) {
91
+ this.#log.error(() => ({
92
+ level: 'error',
93
+ error,
94
+ }));
95
+ }
96
+ #logQuery(compiledQuery, startTime) {
97
+ this.#log.query(() => ({
98
+ level: 'query',
99
+ sql: compiledQuery.sql,
100
+ queryDurationMillis: this.#calculateDurationMillis(startTime),
101
+ }));
102
+ }
103
+ #calculateDurationMillis(startTime) {
104
+ const endTime = process.hrtime.bigint();
105
+ const durationNanos = Number(endTime - startTime);
106
+ return durationNanos / 1_000_000;
107
+ }
108
+ }
@@ -110,7 +110,7 @@ export * from './operation-node/operation-node-source.js';
110
110
  export * from './operation-node/order-by-node.js';
111
111
  export * from './introspection/database-introspector.js';
112
112
  export * from './util/compilable.js';
113
- export { AnyColumn, UnknownRow as AnyRow } from './util/type-utils.js';
113
+ export { AnyColumn, UnknownRow, AnyQueryBuilder } from './util/type-utils.js';
114
114
  export { SelectExpression, SelectExpressionOrList, } from './parser/select-parser.js';
115
115
  export { ReferenceExpression, ReferenceExpressionOrList, } from './parser/reference-parser.js';
116
116
  export { ValueExpression, ValueExpressionOrList, } from './parser/value-parser.js';
@@ -120,3 +120,4 @@ export { JoinReferenceExpression, JoinCallbackExpression, } from './parser/join-
120
120
  export { InsertObject } from './parser/insert-values-parser.js';
121
121
  export { MutationObject } from './parser/update-set-parser.js';
122
122
  export { OrderByExpression, OrderByDirectionExpression, } from './parser/order-by-parser.js';
123
+ export { UnionExpression } from './parser/union-parser.js';
@@ -10,6 +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
14
  /**
14
15
  * The main Kysely class.
15
16
  *
@@ -127,15 +128,20 @@ export declare class Kysely<DB> extends QueryCreator<DB> {
127
128
  */
128
129
  get fn(): FunctionBuilder<DB, keyof DB>;
129
130
  /**
130
- * Starts a transaction. If the callback throws the transaction is rolled back,
131
- * otherwise it's committed.
131
+ * Creates a {@link TransactionBuilder} that can be used to run queries inside a transaction.
132
132
  *
133
- * @example
134
- * In the example below if either query fails or `someFunction` throws, both inserts
135
- * will be rolled back. Otherwise the transaction will be committed by the time the
136
- * `transaction` function returns the output value. The output value of the
137
- * `transaction` method is the value returned from the callback.
133
+ * The returned {@link TransactionBuilder} can be used to configure the transaction. The
134
+ * {@link TransactionBuilder.execute} method can then be called to run the transaction.
135
+ * {@link TransactionBuilder.execute} takes a function that is run inside the
136
+ * transaction. If the function throws, the transaction is rolled back. Otherwise
137
+ * the transaction is committed.
138
+ *
139
+ * The callback function passed to the {@link TransactionBuilder.execute | execute}
140
+ * method gets the transaction object as its only argument. The transaction is
141
+ * of type {@link Transaction} which inherits {@link Kysely}. Any query
142
+ * started through the transaction object is executed inside the transaction.
138
143
  *
144
+ * @example
139
145
  * ```ts
140
146
  * const catto = await db.transaction().execute(async (trx) => {
141
147
  * const jennifer = await trx.insertInto('person')
@@ -188,6 +194,10 @@ export declare class Kysely<DB> extends QueryCreator<DB> {
188
194
  * ```
189
195
  */
190
196
  connection(): ConnectionBuilder<DB>;
197
+ /**
198
+ * Returns a copy of this Kysely instance with the given plugin installed.
199
+ */
200
+ withPlugin(plugin: KyselyPlugin): Kysely<DB>;
191
201
  /**
192
202
  * Returns a copy of this Kysely instance without any plugins.
193
203
  */
@@ -220,8 +230,20 @@ export interface KyselyProps {
220
230
  }
221
231
  export declare function isKyselyProps(obj: unknown): obj is KyselyProps;
222
232
  export interface KyselyConfig {
223
- dialect: Dialect;
224
- plugins?: KyselyPlugin[];
233
+ readonly dialect: Dialect;
234
+ readonly plugins?: KyselyPlugin[];
235
+ /**
236
+ * A list of log levels to log.
237
+ *
238
+ * Currently there's only two levels: `query` and `error`.
239
+ * This will be expanded based on user feedback later.
240
+ *
241
+ * Log levels:
242
+ *
243
+ * - query: Log each query's SQL and duration using console.log
244
+ * - error: Log all query errors using console.error
245
+ */
246
+ readonly log?: ReadonlyArray<LogLevel>;
225
247
  }
226
248
  export declare class ConnectionBuilder<DB> {
227
249
  #private;
@@ -15,6 +15,7 @@ const driver_js_1 = require("./driver/driver.js");
15
15
  const prevent_await_js_1 = require("./util/prevent-await.js");
16
16
  const parse_context_js_1 = require("./parser/parse-context.js");
17
17
  const function_builder_js_1 = require("./query-builder/function-builder.js");
18
+ const log_js_1 = require("./util/log.js");
18
19
  /**
19
20
  * The main Kysely class.
20
21
  *
@@ -60,15 +61,16 @@ class Kysely extends query_creator_js_1.QueryCreator {
60
61
  constructor(args) {
61
62
  if (isKyselyProps(args)) {
62
63
  super({ executor: args.executor, parseContext: args.parseContext });
63
- this.#props = (0, object_utils_js_1.freeze)(args);
64
+ this.#props = (0, object_utils_js_1.freeze)({ ...args });
64
65
  }
65
66
  else {
66
67
  const dialect = args.dialect;
67
68
  const driver = dialect.createDriver();
68
69
  const compiler = dialect.createQueryCompiler();
69
70
  const adapter = dialect.createAdapter();
71
+ const log = new log_js_1.Log(args.log ?? []);
70
72
  const parseContext = new parse_context_js_1.DefaultParseContext(adapter);
71
- const runtimeDriver = new runtime_driver_js_1.RuntimeDriver(driver);
73
+ const runtimeDriver = new runtime_driver_js_1.RuntimeDriver(driver, log);
72
74
  const connectionProvider = new default_connection_provider_js_1.DefaultConnectionProvider(runtimeDriver);
73
75
  const executor = new default_query_executor_js_1.DefaultQueryExecutor(compiler, connectionProvider, args.plugins ?? []);
74
76
  super({ executor, parseContext });
@@ -166,15 +168,20 @@ class Kysely extends query_creator_js_1.QueryCreator {
166
168
  return new function_builder_js_1.FunctionBuilder({ executor: this.#props.executor });
167
169
  }
168
170
  /**
169
- * Starts a transaction. If the callback throws the transaction is rolled back,
170
- * otherwise it's committed.
171
+ * Creates a {@link TransactionBuilder} that can be used to run queries inside a transaction.
171
172
  *
172
- * @example
173
- * In the example below if either query fails or `someFunction` throws, both inserts
174
- * will be rolled back. Otherwise the transaction will be committed by the time the
175
- * `transaction` function returns the output value. The output value of the
176
- * `transaction` method is the value returned from the callback.
173
+ * The returned {@link TransactionBuilder} can be used to configure the transaction. The
174
+ * {@link TransactionBuilder.execute} method can then be called to run the transaction.
175
+ * {@link TransactionBuilder.execute} takes a function that is run inside the
176
+ * transaction. If the function throws, the transaction is rolled back. Otherwise
177
+ * the transaction is committed.
178
+ *
179
+ * The callback function passed to the {@link TransactionBuilder.execute | execute}
180
+ * method gets the transaction object as its only argument. The transaction is
181
+ * of type {@link Transaction} which inherits {@link Kysely}. Any query
182
+ * started through the transaction object is executed inside the transaction.
177
183
  *
184
+ * @example
178
185
  * ```ts
179
186
  * const catto = await db.transaction().execute(async (trx) => {
180
187
  * const jennifer = await trx.insertInto('person')
@@ -231,6 +238,15 @@ class Kysely extends query_creator_js_1.QueryCreator {
231
238
  connection() {
232
239
  return new ConnectionBuilder({ ...this.#props });
233
240
  }
241
+ /**
242
+ * Returns a copy of this Kysely instance with the given plugin installed.
243
+ */
244
+ withPlugin(plugin) {
245
+ return new Kysely({
246
+ ...this.#props,
247
+ executor: this.#props.executor.withPlugin(plugin),
248
+ });
249
+ }
234
250
  /**
235
251
  * Returns a copy of this Kysely instance without any plugins.
236
252
  */
@@ -24,6 +24,38 @@ export declare class MigrationModule {
24
24
  * path.join(__dirname, 'migrations')
25
25
  * )
26
26
  * ```
27
+ *
28
+ * In the next example, we use a record of migration objects instead of file folder path.
29
+ * The keys in the object are migration names that can be anything you want. The order of
30
+ * the migrations is determined based on the alphabetical order of the keys in the record.
31
+ * This can be useful if you are using a bundler like webpack or esbuild.
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * await db.migration.migrateToLatest({
36
+ * migration1: {
37
+ * async up(db: Kysely<any>): Promise<void> {
38
+ * ...
39
+ * },
40
+ *
41
+ * async down(db: Kysely<any>): Promise<void> {
42
+ * ...
43
+ * }
44
+ * },
45
+ *
46
+ * migration2: {
47
+ * async up(db: Kysely<any>): Promise<void> {
48
+ * ...
49
+ * },
50
+ *
51
+ * async down(db: Kysely<any>): Promise<void> {
52
+ * ...
53
+ * }
54
+ * },
55
+ *
56
+ * ...
57
+ * })
58
+ * ```
27
59
  */
28
60
  migrateToLatest(migrationsFolderPath: string): Promise<void>;
29
61
  migrateToLatest(allMigrations: Record<string, Migration>): Promise<void>;
@@ -1,6 +1,6 @@
1
1
  import { ArrayItemType } from '../util/type-utils.js';
2
2
  import { OperationNode } from './operation-node.js';
3
- export declare const OPERATORS: readonly ["=", "==", "!=", "<>", ">", ">=", "<", "<=", "in", "not in", "is", "is not", "like", "not like", "ilike", "not ilike", "@>", "<@", "?", "?", "?&", "!<", "!>", "<=>", "!~", "~", "~*", "!~*", "exists", "not exists"];
3
+ export declare const OPERATORS: readonly ["=", "==", "!=", "<>", ">", ">=", "<", "<=", "in", "not in", "is", "is not", "like", "not like", "ilike", "not ilike", "@>", "<@", "?", "?", "?&", "!<", "!>", "<=>", "!~", "~", "~*", "!~*", "exists", "not exists", "&&"];
4
4
  export declare type Operator = ArrayItemType<typeof OPERATORS>;
5
5
  export interface OperatorNode extends OperationNode {
6
6
  readonly kind: 'OperatorNode';
@@ -33,6 +33,7 @@ exports.OPERATORS = [
33
33
  '!~*',
34
34
  'exists',
35
35
  'not exists',
36
+ '&&',
36
37
  ];
37
38
  /**
38
39
  * @internal
@@ -3,9 +3,9 @@ import { AnyColumn, AnyColumnWithTable } from '../util/type-utils.js';
3
3
  import { ExtractAliasFromTableExpression, TableExpressionDatabase } from './table-parser.js';
4
4
  import { JoinBuilder } from '../query-builder/join-builder.js';
5
5
  import { ParseContext } from './parse-context.js';
6
- export declare type JoinReferenceExpression<DB, TB extends keyof DB, F> = AnyJoinColumn<DB, TB, F> | AnyJoinColumnWithTable<DB, TB, F>;
7
- export declare type JoinCallbackExpression<DB, TB extends keyof DB, F> = (join: JoinBuilder<TableExpressionDatabase<DB, F>, TB | ExtractAliasFromTableExpression<DB, F>>) => JoinBuilder<any, any>;
8
- declare type AnyJoinColumn<DB, TB extends keyof DB, F> = AnyColumn<TableExpressionDatabase<DB, F>, TB | ExtractAliasFromTableExpression<DB, F>>;
9
- declare type AnyJoinColumnWithTable<DB, TB extends keyof DB, F> = AnyColumnWithTable<TableExpressionDatabase<DB, F>, TB | ExtractAliasFromTableExpression<DB, F>>;
6
+ export declare type JoinReferenceExpression<DB, TB extends keyof DB, TE> = AnyJoinColumn<DB, TB, TE> | AnyJoinColumnWithTable<DB, TB, TE>;
7
+ export declare type JoinCallbackExpression<DB, TB extends keyof DB, TE> = (join: JoinBuilder<TableExpressionDatabase<DB, TE>, TB | ExtractAliasFromTableExpression<DB, TE>>) => JoinBuilder<any, any>;
8
+ declare type AnyJoinColumn<DB, TB extends keyof DB, TE> = AnyColumn<TableExpressionDatabase<DB, TE>, TB | ExtractAliasFromTableExpression<DB, TE>>;
9
+ declare type AnyJoinColumnWithTable<DB, TB extends keyof DB, TE> = AnyColumnWithTable<TableExpressionDatabase<DB, TE>, TB | ExtractAliasFromTableExpression<DB, TE>>;
10
10
  export declare function parseJoin(ctx: ParseContext, joinType: JoinType, args: any[]): JoinNode;
11
11
  export {};
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CamelCasePlugin = void 0;
4
- const operation_node_transformer_js_1 = require("../../operation-node/operation-node-transformer.js");
5
4
  const object_utils_js_1 = require("../../util/object-utils.js");
5
+ const camel_case_transformer_js_1 = require("./camel-case-transformer.js");
6
+ const camel_case_js_1 = require("./camel-case.js");
6
7
  /**
7
8
  * A plugin that converts snake_case identifiers in the database into
8
9
  * camelCase in the javascript side.
@@ -72,9 +73,9 @@ class CamelCasePlugin {
72
73
  #snakeCase;
73
74
  #snakeCaseTransformer;
74
75
  constructor(opt = {}) {
75
- this.#camelCase = createCamelCase(opt);
76
- this.#snakeCase = createSnakeCase(opt);
77
- this.#snakeCaseTransformer = new SnakeCaseTransformer(this.snakeCase.bind(this));
76
+ this.#camelCase = (0, camel_case_js_1.createCamelCaseMapper)(opt);
77
+ this.#snakeCase = (0, camel_case_js_1.createSnakeCaseMapper)(opt);
78
+ this.#snakeCaseTransformer = new camel_case_transformer_js_1.SnakeCaseTransformer(this.snakeCase.bind(this));
78
79
  }
79
80
  transformQuery(args) {
80
81
  return this.#snakeCaseTransformer.transformNode(args.node);
@@ -109,118 +110,6 @@ class CamelCasePlugin {
109
110
  }
110
111
  }
111
112
  exports.CamelCasePlugin = CamelCasePlugin;
112
- class SnakeCaseTransformer extends operation_node_transformer_js_1.OperationNodeTransformer {
113
- #snakeCase;
114
- constructor(snakeCase) {
115
- super();
116
- this.#snakeCase = snakeCase;
117
- }
118
- transformIdentifier(node) {
119
- node = super.transformIdentifier(node);
120
- return {
121
- ...node,
122
- identifier: this.#snakeCase(node.identifier),
123
- };
124
- }
125
- }
126
113
  function canMap(obj) {
127
114
  return (0, object_utils_js_1.isObject)(obj) && !(obj instanceof Date) && !Buffer.isBuffer(obj);
128
115
  }
129
- function createSnakeCase({ upperCase = false, underscoreBeforeDigits = false, underscoreBetweenUppercaseLetters = false, } = {}) {
130
- return memoize((str) => {
131
- if (str.length === 0) {
132
- return str;
133
- }
134
- const upper = str.toUpperCase();
135
- const lower = str.toLowerCase();
136
- let out = lower[0];
137
- for (let i = 1, l = str.length; i < l; ++i) {
138
- const char = str[i];
139
- const prevChar = str[i - 1];
140
- const upperChar = upper[i];
141
- const prevUpperChar = upper[i - 1];
142
- const lowerChar = lower[i];
143
- const prevLowerChar = lower[i - 1];
144
- // If underScoreBeforeDigits is true then, well, insert an underscore
145
- // before digits :). Only the first digit gets an underscore if
146
- // there are multiple.
147
- if (underscoreBeforeDigits && isDigit(char) && !isDigit(prevChar)) {
148
- out += '_' + char;
149
- continue;
150
- }
151
- // Test if `char` is an upper-case character and that the character
152
- // actually has different upper and lower case versions.
153
- if (char === upperChar && upperChar !== lowerChar) {
154
- const prevCharacterIsUppercase = prevChar === prevUpperChar && prevUpperChar !== prevLowerChar;
155
- // If underscoreBetweenUppercaseLetters is true, we always place an underscore
156
- // before consecutive uppercase letters (e.g. "fooBAR" becomes "foo_b_a_r").
157
- // Otherwise, we don't (e.g. "fooBAR" becomes "foo_bar").
158
- if (underscoreBetweenUppercaseLetters || !prevCharacterIsUppercase) {
159
- out += '_' + lowerChar;
160
- }
161
- else {
162
- out += lowerChar;
163
- }
164
- }
165
- else {
166
- out += char;
167
- }
168
- }
169
- if (upperCase) {
170
- return out.toUpperCase();
171
- }
172
- else {
173
- return out;
174
- }
175
- });
176
- }
177
- function createCamelCase({ upperCase = false } = {}) {
178
- return memoize((str) => {
179
- if (str.length === 0) {
180
- return str;
181
- }
182
- if (upperCase && isAllUpperCaseSnakeCase(str)) {
183
- // Only convert to lower case if the string is all upper
184
- // case snake_case. This allows camelCase strings to go
185
- // through without changing.
186
- str = str.toLowerCase();
187
- }
188
- let out = str[0];
189
- for (let i = 1, l = str.length; i < l; ++i) {
190
- const char = str[i];
191
- const prevChar = str[i - 1];
192
- if (char !== '_') {
193
- if (prevChar === '_') {
194
- out += char.toUpperCase();
195
- }
196
- else {
197
- out += char;
198
- }
199
- }
200
- }
201
- return out;
202
- });
203
- }
204
- function isAllUpperCaseSnakeCase(str) {
205
- for (let i = 1, l = str.length; i < l; ++i) {
206
- const char = str[i];
207
- if (char !== '_' && char !== char.toUpperCase()) {
208
- return false;
209
- }
210
- }
211
- return true;
212
- }
213
- function isDigit(char) {
214
- return char >= '0' && char <= '9';
215
- }
216
- function memoize(func) {
217
- const cache = new Map();
218
- return (str) => {
219
- let mapped = cache.get(str);
220
- if (!mapped) {
221
- mapped = func(str);
222
- cache.set(str, mapped);
223
- }
224
- return mapped;
225
- };
226
- }
@@ -0,0 +1,8 @@
1
+ import { IdentifierNode } from '../../operation-node/identifier-node.js';
2
+ import { OperationNodeTransformer } from '../../operation-node/operation-node-transformer.js';
3
+ import { StringMapper } from './camel-case.js';
4
+ export declare class SnakeCaseTransformer extends OperationNodeTransformer {
5
+ #private;
6
+ constructor(snakeCase: StringMapper);
7
+ protected transformIdentifier(node: IdentifierNode): IdentifierNode;
8
+ }
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SnakeCaseTransformer = void 0;
4
+ const operation_node_transformer_js_1 = require("../../operation-node/operation-node-transformer.js");
5
+ class SnakeCaseTransformer extends operation_node_transformer_js_1.OperationNodeTransformer {
6
+ #snakeCase;
7
+ constructor(snakeCase) {
8
+ super();
9
+ this.#snakeCase = snakeCase;
10
+ }
11
+ transformIdentifier(node) {
12
+ node = super.transformIdentifier(node);
13
+ return {
14
+ ...node,
15
+ identifier: this.#snakeCase(node.identifier),
16
+ };
17
+ }
18
+ }
19
+ exports.SnakeCaseTransformer = SnakeCaseTransformer;
@@ -0,0 +1,15 @@
1
+ export declare type StringMapper = (str: string) => string;
2
+ /**
3
+ * Creates a function that transforms camel case strings to snake case.
4
+ */
5
+ export declare function createSnakeCaseMapper({ upperCase, underscoreBeforeDigits, underscoreBetweenUppercaseLetters, }?: {
6
+ upperCase?: boolean | undefined;
7
+ underscoreBeforeDigits?: boolean | undefined;
8
+ underscoreBetweenUppercaseLetters?: boolean | undefined;
9
+ }): StringMapper;
10
+ /**
11
+ * Creates a function that transforms snake case strings to camel case.
12
+ */
13
+ export declare function createCamelCaseMapper({ upperCase, }?: {
14
+ upperCase?: boolean | undefined;
15
+ }): StringMapper;