This repository has been archived by the owner on May 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect.js
58 lines (43 loc) · 1.43 KB
/
connect.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
/**
* Author Malindu Warapitiya
*/
var Orientjs = require('orientjs');
var _ = require('underscore');
var CargoError = require('./error');
module.exports = function (opts) {
//Validation of the Cargo options objects
if (_.isString(opts)) {
return new CargoError("CONNECTION_PROPERTY_EMPTY", 'PARAM_TYPE_INVALID');
} else if (_.isObject(opts)) {
propertyCheck(opts);
}
//Connect to the Database using Oriento
var server;
server = Orientjs({
host: opts.host,
port: opts.port,
username: opts.user,
password: opts.password
});
var db = server.use(opts.database);
console.info('Using database from Cargo: ' + db.name);
return db;
};
/**
* Check the properties of the {options} object
* @param opts
*/
function propertyCheck(opts) {
var properties = ['host', 'port', 'user', 'password', 'database'];
_.each(properties, function (prop, index) {
if (!opts.hasOwnProperty(prop)) {
throw new CargoError("CONNECTION_PROPERTY_EMPTY", 'PARAM_TYPE_INVALID');
}
if (!_.isEqual(properties[index], 'port') && !_.isString(opts[prop])) {
throw new CargoError("CONNECTION_PROPERTY_TYPE_INVALID", 'PARAM_TYPE_INVALID');
}
if (_.isEqual(properties[index], 'port') && !_.isNumber(opts.port)) {
throw new CargoError("CONNECTION_PROPERTY_TYPE_INVALID", 'PARAM_TYPE_INVALID');
}
});
};