Skip to content

Commit 3a766a5

Browse files
committed
feat(dialect-bun-worker): add db options
1 parent a77489e commit 3a766a5

File tree

5 files changed

+44
-10
lines changed

5 files changed

+44
-10
lines changed

packages/dialect-bun-worker/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ bun install kysely kysely-bun-worker
1616
import { BunWorkerDialect } from 'kysely-bun-worker'
1717

1818
const dialect = new BunWorkerDialect({
19+
// default
1920
url: ':memory:',
2021
})
2122
```
@@ -36,6 +37,19 @@ createOnMessageCallback(
3637
)
3738
```
3839

40+
### Normal Dialect
41+
42+
In v1.1.0, you can use `BunSqliteDialect` to run SQLs in main thread
43+
44+
```ts
45+
import { BunSqliteDialect } from 'kysely-bun-worker'
46+
47+
const dialect = new BunSqliteDialect({
48+
// default
49+
url: ':memory:',
50+
})
51+
```
52+
3953
## Config
4054

4155
```ts
@@ -56,5 +70,10 @@ export type BunWorkerDialectConfig = {
5670
* custom worker, default is a worker that use bun:sqlite
5771
*/
5872
worker?: Worker
73+
/**
74+
* DB constructor options
75+
* @default { create: true }
76+
*/
77+
dbOptions?: ConstructorParameters<typeof Database>[1]
5978
}
6079
```

packages/dialect-bun-worker/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ export class BunWorkerDialect extends GenericSqliteWorkerDialect<globalThis.Work
1919
new URL('./worker', import.meta.url),
2020
{ type: 'module' },
2121
),
22+
dbOptions: opt = { create: true },
2223
} = config || {}
2324
super(
2425
() => ({
25-
data: { cache, fileName },
26+
data: { cache, fileName, opt },
2627
mitt: createNodeMitt(),
2728
handle: handleWebWorker,
2829
worker,

packages/dialect-bun-worker/src/normal.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ export class BunSqliteDialect extends GenericSqliteDialect {
1111
url = ':memory:',
1212
cacheStatment = false,
1313
onCreateConnection,
14+
dbOptions = { create: true },
1415
} = config || {}
1516
super(
16-
() => createSqliteExecutor(new Database(url), cacheStatment),
17+
() => createSqliteExecutor(new Database(url, dbOptions), cacheStatment),
1718
onCreateConnection,
1819
)
1920
}

packages/dialect-bun-worker/src/type.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1+
import type Database from 'bun:sqlite'
12
import type { IBaseSqliteDialectConfig } from 'kysely-generic-sqlite'
23

4+
export type DBOptions = Exclude<ConstructorParameters<typeof Database>[1], undefined>
5+
36
export interface BunWorkerDialectConfig extends IBaseSqliteDialectConfig {
47
/**
5-
* db file path
8+
* DB file path
69
*
710
* @default ':memory:'
811
*/
912
url?: string
1013
/**
11-
* use `bun:sqlite` built-in statment cache
12-
* @see https://bun.sh/docs/api/sqlite#query
14+
* DB constructor options
15+
* @default { create: true }
1316
*/
17+
dbOptions?: DBOptions
1418
cacheStatment?: boolean
1519
/**
1620
* custom worker, default is a worker that use bun:sqlite
@@ -21,6 +25,7 @@ export interface BunWorkerDialectConfig extends IBaseSqliteDialectConfig {
2125
export type InitData = {
2226
fileName: string
2327
cache: boolean
28+
opt: DBOptions
2429
}
2530

2631
export interface BunSqliteDialectConfig extends IBaseSqliteDialectConfig {
@@ -35,4 +40,9 @@ export interface BunSqliteDialectConfig extends IBaseSqliteDialectConfig {
3540
* @see https://bun.sh/docs/api/sqlite#query
3641
*/
3742
cacheStatment?: boolean
43+
/**
44+
* DB constructor options
45+
* @default { create: true }
46+
*/
47+
dbOptions?: DBOptions
3848
}

packages/dialect-bun-worker/src/worker/utils.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Statement } from 'bun:sqlite'
22
import type { MessageHandleFn } from 'kysely-generic-sqlite/worker'
3-
import type { InitData } from '../type'
3+
import type { DBOptions, InitData } from '../type'
44
import Database from 'bun:sqlite'
55
import { type IGenericSqlite, parseBigInt, type Promisable } from 'kysely-generic-sqlite'
66
import { createWebOnMessageCallback } from 'kysely-generic-sqlite/worker-helper-web'
@@ -17,10 +17,13 @@ async function* iterateData(
1717
}
1818
}
1919

20-
export type CreateDatabaseFn = (fileName: string) => Promisable<Database>
20+
export type CreateDatabaseFn = (
21+
fileName: string,
22+
opt: DBOptions
23+
) => Promisable<Database>
2124

2225
export const defaultCreateDatabaseFn: CreateDatabaseFn
23-
= fileName => new Database(fileName, { create: true })
26+
= (fileName, opt) => new Database(fileName, opt)
2427

2528
/**
2629
* Handle worker message, support custom callback on initialization.
@@ -40,8 +43,8 @@ export function createOnMessageCallback(
4043
message?: MessageHandleFn<Database>,
4144
): void {
4245
createWebOnMessageCallback<InitData, Database>(
43-
async ({ cache, fileName }) => {
44-
const db = await create(fileName)
46+
async ({ cache, fileName, opt }) => {
47+
const db = await create(fileName, opt)
4548
return createSqliteExecutor(db, cache)
4649
},
4750
message,

0 commit comments

Comments
 (0)