-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(@aws-amplify/datastore): improve query performance
- Loading branch information
Showing
4 changed files
with
174 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import Adapter from '../src/storage/adapter/IndexedDBAdapter'; | ||
import 'fake-indexeddb/auto'; | ||
import { | ||
DataStore as DataStoreType, | ||
initSchema as initSchemaType, | ||
} from '../src/datastore/datastore'; | ||
import { PersistentModelConstructor, SortDirection } from '../src/types'; | ||
import { Model, testSchema } from './helpers'; | ||
import { Predicates } from '../src/predicates'; | ||
|
||
let initSchema: typeof initSchemaType; | ||
let DataStore: typeof DataStoreType; | ||
// using any to get access to private methods | ||
const IDBAdapter = <any>Adapter; | ||
|
||
describe('IndexedDBAdapter tests', () => { | ||
describe('Query', () => { | ||
let Model: PersistentModelConstructor<Model>; | ||
const spyOnGetAll = jest.spyOn(IDBAdapter, 'getAll'); | ||
const spyOnEngine = jest.spyOn(IDBAdapter, 'enginePagination'); | ||
const spyOnMemory = jest.spyOn(IDBAdapter, 'inMemoryPagination'); | ||
|
||
beforeAll(async () => { | ||
({ initSchema, DataStore } = require('../src/datastore/datastore')); | ||
|
||
const classes = initSchema(testSchema()); | ||
|
||
({ Model } = classes as { | ||
Model: PersistentModelConstructor<Model>; | ||
}); | ||
|
||
await DataStore.save( | ||
new Model({ | ||
field1: 'Some value', | ||
dateCreated: new Date().toISOString(), | ||
}) | ||
); | ||
await DataStore.save( | ||
new Model({ | ||
field1: 'another value', | ||
dateCreated: new Date().toISOString(), | ||
}) | ||
); | ||
await DataStore.save( | ||
new Model({ | ||
field1: 'a third value', | ||
dateCreated: new Date().toISOString(), | ||
}) | ||
); | ||
}); | ||
|
||
test('Should call getAll & inMemoryPagination for query with a predicate', async () => { | ||
jest.clearAllMocks(); | ||
const results = await DataStore.query(Model, c => | ||
c.field1('eq', 'another value') | ||
); | ||
|
||
expect(results.length).toEqual(1); | ||
expect(spyOnGetAll).toHaveBeenCalled(); | ||
expect(spyOnEngine).not.toHaveBeenCalled(); | ||
expect(spyOnMemory).toHaveBeenCalled(); | ||
}); | ||
|
||
test('Should call getAll & inMemoryPagination for query with sort', async () => { | ||
jest.clearAllMocks(); | ||
const results = await DataStore.query(Model, Predicates.ALL, { | ||
sort: s => s.dateCreated(SortDirection.DESCENDING), | ||
}); | ||
|
||
expect(results.length).toEqual(3); | ||
expect(results[0].field1).toEqual('a third value'); | ||
expect(spyOnGetAll).toHaveBeenCalled(); | ||
expect(spyOnEngine).not.toHaveBeenCalled(); | ||
expect(spyOnMemory).toHaveBeenCalled(); | ||
}); | ||
|
||
test('Should call enginePagination for query with pagination but no sort or predicate', async () => { | ||
jest.clearAllMocks(); | ||
const results = await DataStore.query(Model, Predicates.ALL, { | ||
limit: 1, | ||
}); | ||
|
||
expect(results.length).toEqual(1); | ||
expect(spyOnGetAll).not.toHaveBeenCalled(); | ||
expect(spyOnEngine).toHaveBeenCalled(); | ||
expect(spyOnMemory).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('Should call getAll for query without predicate and pagination', async () => { | ||
jest.clearAllMocks(); | ||
const results = await DataStore.query(Model); | ||
|
||
expect(results.length).toEqual(3); | ||
expect(spyOnGetAll).toHaveBeenCalled(); | ||
expect(spyOnEngine).not.toHaveBeenCalled(); | ||
expect(spyOnMemory).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters