Compare commits

...

1 Commits

Author SHA1 Message Date
Sasha
0865715bc7 chore: support HMR for payload.db 2024-11-16 22:49:12 +02:00

View File

@@ -739,6 +739,12 @@ if (!cached) {
cached = global._payload = { payload: null, promise: null, reload: false, ws: null }
}
// Properties of payload.db that shouldn't be re-initalized even on HMR reload
const cachedDbProperties: Record<string, string[]> = {
postgres: ['pool'],
sqlite: ['client'],
}
export const reload = async (
config: SanitizedConfig,
payload: Payload,
@@ -764,6 +770,22 @@ export const reload = async (
// TODO: support HMR for other props in the future (see payload/src/index init()) that may change on Payload singleton
const dbCache: Record<string, unknown> = cachedDbProperties[payload.db.name]?.reduce(
(acc, property) => {
acc[property] = payload.db[property]
return acc
},
{},
)
payload.db = payload.config.db.init({ payload })
if (dbCache) {
for (const property in dbCache) {
payload.db[property] = dbCache[property]
}
}
// Generate types
if (config.typescript.autoGenerate !== false) {
// We cannot run it directly here, as generate-types imports json-schema-to-typescript, which breaks on turbopack.