bring back sqlite3 and migrations.

This commit is contained in:
Jordan 2025-03-09 20:16:54 -07:00
parent e9c04a7b39
commit 3616592896
2 changed files with 47 additions and 9 deletions

View File

@ -1,11 +1,26 @@
import config from "@/knexfile";
import Knex from "knex";
import * as SQLite from "expo-sqlite";
import { MIGRATE_UP, MIGRATE_DOWN } from "./migrations";
export async function getDb(
environment: "development" | "staging" | "production" = "production",
automigrate = true
) {
const k = Knex(config[environment]);
if (automigrate) await k.migrate.up();
return k;
export async function getDb() {
return await SQLite.openDatabaseAsync("translation_terrace");
}
export async function migrateDb(direction: "up" | "down" = "up") {
const db = await getDb();
const m = direction === "up" ? MIGRATE_UP : MIGRATE_DOWN;
for (let [migration, statements] of Object.entries(m)) {
for (let statement of statements) {
console.log(statement);
try {
const result = await db.runAsync(statement);
console.log(result);
} catch (err) {
console.error(err);
}
}
}
}

23
app/lib/migrations.ts Normal file
View File

@ -0,0 +1,23 @@
export const MIGRATE_UP = {
1: [
`CREATE TABLE IF NOT EXISTS settings (
host_language TEXT,
libretranslate_base_url TEXT,
ui_direction INTEGER,
whisper_model TEXT
)`,
],
2: [
`CREATE TABLE IF NOT EXISTS whisper_models (
model TEXT PRIMARY KEY,
bytes_done INTEGER,
bytes_total INTEGER
)`,
],
};
export const MIGRATE_DOWN = {
1: [`DROP TABLE IF EXISTS settings`],
2: [`DROP TABLE IF EXISTS whisper_models`],
};