UNPKG

2.16 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3'use strict'
4
5const _ = require('lodash')
6const fs = require('fs-extra')
7const request = require('request')
8const yaml = require('js-yaml')
9
10
11const ENTU_DB = process.env.ENTU_DB
12const ENTU_KEY = process.env.ENTU_KEY
13const ENTU_QUERY = process.env.ENTU_QUERY || ''
14const DATA_YAML = process.argv[2]
15
16
17const getPropertyValue = (property) => {
18 if (property.string) { return property.string }
19 if (property.date) { return property.date }
20 if (property.integer) { return property.integer }
21 if (property.boolean) { return property.boolean }
22 if (property.reference) { return property.reference }
23
24 return property
25}
26
27
28request({
29 url: 'https://api.entu.ee/auth',
30 method: 'GET',
31 json: true,
32 auth: {
33 bearer: ENTU_KEY
34 }
35}, (error, response, body) => {
36 if (error) { console.error(error) }
37 if (response.statusCode !== 200) { console.error(body) }
38
39 let token = _.get(body, [ENTU_DB, 'token'], '')
40 let qs = {
41 limit: 1000
42 }
43
44 request({
45 url: 'https://api.entu.ee/entity' + '?' + ENTU_QUERY,
46 method: 'GET',
47 json: true,
48 auth: {
49 bearer: token
50 }
51 }, (error, response, body) => {
52 if (error) { console.error(error) }
53 if (response.statusCode !== 200) { console.error(body) }
54
55 if (body.entities) {
56 let data = []
57
58 for (let i = 0; i < body.entities.length; i++) {
59 let entity = {}
60
61 for (var e in body.entities[i]) {
62 if (!body.entities[i].hasOwnProperty(e)) { continue }
63
64 if (e === '_id') {
65 entity[e] = body.entities[i][e]
66 } else if (body.entities[i][e].length === 1) {
67 entity[e] = getPropertyValue(body.entities[i][e][0])
68 } else {
69 entity[e] = _.map(body.entities[i][e], getPropertyValue)
70 }
71 }
72
73 data.push(entity)
74 }
75
76 fs.outputFileSync(DATA_YAML, yaml.safeDump(data, { indent: 4, lineWidth: 999999999 }))
77 }
78 })
79})