-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseeder.js
More file actions
56 lines (41 loc) · 1.31 KB
/
seeder.js
File metadata and controls
56 lines (41 loc) · 1.31 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
52
53
54
const path = require('path');
const { Seeder } = require('mongo-seeding');
const fetch = require('node-fetch');
const fs =require('fs');
const config = {
database: {
name: 'assessment',
},
dropDatabase: true,
};
const seeder = new Seeder(config);
const saveUserDataToFileSystem = async () => {
const allUsers = await fetch('http://www.mocky.io/v2/5808862710000087232b75ac');
const userContent = (await allUsers.json()).clients
const userContentPasswords = userContent.map(user => ({...user, password: 'elephant'}))
const jsonContent = JSON.stringify(userContentPasswords);
fs.writeFile("data/users/users.json", jsonContent, 'utf8', function (err) {
if (err) {
console.log("An error occured while writing JSON Object to File.");
return console.log(err);
}
console.log("JSON file has been saved.");
});
return jsonContent
}
(async function() {
await saveUserDataToFileSystem();
})();
const collections = seeder.readCollectionsFromPath(
path.resolve('./data'),{
transformers: [Seeder.Transformers.replaceDocumentIdWithUnderscoreId],
}
);
seeder
.import(collections)
.then(async() => {
console.log('Success seed!');
})
.catch(err => {
console.log('Error', err);
});