mirror of
https://github.com/nowcommunity/nowdatabase.git
synced 2026-10-12 19:13:52 +03:00
Merge pull request #1166 from nowcommunity/fix/908-cross-search-export-transform
Optimize cross-search CSV export
This commit is contained in:
@@ -15,6 +15,8 @@ import { CrossSearch } from '../../../frontend/src/shared/types'
|
||||
import { once } from 'events'
|
||||
|
||||
const router = Router()
|
||||
const lineBreakPattern = /[\r\n]+/
|
||||
const globalLineBreakPattern = /[\r\n]+/g
|
||||
|
||||
const parseArrayRouteParameter = (value: string, message: string) => {
|
||||
const parsedValue = JSON.parse(value) as unknown
|
||||
@@ -49,17 +51,13 @@ const crossSearchLocalitiesRouteValidators: ValidationChain[] = [
|
||||
param('sorting').custom(value => parseArrayRouteParameter(value as string, 'Sorting is not an array.')),
|
||||
]
|
||||
|
||||
const transformFunction = (row: CrossSearch & { full_count?: number }) => {
|
||||
const transformFunction = (row: CrossSearch) => {
|
||||
const transformedRow: { [key: string]: string | number | boolean | null } = {}
|
||||
const keys = Object.keys(row) as Array<keyof (CrossSearch & { full_count?: number })>
|
||||
const keys = Object.keys(row) as Array<keyof CrossSearch>
|
||||
for (const key of keys) {
|
||||
if (key === 'full_count') {
|
||||
delete row['full_count']
|
||||
continue
|
||||
}
|
||||
const value = row[key]
|
||||
if (typeof value === 'string') {
|
||||
transformedRow[key] = value.replace(/[\r\n]+/g, ' ')
|
||||
transformedRow[key] = lineBreakPattern.test(value) ? value.replace(globalLineBreakPattern, ' ') : value
|
||||
} else {
|
||||
transformedRow[key] = row[key]
|
||||
}
|
||||
|
||||
@@ -133,7 +133,8 @@ export const getCrossSearchRawSql = async (
|
||||
offset,
|
||||
convertedColumnFilters,
|
||||
convertedOrderBy,
|
||||
descendingOrder
|
||||
descendingOrder,
|
||||
false
|
||||
)
|
||||
const result: Partial<CrossSearch>[] = await nowDb.$queryRaw(sql)
|
||||
if (result.length === 0) break
|
||||
|
||||
@@ -102,7 +102,8 @@ export const generateCrossSearchSql = (
|
||||
offset: number | undefined,
|
||||
columnFilters: ColumnFilter[],
|
||||
orderBy: string | undefined,
|
||||
descendingOrder: boolean
|
||||
descendingOrder: boolean,
|
||||
includeFullCount = true
|
||||
) => {
|
||||
const whereClause = generateWhereClause(showAll, allowedLocalities, columnFilters)
|
||||
|
||||
@@ -310,8 +311,8 @@ export const generateCrossSearchSql = (
|
||||
now_loc.cultural_stage_3,
|
||||
now_loc.regional_culture_1,
|
||||
now_loc.regional_culture_2,
|
||||
now_loc.regional_culture_3,
|
||||
COUNT(*) OVER() AS full_count
|
||||
now_loc.regional_culture_3
|
||||
${includeFullCount ? Prisma.sql`, COUNT(*) OVER() AS full_count` : Prisma.empty}
|
||||
FROM com_species
|
||||
INNER JOIN now_ls ON com_species.species_id = now_ls.species_id
|
||||
INNER JOIN now_loc ON now_ls.lid = now_loc.lid
|
||||
|
||||
Reference in New Issue
Block a user