Skip to content

Commit c510d2d

Browse files
authored
New option formatDates for writeData method (#996)
* New option formatDates for writeData method * Fixing tests
1 parent 991073b commit c510d2d

File tree

4 files changed

+23
-17
lines changed

4 files changed

+23
-17
lines changed

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
".": "./src/index.ts"
66
},
77
"tasks": {
8-
"all-tests": "deno install --allow-scripts=npm:playwright-chromium && deno fmt --check && deno lint && deno check src/index.ts && deno publish --allow-dirty --dry-run && deno test -A --fail-fast",
8+
"all-tests": "deno install --allow-scripts=npm:playwright-chromium && deno fmt --check && deno lint && deno check src/index.ts && deno publish --allow-dirty --dry-run && rm -rf test/output && deno test -A --fail-fast",
99
"test-coverage": "deno test -A --fail-fast --coverage=cov_profile && deno coverage cov_profile",
1010
"patch-no-tests": "deno run -A src/incrementVersion.ts patch",
1111
"patch": "deno task all-tests && deno run -A src/incrementVersion.ts patch",

src/class/SimpleTable.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5385,6 +5385,7 @@ export default class SimpleTable extends Simple {
53855385
* @param options - An optional object with configuration options:
53865386
* @param options.compression - A boolean indicating whether to compress the output file. Defaults to false. If true, CSV and JSON files will be compressed with GZIP while PARQUET files will use ZSTD.
53875387
* @param options.dataAsArrays - A boolean for JSON files. If true, JSON files are written as one object with arrays instead of an array of objects. Convenient to reduce the size of JSON files for web projects. You can use the function arraysToData from the journalism library to bring back the data to its original state.
5388+
* @param options.formatDates - If true, dates will be formatted as ISO strings ("2025-01-01T01:00:00.000Z"). Defaults to false. Works only with CSV and JSON files.
53885389
*
53895390
* @category Exporting data
53905391
*/
@@ -5393,6 +5394,7 @@ export default class SimpleTable extends Simple {
53935394
options: {
53945395
compression?: boolean;
53955396
dataAsArrays?: boolean;
5397+
formatDates?: boolean;
53965398
} = {},
53975399
) {
53985400
createDirectory(file);

src/methods/writeDataQuery.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,29 @@ export default function writeDataQuery(
55
table: string,
66
file: string,
77
fileExtension: string,
8-
options: { compression?: boolean },
8+
options: { compression?: boolean; formatDates?: boolean },
99
) {
1010
const cleanedFile = cleanPath(file);
1111
if (fileExtension === "csv") {
12-
if (options.compression) {
13-
return `COPY "${table}" TO '${
14-
cleanedFile + ".gz"
15-
}' (DELIMITER ',', HEADER TRUE, COMPRESSION GZIP);`;
16-
} else {
17-
return `COPY "${table}" TO '${cleanedFile}' (DELIMITER ',', HEADER TRUE, DATEFORMAT '%xT%X.%gZ', TIMESTAMPFORMAT '%xT%X.%gZ');`;
18-
}
12+
return `COPY "${table}" TO '${
13+
options.compression ? cleanedFile + ".gz" : cleanedFile
14+
}' (DELIMITER ',', HEADER TRUE${
15+
options.compression ? ", COMPRESSION GZIP" : ""
16+
}${
17+
options.formatDates
18+
? ", DATEFORMAT '%xT%X.%gZ', TIMESTAMPFORMAT '%xT%X.%gZ'"
19+
: ""
20+
});`;
1921
} else if (fileExtension === "json") {
20-
if (options.compression) {
21-
return `COPY "${table}" TO '${
22-
cleanedFile + ".gz"
23-
}' (FORMAT JSON, ARRAY TRUE, COMPRESSION GZIP);`;
24-
} else {
25-
return `COPY "${table}" TO '${cleanedFile}' (FORMAT JSON, ARRAY TRUE, DATEFORMAT '%xT%X.%gZ', TIMESTAMPFORMAT '%xT%X.%gZ');`;
26-
}
22+
return `COPY "${table}" TO '${
23+
options.compression ? cleanedFile + ".gz" : cleanedFile
24+
}' (FORMAT JSON, ARRAY TRUE${
25+
options.compression ? ", COMPRESSION GZIP" : ""
26+
}${
27+
options.formatDates
28+
? ", DATEFORMAT '%xT%X.%gZ', TIMESTAMPFORMAT '%xT%X.%gZ'"
29+
: ""
30+
});`;
2731
} else if (fileExtension === "parquet") {
2832
if (options.compression) {
2933
return `COPY "${table}" TO '${cleanedFile}' (FORMAT PARQUET, COMPRESSION ZSTD);`;

test/unit/methods/writeData.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Deno.test("should write a json file with dates", async () => {
8282
key1: new Date("2025-04-08T14:09:24.155Z"),
8383
}]);
8484

85-
await table.writeData(`${output}date-test.json`);
85+
await table.writeData(`${output}date-test.json`, { formatDates: true });
8686

8787
// We test the content of the file
8888
const tableCheck = JSON.parse(

0 commit comments

Comments
 (0)