-
Notifications
You must be signed in to change notification settings - Fork 1
/
dbconfig.js
67 lines (61 loc) · 2.29 KB
/
dbconfig.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
/**
* Creates a Document database in ISC IRIS and
* creates the properties defined in a config file
*/
var fs = require('fs')
var axios = require('axios')
var config = require('config')
var HOST = config.get('dbConfig.host')
var PORT = config.get('dbConfig.port')
var DBNAMESPACE = config.get('dbConfig.namespace')
var DBNAME = config.get('dbConfig.dbname')
const api = axios.create({
baseURL: HOST + ":" + PORT + "/api/docdb/v1/" + DBNAMESPACE,
timeout: 10000,
auth: {
username: config.get('dbConfig.username'),
password: config.get('dbConfig.password')
}
})
// create the properties
function createProperties() {
var fn = config.get("schemaFileName")
var fdata = fs.readFileSync(fn)
var propertyNames = JSON.parse(fdata)
for (var prop in propertyNames) {
let proptype = Object.keys(propertyNames[prop])[0]
// let proptype = "%" + keyname.replace(/^./, keyname[0].toUpperCase())
let url = "/prop/"+DBNAME+"/"+prop+"?type="+proptype+"&path=$."+prop+"&unique=0"
// let url = `/prop/{DBNAME}`
api.post(url, {})
.then(function (response) {
console.log("\nurl: " + url)
console.log("status: " + response.status)
console.log("data: " + JSON.stringify(response.data))
})
.catch(function (error) {
console.log("\nError: " + JSON.stringify(error.message))
console.log("url: " + url)
});
}
}
//// write schema to IRIS ////
api.delete("/db/"+DBNAME) // delete the database
.then((response) => {
console.log("status: " + response.status)
console.log("data: " + JSON.stringify(response.data))
})
.catch((error) => {
console.log(error)
})
.finally(() => { // create the database
api.post("/db/"+DBNAME+"?type=documentType", {})
.then(function (response) {
console.log("status: " + response.status)
console.log("data: " + JSON.stringify(response.data))
createProperties() // create indexed properties
})
.catch(function (error) {
console.log("error: " + JSON.stringify(error.message))
});
});