Skip to content

Commit ebd30eb

Browse files
committed
feat: reopen() and parseOpenV2Flag() utils #7
1 parent b56e00c commit ebd30eb

File tree

4 files changed

+26
-13
lines changed

4 files changed

+26
-13
lines changed

playground/src/worker.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { uuidv7 } from 'uuidv7'
22
import url from '../../dist/wa-sqlite.wasm?url'
3-
import { customFunction, initSQLite, isOpfsSupported, iterator, withExistDB } from '../../src'
3+
import { customFunction, initSQLite, isOpfsSupported, withExistDB } from '../../src'
44
import { useOpfsStorage } from '../../src/vfs/opfs'
55
import { runIterator, runSQLStream } from './runSQL'
66

@@ -22,8 +22,9 @@ onmessage = async ({ data }) => {
2222
// await db.sync(data)
2323
// }
2424
await runSQLStream(db.run, db.stream, data => postMessage(data))
25+
console.log(db.lastInsertRowId(), db.changes(), db.sqliteModule._sqlite3_changes64(db.pointer))
2526
await runIterator(db)
26-
console.log(db.lastInsertRowId(), db.changes())
27+
console.log(db.lastInsertRowId(), db.changes(), db.sqliteModule._sqlite3_changes64(db.pointer))
2728
customFunction(db.sqlite, db.pointer, 'uuidv7', () => uuidv7())
2829
console.log(
2930
'uuidv7():',

src/core.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
import type { InitSQLiteOptions, Promisable, SQLiteDB, SQLiteDBCore } from './types'
2-
import {
3-
Factory,
4-
SQLITE_OPEN_CREATE,
5-
SQLITE_OPEN_READONLY,
6-
SQLITE_OPEN_READWRITE,
7-
} from 'wa-sqlite'
2+
import { Factory } from 'wa-sqlite'
83
import { exportDatabase, importDatabase } from './io'
9-
import { changes, close, lastInsertRowId, run, stream } from './utils'
4+
import { changes, close, lastInsertRowId, parseOpenV2Flag, run, stream } from './utils'
105

116
/**
127
* Load SQLite database, presets:
@@ -48,7 +43,7 @@ export async function initSQLiteCore(
4843
await beforeOpen?.(vfs, path)
4944
const pointer = await sqlite.open_v2(
5045
path,
51-
readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
46+
parseOpenV2Flag(readonly),
5247
)
5348
/// keep-sorted
5449
return {

src/types/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export type SQLiteDBCore = {
9898

9999
export type SQLiteDB = SQLiteDBCore & {
100100
/**
101-
* Close db
101+
* Close db. Throw error if fail to close
102102
*/
103103
close: () => Promise<void>
104104
/**

src/utils.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import type { BaseStorageOptions, SQLiteDBCore } from './types'
2-
import { SQLITE_DETERMINISTIC, SQLITE_DIRECTONLY, SQLITE_OK, SQLITE_UTF8 } from 'wa-sqlite'
2+
import {
3+
SQLITE_DETERMINISTIC,
4+
SQLITE_DIRECTONLY,
5+
SQLITE_OK,
6+
SQLITE_OPEN_CREATE,
7+
SQLITE_OPEN_READONLY,
8+
SQLITE_OPEN_READWRITE,
9+
SQLITE_UTF8,
10+
} from 'wa-sqlite'
311
import { SQLITE_ROW } from 'wa-sqlite/src/sqlite-constants.js'
412
import { importDatabase } from './io'
513

@@ -181,7 +189,6 @@ export function withExistDB<T extends BaseStorageOptions>(
181189

182190
export async function close(core: SQLiteDBCore): Promise<void> {
183191
await core.sqlite.close(core.pointer)
184-
await core.vfs.close()
185192
}
186193

187194
export function changes(core: SQLiteDBCore): number | bigint {
@@ -259,3 +266,13 @@ export async function* iterator(
259266
}
260267
cache = undefined!
261268
}
269+
270+
export function parseOpenV2Flag(readonly?: boolean): number {
271+
return readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
272+
}
273+
274+
export async function reopen(core: SQLiteDBCore, readonly?: boolean): Promise<void> {
275+
await close(core)
276+
const newPointer = await core.sqlite.open_v2(core.path, parseOpenV2Flag(readonly))
277+
core.pointer = newPointer
278+
}

0 commit comments

Comments
 (0)