Common for Models

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.

module.exports = do ->

Dependencies

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

Description

This module regroups shared functions for several models.

  common = {}

addSetInit

This function throws errors for common parameters missing or mistyped. It takes three arguments :

  common.addSetInit = (params, callback, strFields) ->
    if not ld.isObject params
      throw new TypeError 'parameters are mandatory for creation'
    if not ld.isFunction callback
      throw new TypeError 'callback must be a function'
    if not ld.isUndefined params._id
      if not ld.isString(params._id) or ld.isEmpty(params._id)
        throw new TypeError '_id, when defined, must be a string'
    if strFields
      isFS = (s) -> ld.isString(s) and not ld.isEmpty(s)
      ld.forEach strFields, (s) ->
        if not isFS params[s]
          throw new TypeError(s + ' must be a string')

checkExistence

checkExistence is an asynchronous function that takes

  common.checkExistence = (key, callback) ->
    storage.db.get key, (err, res) ->
      return callback err if err
      callback null, !!res

checkMultiExist

checkMultiExist is an asynchronous function that uses common.checkExistence to check multiple keys. It takes :

At the first not found record, callback will be called. FIXME: TCO ?

  common.checkMultiExist = (keys, callback) ->
    done = (err, res) ->
      if keys.length
        return callback err if err
        return callback null, res if not res
        common.checkExistence keys.pop(), done
      else
        return callback null, res
    done null, true

getDel

Model common reading

This function takes mandatory arguments

  common.getDel = (del, PREFIX, key, callback) ->
    throw new TypeError 'key must be a string' if not ld.isString key
    if not ld.isFunction callback
      throw new TypeError 'callback must be a function'
    key = PREFIX + key
    storage.db.get key, (err, obj) ->
      return callback err if err
      if ld.isUndefined obj
        return callback new Error 'key is not found'
      if not del
        return callback null, obj
      else
        storage.db.remove key, (err) ->
          return callback err if err
          return callback null, obj

  return common