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
|
var db = module.exports
var connection = require("./bookshelf")
var bookshelf = connection.bookshelf
var knex = connection.knex
/* MODELS */
var User = db.User = bookshelf.Model.extend({
tableName: 'users',
hasTimestamps: false,
})
var Thread = db.Thread = bookshelf.Model.extend({
tableName: 'threads',
hasTimestamps: false,
})
var Comment = db.Comment = bookshelf.Model.extend({
tableName: 'comments',
hasTimestamps: false,
})
var File = db.File = bookshelf.Model.extend({
tableName: 'files',
hasTimestamps: false,
})
var Keyword = db.Keyword = bookshelf.Model.extend({
tableName: 'keywords',
hasTimestamps: false,
})
var Mailbox = db.Mailbox = bookshelf.Model.extend({
tableName: 'boxes',
hasTimestamps: false,
})
var Message = db.Message = bookshelf.Model.extend({
tableName: 'messages',
hasTimestamps: false,
})
/* USERS */
db.createUser = function(data){
return new db.User(data).save()
}
db.getUsers = function () {
return User.query(function(qb){
qb.orderBy("id", "desc")
}).fetchAll()
}
db.getUser = function(id) {
var model = new User({'id': id})
return model.fetch()
}
db.getUserByUsername = function(username) {
var model = new User({'username': username})
return model.fetch()
}
/* THREADS */
db.getLatestThreads = function () {
return Thread.query(function(qb){
qb.orderBy("id", "desc").limit(50)
}).fetchAll()
}
/* FILES */
db.getFilesForThread = function (id){
return File.query("where", "thread", "=", id).fetchAll()
}
db.getFileCounts = function(ids){
return knex.column('thread').count('* as count').select().from('files').where('thread', 'in', ids).groupBy('thread')
}
db.getFileSizes = function(ids){
return knex.column('thread').sum('size as size').select().from('files').where('thread', 'in', ids).groupBy('thread')
}
/* COMMENTS */
db.getCommentsForThread = function (id){
return Comment.query("where", "thread", "=", id).fetchAll()
}
db.getCommentCounts = function(ids){
return knex.column('thread').count('* as count').select().from('comments').where('thread', 'in', ids).groupBy('thread')
}
|