diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2017-12-08 02:18:49 +0100 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2017-12-08 02:18:49 +0100 |
| commit | bbbd8bbab8737f5067c85376daf79cd8a5a9c4cb (patch) | |
| tree | 844d1385d7b88623eee3c2ea2c420280006ad349 /bucky/db | |
| parent | e4e0cf21a31b74d5ee1e6d45b343ea60ed44f372 (diff) | |
| parent | df674eef8e20c43426c0af5aa3d1a09b5e24c58a (diff) | |
merge
Diffstat (limited to 'bucky/db')
| -rw-r--r-- | bucky/db/bookshelf.js | 24 | ||||
| -rw-r--r-- | bucky/db/fortune.js | 28 | ||||
| -rw-r--r-- | bucky/db/index.js | 187 |
3 files changed, 239 insertions, 0 deletions
diff --git a/bucky/db/bookshelf.js b/bucky/db/bookshelf.js new file mode 100644 index 0000000..69157cc --- /dev/null +++ b/bucky/db/bookshelf.js @@ -0,0 +1,24 @@ +var knex = require('knex')({ + client: 'mysql2', + connection: { + host : process.env.DB_HOST, + user : process.env.DB_USER, + password : process.env.DB_PASS, + database : process.env.DB_NAME, + charset : 'utf8', + typecast : function (field, next) { + console.log(field.type) + if (field.type == 'BLOB') { + return field.string() + } + return next() + } + } +}) + +var bookshelf = require('bookshelf')(knex) + +module.exports = { + bookshelf: bookshelf, + knex: knex, +} diff --git a/bucky/db/fortune.js b/bucky/db/fortune.js new file mode 100644 index 0000000..7adba5a --- /dev/null +++ b/bucky/db/fortune.js @@ -0,0 +1,28 @@ +function choice (a){ return a[ Math.floor(Math.random()*a.length) ] } + +var fs = require("fs"), path = require("path") +var fortunes = {} +var dir = "fortune" + +fs.readdirSync(path.resolve(dir)).forEach(function(fn){ + + var file = dir + '/' + fn + var stat = fs.statSync(file) + + if (stat && ! stat.isDirectory()) { + fortunes[fn] = fs.readFileSync(file) + .toString() + .split("\n") + .filter(function(s){ return !! s }) + } + +}) + +module.exports = function(tag){ + if (tag in fortunes) { + return choice(fortunes[tag]) + } + else { + return "bucky" + } +}
\ No newline at end of file diff --git a/bucky/db/index.js b/bucky/db/index.js new file mode 100644 index 0000000..f376308 --- /dev/null +++ b/bucky/db/index.js @@ -0,0 +1,187 @@ +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("lastmodified", "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() +} +db.createThread = function(data){ + return new db.Thread(data).save() +} +db.updateThread = function(data){ +} +db.removeThread = function(id){ +} + +/* 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') +} +db.createFile = function(data){ + return new db.File(data).save() +} +db.removeFile = function(id){ +} + +/* 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') +} +db.createComment = function(data){ + return new db.Comment(data).save() +} +db.updateComment = function(data){ +} +db.removeComment = function(id){ +} + + +/* KEYWORDS */ + +db.getKeywords = function (keywords){ + return Keyword.query("where", "keyword", "in", keywords).fetchAll() +} +db.getKeyword = function (keyword) { + return Keyword.query("where", "keyword", "=", keyword).fetch() +} +db.createKeyword = function(data){ + return new db.Keyword(data).save() +} +db.updateKeyword = function(data){ +} +db.removeKeyword = function(id){ +} + + +/* 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') +} +db.createMailbox = function(data){ +} + +/* 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 + }) +} +db.createMessage = function(data){ + return new db.Message(data).save() +} +db.updateMessage = function(data){ +} +db.removeMessage = function(id){ +} |
