Swadhin Biswas feat(api): add runner, analytics, packages, and security services · 3/6/2026
28f7954
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* Database Adapter Factory
* Creates the appropriate adapter based on configuration
*/
// import { MongoDBAdapter } from "./mongodb";
// import { PostgresAdapter } from "./postgres";
// import { RedisAdapter } from "./redis";
import { SQLiteAdapter } from "./sqlite";
import { DatabaseAdapter, DatabaseConfig, DatabaseDriver } from "./types";
// Lazy-loaded adapters for optional dependencies
const adapterLoaders: Record<
string,
() => Promise<new (config: DatabaseConfig) => DatabaseAdapter>
> = {
sqlite: async () => SQLiteAdapter,
postgres: async () => {
const { PostgresAdapter } = await import("./postgres");
return PostgresAdapter;
},
turso: async () => {
const { TursoAdapter } = await import("./turso");
return TursoAdapter;
},
cockroachdb: async () => {
// CockroachDB uses the Postgres wire protocol
const { PostgresAdapter } = await import("./postgres");
return PostgresAdapter;
},
custom: async () => {
throw new Error("Custom adapter must be provided explicitly");
},
};
export async function createAdapter(
config: DatabaseConfig,
): Promise<DatabaseAdapter> {
const loader = adapterLoaders[config.driver];
if (!loader) {
throw new Error(`Unsupported database driver: ${config.driver}`);
}
const AdapterClass = await loader();
return new AdapterClass(config);
}
export function parseConnectionString(url: string): Partial<DatabaseConfig> {
try {
// Handle SQLite paths
if (url.startsWith("./") || url.startsWith("/") || url.endsWith(".db")) {
return { driver: "sqlite", url };
}
// Handle file:// URLs (SQLite)
if (url.startsWith("file:")) {
return { driver: "sqlite", url: url.replace("file:", "") };
}
// Handle libsql:// URLs (Turso)
if (url.startsWith("libsql:")) {
return { driver: "turso", url };
}
// Parse standard URLs
const urlObj = new URL(url);
const protocol = urlObj.protocol.replace(":", "");
const driverMap: Record<string, DatabaseDriver> = {
postgres: "postgres",
postgresql: "postgres",
mysql: "mysql",
mariadb: "mariadb",
mongodb: "mongodb",
"mongodb+srv": "mongodb",
redis: "redis",
rediss: "redis",
firestore: "firestore",
dynamodb: "dynamodb",
bolt: "neo4j",
neo4j: "neo4j",
"neo4j+s": "neo4j",
cql: "cassandra",
cassandra: "cassandra",
scylla: "scylladb",
scylladb: "scylladb",
surreal: "surrealdb",
cockroachdb: "cockroachdb",
surrealdb: "surrealdb",
ws: "surrealdb",
wss: "surrealdb",
};
const driver = driverMap[protocol];
if (!driver) {
throw new Error(`Unknown protocol: ${protocol}`);
}
return {
driver,
url,
host: urlObj.hostname,
port: parseInt(urlObj.port) || undefined,
user: urlObj.username || undefined,
password: urlObj.password || undefined,
database: urlObj.pathname.slice(1) || undefined,
ssl: protocol.endsWith("s") || urlObj.searchParams.get("ssl") === "true",
};
} catch (error) {
throw new Error(`Failed to parse connection string: ${error}`);
}
}
export class DatabaseManager {
private static instance: DatabaseManager;
private adapter: DatabaseAdapter | null = null;
private config: DatabaseConfig | null = null;
private constructor() {}
static getInstance(): DatabaseManager {
if (!DatabaseManager.instance) {
DatabaseManager.instance = new DatabaseManager();
}
return DatabaseManager.instance;
}
async initialize(config: DatabaseConfig): Promise<void> {
if (this.adapter?.isConnected()) {
await this.adapter.disconnect();
}
this.config = config;
this.adapter = await createAdapter(config);
await this.adapter.connect();
}
async initializeFromEnv(): Promise<void> {
const driver = (process.env.DATABASE_DRIVER || "sqlite") as DatabaseDriver;
const url = process.env.DATABASE_URL || "./data/opencodehub.db";
const config: DatabaseConfig = {
driver,
url,
authToken: process.env.DATABASE_AUTH_TOKEN,
user: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD,
ssl: process.env.DATABASE_SSL === "true",
};
await this.initialize(config);
}
getAdapter(): DatabaseAdapter {
if (!this.adapter) {
throw new Error("Database not initialized. Call initialize() first.");
}
return this.adapter;
}
getConfig(): DatabaseConfig | null {
return this.config;
}
async disconnect(): Promise<void> {
if (this.adapter) {
await this.adapter.disconnect();
this.adapter = null;
}
}
}
// Export singleton instance
export const db = DatabaseManager.getInstance();
// Convenience function to get adapter
export function getDb(): DatabaseAdapter {
return db.getAdapter();
}