lib/index.js
function toRawDoc(typeInfo, obj) {
    obj = extend(true, {}, obj);
    var doc = {};

    if (obj.rev) {
      doc._rev = obj.rev;
      delete obj.rev;
    }

    if (obj.attachments) {
      doc._attachments = obj.attachments;
      delete obj.attachments;
    }

    var id = obj.id || uuid();
    delete obj.id;
    doc._id = serialize(typeInfo.documentType, id);

    if (typeInfo.relations) {
      Object.keys(typeInfo.relations).forEach(function (field) {
        var relationDef = typeInfo.relations[field];
        var relationType = Object.keys(relationDef)[0];

        if (relationType === 'belongsTo') {
          if (obj[field] && typeof obj[field].id !== 'undefined') {
            obj[field] = obj[field].id;
          }
        } else { // hasMany
          var relatedType = relationDef[relationType];
          if (relatedType.options && relatedType.options.queryInverse) {
            delete obj[field];
            return;
          }
          if (obj[field]) {
            var dependents = obj[field].map(function (dependent) {
              if (dependent && typeof dependent.id !== 'undefined') {
                return dependent.id;
              }
              return dependent;
            });
            obj[field] = dependents;
          } else {
            obj[field] = [];
          }
        }
      });
    }

    doc.data = obj;

    return doc;
  }
function toRawDoc(typeInfo, obj) {
    obj = extend(true, {}, obj);
    var doc = {};

    if (obj.rev) {
      doc._rev = obj.rev;
      delete obj.rev;
    }

    if (obj.attachments) {
      doc._attachments = obj.attachments;
      delete obj.attachments;
    }

    var id = obj.id || uuid();
    delete obj.id;
    doc._id = serialize(typeInfo.documentType, id);

    if (typeInfo.relations) {
      Object.keys(typeInfo.relations).forEach(function (field) {
        var relationDef = typeInfo.relations[field];
        var relationType = Object.keys(relationDef)[0];

        if (relationType === 'belongsTo') {
          if (obj[field] && typeof obj[field].id !== 'undefined') {
            obj[field] = obj[field].id;
          }
        } else { // hasMany
          var relatedType = relationDef[relationType];
          if (relatedType.options && relatedType.options.queryInverse) {
            delete obj[field];
            return;
          }
          if (obj[field]) {
            var dependents = obj[field].map(function (dependent) {
              if (dependent && typeof dependent.id !== 'undefined') {
                return dependent.id;
              }
              return dependent;
            });
            obj[field] = dependents;
          } else {
            obj[field] = [];
          }
        }
      });
    }

    doc.data = obj;

    return doc;
  }
function toRawDoc ( typeInfo , obj ) { obj = extend ( true , { } , obj ) ; var doc = { } ; if ( obj . rev ) { doc . _rev = obj . rev ; delete obj . rev ; } if ( obj . attachments ) { doc . _attachments = obj . attachments ; delete obj . attachments ; } var id = obj . id || uuid ( ) ; delete obj . id ; doc . _id = serialize ( typeInfo . documentType , id ) ; if ( typeInfo . relations ) { Object . keys ( typeInfo . relations ) . forEach ( function ( field ) { var relationDef = typeInfo . relations [ field ] ; var relationType = Object . keys ( relationDef ) [ 0 ] ; if ( relationType === 'belongsTo' ) { if ( obj [ field ] && typeof obj [ field ] . id !== 'undefined' ) { obj [ field ] = obj [ field ] . id ; } } else { // hasMany var relatedType = relationDef [ relationType ] ; if ( relatedType . options && relatedType . options . queryInverse ) { delete obj [ field ] ; return ; } if ( obj [ field ] ) { var dependents = obj [ field ] . map ( function ( dependent ) { if ( dependent && typeof dependent . id !== 'undefined' ) { return dependent . id ; } return dependent ; } ) ; obj [ field ] = dependents ; } else { obj [ field ] = [ ] ; } } } ) ; } doc . data = obj ; return doc ; }
Transform a relational object into a PouchDB doc.
Transform a relational object into a PouchDB doc .