Files
payload/test/dev.ts
Sasha 9069bd3fac fix(db-postgres): sort by localized fields (#8839)
### What?
Fixes https://github.com/payloadcms/payload/issues/5152 issue related to
sorting by a localized field with SQLite / Postgres database adapters.

### Why?
It was an incorrect behaviour.


### How?
Modifies the `getTableColumnFromPath` file to have correct join
conditions. Previously if you had this structure in the _locales table
_locale title parent
en          A    1
es          B     1
we sorted by everything that's here, but we need to sort only by the
passed locale.

Additionally fixes a typescript error in `dev.ts` that I added here
https://github.com/payloadcms/payload/pull/8834

Also, removes the condition with `joins.length` in `countDistinct`. It
was there as for this issue
https://github.com/payloadcms/payload/issues/4889 because sorting by a
localized property caused duplication. This can simnifically improve
performance for `.find` with nested querying/sorting on large data sets,
because `count(*)` is faster than `count(DISTINCT id)`
2024-10-24 14:47:58 -04:00

98 lines
2.4 KiB
TypeScript

import chalk from 'chalk'
import { createServer } from 'http'
import minimist from 'minimist'
import nextImport from 'next'
import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import open from 'open'
import { loadEnv } from 'payload/node'
import { parse } from 'url'
import { getNextRootDir } from './helpers/getNextRootDir.js'
import { runInit } from './runInit.js'
import { child, safelyRunScriptFunction } from './safelyRunScript.js'
import { createTestHooks } from './testHooks.js'
const prod = process.argv.includes('--prod')
process.argv = process.argv.filter((arg) => arg !== '--prod')
if (prod) {
process.env.PAYLOAD_TEST_PROD = 'true'
}
loadEnv()
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
const {
_: [testSuiteArg],
...args
} = minimist(process.argv.slice(2))
if (!fs.existsSync(path.resolve(dirname, testSuiteArg))) {
console.log(chalk.red(`ERROR: The test folder "${testSuiteArg}" does not exist`))
process.exit(0)
}
if (args.turbo === true) {
process.env.TURBOPACK = '1'
}
const { beforeTest } = await createTestHooks(testSuiteArg)
await beforeTest()
const { rootDir, adminRoute } = getNextRootDir(testSuiteArg)
await safelyRunScriptFunction(runInit, 4000, testSuiteArg, true)
// Open the admin if the -o flag is passed
if (args.o) {
await open(`http://localhost:3000${adminRoute}`)
}
const port = process.env.PORT ? Number(process.env.PORT) : 3000
// @ts-expect-error the same as in test/helpers/initPayloadE2E.ts
const app = nextImport({
dev: true,
hostname: 'localhost',
port,
dir: rootDir,
})
const handle = app.getRequestHandler()
let resolveServer
const serverPromise = new Promise((res) => (resolveServer = res))
void app.prepare().then(() => {
createServer(async (req, res) => {
const parsedUrl = parse(req.url, true)
await handle(req, res, parsedUrl)
}).listen(port, () => {
resolveServer()
})
})
await serverPromise
process.env.PAYLOAD_DROP_DATABASE = process.env.PAYLOAD_DROP_DATABASE === 'false' ? 'false' : 'true'
// fetch the admin url to force a render
void fetch(`http://localhost:${port}${adminRoute}`)
// This ensures that the next-server process is killed when this process is killed and doesn't linger around.
process.on('SIGINT', () => {
if (child) {
child.kill('SIGINT')
}
process.exit(0)
})
process.on('SIGTERM', () => {
if (child) {
child.kill('SIGINT')
}
process.exit(0) // Exit the parent process
})