switch to monorepo structure

This commit is contained in:
2025-05-06 11:00:35 +01:00
parent e1f927ad27
commit 07b7f1b51f
63 changed files with 2440 additions and 1011 deletions

View File

@@ -0,0 +1,76 @@
import { Database } from "bun:sqlite"
const SCHEMA_VERSION = 0
const db = new Database("data.sqlite")
const createMetadataTableQuery = db.query(`
CREATE TABLE IF NOT EXISTS metadata(
key TEXT NOT NULL PRIMARY KEY,
value,
UNIQUE(key)
);
`)
const schemaVersionQuery = db.query("SELECT version FROM metadata WHERE key = 'schema_version'")
const setSchemaVersionQuery = db.query("UPDATE metadata SET value = $schemaVersion WHERE key = 'schema_version'")
const migrations = [
`
CREATE TABLE IF NOT EXISTS users(
id TEXT PRIMARY KEY,
username TEXT NOT NULL,
password TEXT NOT NULL,
);
CREATE TABLE IF NOT EXISTS bookmarks(
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
kind TEXT NOT NULL,
title TEXT NOT NULL,
url TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS sessions(
session_id TEXT NOT NULL,
user_id TEXT NOT NULL,
expires_at_unix_ms INTEGER NOT NULL,
PRIMARY KEY (session_id, user_id)
);
CREATE TABLE IF NOT EXISTS auth_tokens(
id TEXT PRIMARY KEY,
token TEXT NOT NULL,
user_id TEXT NOT NULL,
expires_at_unix_ms INTEGER NOT NULL
);
`,
]
const executeMigrations = db.transaction((migration) => {
db.run(migration)
})
function migrateDatabase() {
createMetadataTableQuery.run()
const row = schemaVersionQuery.get()
let currentVersion: number
if (row) {
currentVersion = (row as { version: number }).version
console.log(`Migrating database from version ${currentVersion} to version ${SCHEMA_VERSION}...`)
} else {
currentVersion = -1
console.log("Initializing database...")
}
if (currentVersion < SCHEMA_VERSION) {
executeMigrations(migrations.slice(currentVersion + 1, SCHEMA_VERSION + 1))
setSchemaVersionQuery.run({ schemaVersion: SCHEMA_VERSION })
console.log("Database successfully migrated!")
} else {
console.error("Rolling back database to a previous version is unsupported. Are you trying to downgrade MARKONE?")
}
}
export { db, migrateDatabase }