Skip to content

Commit 88a00af

Browse files
committed
refactor: using postgres.js
1 parent 54b2843 commit 88a00af

File tree

8 files changed

+24
-21
lines changed

8 files changed

+24
-21
lines changed

apps/backend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"nanoid": "^5.0.9",
5959
"nodemailer": "^6.9.16",
6060
"oslo": "^1.2.1",
61-
"pg": "^8.13.1",
61+
"postgres": "^3.4.5",
6262
"radash": "^12.1.0",
6363
"reflect-metadata": "^0.2.2",
6464
"uuid": "^11.0.5",

apps/backend/src/modules/auth/auth.provider.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import { BunSQLiteAdapter, LibSQLAdapter } from "@lucia-auth/adapter-sqlite"
44
import { container, inject, instanceCachingFactory } from "@undb/di"
55
import { Client, DATABASE_CLIENT, DB_PROVIDER, pgSessionTable, pgUsers } from "@undb/persistence/server"
66
import Database from "bun:sqlite"
7-
import { drizzle as drizzlePg } from "drizzle-orm/node-postgres"
7+
import { drizzle as drizzlePg } from "drizzle-orm/postgres-js"
88
import { Adapter, Lucia } from "lucia"
99
import mysql from "mysql2/promise"
10-
import pg from "pg"
10+
import postgres from "postgres"
1111

1212
export const LUCIA_PROVIDER = Symbol.for("LUCIA_PROVIDER")
1313
export const injectLucia = () => inject(LUCIA_PROVIDER)
@@ -57,7 +57,7 @@ const createSqliteLucia = (sqlite: Database) => {
5757
return createLuciaWithAdapter(adapter)
5858
}
5959

