Compare commits
1 Commits
fix/plugin
...
ts-strict3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e057caa886 |
@@ -30,7 +30,7 @@ export const useLivePreview = <T>(props: {
|
||||
let subscription: (event: MessageEvent) => void
|
||||
|
||||
onMounted(() => {
|
||||
subscription = subscribe({
|
||||
subscription = void subscribe({
|
||||
apiRoute,
|
||||
callback: onChange,
|
||||
depth,
|
||||
|
||||
@@ -87,7 +87,7 @@ export const createExport = async (args: CreateExportArgs) => {
|
||||
let isFirstBatch = true
|
||||
|
||||
while (result.docs.length > 0) {
|
||||
const csvInput = result.docs.map((doc) => flattenObject({ doc, fields }))
|
||||
const csvInput = result.docs.map((doc) => flattenObject(doc))
|
||||
const csvString = stringify(csvInput, { header: isFirstBatch })
|
||||
this.push(encoder.encode(csvString))
|
||||
isFirstBatch = false
|
||||
@@ -119,7 +119,7 @@ export const createExport = async (args: CreateExportArgs) => {
|
||||
result = await payload.find(findArgs)
|
||||
|
||||
if (isCSV) {
|
||||
const csvInput = result.docs.map((doc) => flattenObject({ doc, fields }))
|
||||
const csvInput = result.docs.map((doc) => flattenObject(doc))
|
||||
outputData.push(stringify(csvInput, { header: isFirstBatch }))
|
||||
isFirstBatch = false
|
||||
} else {
|
||||
|
||||
@@ -1,61 +1,23 @@
|
||||
import type { Document } from 'payload'
|
||||
|
||||
type Args = {
|
||||
doc: Document
|
||||
fields?: string[]
|
||||
prefix?: string
|
||||
}
|
||||
|
||||
export const flattenObject = ({ doc, fields, prefix }: Args): Record<string, unknown> => {
|
||||
export const flattenObject = (obj: any, prefix: string = ''): Record<string, unknown> => {
|
||||
const result: Record<string, unknown> = {}
|
||||
|
||||
const flatten = (doc: Document, prefix?: string) => {
|
||||
Object.entries(doc).forEach(([key, value]) => {
|
||||
const newKey = prefix ? `${prefix}_${key}` : key
|
||||
Object.entries(obj).forEach(([key, value]) => {
|
||||
const newKey = prefix ? `${prefix}_${key}` : key
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
value.forEach((item, index) => {
|
||||
if (typeof item === 'object' && item !== null) {
|
||||
flatten(item, `${newKey}_${index}`)
|
||||
} else {
|
||||
result[`${newKey}_${index}`] = item
|
||||
}
|
||||
})
|
||||
} else if (typeof value === 'object' && value !== null) {
|
||||
flatten(value, newKey)
|
||||
} else {
|
||||
result[newKey] = value
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
flatten(doc, prefix)
|
||||
|
||||
if (fields) {
|
||||
const orderedResult: Record<string, unknown> = {}
|
||||
|
||||
const fieldToRegex = (field: string): RegExp => {
|
||||
const parts = field.split('.').map((part) => `${part}(?:_\\d+)?`)
|
||||
const pattern = `^${parts.join('_')}`
|
||||
return new RegExp(pattern)
|
||||
if (Array.isArray(value)) {
|
||||
value.forEach((item, index) => {
|
||||
if (typeof item === 'object' && item !== null) {
|
||||
Object.assign(result, flattenObject(item, `${newKey}_${index}`))
|
||||
} else {
|
||||
result[`${newKey}_${index}`] = item
|
||||
}
|
||||
})
|
||||
} else if (typeof value === 'object' && value !== null) {
|
||||
Object.assign(result, flattenObject(value, newKey))
|
||||
} else {
|
||||
result[newKey] = value
|
||||
}
|
||||
|
||||
fields.forEach((field) => {
|
||||
if (result[field.replace(/\./g, '_')]) {
|
||||
const sanitizedField = field.replace(/\./g, '_')
|
||||
orderedResult[sanitizedField] = result[sanitizedField]
|
||||
} else {
|
||||
const regex = fieldToRegex(field)
|
||||
Object.keys(result).forEach((key) => {
|
||||
if (regex.test(key)) {
|
||||
orderedResult[key] = result[key]
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
return orderedResult
|
||||
}
|
||||
})
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -98,19 +98,6 @@ export const Pages: CollectionConfig = {
|
||||
type: 'relationship',
|
||||
relationTo: 'users',
|
||||
},
|
||||
{
|
||||
name: 'virtualRelationship',
|
||||
type: 'text',
|
||||
virtual: 'author.name',
|
||||
},
|
||||
{
|
||||
name: 'virtual',
|
||||
type: 'text',
|
||||
virtual: true,
|
||||
hooks: {
|
||||
afterRead: [() => 'virtual value'],
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'hasManyNumber',
|
||||
type: 'number',
|
||||
|
||||
@@ -10,10 +10,6 @@ export const Users: CollectionConfig = {
|
||||
read: () => true,
|
||||
},
|
||||
fields: [
|
||||
{
|
||||
name: 'name',
|
||||
type: 'text',
|
||||
},
|
||||
// Email added by default
|
||||
// Add more fields as needed
|
||||
],
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import type { CollectionSlug, Payload } from 'payload'
|
||||
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import { fileURLToPath } from 'url'
|
||||
|
||||
@@ -222,68 +221,6 @@ describe('@payloadcms/plugin-import-export', () => {
|
||||
expect(data[0].array_1_field2).toStrictEqual('baz')
|
||||
})
|
||||
|
||||
it('should create a CSV file with columns matching the order of the fields array', async () => {
|
||||
const fields = ['id', 'group.value', 'group.array.field1', 'title', 'createdAt', 'updatedAt']
|
||||
const doc = await payload.create({
|
||||
collection: 'exports',
|
||||
user,
|
||||
data: {
|
||||
collectionSlug: 'pages',
|
||||
fields,
|
||||
format: 'csv',
|
||||
where: {
|
||||
title: { contains: 'Title ' },
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const exportDoc = await payload.findByID({
|
||||
collection: 'exports',
|
||||
id: doc.id,
|
||||
})
|
||||
|
||||
expect(exportDoc.filename).toBeDefined()
|
||||
const expectedPath = path.join(dirname, './uploads', exportDoc.filename as string)
|
||||
const buffer = fs.readFileSync(expectedPath)
|
||||
const str = buffer.toString()
|
||||
|
||||
// Assert that the header row matches the fields array
|
||||
expect(str.indexOf('id')).toBeLessThan(str.indexOf('title'))
|
||||
expect(str.indexOf('group_value')).toBeLessThan(str.indexOf('title'))
|
||||
expect(str.indexOf('group_value')).toBeLessThan(str.indexOf('group_array'))
|
||||
expect(str.indexOf('title')).toBeLessThan(str.indexOf('createdAt'))
|
||||
expect(str.indexOf('createdAt')).toBeLessThan(str.indexOf('updatedAt'))
|
||||
})
|
||||
|
||||
it('should create a CSV file with virtual fields', async () => {
|
||||
const fields = ['id', 'virtual', 'virtualRelationship']
|
||||
const doc = await payload.create({
|
||||
collection: 'exports',
|
||||
user,
|
||||
data: {
|
||||
collectionSlug: 'pages',
|
||||
fields,
|
||||
format: 'csv',
|
||||
where: {
|
||||
title: { contains: 'Virtual ' },
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const exportDoc = await payload.findByID({
|
||||
collection: 'exports',
|
||||
id: doc.id,
|
||||
})
|
||||
|
||||
expect(exportDoc.filename).toBeDefined()
|
||||
const expectedPath = path.join(dirname, './uploads', exportDoc.filename as string)
|
||||
const data = await readCSV(expectedPath)
|
||||
|
||||
// Assert that the csv file contains the expected virtual fields
|
||||
expect(data[0].virtual).toStrictEqual('virtual value')
|
||||
expect(data[0].virtualRelationship).toStrictEqual('name value')
|
||||
})
|
||||
|
||||
it('should create a file for collection csv from array.subfield', async () => {
|
||||
let doc = await payload.create({
|
||||
collection: 'exports',
|
||||
|
||||
@@ -131,7 +131,6 @@ export interface UserAuthOperations {
|
||||
*/
|
||||
export interface User {
|
||||
id: string;
|
||||
name?: string | null;
|
||||
updatedAt: string;
|
||||
createdAt: string;
|
||||
email: string;
|
||||
@@ -200,8 +199,6 @@ export interface Page {
|
||||
)[]
|
||||
| null;
|
||||
author?: (string | null) | User;
|
||||
virtualRelationship?: string | null;
|
||||
virtual?: string | null;
|
||||
hasManyNumber?: number[] | null;
|
||||
relationship?: (string | null) | User;
|
||||
excerpt?: string | null;
|
||||
@@ -447,7 +444,6 @@ export interface PayloadMigration {
|
||||
* via the `definition` "users_select".
|
||||
*/
|
||||
export interface UsersSelect<T extends boolean = true> {
|
||||
name?: T;
|
||||
updatedAt?: T;
|
||||
createdAt?: T;
|
||||
email?: T;
|
||||
@@ -504,8 +500,6 @@ export interface PagesSelect<T extends boolean = true> {
|
||||
};
|
||||
};
|
||||
author?: T;
|
||||
virtualRelationship?: T;
|
||||
virtual?: T;
|
||||
hasManyNumber?: T;
|
||||
relationship?: T;
|
||||
excerpt?: T;
|
||||
|
||||
@@ -6,12 +6,11 @@ import { richTextData } from './richTextData.js'
|
||||
export const seed = async (payload: Payload): Promise<boolean> => {
|
||||
payload.logger.info('Seeding data...')
|
||||
try {
|
||||
const user = await payload.create({
|
||||
await payload.create({
|
||||
collection: 'users',
|
||||
data: {
|
||||
email: devUser.email,
|
||||
password: devUser.password,
|
||||
name: 'name value',
|
||||
},
|
||||
})
|
||||
// create pages
|
||||
@@ -81,16 +80,6 @@ export const seed = async (payload: Payload): Promise<boolean> => {
|
||||
})
|
||||
}
|
||||
|
||||
for (let i = 0; i < 5; i++) {
|
||||
await payload.create({
|
||||
collection: 'pages',
|
||||
data: {
|
||||
author: user.id,
|
||||
title: `Virtual ${i}`,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
for (let i = 0; i < 5; i++) {
|
||||
await payload.create({
|
||||
collection: 'pages',
|
||||
|
||||
Reference in New Issue
Block a user