/
db.js
44 lines (39 loc) · 1011 Bytes
/
db.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
/*jshint node:true */
'use strict';
var q = require('q');
var r = require('rethinkdb');
var dbConnection = {
'host': 'localhost',
'port': 28015,
'db': 'realtime_photo_tutorial'
};
r.connections = [];
r.getNewConnection = function () {
return r.connect(dbConnection)
.then(function (conn) {
conn.use(dbConnection.db);
r.connections.push(conn);
return conn;
});
};
r.connect(dbConnection)
.then(function (conn) {
r.conn = conn;
r.connections.push(conn);
return r.dbCreate(dbConnection.db).run(r.conn)
.catch(function () {})
.then(function () {
r.conn.use(dbConnection.db);
// Create Tables
return r.tableList().run(r.conn)
.then(function (tableList) {
return q()
.then(function() {
if (tableList.indexOf('images') === -1) {
return r.tableCreate('images').run(r.conn);
}
});
});
});
});
module.exports = r;