ditto/src/db.ts

57 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-08-07 06:45:02 +00:00
import fs from 'node:fs/promises';
import path from 'node:path';
import { DenoSqliteDialect, FileMigrationProvider, Kysely, Migrator, Sqlite } from '@/deps.ts';
2023-08-10 18:37:56 +00:00
import { Conf } from '@/config.ts';
2023-07-09 16:47:19 +00:00
interface DittoDB {
events: EventRow;
tags: TagRow;
users: UserRow;
}
interface EventRow {
id: string;
kind: number;
2023-08-06 17:54:00 +00:00
pubkey: string;
content: string;
created_at: number;
tags: string;
sig: string;
2023-08-06 17:54:00 +00:00
}
2023-07-09 16:47:19 +00:00
interface TagRow {
tag: string;
value_1: string | null;
value_2: string | null;
value_3: string | null;
event_id: string;
}
2023-07-09 16:47:19 +00:00
interface UserRow {
pubkey: string;
username: string;
inserted_at: Date;
}
const db = new Kysely<DittoDB>({
dialect: new DenoSqliteDialect({
2023-08-10 18:37:56 +00:00
database: new Sqlite(Conf.dbPath),
2023-08-07 06:45:02 +00:00
}),
});
const migrator = new Migrator({
db,
provider: new FileMigrationProvider({
fs,
path,
migrationFolder: new URL(import.meta.resolve('./db/migrations')).pathname,
}),
});
2023-08-07 01:14:11 +00:00
2023-08-07 06:45:02 +00:00
console.log('Running migrations...');
2023-08-09 00:31:14 +00:00
const results = await migrator.migrateToLatest();
console.log('Migrations finished:', results);
2023-08-07 06:45:02 +00:00
export { db, type DittoDB, type EventRow, type TagRow, type UserRow };