Scratch space for learning atproto app development

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

Changed files
+92 -82
src
+92
src/db.ts
···
+
import SqliteDb from 'better-sqlite3'
+
import {
+
Kysely,
+
Migrator,
+
SqliteDialect,
+
Migration,
+
MigrationProvider,
+
} from 'kysely'
+
+
// Types
+
+
export type DatabaseSchema = {
+
status: Status
+
auth_session: AuthSession
+
auth_state: AuthState
+
}
+
+
export type Status = {
+
authorDid: string
+
status: string
+
updatedAt: string
+
indexedAt: string
+
}
+
+
export type AuthSession = {
+
key: string
+
session: AuthSessionJson
+
}
+
+
export type AuthState = {
+
key: string
+
state: AuthStateJson
+
}
+
+
type AuthStateJson = string
+
+
type AuthSessionJson = string
+
+
// Migrations
+
+
const migrations: Record<string, Migration> = {}
+
+
const migrationProvider: MigrationProvider = {
+
async getMigrations() {
+
return migrations
+
},
+
}
+
+
migrations['001'] = {
+
async up(db: Kysely<unknown>) {
+
await db.schema
+
.createTable('status')
+
.addColumn('authorDid', 'varchar', (col) => col.primaryKey())
+
.addColumn('status', 'varchar', (col) => col.notNull())
+
.addColumn('updatedAt', 'varchar', (col) => col.notNull())
+
.addColumn('indexedAt', 'varchar', (col) => col.notNull())
+
.execute()
+
await db.schema
+
.createTable('auth_session')
+
.addColumn('key', 'varchar', (col) => col.primaryKey())
+
.addColumn('session', 'varchar', (col) => col.notNull())
+
.execute()
+
await db.schema
+
.createTable('auth_state')
+
.addColumn('key', 'varchar', (col) => col.primaryKey())
+
.addColumn('state', 'varchar', (col) => col.notNull())
+
.execute()
+
},
+
async down(db: Kysely<unknown>) {
+
await db.schema.dropTable('auth_state').execute()
+
await db.schema.dropTable('auth_session').execute()
+
await db.schema.dropTable('status').execute()
+
},
+
}
+
+
// APIs
+
+
export const createDb = (location: string): Database => {
+
return new Kysely<DatabaseSchema>({
+
dialect: new SqliteDialect({
+
database: new SqliteDb(location),
+
}),
+
})
+
}
+
+
export const migrateToLatest = async (db: Database) => {
+
const migrator = new Migrator({ db, provider: migrationProvider })
+
const { error } = await migrator.migrateToLatest()
+
if (error) throw error
+
}
+
+
export type Database = Kysely<DatabaseSchema>
-20
src/db/index.ts
···
-
import SqliteDb from 'better-sqlite3'
-
import { Kysely, Migrator, SqliteDialect } from 'kysely'
-
import { migrationProvider } from './migrations'
-
import type { DatabaseSchema } from './schema'
-
-
export const createDb = (location: string): Database => {
-
return new Kysely<DatabaseSchema>({
-
dialect: new SqliteDialect({
-
database: new SqliteDb(location),
-
}),
-
})
-
}
-
-
export const migrateToLatest = async (db: Database) => {
-
const migrator = new Migrator({ db, provider: migrationProvider })
-
const { error } = await migrator.migrateToLatest()
-
if (error) throw error
-
}
-
-
export type Database = Kysely<DatabaseSchema>
-36
src/db/migrations.ts
···
-
import type { Kysely, Migration, MigrationProvider } from 'kysely'
-
-
const migrations: Record<string, Migration> = {}
-
-
export const migrationProvider: MigrationProvider = {
-
async getMigrations() {
-
return migrations
-
},
-
}
-
-
migrations['001'] = {
-
async up(db: Kysely<unknown>) {
-
await db.schema
-
.createTable('status')
-
.addColumn('authorDid', 'varchar', (col) => col.primaryKey())
-
.addColumn('status', 'varchar', (col) => col.notNull())
-
.addColumn('updatedAt', 'varchar', (col) => col.notNull())
-
.addColumn('indexedAt', 'varchar', (col) => col.notNull())
-
.execute()
-
await db.schema
-
.createTable('auth_session')
-
.addColumn('key', 'varchar', (col) => col.primaryKey())
-
.addColumn('session', 'varchar', (col) => col.notNull())
-
.execute()
-
await db.schema
-
.createTable('auth_state')
-
.addColumn('key', 'varchar', (col) => col.primaryKey())
-
.addColumn('state', 'varchar', (col) => col.notNull())
-
.execute()
-
},
-
async down(db: Kysely<unknown>) {
-
await db.schema.dropTable('auth_state').execute()
-
await db.schema.dropTable('auth_session').execute()
-
await db.schema.dropTable('status').execute()
-
},
-
}
-26
src/db/schema.ts
···
-
export type DatabaseSchema = {
-
status: Status
-
auth_session: AuthSession
-
auth_state: AuthState
-
}
-
-
export type Status = {
-
authorDid: string
-
status: string
-
updatedAt: string
-
indexedAt: string
-
}
-
-
export type AuthSession = {
-
key: string
-
session: AuthSessionJson
-
}
-
-
export type AuthState = {
-
key: string
-
state: AuthStateJson
-
}
-
-
type AuthStateJson = string
-
-
type AuthSessionJson = string