-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdata.js
93 lines (78 loc) · 2.89 KB
/
data.js
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const mongoose = require('mongoose');
const fs = require('fs');
const path = require('path');
const folderPath = path.join(__dirname, 'policies');
if (!fs.existsSync(folderPath)) {
fs.mkdirSync(folderPath);
}
const connectionString = 'mongodb+srv://prakulSynergyDB:[email protected]/HMReturnPolicy';
mongoose.connect(connectionString, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => {
console.log('Connected to db!');
})
.catch((err) => {
console.log('Connection error:', err);
});
const DeliverySchema = new mongoose.Schema({
DeliveryPolicy: String
});
const MemberSchema = new mongoose.Schema({
MembershipPolicy: String
});
const ReturnSchema = new mongoose.Schema({
ReturnPolicy: String
});
const HMDelivery = mongoose.model('HMDelivery', DeliverySchema, 'HMDelivery');
const HMMembership = mongoose.model('HMMembership', MemberSchema, 'HMMembership');
const HMPolicies = mongoose.model('HMPolicies', ReturnSchema, 'HMPolicies');
HMDelivery.find({}).then((docs) => {
if (docs.length > 0) {
fs.writeFileSync(path.join(folderPath, 'HMDelivery.json'), JSON.stringify(docs));
} else {
console.log('No documents found in HMDelivery.');
}
}).catch((err) => {
console.log('Error fetching HMDelivery:', err);
});
HMMembership.find({}).then((docs) => {
if (docs.length > 0) {
fs.writeFileSync(path.join(folderPath, 'HMMembership.json'), JSON.stringify(docs));
} else {
console.log('No documents found in HMMembership.');
}
}).catch((err) => {
console.log('Error fetching HMMembership:', err);
});
HMPolicies.find({}).then((docs) => {
if (docs.length > 0) {
fs.writeFileSync(path.join(folderPath, 'HMPolicies.json'), JSON.stringify(docs));
} else {
console.log('No documents found in HMPolicies.');
}
}).catch((err) => {
console.log('Error fetching HMPolicies:', err);
});
const readAndParseJsonFile = (filePath) => {
const fileContent = fs.readFileSync(filePath, 'utf-8');
return JSON.parse(fileContent);
};
// Read each JSON file from the 'policies' folder and parse the content
const hmDeliveryData = readAndParseJsonFile(path.join(folderPath, 'HMDelivery.json'));
const hmMembershipData = readAndParseJsonFile(path.join(folderPath, 'HMMembership.json'));
const hmPoliciesData = readAndParseJsonFile(path.join(folderPath, 'HMPolicies.json'));
// Combine all data into one object
const combinedData = {
HMDelivery: hmDeliveryData,
HMMembership: hmMembershipData,
HMPolicies: hmPoliciesData
};
// Convert combined data to JSON string
const combinedDataString = JSON.stringify(combinedData);
// If you want to save this combined data to a new JSON file, you can do so
fs.writeFileSync(path.join(folderPath, 'CombinedPolicies.json'), combinedDataString);
// Now 'combinedDataString' contains all the combined JSON data as a single string
console.log(combinedDataString);
fs.writeFileSync('output.txt', combinedDataString, 'utf-8');