Skip to content

Commit 380e7a3

Browse files
committed
chore: qa
1 parent 573ebed commit 380e7a3

16 files changed

Lines changed: 345 additions & 281 deletions

README.md

Lines changed: 78 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ import { initSQLite, isOpfsSupported, useMemoryStorage } from '@subframe7536/sql
1515

1616
// optional url
1717
const url = 'https://cdn.jsdelivr.net/npm/@subframe7536/sqlite-wasm@0.5.0/wa-sqlite.wasm'
18-
const url1 = 'https://cdn.jsdelivr.net/gh/subframe7536/sqlite-wasm@v0.5.0/wa-sqlite-fts5/wa-sqlite.wasm'
18+
const url1 =
19+
'https://cdn.jsdelivr.net/gh/subframe7536/sqlite-wasm@v0.5.0/wa-sqlite-fts5/wa-sqlite.wasm'
1920

20-
const { run, changes, lastInsertRowId, close } = await initSQLite(
21-
useMemoryStorage({ url })
22-
)
21+
const { run, changes, lastInsertRowId, close } = await initSQLite(useMemoryStorage({ url }))
2322
```
2423

2524
### IndexedDB
@@ -34,11 +33,10 @@ import { useIdbStorage } from '@subframe7536/sqlite-wasm/idb'
3433

3534
// optional url
3635
const url = 'https://cdn.jsdelivr.net/npm/@subframe7536/sqlite-wasm@0.5.0/wa-sqlite-async.wasm'
37-
const url1 = 'https://cdn.jsdelivr.net/gh/subframe7536/sqlite-wasm@v0.5.0/wa-sqlite-fts5/wa-sqlite-async.wasm'
36+
const url1 =
37+
'https://cdn.jsdelivr.net/gh/subframe7536/sqlite-wasm@v0.5.0/wa-sqlite-fts5/wa-sqlite-async.wasm'
3838

39-
const { run, changes, lastInsertRowId, close } = await initSQLite(
40-
useIdbStorage('test.db', { url })
41-
)
39+
const { run, changes, lastInsertRowId, close } = await initSQLite(useIdbStorage('test.db', { url }))
4240
```
4341

4442
#### IdbMemory
@@ -55,7 +53,7 @@ import { useIdbMemoryStorage } from '@subframe7536/sqlite-wasm/idb-memory'
5553
const url = 'https://cdn.jsdelivr.net/npm/@subframe7536/sqlite-wasm@0.5.0/dist/wa-sqlite-async.wasm'
5654

5755
const { run, changes, lastInsertRowId, close } = await initSQLite(
58-
useIdbMemoryStorage('test.db', { url })
56+
useIdbMemoryStorage('test.db', { url }),
5957
)
6058
```
6159

@@ -76,12 +74,11 @@ import { useOpfsStorage } from '@subframe7536/sqlite-wasm/opfs'
7674
const url = 'https://cdn.jsdelivr.net/npm/@subframe7536/sqlite-wasm@0.5.0/dist/wa-sqlite.wasm'
7775

7876
onmessage = async () => {
79-
if (!await isOpfsSupported()) { // this can be called in main thread
77+
if (!(await isOpfsSupported())) {
78+
// this can be called in main thread
8079
return
8180
}
82-
const { run, changes, lastInsertRowId, close } = await initSQLite(
83-
useOpfsStorage('test.db', url)
84-
)
81+
const { run, changes, lastInsertRowId, close } = await initSQLite(useOpfsStorage('test.db', url))
8582
}
8683
```
8784

