Exemplo n.º 1
0
Arquivo: db.js Projeto: epochtalk/core
boards.find = function(id) {
  var addProperty = function(key, keyName) {
    return db.metadata.getAsync(key)
    .then(function(value) {
      if (_.isNumber(board[keyName]) && _.isNumber(value)) {
        board[keyName] += Number(value);
      }
      else if (_.isNumber(value)) {
        board[keyName] = Number(value);
      }
      else if (_.isString(value)) {
        board[keyName] = value;
      }
    });
  };

  var board;
  var boardKey = Board.key(id);
  var postCountKey = Board.postCountKey(id);
  var threadCountKey = Board.threadCountKey(id);
  var totalPostCountKey = Board.totalPostCountKey(id);
  var totalThreadCountKey = Board.totalThreadCountKey(id);
  var lastPostUsernameKey = Board.lastPostUsernameKey(id);
  var lastPostCreatedAtKey = Board.lastPostCreatedAtKey(id);
  var lastThreadTitleKey = Board.lastThreadTitleKey(id);
  var lastThreadIdKey = Board.lastThreadIdKey(id);

  return db.content.getAsync(boardKey)
  .then(function(boardDb) {
    board = boardDb;
    board.post_count = 0;
    board.thread_count = 0;
    if (board.children_ids && board.children_ids.length > 0) {
      board.children = [];
      return Promise.all(board.children_ids.map(function(childId) {
        return boards.find(childId)
        .then(function(childBoard) {
          board.children.push(childBoard);
        });
      }));
    }
  })
  .then(function() {
    return Promise.join(
      addProperty(postCountKey, 'post_count'),
      addProperty(threadCountKey, 'thread_count'),
      addProperty(totalPostCountKey, 'total_post_count'),
      addProperty(totalThreadCountKey, 'total_thread_count'),
      addProperty(lastPostUsernameKey, 'last_post_username'),
      addProperty(lastPostCreatedAtKey, 'last_post_created_at'),
      addProperty(lastThreadTitleKey, 'last_thread_title'),
      addProperty(lastThreadIdKey, 'last_thread_id'),
      function() { return board; });
  });
};
Exemplo n.º 2
0
Arquivo: db.js Projeto: epochtalk/core
boards.create = function(board) {
  // insert into db
  var timestamp = Date.now();
  if (!board.created_at) {
    board.created_at = timestamp;
    board.updated_at = timestamp;
  }
  else if (!board.updated_at) {
    board.updated_at = board.created_at;
  }
  board.id = helper.genId(board.created_at);
  var boardKey = Board.key(board.id);
  var boardLastPostUsernameKey = Board.lastPostUsernameKey(board.id);
  var boardLastPostCreatedAtKey = Board.lastPostCreatedAtKey(board.id);
  var boardLastThreadTitleKey = Board.lastThreadTitleKey(board.id);
  var boardLastThreadIdKey = Board.lastThreadIdKey(board.id);
  var totalPostCountKey = Board.totalPostCountKey(board.id);
  var totalThreadCountKey = Board.totalThreadCountKey(board.id);
  var postCountKey = Board.postCountKey(board.id);
  var threadCountKey = Board.threadCountKey(board.id);

  var metadataBatch = [
    // TODO: There should be a better solution than initializing with strings
    { type: 'put', key: boardLastPostUsernameKey , value: 'none' },
    { type: 'put', key: boardLastPostCreatedAtKey , value: 0 },
    { type: 'put', key: boardLastThreadTitleKey , value: 'none' },
    { type: 'put', key: boardLastThreadIdKey , value: 'none' },
    { type: 'put', key: totalPostCountKey , value: 0 },
    { type: 'put', key: totalThreadCountKey , value: 0 },
    { type: 'put', key: postCountKey , value: 0 },
    { type: 'put', key: threadCountKey , value: 0 }
  ];
  return db.metadata.batchAsync(metadataBatch)
  .then(function() {
    if (board.parent_id) {
      return addChildToBoard(board.id, board.parent_id);
    }
    else { return; }
  })
  .then(function() { return db.content.putAsync(boardKey, board); })
  .then(function() { return board; });
};
Exemplo n.º 3
0
Arquivo: db.js Projeto: epochtalk/core
boards.decTotalPostCount = function(id) {
  var count;
  var totalPostCountKey = Board.totalPostCountKey(id);

  return new Promise(function(fulfill, reject) {
    postCountLock.runwithlock(function () {
      var promise = { fulfill: fulfill, reject: reject };
      decrement(totalPostCountKey, postCountLock, promise);
    });
  })
  .then(function(dbCount) { count = dbCount; })
  .then(function() { return boards.find(id); })
  .then(function(board) { return board.parent_id; })
  .then(function(parentId) {
    if (parentId && count > 0) {
      return boards.decTotalPostCount(parentId)
      .then(function() { return count; });
    }
    else { return count; }
  });
};
Exemplo n.º 4
0
Arquivo: db.js Projeto: epochtalk/core
 .then(function() {
   var postCountKey = Board.postCountKey(id);
   var threadCountKey = Board.threadCountKey(id);
   var totalPostCountKey = Board.totalPostCountKey(id);
   var totalThreadCountKey = Board.totalThreadCountKey(id);
   var lastPostUsernameKey = Board.lastPostUsernameKey(id);
   var lastPostCreatedAtKey = Board.lastPostCreatedAtKey(id);
   var lastThreadTitleKey = Board.lastThreadTitleKey(id);
   var lastThreadIdKey = Board.lastThreadIdKey(id);
   var deleteBatch = [
     { type: 'del', key: postCountKey },
     { type: 'del', key: threadCountKey },
     { type: 'del', key: totalPostCountKey },
     { type: 'del', key: totalThreadCountKey },
     { type: 'del', key: lastPostUsernameKey },
     { type: 'del', key: lastPostCreatedAtKey },
     { type: 'del', key: lastThreadTitleKey },
     { type: 'del', key: lastThreadIdKey }
   ];
   return db.metadata.batchAsync(deleteBatch);
 })