redgeoff / delta-pouch

Conflict-free collaborative editing for PouchDB
196 stars 13 forks source link

Document Creation Date and Time #46

Open aesculus opened 9 years ago

aesculus commented 9 years ago

It looks like the Save function keeps track of the actual date and time of a save and that is used when the document is merged versus the sequency or _rev? Is that correct?

function save(db, doc) {
  delete(doc._rev); // delete any revision numbers copied from previous docs
  doc.$createdAt = (new Date()).toJSON();
  if (doc.$id) { // update?
    // this format guarantees the docs will be retrieved in order they were created
    doc._id = doc.$id + '_' + doc.$createdAt;
    return db.put(doc).then(function (response) {
      response.$id = doc.$id;
      return response;
    })["catch"](/* istanbul ignore next */ function (err) {
      // It appears there is a bug in pouch that causes a doc conflict even though we are creating a
      // new doc
      if (err.status !== 409) {
        throw err;
      }
    });
  } else { // new
    return db.post(doc).then(function (response) {
      response.$id = response.id;
      return response;
    });
  }
}