-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat_storage.js
26 lines (22 loc) · 941 Bytes
/
chat_storage.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
/*jslint node: true */
"use strict";
var db = require('./db.js');
function store(correspondent_address, message, is_incoming, type) {
var type = type || 'text';
db.query("INSERT INTO chat_messages ('correspondent_address', 'message', 'is_incoming', 'type') VALUES (?, ?, ?, ?)", [correspondent_address, message, is_incoming, type]);
}
function load(correspondent_address, up_to_id, limit, cb) {
if (typeof up_to_id !== 'number')
throw Error('up_to_id is not a number: ' + up_to_id);
db.query("SELECT id, message, creation_date, is_incoming, type FROM chat_messages \n\
WHERE correspondent_address=? AND id < "+up_to_id+" ORDER BY id DESC LIMIT ?", [correspondent_address, limit], function(rows){
cb(rows);
});
}
function purge(correspondent_address) {
db.query("DELETE FROM chat_messages \n\
WHERE correspondent_address=?", [correspondent_address]);
}
exports.store = store;
exports.load = load;
exports.purge = purge;