Skip to content

Commit 1b72a2a

Browse files
committed
feat: add sync function in SQLiteDB
1 parent 62510f3 commit 1b72a2a

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

playground/src/index.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@ import { mitt } from 'zen-mitt'
22
import url from '../../dist/wa-sqlite-async.wasm?url'
33
import syncUrl from '../../dist/wa-sqlite.wasm?url'
44
import {
5+
importDatabase,
56
initSQLite,
67
isIdbSupported,
78
isModuleWorkerSupport,
89
isOpfsSupported,
10+
type SQLiteDB,
911
useMemoryStorage,
1012
withExistDB,
1113
} from '../../src/index'
12-
import { useIdbMemoryStorage } from '../../src/vfs/idb-memory'
14+
// import { useIdbMemoryStorage } from '../../src/vfs/idb-memory'
15+
import { useIdbStorage } from '../../src/vfs/idb'
1316
import { runSQL } from './runSQL'
1417
import OpfsWorker from './worker?worker'
1518

16-
let db = await initSQLite(useIdbMemoryStorage('test.db', { url }))
19+
let db: SQLiteDB
1720

1821
const supportModuleWorker = isModuleWorkerSupport()
1922
const supportIDB = isIdbSupported()
@@ -23,7 +26,8 @@ console.log('support IDBBatchAtomicVFS:', supportIDB)
2326
console.log('support OPFSCoopSyncVFS:', supportOPFS)
2427
document.querySelector('.main')?.addEventListener('click', async () => {
2528
if (!db) {
26-
db = await initSQLite(useIdbMemoryStorage('test.db', { url }))
29+
// db = await initSQLite(useIdbMemoryStorage('test.db', { url }))
30+
db = await initSQLite(useIdbStorage('test.db', { url }))
2731
}
2832
await runSQL(db.run)
2933
await runSQL((await initSQLite(useMemoryStorage({ url: syncUrl }))).run)
@@ -39,8 +43,10 @@ document.querySelector('.import')?.addEventListener('click', async () => {
3943
return
4044
}
4145
db = await initSQLite(
42-
useIdbMemoryStorage('test.db', withExistDB(file, { url })),
46+
// useIdbMemoryStorage('test.db', withExistDB(file, { url })),
47+
useIdbStorage('test.db', { url }),
4348
)
49+
await importDatabase(db.vfs, db.path, file)
4450
console.log(
4551
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"`),
4652
)

src/core.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
SQLITE_OPEN_READWRITE,
77
SQLITE_ROW,
88
} from 'wa-sqlite'
9-
import { exportDatabase } from './io'
9+
import { exportDatabase, importDatabase } from './io'
1010

1111
/**
1212
* Load SQLite database, presets: `useMemoryStorage`, `useIdbStorage`, `useIdbMemoryStorage`, `useOpfsStorage`
@@ -23,6 +23,7 @@ export async function initSQLite(options: Promisable<Options>): Promise<SQLiteDB
2323
lastInsertRowId: () => lastInsertRowId(core),
2424
run: (...args) => run(core, ...args),
2525
stream: (...args) => stream(core, ...args),
26+
sync: file => importDatabase(core.vfs, core.path, file),
2627
}
2728
}
2829

src/types/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ export type SQLiteDB = SQLiteDBCore & {
132132
sql: string,
133133
parameters?: SQLiteCompatibleType[]
134134
) => Promise<Array<Record<string, SQLiteCompatibleType>>>
135+
/**
136+
* Import database from `File` or `ReadableStream`
137+
*/
138+
sync: (data: File | ReadableStream) => Promise<void>
135139
/**
136140
* Export database to `Uint8Array`
137141
*/

0 commit comments

Comments
 (0)