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

OLMIS-7954: Improved the scalability of local storage #35

Merged
merged 4 commits into from
Jul 10, 2024
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
Bug fixes:
* [OLMIS-7931](https://openlmis.atlassian.net/browse/OLMIS-7931): Fix imports for layout-ui jenkins job

Improvements:
* [OLMIS-7954](https://openlmis.atlassian.net/browse/OLMIS-7954): Improved the scalability of local storage

7.2.11 / 2024-04-19
==================
Bug fixes:
Expand Down
25 changes: 25 additions & 0 deletions src/openlmis-local-storage/local-storage.factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@
}

var items = getFromStorage(resourceName);

if (!items) {
items = [];
}

var resource = {
put: put,
putAll: putAll,
getBy: getBy,
getAll: getAll,
search: search,
Expand Down Expand Up @@ -92,6 +94,29 @@
}
}

/**
* @ngdoc method
* @methodOf openlmis-local-storage.localStorageFactory
* @name putAll
*
* @description
* Stores all objects in local storage.
*
* @param {Array} collectionToStore Array of objects to store
*/
function putAll(collectionToStore) {
if (Array.isArray(collectionToStore) && collectionToStore.length > 0) {
executeWithStorageUpdate(function() {
items = collectionToStore.map(function(item) {
if (item.id) {
removeItemBy('id', item.id);
}
return typeof item === 'object' ? JSON.parse(JSON.stringify(item)) : item;
});
});
}
}

/**
* @ngdoc method
* @methodOf openlmis-local-storage.localStorageFactory
Expand Down
26 changes: 26 additions & 0 deletions src/openlmis-local-storage/local-storage.factory.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,32 @@ describe('localStorageFactory', function() {

});

describe('putAll', function() {

beforeEach(function() {
this.putAllItems = [{
id: 3,
name: 'item3'
},
{
id: 4,
name: 'item4'
}];
});

it('should put all items', function() {
this.itemsLocalStorage.putAll(this.putAllItems);

expect(this.putAllItems.length).toBe(2);
});

it('should update storage', function() {
this.itemsLocalStorage.putAll(this.putAllItems);

expect(this.localStorageService.add).toHaveBeenCalledWith('items', angular.toJson(this.putAllItems));
});
});

describe('getBy', function() {

it('should get item by id', function() {
Expand Down
Loading