-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv-test.js
More file actions
51 lines (44 loc) · 1.38 KB
/
csv-test.js
File metadata and controls
51 lines (44 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// const createCsvWriter = require('csv-writer').createObjectCsvWriter;
import { createObjectCsvWriter } from 'csv-writer';
import clinicsJSON from './Data/test_clients.json';
const clinics = [
{ id: 496, name: 'Happy Pets Veterinary' },
{ id: 479, name: 'Comfy Pet Clinic' }
];
const petParents = [
{ id: 1, name: 'John Doe', clinic_id: 496 },
{ id: 2, name: 'Jane Smith', clinic_id: 479 }
];
const pets = [
{ id: 101, name: 'Rex', parent_id: 1 },
{ id: 102, name: 'Bella', parent_id: 2 }
];
// Combine the data
const combinedData = pets.map(pet => {
const parent = petParents.find(parent => parent.id === pet.parent_id);
const clinic = clinics.find(clinic => clinic.id === parent.clinic_id);
return {
pet_id: pet.id,
pet_name: pet.name,
parent_name: parent.name,
clinic_name: clinic.name
};
});
// CSV Writer setup
const csvWriter = createObjectCsvWriter({
path: 'output.csv',
header: [
{ id: 'pet_id', title: 'PET ID' },
{ id: 'pet_name', title: 'PET NAME' },
{ id: 'parent_name', title: 'PARENT NAME' },
{ id: 'clinic_name', title: 'CLINIC NAME' }
]
});
// Write data to CSV
csvWriter.writeRecords(combinedData)
.then(() => {
console.log('The CSV file was written successfully');
})
.catch(err => {
console.error('Error writing CSV file:', err);
});