-
Notifications
You must be signed in to change notification settings - Fork 2
/
init-database.js
105 lines (85 loc) · 2.91 KB
/
init-database.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
'use strict';
module.exports = async function ({ db, dbUser, dbPasswd, dbName, collections }) {
// we shouldn't use root to manage the database
if (dbUser === 'root') throw new Error('Don\'t use root. Change your config.');
// drop database if exists
try {
await db.dropDatabase(dbName);
console.log('recreating database'); // eslint-disable-line no-console
}
catch (err) {
console.log('creating a new database'); // eslint-disable-line no-console
}
// delete user if exists
try {
await db._api.delete(`/user/${dbUser}`);
console.log(`recreating user ${dbUser}`); // eslint-disable-line no-console
} catch (err) {
console.log(`creating a new user ${dbUser}`); // eslint-disable-line no-console
}
// (re)create the database and user
await db.createDatabase(dbName, [{ username: dbUser, passwd: dbPasswd }]);
db.useDatabase(dbName);
for(const collectionName in collections) {
const { type, indexes = [] } = collections[collectionName];
// creating the collection
let col;
if(type === 'document') {
col = db.collection(collectionName);
}
else if(type === 'edge') {
col = db.edgeCollection(collectionName);
}
else{
throw new Error('not document nor edge');
}
await col.create();
// creating indexes
for(const index of indexes){
await col.createIndex(index);
}
}
// ************* graph ************* //
const graph = db.graph('ditup_graph');
// populating graph properties from collections
const graphProperties = (function () {
// the object to return
const properties = {
edgeDefinitions: [],
orphanCollections: []
};
// keeping track of documents which are used as vertexes
const nonOrphans = [];
for(const cnm in collections) {
const collection = collections[cnm];
if(collection.type === 'edge') {
// adding the edge to the edgeDefinitions
properties.edgeDefinitions.push({
collection: cnm,
from: collection.from,
to: collection.to
});
// keeping track of documents used as vertexes
for (const vertex of collection.from){
if(nonOrphans.indexOf(vertex) === -1) nonOrphans.push(vertex);
}
for(const vertex of collection.to){
if(nonOrphans.indexOf(vertex) === -1) nonOrphans.push(vertex);
}
// END
}
}
// populating the orphanCollections (the documents not yet used as vertexes)
for(const cnm in collections) {
const collection = collections[cnm];
// check whether a document is an orphan
if(collection.type === 'document' && nonOrphans.indexOf(cnm) === -1) {
properties.orphanCollections.push(cnm);
}
}
return properties;
})();
const graphInfo = await graph.create(graphProperties);
console.log(graphInfo); // eslint-disable-line no-console
// ************ END graph *************** //
};