完善解决编译时初始化连接问题
This commit is contained in:
@@ -1,20 +1,20 @@
|
||||
import 'dotenv/config'
|
||||
import { drizzle as client, MySql2Database } from 'drizzle-orm/mysql2'
|
||||
import 'server-only'
|
||||
import { drizzle as client, type MySql2Database } from 'drizzle-orm/mysql2'
|
||||
import * as schema from './schema'
|
||||
|
||||
declare global {
|
||||
var drizzle: MySql2Database<typeof schema> | undefined
|
||||
}
|
||||
const globalForDrizzle = globalThis as { drizzle?: MySql2Database<typeof schema> }
|
||||
|
||||
const drizzle = new Proxy({} as MySql2Database<typeof schema>, {
|
||||
const proxy = new Proxy({} as MySql2Database<typeof schema>, {
|
||||
get(_, prop) {
|
||||
if (!global.drizzle && process.env.NODE_ENV !== 'production') {
|
||||
global.drizzle = client(process.env.DATABASE_URL!, { mode: 'default', schema })
|
||||
if (!globalForDrizzle.drizzle) {
|
||||
globalForDrizzle.drizzle = client(process.env.DATABASE_URL!, { mode: 'default', schema })
|
||||
}
|
||||
return global.drizzle![prop as keyof typeof global.drizzle]
|
||||
|
||||
const drizzle = globalForDrizzle.drizzle
|
||||
return drizzle[prop as keyof typeof drizzle]
|
||||
},
|
||||
})
|
||||
|
||||
export default drizzle
|
||||
export default proxy
|
||||
export * from './schema'
|
||||
export * from 'drizzle-orm'
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
import 'server-only'
|
||||
import { createClient } from 'redis'
|
||||
import { createClient, type RedisClientType } from 'redis'
|
||||
|
||||
declare global {
|
||||
var redis: ReturnType<typeof createClient> | undefined
|
||||
const globalForRedis = globalThis as { redis?: RedisClientType }
|
||||
|
||||
if (!globalForRedis.redis) {
|
||||
globalForRedis.redis = createClient({ url: process.env.REDIS_URL })
|
||||
}
|
||||
|
||||
const client = global.redis || createClient({ url: process.env.REDIS_URL })
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
global.redis = client
|
||||
const redis = globalForRedis.redis
|
||||
if (process.env.NODE_ENV === 'production') {
|
||||
await redis.connect()
|
||||
}
|
||||
|
||||
const redis = await client.connect()
|
||||
export default redis
|
||||
|
||||
Reference in New Issue
Block a user