Configuration 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 is the module for MyPads configuration.

module.exports = do ->

Dependencies

  ld = require 'lodash'
  storage = require './storage.js'
  db = storage.db

The closure contains a private DEFAULTS field, holding defaults settings. Configuration data is taken from the database, applying defaults when necessary, for example at the plugin initialization.

  DEFAULTS =
    passwordMin: 8
    passwordMax: 30
    sessionSecret: 'LvP@WHB#I5)u@u@nAX9RL*n7CEQPxs'
  DBPREFIX = storage.DBPREFIX.CONF

configuration object is a closure to interact with the whole config. It will be exported.

  configuration =

init is called when mypads plugin is initialized. It fixes the default data for the configuration into the database. It takes an optional callback function used after db.set abstraction to return an eventual error.

    init: (callback) ->
      callback ?= ld.noop
      if not ld.isFunction callback
        throw new TypeError 'callback must be a function'

Would like to use doBulk but not supported for all ueberDB backends

      kv = ld.transform DEFAULTS, (memo, val, key) ->
        memo[DBPREFIX + key] = val
      storage.fn.setKeys kv, callback

get is an asynchronous function taking :

    get: (key, callback) ->
      if not ld.isString key
        throw new TypeError 'key must be a string'
      if not ld.isFunction callback
        throw new TypeError 'callback must be a function'
      db.get DBPREFIX + key, (err, res) ->
        return callback err if err
        if ld.isUndefined res
          return callback new Error 'Key doesn\'t exist'
        callback null, res

set is an asynchronous function taking two mandatory arguments:

set sets the value for the configuration key.

    set: (key, value, callback) ->
      if not ld.isString key
        throw new TypeError 'key must be a string'
      if  ld.isUndefined value
        throw new TypeError 'value is mandatory'
      if not ld.isFunction callback
        throw new TypeError 'callback must be a function'
      db.set DBPREFIX + key, value, callback

del is an asynchronous function that removes a configuration option. It takes two mandatory arguments :

    del: (key, callback) ->
      if not ld.isString key
        throw new TypeError 'key must be a string'
      if not ld.isFunction callback
        throw new TypeError 'callback must be a function'
      db.remove DBPREFIX + key, callback

all is an asynchronous function that returns the whole configuration from database. Fields / keys are unprefixed. It needs a callback function returning Error if error, null otherwise and the result.

    all: (callback) ->
      if not ld.isFunction callback
        throw new TypeError 'callback must be a function'
      db.findKeys "#{DBPREFIX}*", null, (err, keys) ->
        return callback err if err
        storage.fn.getKeys keys, (err, results) ->
          if results
            results = ld.transform results, (memo, val, key) ->
              memo[key.replace DBPREFIX, ''] = val
          callback arguments[0], results

  return configuration