Skip to content

Commit b2db63c

Browse files
authored
Merge pull request #306 from C2FO/issue272
Added appending to CSV example #272
2 parents 2a065b9 + 507cf2c commit b2db63c

File tree

5 files changed

+151
-3
lines changed

5 files changed

+151
-3
lines changed

History.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
* [ADDED] Ability to Transform Header [#287](https://github.com/C2FO/fast-csv/issues/287)
44
* [ADDED] Example require and import to README [#301](https://github.com/C2FO/fast-csv/issues/301)
55
* [ADDED] Added new formatting option `alwaysWriteHeaders` to always write headers even if no rows are provided [#300](https://github.com/C2FO/fast-csv/issues/300)
6-
* [FIXED] Issue with duplicate headers causing dataloss, duplicate headers will can an error to be emitted. [#276](https://github.com/C2FO/fast-csv/issues/276)
6+
* [ADDED] Appending to csv example and docs [#272](https://github.com/C2FO/fast-csv/issues/300)
7+
* [FIXED] Issue with duplicate headers causing dataloss, duplicate headers will can an error to be emitted. [#276](https://github.com/C2FO/fast-csv/issues/272)
78
* [FIXED] Issue where an error thrown while processing rows causes stream continue to parse, causing duplicate writes or swallowed exceptions.
89

10+
911
# v3.6.0
1012

1113
* [ADDED] `maxRows` option to limit the number of rows parsed. [#275](https://github.com/C2FO/fast-csv/issues/275) [#277](https://github.com/C2FO/fast-csv/pull/277) - [@cbrittingham](https://github.com/cbrittingham)

docs/formatting.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* [`quoteColumns`](#examples-quote-columns)
2727
* [`quoteHeaders`](#examples-quote-headers)
2828
* [Transforming Rows](#examples-transforming)
29+
* [Appending To A CSV](#examples-appending)
2930

3031
<a name="options"></a>
3132
## Options
@@ -838,4 +839,88 @@ VALUE1A,VALUE2A
838839
VALUE1A,VALUE2A
839840
VALUE1A,VALUE2A
840841
VALUE1A,VALUE2A
842+
```
843+
844+
<a name="examples-appending"></a>
845+
### Appending To A CSV
846+
847+
[`examples/formatting/append.example.js`](../examples/formatting/append.example.js)
848+
849+
In this example a new csv is created then appended to.
850+
851+
```javascript
852+
const path = require('path');
853+
const fs = require('fs');
854+
855+
const write = (filestream, rows, options) => {
856+
return new Promise((res, rej) => {
857+
csv.writeToStream(filestream, rows, options)
858+
.on('error', err => rej(err))
859+
.on('finish', () => res());
860+
});
861+
};
862+
863+
// create a new csv
864+
const createCsv = (filePath, rows) => {
865+
const csvFile = fs.createWriteStream(filePath);
866+
return write(csvFile, rows, { headers: true, includeEndRowDelimiter: true });
867+
};
868+
869+
// append the rows to the csv
870+
const appendToCsv = (filePath, rows = []) => {
871+
const csvFile = fs.createWriteStream(filePath, { flags: 'a' });
872+
// notice how headers are set to false
873+
return write(csvFile, rows, { headers: false });
874+
};
875+
876+
// read the file
877+
const readFile = filePath => {
878+
return new Promise((res, rej) => {
879+
fs.readFile(filePath, (err, contents) => {
880+
if (err) {
881+
return rej(err);
882+
}
883+
return res(contents);
884+
});
885+
});
886+
};
887+
888+
const csvFilePath = path.resolve(__dirname, 'tmp', 'append.csv');
889+
890+
// 1. create the csv
891+
createCsv(csvFilePath, [
892+
{ a: 'a1', b: 'b1', c: 'c1' },
893+
{ a: 'a2', b: 'b2', c: 'c2' },
894+
{ a: 'a3', b: 'b3', c: 'c3' },
895+
])
896+
.then(() => {
897+
// 2. append to the csv
898+
return appendToCsv(csvFilePath, [
899+
{ a: 'a4', b: 'b4', c: 'c4' },
900+
{ a: 'a5', b: 'b5', c: 'c5' },
901+
{ a: 'a6', b: 'b6', c: 'c6' },
902+
]);
903+
})
904+
.then(() => readFile(csvFilePath))
905+
.then(contents => {
906+
console.log(`${contents}`);
907+
})
908+
.catch(err => {
909+
console.error(err.stack);
910+
process.exit(1);
911+
});
912+
913+
914+
```
915+
916+
Expected output
917+
918+
```
919+
a,b,c
920+
a1,b1,c1
921+
a2,b2,c2
922+
a3,b3,c3
923+
a4,b4,c4
924+
a5,b5,c5
925+
a6,b6,c6
841926
```
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
const path = require('path');
2+
const fs = require('fs');
3+
const csv = require('../..');
4+
5+
const write = (filestream, rows, options) => {
6+
return new Promise((res, rej) => {
7+
csv.writeToStream(filestream, rows, options)
8+
.on('error', err => rej(err))
9+
.on('finish', () => res());
10+
});
11+
};
12+
13+
// create a new csv
14+
const createCsv = (filePath, rows) => {
15+
const csvFile = fs.createWriteStream(filePath);
16+
return write(csvFile, rows, { headers: true, includeEndRowDelimiter: true });
17+
};
18+
19+
// append the rows to the csv
20+
const appendToCsv = (filePath, rows = []) => {
21+
const csvFile = fs.createWriteStream(filePath, { flags: 'a' });
22+
// notice how headers are set to false
23+
return write(csvFile, rows, { headers: false });
24+
};
25+
26+
// read the file
27+
const readFile = filePath => {
28+
return new Promise((res, rej) => {
29+
fs.readFile(filePath, (err, contents) => {
30+
if (err) {
31+
return rej(err);
32+
}
33+
return res(contents);
34+
});
35+
});
36+
};
37+
38+
const csvFilePath = path.resolve(__dirname, 'tmp', 'append.csv');
39+
40+
// 1. create the csv
41+
createCsv(csvFilePath, [
42+
{ a: 'a1', b: 'b1', c: 'c1' },
43+
{ a: 'a2', b: 'b2', c: 'c2' },
44+
{ a: 'a3', b: 'b3', c: 'c3' },
45+
])
46+
.then(() => {
47+
// 2. append to the csv
48+
return appendToCsv(csvFilePath, [
49+
{ a: 'a4', b: 'b4', c: 'c4' },
50+
{ a: 'a5', b: 'b5', c: 'c5' },
51+
{ a: 'a6', b: 'b6', c: 'c6' },
52+
]);
53+
})
54+
.then(() => readFile(csvFilePath))
55+
.then(contents => {
56+
console.log(`${contents}`);
57+
})
58+
.catch(err => {
59+
console.error(err.stack);
60+
process.exit(1);
61+
});

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fast-csv",
3-
"version": "3.6.0",
3+
"version": "3.7.0",
44
"description": "CSV parser and writer",
55
"main": "./build/src/index.js",
66
"types": "./build/src/index.d.ts",

0 commit comments

Comments
 (0)