-
Notifications
You must be signed in to change notification settings - Fork 0
/
populate.js
125 lines (115 loc) · 3.7 KB
/
populate.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const fs = require("fs-extra")
const axios = require('axios').default;
const path = require('path')
const yaml = require('js-yaml');
const queryAll = require('./queryAll').queryAll
const l = console.log
function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
async function clearDb() {
l(`Dropping DB...`)
await axios.post(process.env.GRAPHQL_SERVER + '/alter', { "drop_all": true }).then(r => console.log(r.status))
l(`DB dropped !`)
}
async function createSchema() {
l(`Updating schema...`)
l(`Reading file "${process.env.SCHEMA_PATH}"`)
const schema = await fs.readFileSync(process.env.SCHEMA_PATH, "utf-8");
l(`Altering database, schema`, schema)
await axios.post(process.env.GRAPHQL_SERVER + '/admin/schema', schema).then(r => console.log(r.status))
l(`Schema updated !\n`)
}
function generateAdd({ type, yml }) {
let obj = yaml.load(yml)
return `
add${capitalize(type)}(input: {name: "${obj.name}"}) {
${type.toLowerCase()} {
name
}
}
`
}
function generateUpdate({ type, yml }) {
let obj = yaml.load(yml)
const name = obj.name
delete obj.name
// Enums should not be inserted with commas
let enums = []
Object.keys(obj).forEach(k => {
// Detect enums, and isolate them
if (k.includes('enum_')) {
if (Array.isArray(obj[k])) // If list of enums
enums.push({ key: k, val: `[${obj[k]}]` })
else
enums.push({ key: k, val: obj[k] })
delete obj[k]
}
})
let json = JSON.stringify(obj)
.replace(/"([^"]+)":/g, '$1:') // Remove commas around keys
.replace(/^{|}$/g, '') // Remove curly braces around json
let enumAsGraphQL = enums.map(e => `${e.key}: ${e.val}`).join()
json = `{${json}${enumAsGraphQL && ','}${enumAsGraphQL}}`
return `
update${capitalize(type)}(input:
{ filter: {name: {eq: "${name}"}},
set: ${json}
}) {
${type.toLowerCase()} {
name
}
}
`
}
async function runQuery(query) {
l(`Running query ${query}`)
return axios.post(process.env.GRAPHQL_SERVER + '/graphql', query, { headers: { "content-type": "application/graphql" } })
.then(r => console.log(r.status))
}
async function populate() {
l(`Populating DB...`)
l(`Reading YML directory "${process.env.YML_DB_PATH}"`)
const dirs = await fs.readdirSync(process.env.YML_DB_PATH)
l(`Found : ${dirs.join(", ")}`)
const recursiveReads = dirs.map(dir =>
fs.readdir(path.join(process.env.YML_DB_PATH, dir)) // Read the content of each dir
.then(files => files.map(file => fs // For each file
// Read it's content and generate the GraphQL add query
.readFile(path.join(process.env.YML_DB_PATH, dir, file), 'utf8')
.then(contents => ({ type: dir, yml: contents }))
))
)
// List of "add" GraphQL queries
let contents = await Promise.all((await Promise.all(recursiveReads)).flat(2))
await runQuery(`
mutation {
${contents.map(generateAdd).join('')}
}
`)
await runQuery(`
mutation {
${contents.map(generateUpdate).join('')}
}
`)
await runQuery(`
mutation {
addTypesList(input: [
{
types: ${JSON.stringify(dirs)}
}
]) {
typesList {
types
}
}
}
`)
l(`DB populated !\n`)
}
async function main() {
await clearDb()
await createSchema()
await populate()
}
main()