-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
db.ts
49 lines (44 loc) · 1.38 KB
/
db.ts
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
import * as Pg from "@effect/sql-pg"
import * as Migrator from "@effect/sql-pg/Migrator"
import { Layer } from "effect"
import { Kysely, PostgresDialect } from "kysely"
import PG from "pg"
import { makePg } from "./kysely"
import type * as DbSchema from "./schema.js"
export interface Database {
users: DbSchema.DbUserEncoded
articles: DbSchema.DbArticleEncoded
comments: DbSchema.DbCommentEncoded
tags: DbSchema.DbTagEncoded
article_tags: DbSchema.DbArticleTagEncoded
favorites: DbSchema.DbFavoriteEncoded
follows: DbSchema.DbFollowEncoded
jwt_tokens: DbSchema.DbJwtTokenEncoded
}
export const RealworldDb = makePg<Database>()
declare global {
interface ImportMetaEnv {
VITE_DATABASE_NAME: string
VITE_DATABASE_HOST: string
VITE_DATABASE_USER: string
VITE_DATABASE_PASSWORD: string
VITE_DATABASE_PORT: string
}
}
const PgLive = RealworldDb.make(() =>
new Kysely({
dialect: new PostgresDialect({
pool: new PG.Pool({
database: import.meta.env.VITE_DATABASE_NAME,
host: import.meta.env.VITE_DATABASE_HOST,
user: import.meta.env.VITE_DATABASE_USER,
password: import.meta.env.VITE_DATABASE_PASSWORD,
port: parseInt(import.meta.env.VITE_DATABASE_PORT, 10)
})
})
})
)
export const DbLive = Layer.provideMerge(
Pg.migrator.layer({ loader: Migrator.fromGlob(import.meta.glob("./migrations/*")) }),
PgLive
)