@@ -92,6 +89,7 @@ Store data through `FileSystemFileHandle`, use modified `OPFSAnyContextVFS` with
9289
[minimal File System Access backend browser version](https://caniuse.com/mdn-api_filesystemhandle)
9390

9491
> [!warning]
92+
>
9593
> 1. maybe have perfarmance issue on Windows with device's local file, see in [#5](https://github.com/subframe7536/sqlite-wasm/pull/5)
9694
> 2. Safari is not supported, see in [caniuse](https://caniuse.com/mdn-api_filesystemwritablefilestream)
9795
@@ -108,7 +106,7 @@ const root = await window.showDirectoryPicker()
108106
const root1 = await navigator.storage.getDirectory()
109107

110108
const { run, changes, lastInsertRowId, close } = await initSQLite(
111-
useFsHandleStorage('test.db', root, url)
109+
useFsHandleStorage('test.db', root, url),
112110
)
113111
```
114112

@@ -141,9 +139,7 @@ async function selectFile(): Promise<File> {
141139
input.click()
142140
})
143141
}
144-
const FileOrReadableStream = remote
145-
? (await fetch(remoteSqliteURL)).body!
146-
: await selectFile()
142+
const FileOrReadableStream = remote ? (await fetch(remoteSqliteURL)).body! : await selectFile()
147143
```
148144

149145
Import while initiaizing
@@ -152,9 +148,7 @@ Import while initiaizing
152148
import { initSQLite, withExistDB } from '@subframe7536/sqlite-wasm'
153149
import { useIdbStorage } from '@subframe7536/sqlite-wasm/idb'
154150

155-
const db = initSQLite(
156-
useIdbStorage('test.db', withExistDB(FileOrReadableStream, { url }))
157-
)
151+
const db = initSQLite(useIdbStorage('test.db', withExistDB(FileOrReadableStream, { url })))
158152
```
159153

160154
or load it later
@@ -196,9 +190,7 @@ import { customFunction, initSQLite, isOpfsSupported } from '@subframe7536/sqlit
196190
import { useOpfsStorage } from '@subframe7536/sqlite-wasm/opfs'
197191
import { uuidv7 } from 'uuidv7'
198192

199-
const { run, sqlite, db } = await initSQLite(
200-
useOpfsStorage('test')
201-
)
193+
const { run, sqlite, db } = await initSQLite(useOpfsStorage('test'))
202194
customFunction(sqlite, db, 'uuidv7', () => uuidv7())
203195
console.log(await run('select uuidv7() as a'))
204196
// [{ "a": "01932f1b-b663-7714-af4d-17a3d9efc7b3" }]
@@ -225,7 +217,7 @@ for await (const row of iterator(core, 'select * from test')) {
225217
console.log(row)
226218
}
227219

228-
customFunctionCore(core, 'test', num => num)
220+
customFunctionCore(core, 'test', (num) => num)
229221
await run(core, 'select test(?)', [1])
230222

231223
const buf = await exportDatabase(core.vfs, core.path)
@@ -235,8 +227,12 @@ await close(core)
235227

236228
### Utils
237229

238-
```ts
239-
function dumpVFS(vfs: FacadeVFS, path: string, onDone?: (vfs: FacadeVFS, path: string) => any): ReadableStream
230+
````ts
231+
function dumpVFS(
232+
vfs: FacadeVFS,
233+
path: string,
234+
onDone?: (vfs: FacadeVFS, path: string) => any,
235+
): ReadableStream
240236

241237
function exportDatabaseFromIDB(vfs: FacadeVFS, path: string): Promise<Uint8Array>
242238

@@ -249,15 +245,23 @@ function exportDatabaseFromFsHandle(vfs: FacadeVFS, path: string): Promise<Uint8
249245
*/
250246
function exportDatabase(vfs: FacadeVFS, path: string): Promise<Uint8Array>
251247

252-
function importDatabaseToIdb(vfs: FacadeVFS, path: string, stream: ReadableStream<Uint8Array>): Promise<void>
248+
function importDatabaseToIdb(
249+
vfs: FacadeVFS,
250+
path: string,
251+
stream: ReadableStream<Uint8Array>,
252+
): Promise<void>
253253

254254
/**
255255
* Import database from `File` or `ReadableStream`
256256
* @param vfs SQLite VFS
257257
* @param path db path
258258
* @param data existing database
259259
*/
260-
function importDatabase(vfs: FacadeVFS, path: string, data: File | ReadableStream<Uint8Array>): Promise<void>
260+
function importDatabase(
261+
vfs: FacadeVFS,
262+
path: string,
263+
data: File | ReadableStream<Uint8Array>,
264+
): Promise<void>
261265

262266
/**
263267
* check if IndexedDB and Web Locks API supported
@@ -293,17 +297,28 @@ function isModuleWorkerSupport(): boolean
293297
* // [{ "a": "01932f1b-b663-7714-af4d-17a3d9efc7b3" }]
294298
* ```
295299
*/
296-
function customFunction<N extends string, T extends SQLiteCompatibleType[]>(sqlite: SQLiteAPI, db: number, fnName: N, fn: N extends '' ? never : (...args: T) => (SQLiteCompatibleType | number[]) | null, options?: {
297-
deterministic?: boolean
298-
directOnly?: boolean
299-
varargs?: boolean
300-
}): void
301-
302-
function customFunctionCore<N extends string, T extends SQLiteCompatibleType[]>(core: SQLiteDBCore, fnName: N, fn: N extends '' ? never : (...args: T) => (SQLiteCompatibleType | number[]) | null, options?: {
303-
deterministic?: boolean
304-
directOnly?: boolean
305-
varargs?: boolean
306-
}): void
300+
function customFunction<N extends string, T extends SQLiteCompatibleType[]>(
301+
sqlite: SQLiteAPI,
302+
db: number,
303+
fnName: N,
304+
fn: N extends '' ? never : (...args: T) => (SQLiteCompatibleType | number[]) | null,
305+
options?: {
306+
deterministic?: boolean
307+
directOnly?: boolean
308+
varargs?: boolean
309+
},
310+
): void
311+
312+
function customFunctionCore<N extends string, T extends SQLiteCompatibleType[]>(
313+
core: SQLiteDBCore,
314+
fnName: N,
315+
fn: N extends '' ? never : (...args: T) => (SQLiteCompatibleType | number[]) | null,
316+
options?: {
317+
deterministic?: boolean
318+
directOnly?: boolean
319+
varargs?: boolean
320+
},
321+
): void
307322

308323
/**
309324
* Parse options with existing database
@@ -319,24 +334,41 @@ function customFunctionCore<N extends string, T extends SQLiteCompatibleType[]>(
319334
* )
320335
* ```
321336
*/
322-
function withExistDB<T extends BaseStorageOptions>(data: File | ReadableStream, options?: Omit<T, 'beforeOpen'>): T
337+
function withExistDB<T extends BaseStorageOptions>(
338+
data: File | ReadableStream,
339+
options?: Omit<T, 'beforeOpen'>,
340+
): T
323341

324342
function close(core: SQLiteDBCore): Promise<void>
325343

326344
function changes(core: SQLiteDBCore): number | bigint
327345

328346
function lastInsertRowId(core: SQLiteDBCore): number | bigint
329347

330-
function stream(core: SQLiteDBCore, onData: (data: Record<string, SQLiteCompatibleType>) => void, sql: string, parameters?: SQLiteCompatibleType[]): Promise<void>
331-
332-
function run(core: SQLiteDBCore, sql: string, parameters?: SQLiteCompatibleType[]): Promise<Array<Record<string, SQLiteCompatibleType>>>
333-
334-
function iterator(core: SQLiteDBCore, sql: string, parameters?: SQLiteCompatibleType[], chunkSize?: number): AsyncIterableIterator<Record<string, SQLiteCompatibleType>[]>
348+
function stream(
349+
core: SQLiteDBCore,
350+
onData: (data: Record<string, SQLiteCompatibleType>) => void,
351+
sql: string,
352+
parameters?: SQLiteCompatibleType[],
353+
): Promise<void>
354+
355+
function run(
356+
core: SQLiteDBCore,
357+
sql: string,
358+
parameters?: SQLiteCompatibleType[],
359+
): Promise<Array<Record<string, SQLiteCompatibleType>>>
360+
361+
function iterator(
362+
core: SQLiteDBCore,
363+
sql: string,
364+
parameters?: SQLiteCompatibleType[],
365+
chunkSize?: number,
366+
): AsyncIterableIterator<Record<string, SQLiteCompatibleType>[]>
335367

336368
function parseOpenV2Flag(readonly?: boolean): number
337369

338370
function reopen(core: SQLiteDBCore, readonly?: boolean): Promise<void>
339-
```
371+
````
340372

341373
## License
342374

oxfmt.config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
import { subfFmt } from '@subf/config/oxfmt'
22

3-
export default subfFmt()
3+
export default subfFmt({
4+
ignorePatterns: ['wa-sqlite-fts5', 'wa-sqlite', 'src/vfs/class/OPFSAnyContextVFS.js'],
5+
})

oxlint.config.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import { subfLint } from '@subf/config/oxlint'
22

33
export default subfLint({
4-
ignorePatterns: [
5-
'wa-sqlite-fts5',
6-
'wa-sqlite',
7-
],
4+
ignorePatterns: ['wa-sqlite-fts5', 'wa-sqlite', 'src/vfs/class/OPFSAnyContextVFS.js'],
85
rules: {
96
'no-constant-condition': 'off',
107
'no-unused-vars': 'off',

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
"dev": "vite serve playground",
3939
"build": "tsdown",
4040
"update": "bun run download.ts",
41-
"release": "bun run typecheck && bun run lint && bun run format && bun run build && bumpp --all"
41+
"qa": "bun run typecheck && bun run lint && bun run format",
42+
"release": "bun run qa && bun run build && bumpp --all"
4243
},
4344
"devDependencies": {
4445
"@subf/config": "^0.2.1",

playground/src/index.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import type { SQLiteDB } from '../../src/index'
2-
31
import { mitt } from 'zen-mitt'
42

3+
import type { SQLiteDB } from '../../src/index'
54
import {
65
initSQLite,
76
isIdbSupported,
@@ -13,6 +12,7 @@ import { useIdbStorage } from '../../src/vfs/idb'
1312
import { useIdbMemoryStorage } from '../../src/vfs/idb-memory'
1413
import url from '../../wa-sqlite-fts5/wa-sqlite-async.wasm?url'
1514
import syncUrl from '../../wa-sqlite-fts5/wa-sqlite.wasm?url'
15+
1616
import { runSQL } from './runSQL'
1717
import OpfsWorker from './worker?worker'
1818

@@ -59,7 +59,9 @@ document.querySelector('.import')?.addEventListener('click', async () => {
5959
)
6060
await db.sync(file)
6161
console.log(
62-
await db.run(`SELECT "type", "tbl_name" AS "table", CASE WHEN "sql" LIKE '%PRIMARY KEY AUTOINCREMENT%' THEN 1 ELSE "name" END AS "name" FROM "sqlite_master"`),
62+
await db.run(
63+
`SELECT "type", "tbl_name" AS "table", CASE WHEN "sql" LIKE '%PRIMARY KEY AUTOINCREMENT%' THEN 1 ELSE "name" END AS "name" FROM "sqlite_master"`,
64+
),
6365
)
6466
})
6567

@@ -203,14 +205,18 @@ async function selectFile(accept?: string): Promise<File> {
203205
input.remove()
204206
}
205207

206-
window.addEventListener('focus', () => {
207-
setTimeout(() => {
208-
if (!input.files?.length) {
209-
reject(new Error('File selection cancelled'))
210-
input.remove()
211-
}
212-
}, 300)
213-
}, { once: true })
208+
window.addEventListener(
209+
'focus',
210+
() => {
211+
setTimeout(() => {
212+
if (!input.files?.length) {
213+
reject(new Error('File selection cancelled'))
214+
input.remove()
215+
}
216+
}, 300)
217+
},
218+
{ once: true },
219+
)
214220

215221
input.click()
216222
})

playground/src/runSQL.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import type { SQLiteDBCore } from '../../src'
2-
32
import { iterator } from '../../src'
43

5-
export async function runSQL(run: (sql: string, parameters?: any[]) => Promise<any>): Promise<void> {
4+
export async function runSQL(
5+
run: (sql: string, parameters?: any[]) => Promise<any>,
6+
): Promise<void> {
67
await run('CREATE TABLE if not exists t1(a INTEGER, b INTEGER, c VARCHAR);')
7-
await run('INSERT INTO t1 VALUES(1, 19147, \'nineteen thousand one hundred forty-seven\');')
8-
await run('INSERT INTO t1 VALUES(2, 26008, \'twenty-six thousand eight\');')
9-
await run('INSERT INTO t1 VALUES(3, 46582, \'forty-six thousand five hundred eighty-two\');')
8+
await run("INSERT INTO t1 VALUES(1, 19147, 'nineteen thousand one hundred forty-seven');")
9+
await run("INSERT INTO t1 VALUES(2, 26008, 'twenty-six thousand eight');")
10+
await run("INSERT INTO t1 VALUES(3, 46582, 'forty-six thousand five hundred eighty-two');")
1011
console.log(await run('select * from t1'))
1112
}
1213
export async function runSQLStream(
@@ -15,16 +16,15 @@ export async function runSQLStream(
1516
onData: (data: any) => void,
1617
): Promise<void> {
1718
await run('CREATE TABLE if not exists t1(a INTEGER, b INTEGER, c VARCHAR);')
18-
await run('INSERT INTO t1 VALUES(1, 19147, \'nineteen thousand one hundred forty-seven\');')
19-
await run('INSERT INTO t1 VALUES(2, 26008, \'twenty-six thousand eight\');')
20-
await run('INSERT INTO t1 VALUES(3, 46582, \'forty-six thousand five hundred eighty-two\');')
19+
await run("INSERT INTO t1 VALUES(1, 19147, 'nineteen thousand one hundred forty-seven');")
20+
await run("INSERT INTO t1 VALUES(2, 26008, 'twenty-six thousand eight');")
21+
await run("INSERT INTO t1 VALUES(3, 46582, 'forty-six thousand five hundred eighty-two');")
2122
await stream(onData, 'select * from t1')
2223
}
2324

24-
export async function runIterator(
25-
core: SQLiteDBCore,
26-
): Promise<void> {
27-
const sql = 'INSERT INTO t1 VALUES(100, 19147, \'nineteen thousand one hundred forty-seven\'), (200, 26008, \'twenty-six thousand eight\'), (300, 46582, \'forty-six thousand five hundred eighty-two\') returning *;'
25+
export async function runIterator(core: SQLiteDBCore): Promise<void> {
26+
const sql =
27+
"INSERT INTO t1 VALUES(100, 19147, 'nineteen thousand one hundred forty-seven'), (200, 26008, 'twenty-six thousand eight'), (300, 46582, 'forty-six thousand five hundred eighty-two') returning *;"
2828
const it = iterator(core, sql)
2929
for await (const row of it) {
3030
console.log('[iterator]', row)

0 commit comments

Comments
 (0)