refactor: extract sqlite types

This commit is contained in:
yyh
2026-01-22 16:15:19 +08:00
parent 6c75893956
commit b6228c99cd
2 changed files with 40 additions and 29 deletions

View File

@ -0,0 +1,31 @@
import type { MemoryVFS } from 'wa-sqlite/src/examples/MemoryVFS.js'
type SQLiteModuleType = typeof import('wa-sqlite')
export type SQLiteValue = string | number | bigint | Uint8Array | null
export type SQLiteQueryResult = {
columns: string[]
values: SQLiteValue[][]
}
export type SQLiteAPI = ReturnType<SQLiteModuleType['Factory']>
export type SQLiteVFS = Parameters<SQLiteAPI['vfs_register']>[0]
export type SQLiteClient = {
sqlite3: SQLiteAPI
sqlite: SQLiteModuleType
vfs: MemoryVFS
}
export type SQLiteState = {
tables: string[]
isLoading: boolean
error: Error | null
}
export type SQLiteAction
= | { type: 'reset' }
| { type: 'loading' }
| { type: 'success', tables: string[] }
| { type: 'error', error: Error }

View File

@ -1,12 +1,14 @@
import type { MemoryVFS } from 'wa-sqlite/src/examples/MemoryVFS.js'
import type {
SQLiteAction,
SQLiteClient,
SQLiteQueryResult,
SQLiteState,
SQLiteValue,
SQLiteVFS,
} from './sqlite/types'
import { useCallback, useEffect, useReducer, useRef } from 'react'
export type SQLiteValue = string | number | bigint | Uint8Array | null
export type SQLiteQueryResult = {
columns: string[]
values: SQLiteValue[][]
}
export type { SQLiteQueryResult, SQLiteValue } from './sqlite/types'
export type UseSQLiteDatabaseResult = {
tables: string[]
@ -15,28 +17,6 @@ export type UseSQLiteDatabaseResult = {
queryTable: (tableName: string, limit?: number) => Promise<SQLiteQueryResult | null>
}
type SQLiteModuleType = typeof import('wa-sqlite')
type SQLiteAPI = ReturnType<SQLiteModuleType['Factory']>
type SQLiteVFS = Parameters<SQLiteAPI['vfs_register']>[0]
type SQLiteClient = {
sqlite3: ReturnType<SQLiteModuleType['Factory']>
sqlite: SQLiteModuleType
vfs: MemoryVFS
}
type SQLiteState = {
tables: string[]
isLoading: boolean
error: Error | null
}
type SQLiteAction
= | { type: 'reset' }
| { type: 'loading' }
| { type: 'success', tables: string[] }
| { type: 'error', error: Error }
const TABLES_QUERY = 'SELECT name FROM sqlite_master WHERE type=\'table\' AND name NOT LIKE \'sqlite_%\' ORDER BY name'
let sqliteClientPromise: Promise<SQLiteClient> | null = null