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
|
|
|
|
2023-08-09 06:53:50 +00:00
|
|
|
interface DittoDB {
|
2023-08-07 05:50:12 +00:00
|
|
|
events: EventRow;
|
|
|
|
tags: TagRow;
|
|
|
|
users: UserRow;
|
2023-08-14 19:11:28 +00:00
|
|
|
relays: RelayRow;
|
2023-08-07 05:50:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface EventRow {
|
|
|
|
id: string;
|
|
|
|
kind: number;
|
2023-08-06 17:54:00 +00:00
|
|
|
pubkey: string;
|
2023-08-07 05:50:12 +00:00
|
|
|
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
|
|
|
|
2023-08-07 05:50:12 +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
|
|
|
|
2023-08-07 05:50:12 +00:00
|
|
|
interface UserRow {
|
|
|
|
pubkey: string;
|
|
|
|
username: string;
|
|
|
|
inserted_at: Date;
|
|
|
|
}
|
2023-07-09 17:27:10 +00:00
|
|
|
|
2023-08-14 19:11:28 +00:00
|
|
|
interface RelayRow {
|
|
|
|
url: string;
|
|
|
|
}
|
|
|
|
|
2023-08-09 06:53:50 +00:00
|
|
|
const db = new Kysely<DittoDB>({
|
2023-08-07 05:50:12 +00:00
|
|
|
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 05:50:12 +00:00
|
|
|
}),
|
|
|
|
});
|
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
|
|
|
|
2023-08-09 06:53:50 +00:00
|
|
|
export { db, type DittoDB, type EventRow, type TagRow, type UserRow };
|