This repository has been archived by the owner on Jun 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql.js
58 lines (53 loc) · 1.46 KB
/
sql.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
var Knex = require("knex");
var getMessages = function (knex, channel, msgHandler, cb) {
return knex('messages')
.where({
channel: channel,
})
.select('*')
.then(function (rows) {
rows.forEach(function (row) {
msgHandler(row.content);
});
cb();
})
.catch(function (e) {
console.error(e);
cb();
});
};
var insert = function (knex, channel, content, cb) {
knex.table('messages').insert({
channel: channel,
content: content,
})
.then(function () {
cb();
});
};
module.exports.create = function (conf, cb) {
var knex = Knex({
dialect: conf.sqlDialect,
connection: conf.dbConnection,
useNullAsDefault: true,
});
knex.schema.hasTable('messages').then(function (exists) {
if (exists) { return; }
return knex.schema.createTable('messages', function (table) {
table.increments('id');
table.string('content');
table.string('channel');
table.timestamps();
});
})
.then(function () {
cb({
message: function (channelName, content, cb) {
insert(knex, channelName, content, cb);
},
getMessages: function (channelName, msgHandler, cb) {
getMessages(knex, channelName, msgHandler, cb);
},
});
});
};