60-
const createPostgresLucia = (pool: pg.Pool) => {
60+
const createPostgresLucia = (pool: postgres.Sql) => {
6161
const db = drizzlePg(pool)
6262

6363
const adapter = new DrizzlePostgreSQLAdapter(db, pgSessionTable, pgUsers)
@@ -98,7 +98,7 @@ container.register(LUCIA_PROVIDER, {
9898
const sqlite = c.resolve<Database>(DATABASE_CLIENT)
9999
return createSqliteLucia(sqlite)
100100
} else if (dbProvider === "postgres") {
101-
const pool = c.resolve<pg.Pool>(DATABASE_CLIENT)
101+
const pool = c.resolve<postgres.Sql>(DATABASE_CLIENT)
102102
return createPostgresLucia(pool)
103103
} else if (dbProvider === "mysql") {
104104
const connection = c.resolve<mysql.Pool>(DATABASE_CLIENT)

apps/backend/src/registry/db.registry.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ import { WEBHOOK_QUERY_REPOSITORY, WEBHOOK_REPOSITORY } from "@undb/webhook"
7777
import Database from "bun:sqlite"
7878
import mysql from "mysql2/promise"
7979
import { AsyncLocalStorage } from "node:async_hooks"
80-
import pg from "pg"
80+
import postgres from "postgres"
8181

8282
const txContext = new AsyncLocalStorage<TxContext>()
8383

@@ -106,7 +106,7 @@ export const registerDb = () => {
106106
const sqlite = c.resolve<Database>(DATABASE_CLIENT)
107107
return createSqliteQueryBuilder(sqlite)
108108
} else if (dbProvider === "postgres") {
109-
const pg = c.resolve<pg.Pool>(DATABASE_CLIENT)
109+
const pg = c.resolve<postgres.Sql>(DATABASE_CLIENT)
110110
return createPostgresQueryBuilder(pg)
111111
} else if (dbProvider === "mysql") {
112112
const mysql = c.resolve<mysql.Pool>(DATABASE_CLIENT)

bun.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"nodemailer": "^6.9.16",
6868
"oslo": "^1.2.1",
6969
"pg": "^8.13.1",
70+
"postgres": "^3.4.5",
7071
"radash": "^12.1.0",
7172
"reflect-metadata": "^0.2.2",
7273
"uuid": "^11.0.5",
@@ -550,6 +551,7 @@
550551
"es-toolkit": "^1.31.0",
551552
"kysely": "^0.27.5",
552553
"kysely-bun-sqlite": "^0.3.2",
554+
"kysely-postgres-js": "^2.0.0",
553555
"kysely-wasm": "^0.7.0",
554556
"mysql2": "^3.12.0",
555557
"pg": "^8.13.1",
@@ -2389,6 +2391,8 @@
23892391

23902392
"kysely-bun-sqlite": ["[email protected]", "https://registry.npmmirror.com/kysely-bun-sqlite/-/kysely-bun-sqlite-0.3.2.tgz", { "dependencies": { "bun-types": "^1.0.25" }, "peerDependencies": { "kysely": "^0.27.2" } }, "sha512-YucMcOGGxNCmlAnkvNfTKZX6Bk1sYsuVVXlQN/5ZUgerlq/J9/EuR3aMOOZ25ASLM7oMglSxfeQxkiw0+hrEOQ=="],
23912393

2394+
"kysely-postgres-js": ["[email protected]", "https://registry.npmmirror.com/kysely-postgres-js/-/kysely-postgres-js-2.0.0.tgz", { "peerDependencies": { "kysely": ">= 0.24.0 < 1", "postgres": ">= 3.4.0 < 4" } }, "sha512-R1tWx6/x3tSatWvsmbHJxpBZYhNNxcnMw52QzZaHKg7ZOWtHib4iZyEaw4gb2hNKVctWQ3jfMxZT/ZaEMK6kBQ=="],
2395+
23922396
"kysely-wasm": ["[email protected]", "https://registry.npmmirror.com/kysely-wasm/-/kysely-wasm-0.7.0.tgz", { "peerDependencies": { "kysely": ">=0.26" } }, "sha512-SUzSCJdY4AioyivtcjJBXXnXE2Y3Tdsv96sJYfey6KlFPNVf7JrUl7SiI8gimZJqFLtdvmzjPJwevc5ReLSHjQ=="],
23932397

23942398
"levn": ["[email protected]", "https://registry.npmmirror.com/levn/-/levn-0.4.1.tgz", { "dependencies": { "prelude-ls": "^1.2.1", "type-check": "~0.4.0" } }, "sha512-+bT2uH4E5LGE7h/n3evcS/sQlJXCpIp6ym8OWJ5eV6+67Dsql/LaaT7qJBAt2rzfoa/5QBGBhxDix1dMt2kQKQ=="],

packages/persistence/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
"es-toolkit": "^1.31.0",
4343
"kysely": "^0.27.5",
4444
"kysely-bun-sqlite": "^0.3.2",
45+
"kysely-postgres-js": "^2.0.0",
4546
"kysely-wasm": "^0.7.0",
46-
"mysql2": "^3.12.0",
4747
"pg": "^8.13.1",
4848
"postgres": "^3.4.5",
4949
"radash": "^12.1.0",

packages/persistence/src/db-client.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { createClient } from "@libsql/client"
22
import { inject } from "@undb/di"
33
import Database from "bun:sqlite"
44
import mysql from "mysql2/promise"
5-
import pg from "pg"
5+
import postgres from "postgres"
66

77
export const DATABASE_CLIENT = Symbol.for("DATABASE_CLIENT")
88

@@ -17,9 +17,7 @@ export const createSqliteClient = (fileName: string): Database => {
1717
}
1818

1919
export const createPostgresClient = (connectionString: string) => {
20-
return new pg.Pool({
21-
connectionString,
22-
})
20+
return postgres(connectionString)
2321
}
2422

2523
export const createMysqlClient = (connectionString: string): mysql.Pool => {

packages/persistence/src/migrate.server.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import { drizzle as libsqlDrizzle } from "drizzle-orm/libsql"
77
import { migrate as libsqlMigrate } from "drizzle-orm/libsql/migrator"
88
import { drizzle as mysqlDrizzle } from "drizzle-orm/mysql2"
99
import { migrate as mysqlMigrate } from "drizzle-orm/mysql2/migrator"
10-
import { drizzle as postgresDrizzle } from "drizzle-orm/node-postgres"
11-
import { migrate as postgresMigrate } from "drizzle-orm/node-postgres/migrator"
10+
import { drizzle as postgresDrizzle } from "drizzle-orm/postgres-js"
11+
import { migrate as postgresMigrate } from "drizzle-orm/postgres-js/migrator"
1212
import mysql from "mysql2/promise"
13-
import pg from "pg"
13+
import postgres from "postgres"
1414
import { DATABASE_CLIENT } from "./db-client"
1515
import { DrizzleLogger } from "./db.logger"
1616
import { DB_PROVIDER } from "./db.provider"
@@ -28,7 +28,7 @@ export async function dbMigrate() {
2828
sqliteMigrate(db, { migrationsFolder: "./drizzle/sqlite" })
2929
return
3030
} else if (dbProvider === "postgres") {
31-
const pg = container.resolve<pg.Pool>(DATABASE_CLIENT)
31+
const pg = container.resolve<postgres.Sql>(DATABASE_CLIENT)
3232

3333
const db = postgresDrizzle(pg, {
3434
logger,

packages/persistence/src/qb.server.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import type { Client } from "@libsql/client"
22
import { LibsqlDialect } from "@libsql/kysely-libsql"
33
import { Database as SqliteDatabase } from "bun:sqlite"
4-
import { MysqlDialect, PostgresDialect } from "kysely"
4+
import { MysqlDialect } from "kysely"
55
import { BunSqliteDialect } from "kysely-bun-sqlite"
6+
import { PostgresJSDialect } from "kysely-postgres-js"
67
import mysql from "mysql2/promise"
7-
import pg from "pg"
8+
import postgres from "postgres"
89
import { createQueryBuilderWithDialect } from "./qb.util"
910

1011
export function createTursoQueryBuilder(client: Client) {
@@ -23,10 +24,10 @@ export function createSqliteQueryBuilder(sqlite: SqliteDatabase) {
2324
)
2425
}
2526

26-
export function createPostgresQueryBuilder(pg: pg.Pool) {
27+
export function createPostgresQueryBuilder(pg: postgres.Sql) {
2728
return createQueryBuilderWithDialect(
28-
new PostgresDialect({
29-
pool: pg,
29+
new PostgresJSDialect({
30+
postgres: pg,
3031
}),
3132
)
3233
}

0 commit comments

Comments
 (0)