From b1cd75505694d1a219053a0c592824a6f2dc8f1c Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Ronssin Date: Sun, 28 Apr 2024 15:09:57 +0200 Subject: [PATCH 1/2] add null flag --- messages/data.export.md | 4 ++++ src/commands/texei/data/export.ts | 26 +++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/messages/data.export.md b/messages/data.export.md index ffbb876..377fb73 100644 --- a/messages/data.export.md +++ b/messages/data.export.md @@ -17,3 +17,7 @@ path to data plan file # flags.apitype.summary API Type to use + +# flags.excludenullfields.summary + +Exclude null fields from exported JSON files diff --git a/src/commands/texei/data/export.ts b/src/commands/texei/data/export.ts index aef8be1..e508739 100644 --- a/src/commands/texei/data/export.ts +++ b/src/commands/texei/data/export.ts @@ -62,6 +62,12 @@ export default class Export extends SfCommand { options: ['rest', 'bulk'], default: 'rest', }), + // new flag to exclude null fields + excludenullfields: Flags.boolean({ + char: 'e', + summary: messages.getMessage('flags.excludenullfields.summary'), + default: false, + }), // loglevel is a no-op, but this flag is added to avoid breaking scripts and warn users who are using it loglevel, }; @@ -300,7 +306,14 @@ export default class Export extends SfCommand { const recordFile: any = {}; recordFile.attributes = objectAttributes; - recordFile.records = recordResults; + + if (Export.flags.excludenullfields) { + // Filtrer les champs null de chaque enregistrement + const filteredRecords = recordResults.map(removeNullFields); + recordFile.records = filteredRecords; + } else { + recordFile.records = recordResults; + } // eslint-disable-next-line @typescript-eslint/no-unsafe-return return recordFile; @@ -414,3 +427,14 @@ export default class Export extends SfCommand { }); } } + +// add function to remove null values from object +function removeNullFields(record: { [key: string]: any }): { [key: string]: any } { + const filteredRecord: { [key: string]: any } = {}; + for (const key in record) { + if (record[key] !== null) { + filteredRecord[key] = record[key]; + } + } + return filteredRecord; +} From 60f5f8552023a149cfbea93ca1f530f3cce85ad3 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Ronssin Date: Sun, 28 Apr 2024 15:33:55 +0200 Subject: [PATCH 2/2] Refactor getsObjectRecords function in export.ts to include null fields based on the excludenullfields flag --- src/commands/texei/data/export.ts | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/commands/texei/data/export.ts b/src/commands/texei/data/export.ts index e508739..c12effd 100644 --- a/src/commands/texei/data/export.ts +++ b/src/commands/texei/data/export.ts @@ -119,7 +119,7 @@ export default class Export extends SfCommand { }); const fileName = `${index}-${obj.name}${obj.label ? '-' + obj.label : ''}.json`; - const objectRecords: any = await this.getsObjectRecords(obj, recordIdsMap, flags.apitype); + const objectRecords: any = await this.getsObjectRecords(obj, recordIdsMap, flags.apitype, flags); await this.saveFile(objectRecords, fileName, flags.outputdir); index++; @@ -129,7 +129,12 @@ export default class Export extends SfCommand { return { message: 'Data exported' }; } - private async getsObjectRecords(sobject: DataPlanSObject, recordIdsMap: Map, apitype: string) { + private async getsObjectRecords( + sobject: DataPlanSObject, + recordIdsMap: Map, + apitype: string, + flags: { [key: string]: any } + ) { // Query to retrieve creatable sObject fields let fields: string[] = []; const lookups = []; @@ -307,13 +312,7 @@ export default class Export extends SfCommand { const recordFile: any = {}; recordFile.attributes = objectAttributes; - if (Export.flags.excludenullfields) { - // Filtrer les champs null de chaque enregistrement - const filteredRecords = recordResults.map(removeNullFields); - recordFile.records = filteredRecords; - } else { - recordFile.records = recordResults; - } + recordFile.records = recordResults.map((record) => removeNullFields(record, flags.excludenullfields)); // eslint-disable-next-line @typescript-eslint/no-unsafe-return return recordFile; @@ -429,7 +428,11 @@ export default class Export extends SfCommand { } // add function to remove null values from object -function removeNullFields(record: { [key: string]: any }): { [key: string]: any } { +function removeNullFields(record: { [key: string]: any }, excludeNullFields: boolean): { [key: string]: any } { + if (!excludeNullFields) { + return record; + } + const filteredRecord: { [key: string]: any } = {}; for (const key in record) { if (record[key] !== null) {