Skip to content

Commit

Permalink
fix: support multiple createRecord calls (fixes pouchdb-community#239)
Browse files Browse the repository at this point in the history
This commit changes how ember-pouch implements `Adapter.createRecord`,
which is invoked after calling `.save()` on a new record,
so that it does not create multiple records if `.save()` is called
more than once before the first operation has finished.

If `.save()` is only invoked once before the record has finished persisting
to the DB (i.e. the promise that it returns has resolved) then
the behavior is unchanged. However, subsequent calls will wait for
the previously returned promise to resolve and then, if changes have
been made to the record, it will delegate the task to `updateRecord`.

To avoid a problem caused by ember-data changing the ID associated
with the internalModel/record when the record has finished persisting,
the `Adapter.generateIdForRecord` method has been implemented so
that it is available immediately. Previously ember-pouch had still
been generating this id during `createRecord`, but ember-data was
not being made aware of this until its returned promise resolved.

Finally, rather than rely on `adapter.db.rel.uuid()` to generate
an RFC4122 v4 UUID (requiring initialization to have completed),
this has been replaced by the equivalent `uuid` module from npm,
and the ember-auto-import addon has been installed to make it
easy to access this from within ember-pouch.
  • Loading branch information
jacobq committed Nov 11, 2019
1 parent 294b132 commit 881c84b
Show file tree
Hide file tree
Showing 4 changed files with 14,908 additions and 19 deletions.
55 changes: 38 additions & 17 deletions addon/adapters/pouch.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Ember from 'ember';
import DS from 'ember-data';
import { pluralize } from 'ember-inflector';
import { v4 } from 'uuid';
//import BelongsToRelationship from 'ember-data/-private/system/relationships/state/belongs-to';

import {
Expand Down Expand Up @@ -153,7 +154,7 @@ export default DS.RESTAdapter.extend({
willDestroy: function() {
this._stopChangesListener();
},

_indexPromises: [],

_init: function (store, type) {
Expand Down Expand Up @@ -206,8 +207,9 @@ export default DS.RESTAdapter.extend({
relModel = (typeof rel.type === 'string' ? store.modelFor(rel.type) : rel.type);
if (relModel) {
let includeRel = true;
if (!('options' in rel)) rel.options = {};

if (!('options' in rel)) {
rel.options = {};
}
if (typeof(rel.options.async) === "undefined") {
rel.options.async = config.emberPouch && !Ember.isEmpty(config.emberPouch.async) ? config.emberPouch.async : true;//default true from https://github.com/emberjs/data/pull/3366
}
Expand Down Expand Up @@ -464,27 +466,46 @@ export default DS.RESTAdapter.extend({
});
},

generateIdForRecord: function(/* store, type, inputProperties */) {
return v4();
},

createdRecords: {},
createRecord: function(store, type, record) {
this._init(store, type);
var data = this._recordToData(store, type, record);
let rel = this.get('db').rel;

let id = data.id;
if (!id) {
id = data.id = rel.uuid();
createRecord: function(store, type, snapshot) {
const record = snapshot.record;
if (record._emberPouchSavePromise) {
const changes = record.changedAttributes();
record._emberPouchSavePromise = record._emberPouchSavePromise.then(records => {
// If there have been changes since the document was created then we should update the record now
if (Object.keys(changes).length > 0) {
const rev = records[Object.keys(records)[0]][0].rev;
(snapshot.__attributes || snapshot._attributes).rev = rev; // FIXME: it should be possible to do this elsewhere
return this.updateRecord(store, type, snapshot);
}
return records;
});
return record._emberPouchSavePromise;
}

this._init(store, type);
var data = this._recordToData(store, type, snapshot);
const rel = this.get('db').rel;
const id = data.id;
this.createdRecords[id] = true;

return rel.save(this.getRecordTypeName(type), data).catch((e) => {
delete this.createdRecords[id];
throw e;
Object.defineProperty(record, '_emberPouchSavePromise', {
enumerable: false,
writable: true,
value: rel.save(this.getRecordTypeName(type), data).catch((e) => {
delete this.createdRecords[id];
throw e;
}),
});
return record._emberPouchSavePromise;
},

updateRecord: function (store, type, record) {
updateRecord: function (store, type, snapshot) {
this._init(store, type);
var data = this._recordToData(store, type, record);
var data = this._recordToData(store, type, snapshot);
return this.get('db').rel.save(this.getRecordTypeName(type), data);
},

Expand Down
10 changes: 9 additions & 1 deletion ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ const EmberAddon = require('ember-cli/lib/broccoli/ember-addon');

module.exports = function(defaults) {
let app = new EmberAddon(defaults, {
// Add options here
autoImport: {
webpack: {
node: {
global: true
}
},
// We could use ember-auto-import for these, but index.js is already handling them
exclude: ['pouchdb', 'pouchdb-find', 'relational-pouch']
}
});

/*
Expand Down
Loading

0 comments on commit 881c84b

Please sign in to comment.