Skip to content

Commit

Permalink
feat: clear DataStore without first starting (aws-amplify#9768)
Browse files Browse the repository at this point in the history
Co-authored-by: Jon Wire <[email protected]>
  • Loading branch information
dpilch and svidgen authored Apr 21, 2022
1 parent 58d7e00 commit 38bdabd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
14 changes: 14 additions & 0 deletions packages/datastore/__tests__/DataStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ describe('DataStore observe, unmocked, with fake-indexeddb', () => {
await DataStore.clear();
});

test('clear without starting', async () => {
await DataStore.save(
new Model({
field1: 'Smurfs',
optionalField1: 'More Smurfs',
dateCreated: new Date().toISOString(),
})
);
expect(await DataStore.query(Model)).toHaveLength(1);
await DataStore.stop();
await DataStore.clear();
expect(await DataStore.query(Model)).toHaveLength(0);
});

test('subscribe to all models', async done => {
try {
const sub = DataStore.observe().subscribe(
Expand Down
11 changes: 10 additions & 1 deletion packages/datastore/src/datastore/datastore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1385,7 +1385,16 @@ class DataStore {

clear = async function clear() {
if (this.storage === undefined) {
return;
// connect to storage so that it can be cleared without fully starting DataStore
this.storage = new Storage(
schema,
namespaceResolver,
getModelConstructorByModelName,
modelInstanceCreator,
this.storageAdapter,
this.sessionId
);
await this.storage.init();
}

if (syncSubscription && !syncSubscription.closed) {
Expand Down

0 comments on commit 38bdabd

Please sign in to comment.