temp3
This commit is contained in:
@@ -25,11 +25,11 @@ import {
|
|||||||
UploadFeature,
|
UploadFeature,
|
||||||
lexicalEditor,
|
lexicalEditor,
|
||||||
} from '@payloadcms/richtext-lexical'
|
} from '@payloadcms/richtext-lexical'
|
||||||
import { MongoMemoryReplSet } from 'mongodb-memory-server'
|
|
||||||
// import { slateEditor } from '@payloadcms/richtext-slate'
|
// import { slateEditor } from '@payloadcms/richtext-slate'
|
||||||
import { type Config, buildConfig } from 'payload/config'
|
import { type Config, buildConfig } from 'payload/config'
|
||||||
import sharp from 'sharp'
|
import sharp from 'sharp'
|
||||||
|
|
||||||
|
import { getDBAdapter } from './getDBAdapter.js'
|
||||||
import { reInitEndpoint } from './helpers/reInit.js'
|
import { reInitEndpoint } from './helpers/reInit.js'
|
||||||
import { localAPIEndpoint } from './helpers/sdk/endpoint.js'
|
import { localAPIEndpoint } from './helpers/sdk/endpoint.js'
|
||||||
// process.env.PAYLOAD_DATABASE = 'postgres'
|
// process.env.PAYLOAD_DATABASE = 'postgres'
|
||||||
@@ -62,57 +62,14 @@ const databaseAdapters = {
|
|||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
let cached = global._cachedDBAdapter
|
|
||||||
|
|
||||||
if (!cached) {
|
|
||||||
// eslint-disable-next-line no-multi-assign
|
|
||||||
cached = global._cachedDBAdapter = {
|
|
||||||
promise: null,
|
|
||||||
adapter: null,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function buildConfigWithDefaults(
|
export async function buildConfigWithDefaults(
|
||||||
testConfig?: Partial<Config>,
|
testConfig?: Partial<Config>,
|
||||||
): Promise<SanitizedConfig> {
|
): Promise<SanitizedConfig> {
|
||||||
if (!process.env.PAYLOAD_DATABASE || process.env.PAYLOAD_DATABASE === 'mongodb') {
|
const adapter = await getDBAdapter()
|
||||||
if (process.env.JEST_WORKER_ID || process.env.PW_TS_ESM_LOADER_ON) {
|
|
||||||
console.log('Good: Using in-memory MongoDB for tests')
|
|
||||||
if (cached.adapter) {
|
|
||||||
console.log('MDB: Cached')
|
|
||||||
databaseAdapters.mongodb = cached.adapter
|
|
||||||
} else {
|
|
||||||
if (!cached.promise) {
|
|
||||||
console.log('MDB: Creating')
|
|
||||||
cached.promise = MongoMemoryReplSet.create({
|
|
||||||
replSet: {
|
|
||||||
count: 3,
|
|
||||||
dbName: 'payloadmemory',
|
|
||||||
},
|
|
||||||
}).then((server) => {
|
|
||||||
const url = server.getUri()
|
|
||||||
return mongooseAdapter({
|
|
||||||
mongoMemoryServer: server,
|
|
||||||
url,
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
console.log('MDB: Awaiting')
|
|
||||||
|
|
||||||
cached.adapter = await cached.promise
|
|
||||||
cached.promise = null
|
|
||||||
|
|
||||||
databaseAdapters.mongodb = cached.adapter
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
console.log('Bad1!!')
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
console.log('Bad2!!')
|
|
||||||
}
|
|
||||||
|
|
||||||
const config: Config = {
|
const config: Config = {
|
||||||
db: databaseAdapters[process.env.PAYLOAD_DATABASE || 'mongodb'],
|
db: adapter || databaseAdapters[process.env.PAYLOAD_DATABASE || 'mongodb'],
|
||||||
secret: 'TEST_SECRET',
|
secret: 'TEST_SECRET',
|
||||||
//editor: slateEditor({}),
|
//editor: slateEditor({}),
|
||||||
// editor: slateEditor({
|
// editor: slateEditor({
|
||||||
|
|||||||
54
test/getDBAdapter.ts
Normal file
54
test/getDBAdapter.ts
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
import type { DatabaseAdapterObj } from 'payload/database'
|
||||||
|
|
||||||
|
import { mongooseAdapter } from '@payloadcms/db-mongodb'
|
||||||
|
import { MongoMemoryReplSet } from 'mongodb-memory-server'
|
||||||
|
|
||||||
|
let cached: {
|
||||||
|
adapter: DatabaseAdapterObj | null
|
||||||
|
promise: Promise<DatabaseAdapterObj> | null
|
||||||
|
} = global._cachedDBAdapter
|
||||||
|
|
||||||
|
if (!cached) {
|
||||||
|
// eslint-disable-next-line no-multi-assign
|
||||||
|
cached = global._cachedDBAdapter = {
|
||||||
|
promise: null,
|
||||||
|
adapter: null,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getDBAdapter = async (): Promise<DatabaseAdapterObj> => {
|
||||||
|
if (!process.env.PAYLOAD_DATABASE || process.env.PAYLOAD_DATABASE === 'mongodb') {
|
||||||
|
if (process.env.JEST_WORKER_ID || process.env.PW_TS_ESM_LOADER_ON) {
|
||||||
|
console.log('Good: Using in-memory MongoDB for tests')
|
||||||
|
if (cached.adapter) {
|
||||||
|
console.log('MDB: Cached')
|
||||||
|
return cached.adapter
|
||||||
|
}
|
||||||
|
if (!cached.promise) {
|
||||||
|
console.log('MDB: Creating')
|
||||||
|
cached.promise = MongoMemoryReplSet.create({
|
||||||
|
replSet: {
|
||||||
|
count: 3,
|
||||||
|
dbName: 'payloadmemory',
|
||||||
|
},
|
||||||
|
}).then((server) => {
|
||||||
|
const url = server.getUri()
|
||||||
|
return mongooseAdapter({
|
||||||
|
mongoMemoryServer: server,
|
||||||
|
url,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
console.log('MDB: Awaiting')
|
||||||
|
|
||||||
|
cached.adapter = await cached.promise
|
||||||
|
|
||||||
|
return cached.adapter
|
||||||
|
} else {
|
||||||
|
console.log('Bad1!!')
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.log('Bad2!!')
|
||||||
|
}
|
||||||
|
return cached.adapter
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user