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.
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
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
is a function for multiple asynchronous gets and removes,
taking :
del
boolean, for removals to truekeys
array, wich contains a list of keys to retrievecallback
function, called if error or when finished with null and
the results
object, which is composed of keys and values for gets,
true for removals
FIXME: TCO ? 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
is an helper around storage.fn.getDelKeys
with del
argument
to false.
storage.fn.getKeys = ld.partial storage.fn.getDelKeys, false
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 :
kv
object, wich contains a list a keys and values to setcallback
function, called if error or when finished with null
FIXME: TCO ? 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