chore: finishes pattern for server hmr

This commit is contained in:
James
2024-03-05 15:18:36 -05:00
parent 2576291d9f
commit b0edd2d137
6 changed files with 41 additions and 27 deletions

View File

@@ -165,8 +165,6 @@
"dotenv": "$dotenv",
"drizzle-orm": "$drizzle-orm",
"graphql": "^16.8.1",
"pino": "8.15.0",
"pino-pretty": "10.2.0",
"react": "$react",
"react-dom": "$react-dom",
"ts-node": "$ts-node",

View File

@@ -46,7 +46,14 @@ const connectWithReconnect = async function ({
})
}
export const connect: Connect = async function connect(this: PostgresAdapter, payload) {
export const connect: Connect = async function connect(
this: PostgresAdapter,
options = {
hotReload: false,
},
) {
const { hotReload } = options
this.schema = {
...this.tables,
...this.relations,
@@ -55,23 +62,26 @@ export const connect: Connect = async function connect(this: PostgresAdapter, pa
try {
this.pool = new Pool(this.poolOptions)
await connectWithReconnect({ adapter: this, payload })
await connectWithReconnect({ adapter: this, payload: this.payload })
const logger = this.logger || false
this.drizzle = drizzle(this.pool, { logger, schema: this.schema })
if (process.env.PAYLOAD_DROP_DATABASE === 'true') {
this.payload.logger.info(`---- DROPPING TABLES SCHEMA(${this.schemaName || 'public'}) ----`)
await this.drizzle.execute(
sql.raw(`
drop schema if exists ${this.schemaName || 'public'} cascade;
create schema ${this.schemaName || 'public'};
`),
)
this.payload.logger.info('---- DROPPED TABLES ----')
if (!hotReload) {
if (process.env.PAYLOAD_DROP_DATABASE === 'true') {
this.payload.logger.info(`---- DROPPING TABLES SCHEMA(${this.schemaName || 'public'}) ----`)
await this.drizzle.execute(
sql.raw(`
drop schema if exists ${this.schemaName || 'public'} cascade;
create schema ${this.schemaName || 'public'};
`),
)
this.payload.logger.info('---- DROPPED TABLES ----')
}
}
} catch (err) {
payload.logger.error(`Error: cannot connect to Postgres. Details: ${err.message}`, err)
this.payload.logger.error(`Error: cannot connect to Postgres. Details: ${err.message}`, err)
process.exit(1)
}

View File

@@ -20,6 +20,7 @@ const withPayload = (nextConfig = {}) => {
'libsql',
'pino',
'pino-pretty',
'payload',
],
},
webpack: (webpackConfig, webpackOptions) => {
@@ -34,8 +35,8 @@ const withPayload = (nextConfig = {}) => {
...(incomingWebpackConfig?.externals || []),
'drizzle-kit',
'drizzle-kit/payload',
// 'pino',
// 'pino-pretty',
'pino',
'pino-pretty',
'sharp',
'libsql',
],

View File

@@ -26,14 +26,19 @@ const getLogger = (
name = 'payload',
options?: pino.LoggerOptions,
destination?: pino.DestinationStream,
): PayloadLogger =>
pino(
{
name: options?.name || name,
enabled: process.env.DISABLE_LOGGING !== 'true',
...(options || defaultLoggerOptions),
},
destination,
)
): PayloadLogger => {
if (options) {
return pino(
{
name: options?.name || name,
enabled: process.env.DISABLE_LOGGING !== 'true',
...(options || defaultLoggerOptions),
},
destination,
)
}
return pino(prettySyncLoggerDestination)
}
export default getLogger

View File

@@ -1,5 +1,5 @@
'use client'
import type { I18n, LanguageTranslations } from '@payloadcms/translations'
import type { I18n, LanguageTranslations, Translations } from '@payloadcms/translations'
import type { ClientConfig } from 'payload/types'
import { t } from '@payloadcms/translations'
@@ -46,7 +46,7 @@ export const TranslationProvider: React.FC<{
fallbackLanguage: fallbackLang,
language: lang,
t: nextT,
translations,
translations: translations as Translations,
},
languageOptions,
t: nextT,