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'
This module regroups shared functions for several models.
common = {}
This function throws errors for common parameters missing or mistyped. It takes three arguments :
params
JS objectcallback
functionstrFields
array, with fields of the params
objects that
must be not empty strings 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
is an asynchronous function that takes
key
callback
function, returnning an Error or a boolean for existence common.checkExistence = (key, callback) ->
storage.db.get key, (err, res) ->
return callback err if err
callback null, !!res
checkMultiExist
is an asynchronous function that uses
common.checkExistence
to check multiple keys.
It takes :
keys
callback
function, returning an Error or null and a boolean for
existence.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
Model common reading
This function takes mandatory arguments
del
boolean, to add a second step, removal, in the case of truePREFIX
, used to compute real keykey
, the unique identifier of the objectcallback
function, that returns an error if there is a problem or if
the key is not found. In the other case, it returns null if del
or
null plus the model object. 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