@zgeoff/kysely-bun-sqlite 0.1.0 → 0.2.0

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.
@@ -0,0 +1,3 @@
1
+ import type { Database } from 'bun:sqlite';
2
+ import type { Driver } from 'kysely';
3
+ export declare function buildBunSQLiteDriver(sqlite: Database): Driver;
package/dist/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  export { buildSQLiteDialect } from './build-sqlite-dialect.ts';
2
- export { BunSQLiteDriver } from './bun-sqlite-driver.ts';
2
+ export { buildBunSQLiteDriver } from './build-bun-sqlite-driver.ts';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zgeoff/kysely-bun-sqlite",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "A Kysely driver and dialect for a bun:sqlite database handle",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -29,9 +29,9 @@
29
29
  "typecheck": "tsc"
30
30
  },
31
31
  "devDependencies": {
32
- "@types/bun": "1.3.14",
32
+ "@types/bun": "1.4.2",
33
33
  "kysely": "0.29.6",
34
- "typescript": "7.0.1-rc"
34
+ "typescript": "7.0.2"
35
35
  },
36
36
  "peerDependencies": {
37
37
  "kysely": ">=0.29.5 <0.30.0"
@@ -7,60 +7,54 @@ import type { DatabaseConnection, Driver, QueryResult } from 'kysely';
7
7
  // Kysely's RuntimeDriver serializes the connection: SqliteAdapter reports no
8
8
  // support for multiple connections, so RuntimeDriver holds a mutex from each
9
9
  // acquire to its release.
10
- export class BunSQLiteDriver implements Driver {
11
- private readonly connection: DatabaseConnection;
10
+ export function buildBunSQLiteDriver(sqlite: Database): Driver {
11
+ const connection = buildConnection(sqlite);
12
12
 
13
13
  // The driver's own turn chain exists only for the destroy() wait, because
14
14
  // Kysely calls destroy() outside RuntimeDriver's mutex.
15
- private turn: Promise<void> = Promise.resolve();
15
+ let turn: Promise<void> = Promise.resolve();
16
+ let releaseTurn: (() => void) | undefined;
16
17
 
17
- private releaseTurn: () => void = () => {};
18
-
19
- constructor(sqlite: Database) {
20
- this.connection = buildConnection(sqlite);
21
- }
22
-
23
- init(): Promise<void> {
24
- return Promise.resolve();
25
- }
26
-
27
- async acquireConnection(): Promise<DatabaseConnection> {
28
- const previous = this.turn;
18
+ const acquireConnection = async (): Promise<DatabaseConnection> => {
19
+ const previous = turn;
29
20
  const next = Promise.withResolvers<void>();
30
21
 
31
- this.turn = next.promise;
22
+ turn = next.promise;
32
23
 
33
24
  await previous;
34
25
 
35
- this.releaseTurn = next.resolve;
26
+ releaseTurn = next.resolve;
36
27
 
37
- return this.connection;
38
- }
39
-
40
- async beginTransaction(connection: DatabaseConnection): Promise<void> {
41
- await connection.executeQuery(CompiledQuery.raw('begin'));
42
- }
28
+ return connection;
29
+ };
43
30
 
44
- async commitTransaction(connection: DatabaseConnection): Promise<void> {
45
- await connection.executeQuery(CompiledQuery.raw('commit'));
46
- }
31
+ const releaseConnection = (): Promise<void> => {
32
+ releaseTurn?.();
47
33
 
48
- async rollbackTransaction(connection: DatabaseConnection): Promise<void> {
49
- await connection.executeQuery(CompiledQuery.raw('rollback'));
50
- }
34
+ return Promise.resolve();
35
+ };
51
36
 
52
- releaseConnection(): Promise<void> {
53
- this.releaseTurn();
37
+ return {
38
+ init: () => Promise.resolve(),
39
+ acquireConnection,
40
+ beginTransaction: async (heldConnection) => {
41
+ await heldConnection.executeQuery(CompiledQuery.raw('begin'));
42
+ },
43
+ commitTransaction: async (heldConnection) => {
44
+ await heldConnection.executeQuery(CompiledQuery.raw('commit'));
45
+ },
46
+ rollbackTransaction: async (heldConnection) => {
47
+ await heldConnection.executeQuery(CompiledQuery.raw('rollback'));
48
+ },
49
+ releaseConnection,
54
50
 
55
- return Promise.resolve();
56
- }
57
-
58
- // The opener owns the handle and closes it, so destroy() leaves the handle
59
- // open and only waits for the caller that holds the connection.
60
- async destroy(): Promise<void> {
61
- await this.acquireConnection();
62
- await this.releaseConnection();
63
- }
51
+ // The opener owns the handle and closes it, so destroy() leaves the handle
52
+ // open and only waits for the caller that holds the connection.
53
+ destroy: async () => {
54
+ await acquireConnection();
55
+ await releaseConnection();
56
+ },
57
+ };
64
58
  }
65
59
 
66
60
  function buildConnection(sqlite: Database): DatabaseConnection {
@@ -85,7 +79,7 @@ function buildConnection(sqlite: Database): DatabaseConnection {
85
79
  },
86
80
 
87
81
  streamQuery<R>(): AsyncIterableIterator<QueryResult<R>> {
88
- throw new Error('BunSQLiteDriver does not support streaming queries');
82
+ throw new Error('The bun:sqlite driver does not support streaming queries');
89
83
  },
90
84
  };
91
85
  }
@@ -1,12 +1,12 @@
1
1
  import type { Database } from 'bun:sqlite';
2
2
  import { SqliteAdapter, SqliteIntrospector, SqliteQueryCompiler } from 'kysely';
3
3
  import type { Dialect } from 'kysely';
4
- import { BunSQLiteDriver } from './bun-sqlite-driver.ts';
4
+ import { buildBunSQLiteDriver } from './build-bun-sqlite-driver.ts';
5
5
 
6
6
  export function buildSQLiteDialect(sqlite: Database): Dialect {
7
7
  return {
8
8
  createAdapter: () => new SqliteAdapter(),
9
- createDriver: () => new BunSQLiteDriver(sqlite),
9
+ createDriver: () => buildBunSQLiteDriver(sqlite),
10
10
  createIntrospector: (kysely) => new SqliteIntrospector(kysely),
11
11
  createQueryCompiler: () => new SqliteQueryCompiler(),
12
12
  };
package/src/index.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  export { buildSQLiteDialect } from './build-sqlite-dialect.ts';
2
- export { BunSQLiteDriver } from './bun-sqlite-driver.ts';
2
+ export { buildBunSQLiteDriver } from './build-bun-sqlite-driver.ts';
@@ -1,15 +0,0 @@
1
- import type { Database } from 'bun:sqlite';
2
- import type { DatabaseConnection, Driver } from 'kysely';
3
- export declare class BunSQLiteDriver implements Driver {
4
- private readonly connection;
5
- private turn;
6
- private releaseTurn;
7
- constructor(sqlite: Database);
8
- init(): Promise<void>;
9
- acquireConnection(): Promise<DatabaseConnection>;
10
- beginTransaction(connection: DatabaseConnection): Promise<void>;
11
- commitTransaction(connection: DatabaseConnection): Promise<void>;
12
- rollbackTransaction(connection: DatabaseConnection): Promise<void>;
13
- releaseConnection(): Promise<void>;
14
- destroy(): Promise<void>;
15
- }