I have a 3-step process in my offline app running on AngularJS & IndexedDB.
On page load I make a DB call to list all records with today's date (into a recordlist
variable):
$indexedDB.openStore('records', function(store){
$scope.todaystart = moment().startOf('day').format();
$scope.todayend = moment().endOf('day').format();
var find = store.query();
find = find.$between($scope.todaystart, $scope.todayend, false, false);
find = find.$index("date_idx");
store.eachWhere(find).then(function(e){
$scope.recordlist = e;
});
});
Then I can add new records, but in order to update recordlist
, I'm doing yet another call:
$scope.addRecord = function (records) {
$indexedDB.openStore('records', function(store){
$scope.newrecord = {
"date": $scope.dateJson,
"time": $scope.time,
"arrival": '',
"inserted": ''
}
store.insert($scope.newrecord).then(function(e){});
var find = store.query();
find = find.$eq($scope.dateJson);
find = find.$index("date_idx");
store.eachWhere(find).then(function(e){
$scope.recordlist = e;
});
});
};
And finally I update the record with an arrival time, doing yet another DB call:
$scope.insertRecord = function (record) {
$indexedDB.openStore('records', function(store){
store.upsert(
{
"ssn": record.ssn,
"date": record.date,
"time": record.time,
"arrival": moment().format("HH.mm.ss"),
"inserted": true,
}).then(function (e) {
var find = store.query();
find = find.$eq($scope.dateJson);
find = find.$index("date_idx");
store.eachWhere(find).then(function(e){
$scope.recordlist = e;
});
});
});
};
My question is, considering that I'm already retrieving recordlist
on the first call, wouldn't it be cleaner-nicer-easier to update it on-the-fly without having to make a server query on every step (add/insert)?
At the end, the data will be saved anyway, is there a need to retrieve it over and over again with the performance issues that this carries when I could probably just append the new data to recordlist
?
And if so, how would I do that?
I'm new on working with databases and I'm a bit lost when it comes to best practices and workflow...