diff options
| author | Jules Laplace <jules@okfoc.us> | 2015-09-25 17:36:53 -0400 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2015-09-25 17:36:53 -0400 |
| commit | 3aa171fbaf05d0ee5b82673443da51ed43719475 (patch) | |
| tree | 291426c3102cffff23f8b202ec14cd03df91e799 /lib/db/index.js | |
| parent | ccd2a51e9206aa858313ae97550b09b9bf8476db (diff) | |
moving things around
Diffstat (limited to 'lib/db/index.js')
| -rw-r--r-- | lib/db/index.js | 156 |
1 files changed, 0 insertions, 156 deletions
diff --git a/lib/db/index.js b/lib/db/index.js deleted file mode 100644 index b6fa235..0000000 --- a/lib/db/index.js +++ /dev/null @@ -1,156 +0,0 @@ -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() -} -db.getLastlog = function(limit){ - return knex.column('username').column('lastseen').select().from('users').orderBy('lastseen', 'desc').limit(limit || 10) -} - - -/* THREADS */ - -db.getLatestThreads = function () { - return Thread.query(function(qb){ - qb.orderBy("id", "desc").limit(50) - }).fetchAll() -} -db.getThreadsForKeyword = function (keyword) { - return Thread.query(function(qb){ - qb.where("keyword", "=", keyword).orderBy("id", "desc") - }).fetchAll() -} -db.getThread = function (id) { - return Thread.query("where", "id", "=", id).fetch() -} - - -/* 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, limit, offset, order){ - order = order || "asc" - return Comment.query(function(qb){ - qb.where("thread", "=", id).orderBy("id", order) - if (limit) { - qb.limit(limit) - } - if (offset) { - qb.offset(offset) - } - }).fetchAll().then(function(comments){ - comments.forEach(function(comment){ - comment.set("comment", comment.get("comment").toString() ) - }) - return comments - }) -} -db.getCommentCounts = function(ids){ - return knex.column('thread').count('* as count').select().from('comments').where('thread', 'in', ids).groupBy('thread') -} - - -/* KEYWORDS */ - -db.getKeywords = function (keywords){ - return Keyword.query("where", "keyword", "in", keywords).fetchAll() -} -db.getKeyword = function (keyword) { - return Keyword.query("where", "keyword", "=", keyword).fetch() -} - - -/* MAILBOXES */ - -db.getMailboxes = function(username){ - return Mailbox.query("where", "owner", "=", username).fetchAll() -} -db.getMailboxCounts = function(boxes){ - return knex.column('mbox').count('* as count').select().from('messages').where('mbox', 'in', boxes).groupBy('mbox') -} - - -/* MESSAGES */ - -db.getMessages = function(username, box, limit, offset){ - var mbox = username + "." + box - return Message.query(function(qb){ - qb.column('id', 'mbox', 'unread', 'sender', 'recipient', 'date', 'subject', knex.raw("CHAR_LENGTH(body) as size")).where("mbox", "=", mbox).orderBy("id", "desc") - if (limit) { - qb.limit(limit) - } - if (offset) { - qb.offset(offset) - } - }).fetchAll() -} -db.getMessage = function (id){ - var model = new Message({'id': id}) - return model.fetch().then(function(message){ - message.set("body", message.get("body").toString() ) - return message - }) -} |
