Skip to content

Commit 253100b

Browse files
committed
feat: add useIdbMemoryStorage
1 parent f1886a3 commit 253100b

File tree

5 files changed

+66
-22
lines changed

5 files changed

+66
-22
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const { run, changes, lastInsertRowId, close } = await initSQLite(
2323

2424
#### IndexedDB
2525

26-
use IDBBatchAtomicVFS with `wa-sqlite-async.wasm`, larger than sync version, better compatibility
26+
use `IDBBatchAtomicVFS` with `wa-sqlite-async.wasm`, larger than sync version, better compatibility
2727

2828
[minimal IndexedDB backend browser version](https://caniuse.com/mdn-api_lockmanager)
2929

@@ -41,7 +41,7 @@ const { run, changes, lastInsertRowId, close } = await initSQLite(
4141

4242
#### OPFS
4343

44-
use OPFSCoopSyncVFS with `wa-sqlite.wasm`, smaller than async version
44+
use `OPFSCoopSyncVFS` with `wa-sqlite.wasm`, smaller than async version
4545

4646
[minimal OPFS backend browser version](https://caniuse.com/mdn-api_filesystemsyncaccesshandle)
4747

playground/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
isOpfsSupported,
99
useMemoryStorage,
1010
} from '../../src'
11-
import { useIdbStorage } from '../../src/vfs/idb'
11+
import { useIdbMemoryStorage, useIdbStorage } from '../../src/vfs/idb'
1212
import { runSQL } from './runSQL'
1313
import OpfsWorker from './worker?worker'
1414

src/types/index.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,36 +29,39 @@ export type Options = {
2929

3030
export type SQLiteDB = {
3131
/**
32-
* file name (IDBBatchAtomicVFS) or directory path (AccessHandlePoolVFS)
32+
* File name (`IDBBatchAtomicVFS`) or directory path (`OPFSCoopSyncVFS`)
3333
*/
3434
path: string
3535
/**
36-
* db pointer
36+
* DB pointer
3737
*/
3838
db: number
3939
/**
40-
* sqlite apis
40+
* SQLite apis
4141
*/
4242
sqlite: SQLiteAPI
43+
/**
44+
* Wasm build module
45+
*/
4346
sqliteModule: any
4447
/**
45-
* sqlite vfs
48+
* SQLite vfs
4649
*/
4750
vfs: SQLiteVFS
4851
/**
49-
* close db
52+
* Close db
5053
*/
5154
close: () => Promise<void>
5255
/**
53-
* get db changes
56+
* Get db changes
5457
*/
5558
changes: () => number
5659
/**
57-
* get lastInsertRowId
60+
* Get lastInsertRowId
5861
*/
5962
lastInsertRowId: () => number
6063
/**
61-
* run sql and return result list
64+
* Run sql and return result list
6265
* @param onData trigger onn stream has data received
6366
* @param sql raw sql with placeholder
6467
* @param parameters params that replace the placeholder
@@ -67,7 +70,7 @@ export type SQLiteDB = {
6770
*/
6871
stream: (onData: (data: Record<string, SQLiteCompatibleType>) => void, sql: string, parameters?: SQLiteCompatibleType[]) => Promise<void>
6972
/**
70-
* run sql and return result list
73+
* Run sql and return result list
7174
* @param sql raw sql with placeholder
7275
* @param parameters params that replace the placeholder
7376
* @example

src/types/wa-sqlite.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ declare module 'wa-sqlite/src/examples/IDBBatchAtomicVFS.js' {
44
}
55
}
66

7+
declare module 'wa-sqlite/src/examples/IDBMirrorVFS.js' {
8+
export class IDBMirrorVFS {
9+
static create(name: string, module: any): Promise<SQLiteVFS>
10+
}
11+
}
12+
713
declare module 'wa-sqlite/src/examples/OPFSCoopSyncVFS.js' {
814
export class OPFSCoopSyncVFS {
915
static create(name: string, module: any): Promise<SQLiteVFS>

src/vfs/idb.ts

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
import type { BaseOptions, IDBBatchAtomicVFSOptions, Options } from '../types'
22
import SQLiteAsyncESMFactory from 'wa-sqlite/dist/wa-sqlite-async.mjs'
33
import { IDBBatchAtomicVFS } from 'wa-sqlite/src/examples/IDBBatchAtomicVFS.js'
4+
import { IDBMirrorVFS } from 'wa-sqlite/src/examples/IDBMirrorVFS.js'
45

56
export { IDBBatchAtomicVFS } from 'wa-sqlite/src/examples/IDBBatchAtomicVFS.js'
7+
export { IDBMirrorVFS } from 'wa-sqlite/src/examples/IDBMirrorVFS.js'
68

7-
export type IDBVFSOptions = IDBBatchAtomicVFSOptions & {
8-
/**
9-
* @default 'idb-sqlite-vfs'
10-
*/
11-
idbName?: string
12-
}
9+
export type IDBVFSOptions = IDBBatchAtomicVFSOptions
1310

1411
/**
15-
* storage data in IndexedDB,
16-
* use IDBBatchAtomicVFS with `wa-sqlite-async.wasm` (larger than sync version), better compatibility
12+
* Store data in IndexedDB,
13+
* Use `IDBBatchAtomicVFS` with `wa-sqlite-async.wasm` (larger than sync version), better compatibility
1714
* @param fileName db file name
1815
* @param options options
1916
* @example
@@ -35,21 +32,59 @@ export async function useIdbStorage(
3532
): Promise<Options> {
3633
const {
3734
url,
38-
idbName = 'idb-sqlite-vfs',
3935
lockPolicy = 'shared+hint',
4036
lockTimeout = Infinity,
4137
readonly,
4238
} = options
4339
const sqliteModule = await SQLiteAsyncESMFactory(
4440
url ? { locateFile: () => url } : undefined,
4541
)
42+
const idbName = fileName.endsWith('.db') ? fileName : `${fileName}.db`
4643
const vfsOptions = { idbName, lockPolicy, lockTimeout }
4744
/// keep-sorted
45+
return {
46+
path: idbName,
47+
readonly,
48+
sqliteModule,
49+
vfsFn: IDBMirrorVFS.create,
50+
vfsOptions,
51+
}
52+
}
53+
54+
/**
55+
* Store data in memory and sync to IndexedDB,
56+
* Use `IDBMirrorVFS` with `wa-sqlite-async.wasm` (larger than sync version), better performance compare to `IDBBatchAtomicVFS`
57+
* @param fileName db file name
58+
* @param options options
59+
* @example
60+
* ```ts
61+
* import { useIdbMemoryStorage } from '@subframe7536/sqlite-wasm/idb'
62+
* import { getAsyncWasmURL, initSQLite } from '@subframe7536/sqlite-wasm'
63+
*
64+
* // const url = 'https://cdn.jsdelivr.net/gh/rhashimoto/wa-sqlite@v0.9.9/dist/wa-sqlite-async.wasm'
65+
* const url = getAsyncWasmURL()
66+
*
67+
* const { run, changes, lastInsertRowId, close, sqlite, db } = await initSQLite(
68+
* useIdbMemoryStorage('test', { url })
69+
* )
70+
* ```
71+
*/
72+
export async function useIdbMemoryStorage(
73+
fileName: string,
74+
options: BaseOptions = {},
75+
): Promise<Options> {
76+
const {
77+
url,
78+
readonly,
79+
} = options
80+
const sqliteModule = await SQLiteAsyncESMFactory(
81+
url ? { locateFile: () => url } : undefined,
82+
)
83+
/// keep-sorted
4884
return {
4985
path: fileName.endsWith('.db') ? fileName : `${fileName}.db`,
5086
readonly,
5187
sqliteModule,
5288
vfsFn: IDBBatchAtomicVFS.create,
53-
vfsOptions,
5489
}
5590
}

0 commit comments

Comments
 (0)