forked from graphql-compose/graphql-compose-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed.js
More file actions
35 lines (30 loc) · 939 Bytes
/
Copy pathseed.js
File metadata and controls
35 lines (30 loc) · 939 Bytes
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
// This script scans `examples` folder for `data/seed.js` files and run them for seeding DB.
import { MongoClient } from 'mongodb';
import fs from 'fs';
import { getExampleNames, resolveExamplePath, mongoUri } from './config';
let db;
async function run() {
db = await MongoClient.connect(mongoUri, { promiseLibrary: Promise });
const exampleNames = getExampleNames();
for (let name of exampleNames) {
console.log(`Starting seed '${name}'...`);
const seedFile = resolveExamplePath(name, 'data/seed.js');
try {
fs.accessSync(seedFile, fs.F_OK);
let seedFn = require(seedFile).default;
await seedFn(db);
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
console.log(` file '${seedFile}' not found. Skipping...`);
} else {
console.log(e);
}
}
}
console.log('Seed competed!');
db.close();
};
run().catch(e => {
console.log(e);
process.exit(0);
});