MCPcopy
hub / github.com/21st-dev/1code / initDatabase

Function initDatabase

src/main/lib/db/index.ts:43–72  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

41 * Initialize the database with Drizzle ORM
42 */
43export function initDatabase() {
44 if (db) {
45 return db
46 }
47
48 const dbPath = getDatabasePath()
49 console.log(`[DB] Initializing database at: ${dbPath}`)
50
51 // Create SQLite connection
52 sqlite = new Database(dbPath)
53 sqlite.pragma("journal_mode = WAL")
54 sqlite.pragma("foreign_keys = ON")
55
56 // Create Drizzle instance
57 db = drizzle(sqlite, { schema })
58
59 // Run migrations
60 const migrationsPath = getMigrationsPath()
61 console.log(`[DB] Running migrations from: ${migrationsPath}`)
62
63 try {
64 migrate(db, { migrationsFolder: migrationsPath })
65 console.log("[DB] Migrations completed")
66 } catch (error) {
67 console.error("[DB] Migration error:", error)
68 throw error
69 }
70
71 return db
72}
73
74/**
75 * Get the database instance

Callers 2

index.tsFile · 0.90
getDatabaseFunction · 0.85

Calls 2

getDatabasePathFunction · 0.85
getMigrationsPathFunction · 0.85

Tested by

no test coverage detected