rudiment.js
function(doc) {
      var that = this;
      doc = that._in_map(doc);

      return this._init.then(function() {
        return that.isAdmissible(doc);
      }).then(function(admissible) {
        if (!admissible) {
          throw new Error('Document not admissible');
        }

        doc = that.clean(doc);

        if (that._index) {
          delete doc[that._index];
        }

        return that.getNextIndex();
      }).then(function(max) {
        if (that._index && typeof max === 'number') {
          doc[that._index] = max;
        }

        return that._dbApi.create(doc).then(function(doc) {
          if (!doc) {
            throw new Error('Document not created');
          }

          return doc;
        });
      });
    }
function(doc) {
      var that = this;
      doc = that._in_map(doc);

      return this._init.then(function() {
        return that.isAdmissible(doc);
      }).then(function(admissible) {
        if (!admissible) {
          throw new Error('Document not admissible');
        }

        doc = that.clean(doc);

        if (that._index) {
          delete doc[that._index];
        }

        return that.getNextIndex();
      }).then(function(max) {
        if (that._index && typeof max === 'number') {
          doc[that._index] = max;
        }

        return that._dbApi.create(doc).then(function(doc) {
          if (!doc) {
            throw new Error('Document not created');
          }

          return doc;
        });
      });
    }
function ( doc ) { var that = this ; doc = that . _in_map ( doc ) ; return this . _init . then ( function ( ) { return that . isAdmissible ( doc ) ; } ) . then ( function ( admissible ) { if ( ! admissible ) { throw new Error ( 'Document not admissible' ) ; } doc = that . clean ( doc ) ; if ( that . _index ) { delete doc [ that . _index ] ; } return that . getNextIndex ( ) ; } ) . then ( function ( max ) { if ( that . _index && typeof max === 'number' ) { doc [ that . _index ] = max ; } return that . _dbApi . create ( doc ) . then ( function ( doc ) { if ( ! doc ) { throw new Error ( 'Document not created' ) ; } return doc ; } ) ; } ) ; }
Create and insert a new document into the database

@param {Object} doc - a document to insert
@return {Promise}
Create and insert a new document into the database