Skip to content

Commit

Permalink
Fix bug when trying to delete a model instance that is not persisted
Browse files Browse the repository at this point in the history
  • Loading branch information
manueliglesias committed Mar 17, 2020
1 parent 790f102 commit db27ab3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
16 changes: 15 additions & 1 deletion packages/datastore/__tests__/indexeddb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ describe('Indexed db storage test', () => {
});
});
test('setup function', async () => {
const namespaceResolver = jest.fn();
db = await idb.openDB('amplify-datastore', 1);

const createdObjStores = db.objectStoreNames;
Expand Down Expand Up @@ -369,4 +368,19 @@ describe('Indexed db storage test', () => {
.getAll(p1.id);
expect(refResult).toHaveLength(0);
});

test('delete non existent', async () => {
const author = new Author({ name: 'author1' });

const deleted = await DataStore.delete(author);

expect(deleted).toStrictEqual(author);

const fromDB = await db
.transaction(`${USER}_Author`, 'readonly')
.objectStore(`${USER}_Author`)
.get(author.id);

expect(fromDB).toBeUndefined();
});
});
7 changes: 7 additions & 0 deletions packages/datastore/src/storage/adapter/indexeddb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,13 @@ class IndexedDBAdapter implements Adapter {

const fromDB = await store.get(model.id);

if (fromDB === undefined) {
const msg = 'Model instance not found in storage';
logger.warn(msg, { model });

return [[model], []];
}

const predicates = ModelPredicateCreator.getPredicates(condition);
const { predicates: predicateObjs, type } = predicates;

Expand Down

0 comments on commit db27ab3

Please sign in to comment.