Repository navigation
Expand file tree
/
Copy pathdatabase-config.ts
More file actions
74 lines (71 loc) · 1.87 KB
/
Copy pathdatabase-config.ts
File metadata and controls
74 lines (71 loc) · 1.87 KB
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
import { resolve } from "node:path";
import type { HackKitDatabaseOptions } from "@hackkit/core";
import { SqliteDriver } from "@mikro-orm/sqlite";
import { LibSqlDriver } from "@mikro-orm/libsql";
import { MySqlDriver } from "@mikro-orm/mysql";
import { PostgreSqlDriver } from "@mikro-orm/postgresql";
import { env } from "../env";
// Next and the application's database scripts run from the application directory.
const appDirectory = process.cwd();
const migrations = {
path: resolve(
appDirectory,
"db/migrations",
env.databaseDialect === "libsql" ? "sqlite" : env.databaseDialect,
),
emit: "ts" as const,
snapshot: true,
snapshotName: ".snapshot",
snapshotOnMigrate: false,
};
function resolveDatabaseOptions(): HackKitDatabaseOptions {
switch (env.databaseDialect) {
case "sqlite":
if (
/^[a-z][a-z\d+.-]*:/i.test(env.databaseUrl) &&
!env.databaseUrl.startsWith("file:") &&
env.databaseUrl !== ":memory:"
) {
throw new Error(
"SQLite DATABASE_URL must be a local file path or :memory:.",
);
}
return {
driver: SqliteDriver,
dbName:
env.databaseUrl === ":memory:"
? ":memory:"
: resolve(
appDirectory,
env.databaseUrl.replace(/^file:/, ""),
),
migrations,
};
case "libsql":
return {
driver: LibSqlDriver,
dbName: env.databaseUrl,
password: env.tursoAuthToken,
migrations,
};
case "mysql":
return {
driver: MySqlDriver,
clientUrl: env.databaseUrl,
// MySQL DDL commits implicitly. A failed migration can leave
// partial schema changes even when its history entry is absent.
migrations: {
...migrations,
transactional: false,
allOrNothing: false,
},
};
case "postgresql":
return {
driver: PostgreSqlDriver,
clientUrl: env.databaseUrl,
migrations,
};
}
}
export const databaseOptions = resolveDatabaseOptions();