-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
149 lines (128 loc) · 3.75 KB
/
index.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const DataLayerErrors = {
connectionError: 1,
connectionDrop: 2,
connectionTimeout: 3
};
/**
* Low level wrapper for the underlying database. You must connect a module before starting using it.
*
* Connect a module colling the connectModule method.
*
* @param options
* @param rdb
* @constructor
*/
function DataLayer(options, rdb) {
const r = rdb || require('rethinkdb');
this._options = options || {};
this._connection = null;
this._connecting = false;
/**
* Initialise the connection
*
* @param module
*/
this.connectModule = function (module) {
const self = this;
if (null === this._connection && !this._connecting) {
this._connecting = true;
r.connect(this._options, function (err, conn) {
if (err) module(new Error('Unable to connect to the database', DataLayerErrors.connectionError), null);
else {
self._connection = conn;
self._connecting = false;
self.monitorConnection();
module(null, self);
}
});
} else if (this._connecting) {
var ticker = setInterval(function () {
if (!self._connecting) {
clearInterval(ticker);
module(null, self);
}
}, 100);
} else {
module(null, this);
}
};
/**
* Monitor the connection and throw an error in case of failure
*/
this.monitorConnection = function (errorHandler) {
function failedConnection(code, error) {
return errorHandler ? errorHandler : function () {
throw new Error('Database connection failure', code);
};
}
if ('function' === typeof this._connection.on) {
this._connection.on('error', failedConnection(DataLayerErrors.connectionError));
this._connection.on('close', failedConnection(DataLayerErrors.connectionDrop));
this._connection.on('timeout', failedConnection(DataLayerErrors.connectionTimeout));
}
};
/**
* Returns the options.
*
* @returns {{}}
*/
this.getOptions = function () {
return this._options;
};
/**
* Returns the RethinkDb connection.
*
* @returns r.net.Connection
*/
this.getConnection = function () {
return this._connection;
};
/**
* Returns the database native query builder
*
* @return rethinkdb
*/
this.query = function () {
return r;
};
/**
* Execute the given query against the database and return the results to the callback
*
* @param query
* @param callback
*/
this.execute = function (query, callback) {
try {
query.run(this._connection, callback);
} catch (e) {
callback(e)
}
};
// shorthand self-explanatory methods
this.dbList = function (callback) {
this.execute(this.query().dbList(), callback);
};
this.dbCreate = function (database, callback) {
this.execute(this.query().dbCreate(database), callback);
};
this.tableList = function (database, callback) {
this.execute(this.query().db(database).tableList(), callback);
};
this.tableCreate = function (database, table, callback) {
this.execute(this.query().db(database).tableCreate(table), callback);
};
this.tableDelete = function (database, table, callback) {
this.execute(this.query().db(database).tableDrop(table), callback);
};
this.get = function (database, table, id, callback) {
this.execute(this.query().db(database).table(table).get(id), callback);
};
this.delete = function (database, table, id, callback) {
this.execute(this.query().db(database).table(table).get(id).delete(), callback);
};
this.insert = function (database, table, document, options, callback) {
this.execute(this.query().db(database).table(table).insert(document, options), callback);
};
}
exports.DataLayerErrors = DataLayerErrors;
exports.DataLayer = DataLayer;