Database Module

License

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Description

This module consists only on a wrapper around etherpad database.

module.exports = do ->

Dependencies

  ld = require 'lodash'

  storage = {}

Database PREFIX CONSTANTS

  storage.DBPREFIX = GLOBAL: 'mypads:'
  DBPG = storage.DBPREFIX.GLOBAL
  storage.DBPREFIX.CONF = "#{DBPG}conf:"
  storage.DBPREFIX.USER = "#{DBPG}user:"
  storage.DBPREFIX.GROUP = "#{DBPG}group:"
  storage.DBPREFIX.PAD = "#{DBPG}pad:"

Normal case : when installed as a plugin

  try
    storage.db = require('ep_etherpad-lite/node/db/DB').db
  catch e

Testing case : we need to mock the database connection, using ueberDB and coherent default configuration with eptherpad-lite one.

    ueberDB = require 'ueberDB'
    storage.db = new ueberDB.database 'dirty', filename: './test.db'
    storage.db.init (err) -> console.log err if err

init function for in memory secondary indexes. At the moment only user / logins.

  storage.init = (callback) ->
    user = require './model/user.js'
    user.init callback

Internal functions fn

These functions are not private like with closures, for testing purposes, but they are expected be used only internally by other MyPads functions.

  storage.fn = {}

getDelKeys

getDelKeys is a function for multiple asynchronous gets and removes, taking :

  storage.fn.getDelKeys = (del, keys, callback) ->
    results = if del then true else {}
    action = if del then 'remove' else 'get'
    getDel = (k) ->
      storage.db[action](k, (err, res) ->
        return callback err if err
        results[k] = res if not del
        done()
      )
    done = ->
      if keys.length
        getDel keys.pop()
      else
        return callback null, results
    done()

getKeys

getKeys is an helper around storage.fn.getDelKeys with del argument to false.

  storage.fn.getKeys = ld.partial storage.fn.getDelKeys, false

delKeys

delKeys is an helper around storage.fn.getDelKeys with del argument to true.

  storage.fn.delKeys = ld.partial storage.fn.getDelKeys, true

setKeys is a function for multiple asynchronous sets, taking :

  storage.fn.setKeys = (kv, callback) ->
    pairs = ld.pairs kv
    set = (k, v) -> storage.db.set k, v, done
    done = (err) ->
      return callback err if err
      if pairs.length
        pair = pairs.pop()
        set pair[0], pair[1]
      else
        return callback null
    done()

  return storage