Skip to content

Commit a45007a

Browse files
committed
feat!: add generic sqlite dialect (#15)
1 parent bb75775 commit a45007a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+1757
-2798
lines changed

.vscode/settings.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
{
2-
// Enable the ESlint flat config support
3-
"eslint.experimental.useFlatConfig": true,
42
// Disable the default formatter, use eslint instead
53
"prettier.enable": false,
64
"editor.formatOnSave": false,

commitlint.config.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module.exports = {
1616
'dialect-wasm',
1717
'dialect-bun-worker',
1818
'dialect-sqlite-worker',
19+
'dialect-generic-sqlite',
1920
'dialect-wasqlite-worker',
2021
// 'builder',
2122
'workspace',

eslint.config.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineEslintConfig } from '@subframe7536/eslint-config'
1+
import { defineEslintConfig, GLOB_JSON } from '@subframe7536/eslint-config'
22

33
export default defineEslintConfig({
44
overrideRules: {
@@ -11,5 +11,9 @@ export default defineEslintConfig({
1111
files: './playground/**/*.{ts,vue}',
1212
rules: ['ts/explicit-function-return-type'],
1313
},
14+
{
15+
files: GLOB_JSON,
16+
rules: ['antfu/consistent-list-newline'],
17+
},
1418
],
1519
})

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@
1212
"license": "MIT",
1313
"scripts": {
1414
"preinstall": "npx only-allow pnpm",
15-
"dev": "pnpm -r -F \"./packages/*\" dev",
16-
"build": "pnpm -r -F \"./packages/*\" build",
15+
"dev": "pnpm -r -F \"./packages/dialect*\" dev",
16+
"build": "pnpm -r -F \"./packages/dialect*\" build",
1717
"release": "pnpm i && bumpp package.json packages/dialect*/package.json packages/plugin*/package.json",
1818
"serve": "cd playground && pnpm run dev",
19-
"publish": "pnpm -r -F \"./packages/*\" publish --no-git-checks",
20-
"test": "pnpm run build && vitest run --root ./test --passWithNoTests",
21-
"test:dev": "vitest --root ./packages/sqlite-builder",
22-
"test:bun": "bun test test-bun",
19+
"publish": "pnpm -r -F \"./packages/dialect*\" publish --no-git-checks",
20+
"test": "pnpm run build && vitest run --root ./test --passWithNoTests && pnpm run test:bun",
21+
"test:bun": "bun test ./packages/dialect-bun-worker/test/index.test.ts",
2322
"typecheck": "tsc --noEmit",
2423
"format": "eslint . --fix",
2524
"lint": "eslint .",
@@ -38,13 +37,14 @@
3837
"@vlcn.io/crsqlite-wasm": "^0.16.0",
3938
"better-sqlite3": "^11.8.0",
4039
"bumpp": "^9.10.1",
40+
"bun-types": "^1.1.43",
4141
"cz-git": "^1.11.0",
4242
"czg": "^1.11.0",
4343
"eslint": "^9.18.0",
4444
"eslint-plugin-vue": "^9.32.0",
4545
"eslint-processor-vue-blocks": "^1.0.0",
4646
"husky": "^9.1.7",
47-
"kysely": "^0.27.5",
47+
"kysely": "catalog:",
4848
"lint-staged": "^15.4.0",
4949
"node-sqlite3-wasm": "^0.8.31",
5050
"sql.js": "^1.12.0",

packages/dialect-bun-worker/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ const dialect = new BunWorkerDialect({
2525
in `worker.ts`
2626

2727
```ts
28-
import { createOnMessageCallback } from 'kysely-bun-worker'
28+
import { createOnMessageCallback, defaultCreateDatabaseFn } from 'kysely-bun-worker'
2929

30-
onmessage = createOnMessageCallback(
31-
async (db) => {
30+
createOnMessageCallback(
31+
async (...args) => {
32+
const db = defaultCreateDatabaseFn(...args)
3233
db.loadExtension(/* ... */)
34+
return db
3335
}
3436
)
3537
```

packages/dialect-bun-worker/package.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
},
99
"license": "MIT",
1010
"homepage": "https://github.com/subframe7536/kysely-sqlite-tools/tree/master/packages/dialect-bun-worker",
11-
"repository": "https://github.com/subframe7536/kysely-sqlite-tools/tree/master/packages/dialect-bun-worker",
11+
"repository": {
12+
"type": "git",
13+
"url": "git+https://github.com/subframe7536/kysely-sqlite-tools.git#master"
14+
},
1215
"bugs": "https://github.com/subframe7536/kysely-sqlite-tools/issues",
1316
"keywords": [
1417
"kysely",
@@ -43,13 +46,17 @@
4346
"scripts": {
4447
"dev": "tsup --watch",
4548
"test": "bun test",
49+
"test:dev": "tsup && bun test",
4650
"build": "tsup"
4751
},
4852
"peerDependencies": {
4953
"bun-types": ">=1.1.14",
5054
"kysely": ">=0.26"
5155
},
56+
"dependencies": {
57+
"kysely-generic-sqlite": "workspace:*"
58+
},
5259
"devDependencies": {
53-
"bun-types": "^1.1.43"
60+
"kysely": "catalog:"
5461
}
5562
}

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

Lines changed: 0 additions & 165 deletions
This file was deleted.
Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
1-
import type { DatabaseIntrospector, Dialect, DialectAdapter, Driver, Kysely, QueryCompiler } from 'kysely'
2-
import type { BunWorkerDialectConfig } from './type'
3-
import { SqliteAdapter, SqliteIntrospector, SqliteQueryCompiler } from 'kysely'
4-
import { BunWorkerDriver } from './driver'
1+
import type { BunWorkerDialectConfig, InitData } from './type'
2+
import { GenericSqliteWorkerDialect } from 'kysely-generic-sqlite/worker'
3+
import { createNodeMitt } from 'kysely-generic-sqlite/worker-helper-node'
4+
import { handleWebWorker } from 'kysely-generic-sqlite/worker-helper-web'
55

6-
export type { BunWorkerDialectConfig, Promisable } from './type'
7-
export { createOnMessageCallback } from './worker/utils'
6+
export * from './type'
7+
export * from './worker/utils'
88

9-
export class BunWorkerDialect implements Dialect {
9+
export class BunWorkerDialect extends GenericSqliteWorkerDialect<globalThis.Worker, InitData> {
1010
/**
1111
* dialect for `bun:sqlite`, run sql in worker
1212
*/
13-
constructor(
14-
private config?: BunWorkerDialectConfig,
15-
) { }
16-
17-
createDriver(): Driver {
18-
return new BunWorkerDriver(this.config)
19-
}
20-
21-
createQueryCompiler(): QueryCompiler {
22-
return new SqliteQueryCompiler()
23-
}
24-
25-
createAdapter(): DialectAdapter {
26-
return new SqliteAdapter()
27-
}
28-
29-
createIntrospector(db: Kysely<any>): DatabaseIntrospector {
30-
return new SqliteIntrospector(db)
13+
constructor(config?: BunWorkerDialectConfig) {
14+
const {
15+
url: fileName = ':memory:',
16+
cacheStatment: cache = false,
17+
onCreateConnection,
18+
worker = new Worker(
19+
new URL('./worker', import.meta.url),
20+
{ type: 'module' },
21+
),
22+
} = config || {}
23+
super(
24+
() => ({
25+
data: { cache, fileName },
26+
mitt: createNodeMitt(),
27+
handle: handleWebWorker,
28+
worker,
29+
}),
30+
onCreateConnection,
31+
)
3132
}
3233
}
Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import type { DatabaseConnection, QueryResult } from 'kysely'
1+
import type { IBaseSqliteDialectConfig } from 'kysely-generic-sqlite'
22

3-
export type Promisable<T> = T | Promise<T>
4-
5-
export type BunWorkerDialectConfig = {
3+
export interface BunWorkerDialectConfig extends IBaseSqliteDialectConfig {
64
/**
75
* db file path
86
*
@@ -18,44 +16,9 @@ export type BunWorkerDialectConfig = {
1816
* custom worker, default is a worker that use bun:sqlite
1917
*/
2018
worker?: Worker
21-
onCreateConnection?: (connection: DatabaseConnection) => Promisable<void>
22-
}
23-
24-
export type RunMsg = [
25-
type: 1,
26-
isSelect: boolean,
27-
sql: string,
28-
parameters?: readonly unknown[],
29-
]
30-
31-
type CloseMsg = [type: 2]
32-
33-
type InitMsg = [
34-
type: 0,
35-
url?: string,
36-
cache?: boolean,
37-
]
38-
39-
type StreamMsg = [
40-
type: 3,
41-
sql: string,
42-
parameters?: readonly unknown[],
43-
]
44-
45-
export type MainToWorkerMsg = InitMsg | RunMsg | CloseMsg | StreamMsg
46-
47-
export type WorkerToMainMsg = {
48-
[K in keyof Events]: [ type: K, data: Events[K], err: unknown ]
49-
}[keyof Events]
50-
51-
type Events = {
52-
0: null
53-
1: QueryResult<any> | null
54-
2: null
55-
3: QueryResult<any>[] | null
56-
4: null
5719
}
5820

59-
export type EventWithError = {
60-
[K in keyof Events]: [ data: Events[K], err: unknown ]
21+
export type InitData = {
22+
fileName: string
23+
cache: boolean
6124
}

0 commit comments

Comments
 (0)