Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix adapter format issues #83

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 22 additions & 17 deletions addon/adapters/localforage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import DS from 'ember-data';
import LFQueue from 'ember-localforage-adapter/utils/queue';
import LFCache from 'ember-localforage-adapter/utils/cache';
import { v4 as uuid } from "ember-uuid";
import localforage from 'localforage';

export default DS.Adapter.extend(Evented, {

Expand Down Expand Up @@ -31,7 +32,7 @@ export default DS.Adapter.extend(Evented, {
* @param {Object|String|Integer|null} id
*/
findRecord(store, type, id) {
return this._getNamespaceData(type).then((namespaceData) => {
return this._getNamespaceData(type).then(namespaceData => {
const record = namespaceData.records[id];

if (!record) {
Expand All @@ -43,19 +44,19 @@ export default DS.Adapter.extend(Evented, {
},

findAll(store, type) {
return this._getNamespaceData(type).then((namespaceData) => {
return this._getNamespaceData(type).then(namespaceData => {
const records = [];

for (let id in namespaceData.records) {
records.push(namespaceData.records[id]);
}

return records;
return { data: records.map(record => record.data) };
});
},

findMany(store, type, ids) {
return this._getNamespaceData(type).then((namespaceData) => {
return this._getNamespaceData(type).then(namespaceData => {
const records = [];

for (let i = 0; i < ids.length; i++) {
Expand All @@ -71,7 +72,7 @@ export default DS.Adapter.extend(Evented, {
},

queryRecord(store, type, query) {
return this._getNamespaceData(type).then((namespaceData) => {
return this._getNamespaceData(type).then(namespaceData => {
const record = this._query(namespaceData.records, query, true);

if (!record) {
Expand All @@ -96,8 +97,11 @@ export default DS.Adapter.extend(Evented, {
* { complete: true, name: /foo|bar/ }
*/
query(store, type, query) {
return this._getNamespaceData(type).then((namespaceData) => {
return this._query(namespaceData.records, query);
return this._getNamespaceData(type).then(namespaceData => {
let records = this._query(namespaceData.records, query);
let result = { data: records.map(record => record.data) };

return result;
});
},

Expand All @@ -106,15 +110,16 @@ export default DS.Adapter.extend(Evented, {

for (let id in records) {
const record = records[id];
const attributes = record.data.attributes;
let isMatching = false;

for (let property in query) {
const queryValue = query[property];

if (queryValue instanceof RegExp) {
isMatching = queryValue.test(record[property]);
isMatching = queryValue.test(attributes[property]);
} else {
isMatching = record[property] === queryValue;
isMatching = attributes[property] === queryValue;
}

if (!isMatching) {
Expand All @@ -139,8 +144,8 @@ export default DS.Adapter.extend(Evented, {
updateRecord: updateOrCreate,

deleteRecord(store, type, snapshot) {
return this.queue.attach((resolve) => {
this._getNamespaceData(type).then((namespaceData) => {
return this.queue.attach(resolve => {
this._getNamespaceData(type).then(namespaceData => {
delete namespaceData.records[snapshot.id];

this._setNamespaceData(type, namespaceData).then(() => {
Expand All @@ -159,14 +164,14 @@ export default DS.Adapter.extend(Evented, {
_setNamespaceData(type, namespaceData) {
const modelNamespace = this._modelNamespace(type);

return this._loadData().then((storage) => {
return this._loadData().then(storage => {
if (this.caching !== 'none') {
this.cache.set(modelNamespace, namespaceData);
}

storage[modelNamespace] = namespaceData;

return window.localforage.setItem(this._adapterNamespace(), storage);
return localforage.setItem(this._adapterNamespace(), storage);
});
},

Expand All @@ -181,8 +186,8 @@ export default DS.Adapter.extend(Evented, {
}
}

return this._loadData().then((storage) => {
const namespaceData = storage && storage[modelNamespace] || { records: {} };
return this._loadData().then(storage => {
const namespaceData = (storage && storage[modelNamespace]) || { records: {} };

if (this.caching === 'model') {
this.cache.set(modelNamespace, namespaceData);
Expand All @@ -197,7 +202,7 @@ export default DS.Adapter.extend(Evented, {
},

_loadData() {
return window.localforage.getItem(this._adapterNamespace()).then((storage) => {
return localforage.getItem(this._adapterNamespace()).then(storage => {
return storage ? storage : {};
});
},
Expand All @@ -208,7 +213,7 @@ export default DS.Adapter.extend(Evented, {

_adapterNamespace() {
return this.get('namespace') || 'DS.LFAdapter';
}
},
});

function updateOrCreate(store, type, snapshot) {
Expand Down
Loading