-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
122 lines (114 loc) · 3.83 KB
/
database.js
File metadata and controls
122 lines (114 loc) · 3.83 KB
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
let arangojs = require('arangojs');
let collectionsConfig = require('./collections-config');
const host = 'http://localhost:8529';
const username = 'root';
const password = '';
const databaseName = 'mydatabase';
const db = new arangojs.Database({url: host});
async function verifyCollectionExists (collectionName) {
return await db.collection(collectionName).exists();
}
async function verifyDatabaseExists () {
return await db.exists();
}
async function startCollections () {
let response = true;
for (let collectionName of collectionsConfig) {
await verifyCollectionExists(collectionName.name)
.then(async (result) => {
if (result) {
console.log(`Collection ${collectionName.name} founded...`);
} else {
console.log(`Initializating collection ${collectionName.name}`);
if (collectionName.type === 1) {
let collection = db.collection(collectionName.name);
await collection.create();
} else if (collectionName.type === 2) {
let collection = db.edgeCollection(collectionName.name);
await collection.create();
}
console.log(`Collection ${collectionName.name} created`);
}
})
.catch((error) => {
if (error.statusCode === 401) {
console.log(`startCollections: User or password invalid!`);
} else if (error.code === 'ECONNREFUSED') {
console.log(`startDatabase: Cannot connect to host ${host}!`);
} else if (error.code === 'ETIMEDOUT') {
console.log(`startDatabase: Timeout connect to host ${host}!`);
} else {
console.log(error);
}
response = false;
});
}
return response;
}
async function startDatabase() {
let response = true;
db.useDatabase(databaseName);
await verifyDatabaseExists()
.then(async (result) => {
if (result) {
console.log(`Database ${databaseName} founded...`);
} else {
console.error(`Database not found...`);
response = false;
}
})
.catch((error) => {
if (error.statusCode === 401) {
console.log(`startDatabase: User or password invalid!`);
} else if (error.code === 'ECONNREFUSED') {
console.log(`startDatabase: Cannot connect to host ${host}!`);
} else if (error.code === 'ETIMEDOUT') {
console.log(`startDatabase: Timeout connect to host ${host}!`);
} else {
console.log(error);
}
response = false;
});
return response;
}
async function startSystem() {
let response = true;
await startDatabase()
.then((result) => {
if (result) {
console.log('Database ready!');
} else {
console.log('Database not started!');
response = false;
}
}).catch((error) => {
console.log(error)
});
if (response) {
await startCollections()
.then((result) => {
if (result) {
console.log('Collections ready!');
} else {
console.log('Collections not started!');
response = false;
}
}).catch((error) => {
console.log(error);
});
}
return response;
};
(async function() {
db.useBasicAuth(username, password);
await startSystem().then((result) => {
if (result) {
console.log('System already. Have a fun!');
} else {
console.log('System not started. Something wrong!');
}
}).catch((error) => {
console.log(error);
});
})();
module.exports = db;