Skip to content

Commit

Permalink
fix(@aws-amplify/datastore): improve IDB query performance (#7746)
Browse files Browse the repository at this point in the history
  • Loading branch information
iartemiev authored Feb 20, 2021
1 parent aecdf3c commit 5b87ad4
Show file tree
Hide file tree
Showing 6 changed files with 379 additions and 106 deletions.
126 changes: 126 additions & 0 deletions packages/datastore/__tests__/AsyncStorageAdapter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import AsyncStorageAdapter from '../src/storage/adapter/AsyncStorageAdapter';
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 ASAdapter = <any>AsyncStorageAdapter;

describe('AsyncStorageAdapter tests', () => {
describe('Query', () => {
let Model: PersistentModelConstructor<Model>;
let model1Id: string;
const spyOnGetOne = jest.spyOn(ASAdapter, 'getById');
const spyOnGetAll = jest.spyOn(ASAdapter, 'getAll');
const spyOnMemory = jest.spyOn(ASAdapter, 'inMemoryPagination');

beforeAll(async () => {
jest.doMock('../src/storage/adapter/getDefaultAdapter', () => {
return () =>
// we're returning a new instance of AsyncStorageAdapter in getDefaultAdapter/index.ts:13
// adding a mock to return default instead, so that we can spy on its methods
require('../src/storage/adapter/AsyncStorageAdapter').default;
});

({ initSchema, DataStore } = require('../src/datastore/datastore'));

const classes = initSchema(testSchema());

({ Model } = classes as {
Model: PersistentModelConstructor<Model>;
});

({ id: model1Id } = 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(),
})
);
});

beforeEach(() => {
jest.clearAllMocks();
});

it('Should call getById for query by id', async () => {
const result = await DataStore.query(Model, model1Id);

expect(result.field1).toEqual('Some value');
expect(spyOnGetOne).toHaveBeenCalled();
expect(spyOnGetAll).not.toHaveBeenCalled();
expect(spyOnMemory).not.toHaveBeenCalled();
});

it('Should call getAll for query with a predicate', async () => {
const results = await DataStore.query(Model, c =>
c.field1('contains', 'value')
);

expect(results.length).toEqual(3);
expect(spyOnGetAll).toHaveBeenCalled();
expect(spyOnMemory).toHaveBeenCalled();
});

it('Should call getAll & inMemoryPagination for query with a predicate and sort', async () => {
const results = await DataStore.query(
Model,
c => c.field1('contains', 'value'),
{
sort: s => s.dateCreated(SortDirection.DESCENDING),
}
);

expect(results.length).toEqual(3);
expect(results[0].field1).toEqual('a third value');
expect(spyOnGetAll).toHaveBeenCalled();
expect(spyOnMemory).toHaveBeenCalled();
});

it('Should call getAll & inMemoryPagination for query with sort', async () => {
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(spyOnMemory).toHaveBeenCalled();
});

it('Should call getAll & inMemoryPagination for query with pagination but no sort or predicate', async () => {
const results = await DataStore.query(Model, Predicates.ALL, {
limit: 1,
});

expect(results.length).toEqual(1);
expect(spyOnGetAll).toHaveBeenCalled();
expect(spyOnMemory).toHaveBeenCalled();
});

it('Should call getAll for query without predicate and pagination', async () => {
const results = await DataStore.query(Model);

expect(results.length).toEqual(3);
expect(spyOnGetAll).toHaveBeenCalled();
expect(spyOnMemory).not.toHaveBeenCalled();
});
});
});
1 change: 0 additions & 1 deletion packages/datastore/__tests__/DataStore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'fake-indexeddb/auto';
import FDBCursor from 'fake-indexeddb/build/FDBCursor';
import { decodeTime } from 'ulid';
import uuidValidate from 'uuid-validate';
import Observable from 'zen-observable-ts';
Expand Down
111 changes: 111 additions & 0 deletions packages/datastore/__tests__/IndexedDBAdapter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
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>;
let model1Id: string;
const spyOnGetOne = jest.spyOn(IDBAdapter, 'getById');
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>;
});

({ id: model1Id } = 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(),
})
);
});

beforeEach(() => {
jest.clearAllMocks();
});

it('Should call getById for query by id', async () => {
const result = await DataStore.query(Model, model1Id);

expect(result.field1).toEqual('Some value');
expect(spyOnGetOne).toHaveBeenCalled();
expect(spyOnGetAll).not.toHaveBeenCalled();
expect(spyOnEngine).not.toHaveBeenCalled();
expect(spyOnMemory).not.toHaveBeenCalled();
});

it('Should call getAll & inMemoryPagination for query with a predicate', async () => {
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();
});

it('Should call getAll & inMemoryPagination for query with sort', async () => {
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();
});

it('Should call enginePagination for query with pagination but no sort or predicate', async () => {
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();
});

it('Should call getAll for query without predicate and pagination', async () => {
const results = await DataStore.query(Model);

expect(results.length).toEqual(3);
expect(spyOnGetAll).toHaveBeenCalled();
expect(spyOnEngine).not.toHaveBeenCalled();
expect(spyOnMemory).not.toHaveBeenCalled();
});
});
});
11 changes: 9 additions & 2 deletions packages/datastore/src/datastore/datastore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,10 @@ class DataStore {
predicate: ModelPredicateCreator.getPredicates(predicate, false),
pagination: {
...pagination,
sort: ModelSortPredicateCreator.getPredicates(pagination.sort, false),
sort: ModelSortPredicateCreator.getPredicates(
pagination && pagination.sort,
false
),
},
});

Expand Down Expand Up @@ -1121,10 +1124,14 @@ class DataStore {
private processPagination<T extends PersistentModel>(
modelDefinition: SchemaModel,
paginationProducer: ProducerPaginationInput<T>
): PaginationInput<T> {
): PaginationInput<T> | undefined {
let sortPredicate: SortPredicate<T>;
const { limit, page, sort } = paginationProducer || {};

if (limit === undefined && page === undefined && sort === undefined) {
return undefined;
}

if (page !== undefined && limit === undefined) {
throw new Error('Limit is required when requesting a page');
}
Expand Down
Loading

0 comments on commit 5b87ad4

Please sign in to comment.