src/lib/common.js
function doCreate(data) {
  console.log('create called', data);

  var i, keys = false;

  if (data instanceof Array) {
    if (typeof data[0] === 'undefined') {
      return Promise.reject(new Error('Can\'t have undefined keys'));
    }
    // Check if a key is the first element
    if (keyTypes.indexOf(typeof data[0]) !== -1) {
      // Check for an even amount of elements
      if (data.length % 2) {
        return Promise.reject(new Error('uneven number of key/values given to create'));
      }

      // Check every second item are keys
      for (i = 2; i < data.length; i = i + 2) {
        if (keyTypes.indexOf(typeof data[i]) === -1) {
          return Promise.reject(new Error('Invalid key value for key ' + (i + 1)
              + ' (' + typeof data[i] + ' given)'));
        }
      }

      keys = true;
    } else {
      if (!this.options.id) {
        return Promise.reject('Can\'t give an array of data objects if not key has been specified');
      }

      // Check if each object value has or doesn't have an id
      for (i = 0; i < data.length; i++) {
        if (typeof data[i][this.options.id] === 'undefined') {
          return Promise.reject(new Error('Can\'t have objects in array without a key'));
        }
      }
    }

    return this.save.call(this, {
      keys: keys,
      data: data,
      replace: false
    });
  } else if (data instanceof Object) {
    // Find and update
    return this.save.call(this, {
      keys: true,
      data: data,
      replace: false
    });
  } else {
    return Promise.reject(new Error('Unknown data type given'));
  }
}
function doCreate(data) {
  console.log('create called', data);

  var i, keys = false;

  if (data instanceof Array) {
    if (typeof data[0] === 'undefined') {
      return Promise.reject(new Error('Can\'t have undefined keys'));
    }
    // Check if a key is the first element
    if (keyTypes.indexOf(typeof data[0]) !== -1) {
      // Check for an even amount of elements
      if (data.length % 2) {
        return Promise.reject(new Error('uneven number of key/values given to create'));
      }

      // Check every second item are keys
      for (i = 2; i < data.length; i = i + 2) {
        if (keyTypes.indexOf(typeof data[i]) === -1) {
          return Promise.reject(new Error('Invalid key value for key ' + (i + 1)
              + ' (' + typeof data[i] + ' given)'));
        }
      }

      keys = true;
    } else {
      if (!this.options.id) {
        return Promise.reject('Can\'t give an array of data objects if not key has been specified');
      }

      // Check if each object value has or doesn't have an id
      for (i = 0; i < data.length; i++) {
        if (typeof data[i][this.options.id] === 'undefined') {
          return Promise.reject(new Error('Can\'t have objects in array without a key'));
        }
      }
    }

    return this.save.call(this, {
      keys: keys,
      data: data,
      replace: false
    });
  } else if (data instanceof Object) {
    // Find and update
    return this.save.call(this, {
      keys: true,
      data: data,
      replace: false
    });
  } else {
    return Promise.reject(new Error('Unknown data type given'));
  }
}
function doCreate ( data ) { console . log ( 'create called' , data ) ; var i , keys = false ; if ( data instanceof Array ) { if ( typeof data [ 0 ] === 'undefined' ) { return Promise . reject ( new Error ( 'Can\'t have undefined keys' ) ) ; } // Check if a key is the first element if ( keyTypes . indexOf ( typeof data [ 0 ] ) !== - 1 ) { // Check for an even amount of elements if ( data . length % 2 ) { return Promise . reject ( new Error ( 'uneven number of key/values given to create' ) ) ; } // Check every second item are keys for ( i = 2 ; i < data . length ; i = i + 2 ) { if ( keyTypes . indexOf ( typeof data [ i ] ) === - 1 ) { return Promise . reject ( new Error ( 'Invalid key value for key ' + ( i + 1 ) + ' (' + typeof data [ i ] + ' given)' ) ) ; } } keys = true ; } else { if ( ! this . options . id ) { return Promise . reject ( 'Can\'t give an array of data objects if not key has been specified' ) ; } // Check if each object value has or doesn't have an id for ( i = 0 ; i < data . length ; i ++ ) { if ( typeof data [ i ] [ this . options . id ] === 'undefined' ) { return Promise . reject ( new Error ( 'Can\'t have objects in array without a key' ) ) ; } } } return this . save . call ( this , { keys : keys , data : data , replace : false } ) ; } else if ( data instanceof Object ) { // Find and update return this . save . call ( this , { keys : true , data : data , replace : false } ) ; } else { return Promise . reject ( new Error ( 'Unknown data type given' ) ) ; } }
Inserts data into the JSON database.

@param {Array|Object} data Either an object of key-value pairs, an array
containing key/value pairs ([key, value,...]) or, if the key field has
been specified, an array of object values each with the key field set

@returns {Promise} A promise that will resolve with an array containing
keys or errors in creating the data of the inserted data.
Inserts data into the JSON database .