-
Notifications
You must be signed in to change notification settings - Fork 0
/
miya.js
124 lines (113 loc) · 3.56 KB
/
miya.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
/* _
* _ __ ___ (_)_ _ __ _
* | '_ ` _ \| | | | |/ _` |
* | | | | | | | |_| | (_| |
* |_| |_| |_|_|\__, |\__,_|
* |___/
*/
// Node modules
const newman = require('newman')
const jq = require('node-jq')
const { MongoClient } = require('mongodb')
require('log-timestamp')
// Configuration
const miya = require('./config.js')
// Main
console.log('Collection Runner for Postman')
start()
.catch((e) => { console.error(e) })
async function start () {
for (const config of Object.values(miya)) {
console.log(`Postman collection "${config.name}"`)
const client = new MongoClient(config.mongodb.uri, { useNewUrlParser: true, useUnifiedTopology: true })
await database(config, client)
.catch((e) => { console.error(e) })
}
}
// Functions
async function database (config, client) {
try {
await client.connect()
const dbo = client.db(config.mongodb.database)
await runner(dbo, config)
.then((log) => {
console.log(log)
})
.catch((e) => {
console.error(e)
})
} finally {
await client.close()
}
}
async function runner (dbo, config) {
const a = []
return new Promise((resolve, reject) => {
let options
const folder = config.postman.folder
if (typeof folder !== 'undefined') {
options = { collection: config.postman.collection, folder: folder }
} else {
options = { collection: config.postman.collection }
}
// Postman collection
newman.run(options)
// Postman request
.on('request', (e, args) => {
if (e) reject(e)
const raw = args.response.stream // Buffer
a.push({ collection: args.item.name, json: raw.toString() })
})
.on('start', () => {
console.log('Running...')
})
.on('done', async (e, summary) => {
if (e) {
reject(e)
} else {
console.log(`${summary.run.stats.assertions.total} requests executed (${summary.run.stats.assertions.failed} failed)`)
console.log(`${(summary.run.transfers.responseTotal / 1024 / 1024).toFixed(3)} MB received`)
console.log('Database update in progress. Please wait...')
await parser(config, dbo, a, config.postman.result)
.catch((e) => { console.error(e) })
resolve('Database successfully updated!')
}
})
})
}
async function parser (config, dbo, array, data = '.result') {
for (const obj of array) {
const collections = config.mongodb.collections
for (const collection of Object.values(collections)) {
if (collection.name === obj.collection) {
const result = await filter(obj.json, data)
.catch((e) => { console.error(e) })
const length = await filter(result, '. | length')
.catch((e) => { console.error(e) })
for (let i = 0; i < length; i++) {
const doc = await filter(result, `.[${i}]`, { input: 'string', output: 'json' })
.catch((e) => { console.error(e) })
await upsert(dbo, obj.collection, doc, collection.unique)
.catch((e) => { console.error(e) })
}
}
}
}
}
async function filter (input, filter, options = { input: 'string', output: 'string' }) {
try {
const out = await jq.run(filter, input, options)
return out
} catch (e) {
throw Error(e)
}
}
async function upsert (dbo, collection, doc, unique = 'id') {
try {
const filter = { [unique]: doc[unique] }
const out = await dbo.collection(collection).updateOne(filter, { $set: doc }, { upsert: true })
return out.result
} catch (e) {
throw Error(e)
}
}