the statusphere demo reworked into a vite/react app in a monorepo

Merge /src/db/* into /src/db.ts

+92 -82
+92
src/db.ts
··· 1 + import SqliteDb from 'better-sqlite3' 2 + import { 3 + Kysely, 4 + Migrator, 5 + SqliteDialect, 6 + Migration, 7 + MigrationProvider, 8 + } from 'kysely' 9 + 10 + // Types 11 + 12 + export type DatabaseSchema = { 13 + status: Status 14 + auth_session: AuthSession 15 + auth_state: AuthState 16 + } 17 + 18 + export type Status = { 19 + authorDid: string 20 + status: string 21 + updatedAt: string 22 + indexedAt: string 23 + } 24 + 25 + export type AuthSession = { 26 + key: string 27 + session: AuthSessionJson 28 + } 29 + 30 + export type AuthState = { 31 + key: string 32 + state: AuthStateJson 33 + } 34 + 35 + type AuthStateJson = string 36 + 37 + type AuthSessionJson = string 38 + 39 + // Migrations 40 + 41 + const migrations: Record<string, Migration> = {} 42 + 43 + const migrationProvider: MigrationProvider = { 44 + async getMigrations() { 45 + return migrations 46 + }, 47 + } 48 + 49 + migrations['001'] = { 50 + async up(db: Kysely<unknown>) { 51 + await db.schema 52 + .createTable('status') 53 + .addColumn('authorDid', 'varchar', (col) => col.primaryKey()) 54 + .addColumn('status', 'varchar', (col) => col.notNull()) 55 + .addColumn('updatedAt', 'varchar', (col) => col.notNull()) 56 + .addColumn('indexedAt', 'varchar', (col) => col.notNull()) 57 + .execute() 58 + await db.schema 59 + .createTable('auth_session') 60 + .addColumn('key', 'varchar', (col) => col.primaryKey()) 61 + .addColumn('session', 'varchar', (col) => col.notNull()) 62 + .execute() 63 + await db.schema 64 + .createTable('auth_state') 65 + .addColumn('key', 'varchar', (col) => col.primaryKey()) 66 + .addColumn('state', 'varchar', (col) => col.notNull()) 67 + .execute() 68 + }, 69 + async down(db: Kysely<unknown>) { 70 + await db.schema.dropTable('auth_state').execute() 71 + await db.schema.dropTable('auth_session').execute() 72 + await db.schema.dropTable('status').execute() 73 + }, 74 + } 75 + 76 + // APIs 77 + 78 + export const createDb = (location: string): Database => { 79 + return new Kysely<DatabaseSchema>({ 80 + dialect: new SqliteDialect({ 81 + database: new SqliteDb(location), 82 + }), 83 + }) 84 + } 85 + 86 + export const migrateToLatest = async (db: Database) => { 87 + const migrator = new Migrator({ db, provider: migrationProvider }) 88 + const { error } = await migrator.migrateToLatest() 89 + if (error) throw error 90 + } 91 + 92 + export type Database = Kysely<DatabaseSchema>
-20
src/db/index.ts
··· 1 - import SqliteDb from 'better-sqlite3' 2 - import { Kysely, Migrator, SqliteDialect } from 'kysely' 3 - import { migrationProvider } from './migrations' 4 - import type { DatabaseSchema } from './schema' 5 - 6 - export const createDb = (location: string): Database => { 7 - return new Kysely<DatabaseSchema>({ 8 - dialect: new SqliteDialect({ 9 - database: new SqliteDb(location), 10 - }), 11 - }) 12 - } 13 - 14 - export const migrateToLatest = async (db: Database) => { 15 - const migrator = new Migrator({ db, provider: migrationProvider }) 16 - const { error } = await migrator.migrateToLatest() 17 - if (error) throw error 18 - } 19 - 20 - export type Database = Kysely<DatabaseSchema>
-36
src/db/migrations.ts
··· 1 - import type { Kysely, Migration, MigrationProvider } from 'kysely' 2 - 3 - const migrations: Record<string, Migration> = {} 4 - 5 - export const migrationProvider: MigrationProvider = { 6 - async getMigrations() { 7 - return migrations 8 - }, 9 - } 10 - 11 - migrations['001'] = { 12 - async up(db: Kysely<unknown>) { 13 - await db.schema 14 - .createTable('status') 15 - .addColumn('authorDid', 'varchar', (col) => col.primaryKey()) 16 - .addColumn('status', 'varchar', (col) => col.notNull()) 17 - .addColumn('updatedAt', 'varchar', (col) => col.notNull()) 18 - .addColumn('indexedAt', 'varchar', (col) => col.notNull()) 19 - .execute() 20 - await db.schema 21 - .createTable('auth_session') 22 - .addColumn('key', 'varchar', (col) => col.primaryKey()) 23 - .addColumn('session', 'varchar', (col) => col.notNull()) 24 - .execute() 25 - await db.schema 26 - .createTable('auth_state') 27 - .addColumn('key', 'varchar', (col) => col.primaryKey()) 28 - .addColumn('state', 'varchar', (col) => col.notNull()) 29 - .execute() 30 - }, 31 - async down(db: Kysely<unknown>) { 32 - await db.schema.dropTable('auth_state').execute() 33 - await db.schema.dropTable('auth_session').execute() 34 - await db.schema.dropTable('status').execute() 35 - }, 36 - }
-26
src/db/schema.ts
··· 1 - export type DatabaseSchema = { 2 - status: Status 3 - auth_session: AuthSession 4 - auth_state: AuthState 5 - } 6 - 7 - export type Status = { 8 - authorDid: string 9 - status: string 10 - updatedAt: string 11 - indexedAt: string 12 - } 13 - 14 - export type AuthSession = { 15 - key: string 16 - session: AuthSessionJson 17 - } 18 - 19 - export type AuthState = { 20 - key: string 21 - state: AuthStateJson 22 - } 23 - 24 - type AuthStateJson = string 25 - 26 - type AuthSessionJson = string