-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.js
76 lines (70 loc) · 2 KB
/
db.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
const { GraphQLClient } = require('graphql-request')
const env = require('./.env')
const sleep = ms => new Promise(res => setTimeout(res, ms))
const logger = require('logging').default('db')
var EventEmitter = require('events')
Array.prototype.shuffle = function () {
var i = this.length; var j; var temp
if (i == 0) return this
while (--i) {
j = Math.floor(Math.random() * (i + 1))
temp = this[i]
this[i] = this[j]
this[j] = temp
}
return this
}
var gqlEndpoint = env.prismaAPI
const token = env.prismaJwt
const client = new GraphQLClient(gqlEndpoint, { headers: {Authorization: 'Bearer ' + token}})
async function doMany (commands, db) {
if (!db) db = client
commands.length
var i = 0
for (var command of commands) {
i += 0.1
const sleeping = parseInt(10 + i)
// logger.info('DoMany Sleeping:',sleeping)
await gql(command.cmd, command.vars, null, db).catch(logger.error)
await sleep(sleeping)
}
}
const gql = async (gql, vars, type, db) => {
try {
if (!db) db = client
gql = gql.replace(/\r?\n|\r/g, '')
if (!type) type = ((/\{(.*?)(\(|\{)/.exec(gql))[1]).trim()
const result = await db.request(gql, vars)
return result[type]
} catch (error) {
// logger.info('got GQL ERROR',error)
return Promise.reject(error)
}
}
function batch ({ type, query, vars, batchSize }, thisClient) {
const emitter = new EventEmitter()
if (!thisClient) var client = dbClient
else var client = thisClient
if (!batchSize) batchSize = 5
if (!vars) vars = {}
var results = []
var i = 0
function loop () {
vars.i = i
getData(type, query, vars, client).then((data) => {
if (data) {
emitter.emit('data', data)
data.forEach((el) => { results.push(el) })
i += batchSize
loop()
}
})
}
loop()
return emitter
}
var getData = async function (type, query, vars, client) {
var data = await client.request(query, vars).catch(logger.error)
return data[type]
}
module.exports = { gql, batch, client, doMany }