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