Skip to content

Commit

Permalink
refactor: using postgres.js
Browse files Browse the repository at this point in the history
  • Loading branch information
nichenqin committed Jan 24, 2025
1 parent 54b2843 commit 88a00af
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 21 deletions.
2 changes: 1 addition & 1 deletion apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"nanoid": "^5.0.9",
"nodemailer": "^6.9.16",
"oslo": "^1.2.1",
"pg": "^8.13.1",
"postgres": "^3.4.5",
"radash": "^12.1.0",
"reflect-metadata": "^0.2.2",
"uuid": "^11.0.5",
Expand Down
8 changes: 4 additions & 4 deletions apps/backend/src/modules/auth/auth.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { BunSQLiteAdapter, LibSQLAdapter } from "@lucia-auth/adapter-sqlite"
import { container, inject, instanceCachingFactory } from "@undb/di"
import { Client, DATABASE_CLIENT, DB_PROVIDER, pgSessionTable, pgUsers } from "@undb/persistence/server"
import Database from "bun:sqlite"
import { drizzle as drizzlePg } from "drizzle-orm/node-postgres"
import { drizzle as drizzlePg } from "drizzle-orm/postgres-js"
import { Adapter, Lucia } from "lucia"
import mysql from "mysql2/promise"
import pg from "pg"
import postgres from "postgres"

export const LUCIA_PROVIDER = Symbol.for("LUCIA_PROVIDER")
export const injectLucia = () => inject(LUCIA_PROVIDER)
Expand Down Expand Up @@ -57,7 +57,7 @@ const createSqliteLucia = (sqlite: Database) => {
return createLuciaWithAdapter(adapter)
}

