chore: refactor sqlite service

This commit is contained in:
Ben Allfree
2023-11-05 20:39:42 -08:00
parent f0b3d837c3
commit 3bb1da666e
4 changed files with 14 additions and 73 deletions

View File

@@ -66,7 +66,6 @@
"ora": "^7.0.1",
"pocketbase": "^0.19.0",
"semver": "^7.5.4",
"sqlite": "^4.2.1",
"sqlite3": "^5.1.6",
"tail": "^2.2.6",
"tmp": "^0.2.1",

7
pnpm-lock.yaml generated
View File

@@ -89,9 +89,6 @@ importers:
semver:
specifier: ^7.5.4
version: 7.5.4
sqlite:
specifier: ^4.2.1
version: 4.2.1
sqlite3:
specifier: ^5.1.6
version: 5.1.6
@@ -6339,10 +6336,6 @@ packages:
- supports-color
dev: false
/sqlite@4.2.1:
resolution: {integrity: sha512-Tll0Ndvnwkuv5Hn6WIbh26rZiYQORuH1t5m/or9LUpSmDmmyFG89G9fKrSeugMPxwmEIXoVxqTun4LbizTs4uw==}
dev: false
/ssh2@1.14.0:
resolution: {integrity: sha512-AqzD1UCqit8tbOKoj6ztDDi1ffJZ2rV2SwlgrVVrHPkV5vWqGJOVp5pmtj18PunkPJAuKQsnInyKV+/Nb2bUnA==}
engines: {node: '>=10.16.0'}

View File

@@ -14,13 +14,13 @@ import {
PocketbaseReleaseVersionService,
PocketbaseService,
PortService,
SqliteService,
centralDbService,
ftpService,
instanceService,
ipWhitelistService,
proxyService,
realtimeLog,
sqliteService,
} from '$services'
import { LogLevelName, LoggerService } from '$shared'
import EventSource from 'eventsource'
@@ -97,7 +97,7 @@ global.EventSource = EventSource
coreInternalUrl: url,
})
await ipWhitelistService({})
await sqliteService({})
await SqliteService({})
await realtimeLog({})
await instanceService({
instanceApiCheckIntervalMs: 50,

View File

@@ -1,38 +1,18 @@
import {
createCleanupManager,
createEvent,
LoggerService,
mkSingleton,
serialAsyncExecutionGuard,
SingletonBaseConfig,
} from '$shared'
import { Database, open } from 'sqlite'
import { JsonObject } from 'type-fest'
import knex from 'knex'
export type SqliteUnsubscribe = () => void
export type SqliteChangeHandler<TRecord extends JsonObject> = (
e: SqliteChangeEvent<TRecord>,
) => void
export type SqliteEventType = 'update' | 'insert' | 'delete'
export type SqliteChangeEvent<TRecord extends JsonObject> = {
table: string
action: SqliteEventType
record: TRecord
}
export type SqliteServiceApi = {
all: Database['all']
get: Database['get']
migrate: Database['migrate']
exec: Database['exec']
subscribe: <TRecord extends JsonObject>(
cb: SqliteChangeHandler<TRecord>,
) => SqliteUnsubscribe
}
export type SqliteServiceApi = ReturnType<typeof knex>
export type SqliteServiceConfig = SingletonBaseConfig & {}
export type SqliteService = ReturnType<typeof sqliteService>
export type SqliteService = ReturnType<typeof SqliteService>
export const sqliteService = mkSingleton((config: SqliteServiceConfig) => {
export const SqliteService = mkSingleton((config: SqliteServiceConfig) => {
const { dbg, trace } = LoggerService().create(`sqliteService`)
const connections: { [_: string]: SqliteServiceApi } = {}
@@ -53,52 +33,21 @@ export const sqliteService = mkSingleton((config: SqliteServiceConfig) => {
dbg(`Not yet opened`)
const api = await (async () => {
const db = await open({ filename, driver: Database })
dbg(`Database opened`)
db.db.addListener(
'change',
async (
eventType: SqliteEventType,
database: string,
table: string,
rowId: number,
) => {
trace(`Got a raw change event`, {
eventType,
database,
table,
rowId,
})
if (eventType === 'delete') return // Not supported
dbg(`Opening ${filename}`)
const record = await db.get(
`select * from ${table} where rowid = '${rowId}'`,
)
const e: SqliteChangeEvent<any> = {
table,
action: eventType,
record,
}
fireChange(e)
const db = knex({
client: 'sqlite3', // or 'better-sqlite3'
connection: {
filename,
},
)
})
cm.add(() => {
dbg(`Closing connection`)
db.db.removeAllListeners()
db.close()
db.destroy()
})
db.migrate
const [onChange, fireChange] = createEvent<SqliteChangeEvent<any>>()
const api: SqliteServiceApi = {
all: db.all.bind(db),
get: db.get.bind(db),
migrate: db.migrate.bind(db),
exec: db.exec.bind(db),
subscribe: onChange,
}
return api
return db
})().catch(error)
if (!api) {
throw new Error(`Unable to connect to SQLite`)