-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
163 lines (150 loc) · 5.91 KB
/
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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// A fork of the [node.js chat app](https://github.com/eiriksm/chat-test-2k)
// by [@orkj](https://twitter.com/orkj) using socket.io, rethinkdb, passport and bcrypt on an express app.
//
// See the [GitHub README](https://github.com/rethinkdb/rethinkdb-example-nodejs-chat/blob/master/README.md)
// for details of the complete stack, installation, and running the app.
var r = require('rethinkdb')
, util = require('util')
, assert = require('assert')
, logdebug = require('debug')('rdb:debug')
, logerror = require('debug')('rdb:error');
// #### Connection details
var dbConfig = {
host: process.env.RDB_HOST || 'localhost',
port: parseInt(process.env.RDB_PORT) || 28015,
db : process.env.RDB_DB || 'speakout',
tables: {
'chats': 'id',
'users': 'id'
}
};
/*
Setup
*/
module.exports.setup = function() {
r.connect({host: dbConfig.host, port: dbConfig.port }, function (err, connection) {
assert.ok(err === null, err);
r.db(dbConfig['db']).tableDrop('chats').run(connection, function(){
r.dbCreate(dbConfig.db).run(connection, function(err, result) {
if(err) {
logdebug("[DEBUG] RethinkDB database '%s' already exists (%s:%s)\n%s", dbConfig.db, err.name, err.msg, err.message);
}
else {
logdebug("[INFO ] RethinkDB database '%s' created", dbConfig.db);
}
for(var tbl in dbConfig.tables) {
(function (tableName) {
r.db(dbConfig.db).tableCreate(tableName, {primaryKey: dbConfig.tables[tbl]}).run(connection, function(err, result) {
if(err) {
logdebug("[DEBUG] RethinkDB table '%s' already exists (%s:%s)\n%s", tableName, err.name, err.msg, err.message);
}
else {
logdebug("[INFO ] RethinkDB table '%s' created", tableName);
}
});
})(tbl);
}
});
});
});
};
module.exports.createChat = function (hash, private, password) {
onConnect(function (err, connection) {
r.db(dbConfig['db']).table('chats').insert({
"hash": hash,
"private": private,
"password": password,
"users": [],
"messages": []
}).run(connection, function() {
connection.close();
});
});
};
module.exports.addUserToRoom = function (id, nick, callback) {
onConnect(function (err, connection) {
findChatById(id, function(a,b,uuid) {
r.db(dbConfig['db']).table('chats').get(uuid['id']).update({ users: r.row('users').append(nick) }).run(connection, function() {
callback(uuid);
connection.close();
});
});
});
};
module.exports.removeUserFromRoom = function (id, nick, callback) {
onConnect(function (err, connection) {
findChatById(id, function(a,b,uuid) {
var new_users = uuid['users'];
var index = new_users.indexOf(nick);
if (index > -1) {
new_users.splice(index, 1);
}
r.db(dbConfig['db']).table('chats').get(uuid['id']).update({ users: new_users }).run(connection, function() {
callback(new_users);
connection.close();
});
});
});
};
module.exports.findChatById = function (id, callback) {
onConnect(function (err, connection) {
r.db(dbConfig['db']).table('chats').filter({ hash: id}).limit(1).run(connection, function(err, cursor) {
if(err) {
logerror("[ERROR][%s][findChatById] %s:%s\n%s", connection['_id'], err.name, err.msg, err.message);
callback(true, false, null);
} else {
cursor.next(function (err, row) {
if(err) {
logerror("[ERROR][%s][findChatById] %s:%s\n%s", connection['_id'], err.name, err.msg, err.message);
callback(false, false, null); // no user, cursor is empty
} else {
callback(false, true, row);
}
connection.close();
});
}
});
});
};
function findChatById(id, callback) {
onConnect(function (err, connection) {
r.db(dbConfig['db']).table('chats').filter({ hash: id}).limit(1).run(connection, function(err, cursor) {
if(err) {
logerror("[ERROR][%s][findChatById] %s:%s\n%s", connection['_id'], err.name, err.msg, err.message);
callback(true, false, null);
} else {
cursor.next(function (err, row) {
if(err) {
logerror("[ERROR][%s][findChatById] %s:%s\n%s", connection['_id'], err.name, err.msg, err.message);
callback(false, false, null); // no user, cursor is empty
} else {
callback(false, true, row);
}
connection.close();
});
}
});
});
}
function onConnect(callback) {
r.connect({host: dbConfig.host, port: dbConfig.port }, function(err, connection) {
assert.ok(err === null, err);
connection['_id'] = Math.floor(Math.random()*10001);
callback(err, connection);
});
}
// #### Connection management
//
// This application uses a new connection for each query needed to serve
// a user request. In case generating the response would require multiple
// queries, the same connection should be used for all queries.
//
// Example:
//
// onConnect(function (err, connection)) {
// if(err) { return callback(err); }
//
// query1.run(connection, callback);
// query2.run(connection, callback);
// }
//