const createPostgresLucia = (pool: pg.Pool) => {
const createPostgresLucia = (pool: postgres.Sql) => {
const db = drizzlePg(pool)

const adapter = new DrizzlePostgreSQLAdapter(db, pgSessionTable, pgUsers)
Expand Down Expand Up @@ -98,7 +98,7 @@ container.register(LUCIA_PROVIDER, {
const sqlite = c.resolve<Database>(DATABASE_CLIENT)
return createSqliteLucia(sqlite)
} else if (dbProvider === "postgres") {
const pool = c.resolve<pg.Pool>(DATABASE_CLIENT)
const pool = c.resolve<postgres.Sql>(DATABASE_CLIENT)
return createPostgresLucia(pool)
} else if (dbProvider === "mysql") {
const connection = c.resolve<mysql.Pool>(DATABASE_CLIENT)
Expand Down
4 changes: 2 additions & 2 deletions apps/backend/src/registry/db.registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ import { WEBHOOK_QUERY_REPOSITORY, WEBHOOK_REPOSITORY } from "@undb/webhook"
import Database from "bun:sqlite"
import mysql from "mysql2/promise"
import { AsyncLocalStorage } from "node:async_hooks"
import pg from "pg"
import postgres from "postgres"

const txContext = new AsyncLocalStorage<TxContext>()

Expand Down Expand Up @@ -106,7 +106,7 @@ export const registerDb = () => {
const sqlite = c.resolve<Database>(DATABASE_CLIENT)
return createSqliteQueryBuilder(sqlite)
} else if (dbProvider === "postgres") {
const pg = c.resolve<pg.Pool>(DATABASE_CLIENT)
const pg = c.resolve<postgres.Sql>(DATABASE_CLIENT)
return createPostgresQueryBuilder(pg)
} else if (dbProvider === "mysql") {
const mysql = c.resolve<mysql.Pool>(DATABASE_CLIENT)
Expand Down
4 changes: 4 additions & 0 deletions bun.lock
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"nodemailer": "^6.9.16",
"oslo": "^1.2.1",
"pg": "^8.13.1",
"postgres": "^3.4.5",
"radash": "^12.1.0",
"reflect-metadata": "^0.2.2",
"uuid": "^11.0.5",
Expand Down Expand Up @@ -550,6 +551,7 @@
"es-toolkit": "^1.31.0",
"kysely": "^0.27.5",
"kysely-bun-sqlite": "^0.3.2",
"kysely-postgres-js": "^2.0.0",
"kysely-wasm": "^0.7.0",
"mysql2": "^3.12.0",
"pg": "^8.13.1",
Expand Down Expand Up @@ -2389,6 +2391,8 @@

"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=="],

"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=="],

"kysely-wasm": ["[email protected]", "https://registry.npmmirror.com/kysely-wasm/-/kysely-wasm-0.7.0.tgz", { "peerDependencies": { "kysely": ">=0.26" } }, "sha512-SUzSCJdY4AioyivtcjJBXXnXE2Y3Tdsv96sJYfey6KlFPNVf7JrUl7SiI8gimZJqFLtdvmzjPJwevc5ReLSHjQ=="],

"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=="],
Expand Down
2 changes: 1 addition & 1 deletion packages/persistence/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
"es-toolkit": "^1.31.0",
"kysely": "^0.27.5",
"kysely-bun-sqlite": "^0.3.2",
"kysely-postgres-js": "^2.0.0",
"kysely-wasm": "^0.7.0",
"mysql2": "^3.12.0",
"pg": "^8.13.1",
"postgres": "^3.4.5",
"radash": "^12.1.0",
Expand Down
6 changes: 2 additions & 4 deletions packages/persistence/src/db-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createClient } from "@libsql/client"
import { inject } from "@undb/di"
import Database from "bun:sqlite"
import mysql from "mysql2/promise"
import pg from "pg"
import postgres from "postgres"

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

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

export const createPostgresClient = (connectionString: string) => {
return new pg.Pool({
connectionString,
})
return postgres(connectionString)
}

export const createMysqlClient = (connectionString: string): mysql.Pool => {
Expand Down
8 changes: 4 additions & 4 deletions packages/persistence/src/migrate.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import { drizzle as libsqlDrizzle } from "drizzle-orm/libsql"
import { migrate as libsqlMigrate } from "drizzle-orm/libsql/migrator"
import { drizzle as mysqlDrizzle } from "drizzle-orm/mysql2"
import { migrate as mysqlMigrate } from "drizzle-orm/mysql2/migrator"
import { drizzle as postgresDrizzle } from "drizzle-orm/node-postgres"
import { migrate as postgresMigrate } from "drizzle-orm/node-postgres/migrator"
import { drizzle as postgresDrizzle } from "drizzle-orm/postgres-js"
import { migrate as postgresMigrate } from "drizzle-orm/postgres-js/migrator"
import mysql from "mysql2/promise"
import pg from "pg"
import postgres from "postgres"
import { DATABASE_CLIENT } from "./db-client"
import { DrizzleLogger } from "./db.logger"
import { DB_PROVIDER } from "./db.provider"
Expand All @@ -28,7 +28,7 @@ export async function dbMigrate() {
sqliteMigrate(db, { migrationsFolder: "./drizzle/sqlite" })
return
} else if (dbProvider === "postgres") {
const pg = container.resolve<pg.Pool>(DATABASE_CLIENT)
const pg = container.resolve<postgres.Sql>(DATABASE_CLIENT)

const db = postgresDrizzle(pg, {
logger,
Expand Down
11 changes: 6 additions & 5 deletions packages/persistence/src/qb.server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { Client } from "@libsql/client"
import { LibsqlDialect } from "@libsql/kysely-libsql"
import { Database as SqliteDatabase } from "bun:sqlite"
import { MysqlDialect, PostgresDialect } from "kysely"
import { MysqlDialect } from "kysely"
import { BunSqliteDialect } from "kysely-bun-sqlite"
import { PostgresJSDialect } from "kysely-postgres-js"
import mysql from "mysql2/promise"
import pg from "pg"
import postgres from "postgres"
import { createQueryBuilderWithDialect } from "./qb.util"

export function createTursoQueryBuilder(client: Client) {
Expand All @@ -23,10 +24,10 @@ export function createSqliteQueryBuilder(sqlite: SqliteDatabase) {
)
}

export function createPostgresQueryBuilder(pg: pg.Pool) {
export function createPostgresQueryBuilder(pg: postgres.Sql) {
return createQueryBuilderWithDialect(
new PostgresDialect({
pool: pg,
new PostgresJSDialect({
postgres: pg,
}),
)
}
Expand Down

0 comments on commit 88a00af

Please sign in to comment.