Skip to content

Commit

Permalink
Merge pull request #53 from artemis-ag/fix-delete-method
Browse files Browse the repository at this point in the history
Fix store delete method
  • Loading branch information
Joe Torreggiani authored Jun 16, 2020
2 parents b29ca50 + 4f1a863 commit 6bcb179
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 124 deletions.
75 changes: 33 additions & 42 deletions dist/mobx-async-store.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ function ObjectPromiseProxy(promise, target) {
// uuid id as its only reference to the newly persisted record.
// TODO: Figure out a way to update associated records to use the
// newly persisted id.
target.store.data[target.type].records[tmpId] = target;
target.store.data[target.type].records[target.id] = target;
target.store.data[target.type].records.set(String(tmpId), target);
target.store.data[target.type].records.set(String(target.id), target);
});
return _context.abrupt("return", target);

Expand Down Expand Up @@ -1506,8 +1506,8 @@ function () {
value: function reset(type) {
if (type) {
this.data[type] = {
records: {},
cache: {}
records: mobx.observable.map({}),
cache: mobx.observable.map({})
};
} else {
this.initializeObservableDataProperty();
Expand Down Expand Up @@ -1578,8 +1578,8 @@ function () {

types.forEach(function (modelKlass) {
_this2.data[modelKlass.type] = {
records: {},
cache: {}
records: mobx.observable.map({}),
cache: mobx.observable.map({})
};
});
}
Expand Down Expand Up @@ -1666,7 +1666,7 @@ function () {
throw new Error("Could not find a collection for type '".concat(type, "'"));
}

var record = this.getType(type).records[id];
var record = this.getType(type).records.get(String(id));
if (!record || record === 'undefined') return;
return record;
}
Expand All @@ -1681,7 +1681,7 @@ function () {
}, {
key: "getRecords",
value: function getRecords(type) {
var records = Object.values(this.getType(type).records).filter(function (value) {
var records = Array.from(this.getType(type).records.values()).filter(function (value) {
return value && value !== 'undefined';
}); // NOTE: Handles a scenario where the store keeps around a reference
// to a newly persisted record by its temp uuid. This is required
Expand Down Expand Up @@ -1739,15 +1739,15 @@ function () {
}, {
key: "getCachedIds",
value: function getCachedIds(type, url) {
var ids = this.getType(type).cache[url];
var ids = this.getType(type).cache.get(url);
if (!ids) return [];
var idsSet = new Set(mobx.toJS(ids));
return Array.from(idsSet);
}
/**
* Gets records from store based on cached query
*
* @method getCachedIds
* @method getCachedId
* @param {String} type
* @param {String} url
* @return {Array} array of ids
Expand All @@ -1756,7 +1756,7 @@ function () {
}, {
key: "getCachedId",
value: function getCachedId(type, id) {
return this.getType(type).cache[id];
return this.getType(type).cache.get(String(id));
}
/**
* Get multiple records by id
Expand Down Expand Up @@ -1824,8 +1824,6 @@ function () {
}, {
key: "createOrUpdateModel",
value: function createOrUpdateModel(dataObject) {
var _this4 = this;

var _dataObject$attribute = dataObject.attributes,
attributes = _dataObject$attribute === void 0 ? {} : _dataObject$attribute,
id = dataObject.id,
Expand All @@ -1838,7 +1836,6 @@ function () {
// Update existing object attributes
Object.keys(attributes).forEach(function (key) {
mobx.set(record, key, attributes[key]);
mobx.set(_this4.data[type].records, id, record);
}); // If relationships are present, update relationships

if (relationships) {
Expand All @@ -1847,7 +1844,6 @@ function () {
if (!relationships[key].meta) {
// defensive against existingRecord.relationships being undefined
mobx.set(record, 'relationships', _objectSpread$2({}, record.relationships, _defineProperty({}, key, relationships[key])));
mobx.set(_this4.data[type].records, id, record);
}
});
}
Expand All @@ -1860,9 +1856,9 @@ function () {
attributes: attributes,
relationships: relationships
});
this.data[type].records[record.id] = record;
}

this.data[type].records.set(String(record.id), record);
return record;
}
/**
Expand All @@ -1875,15 +1871,15 @@ function () {
}, {
key: "createModelsFromData",
value: function createModelsFromData(data) {
var _this5 = this;
var _this4 = this;

return mobx.transaction(function () {
return data.map(function (dataObject) {
// Only build objects for which we have a type defined.
// And ignore silently anything else included in the JSON response.
// TODO: Put some console message in development mode
if (_this5.getType(dataObject.type)) {
return _this5.createOrUpdateModel(dataObject);
if (_this4.getType(dataObject.type)) {
return _this4.createOrUpdateModel(dataObject);
}
});
});
Expand Down Expand Up @@ -1950,7 +1946,7 @@ function () {
var _fetchAll = _asyncToGenerator(
/*#__PURE__*/
_regeneratorRuntime.mark(function _callee(type, queryParams) {
var _this6 = this;
var _this5 = this;

var store, url, response, json;
return _regeneratorRuntime.wrap(function _callee$(_context) {
Expand All @@ -1972,7 +1968,7 @@ function () {
break;
}

this.data[type].cache[url] = [];
this.data[type].cache.set(url, []);
_context.next = 9;
return response.json();

Expand All @@ -1990,15 +1986,18 @@ function () {
attributes = _dataObject$attribute2 === void 0 ? {} : _dataObject$attribute2,
_dataObject$relations2 = dataObject.relationships,
relationships = _dataObject$relations2 === void 0 ? {} : _dataObject$relations2;
var ModelKlass = _this6.modelTypeIndex[type];
var ModelKlass = _this5.modelTypeIndex[type];
var record = new ModelKlass(_objectSpread$2({
store: store,
relationships: relationships
}, attributes));

_this6.data[type].cache[url].push(id);
var cachedIds = _this5.data[type].cache.get(url);

_this5.data[type].cache.set(url, [].concat(_toConsumableArray(cachedIds), [id]));

_this5.data[type].records.set(String(id), record);

_this6.data[type].records[id] = record;
return record;
});
}));
Expand Down Expand Up @@ -2051,7 +2050,7 @@ function () {
response = _context2.sent;

if (!(response.status === 200)) {
_context2.next = 16;
_context2.next = 15;
break;
}

Expand All @@ -2067,14 +2066,13 @@ function () {
}

record = this.createOrUpdateModel(data);
this.data[type].cache[url] = [];
this.data[type].cache[url].push(record.id);
this.data[type].cache.set(url, [record.id]);
return _context2.abrupt("return", record);

case 16:
case 15:
return _context2.abrupt("return", null);

case 17:
case 16:
case "end":
return _context2.stop();
}
Expand Down Expand Up @@ -2103,17 +2101,18 @@ function () {
enumerable: true,
writable: true,
initializer: function initializer() {
var _this7 = this;
var _this6 = this;

return function (type, attributes) {
var id = dbOrNewId(attributes);

var model = _this7.createModel(type, id, {
var model = _this6.createModel(type, id, {
attributes: attributes
}); // Add the model to the type records index


_this7.data[type].records[id] = model;
_this6.data[type].records.set(String(id), model);

return model;
};
}
Expand All @@ -2122,18 +2121,10 @@ function () {
enumerable: true,
writable: true,
initializer: function initializer() {
var _this8 = this;
var _this7 = this;

return function (type, id) {
var records = _this8.getRecords(type);

_this8.data[type].records = records.reduce(function (hash, record) {
if (String(record.id) !== String(id)) {
hash[record.id] = record;
}

return hash;
}, {});
_this7.data[type].records.delete(String(id));
};
}
})), _class$1);
Expand Down
Loading

0 comments on commit 6bcb179

Please sign in to comment.