Skip to content

Commit 708dddd

Browse files
Fixed bug in pouchDBStore and updated version to 1.2.3
1 parent 62b4f4f commit 708dddd

9 files changed

+85
-44
lines changed

JSDOC.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# offline-persistence-toolkit 1.2.2 #
1+
# offline-persistence-toolkit 1.2.3 #
22

33
## Introduction ##
44

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# offline-persistence-toolkit 1.2.2 #
1+
# offline-persistence-toolkit 1.2.3 #
22

33
offline-persistence-toolkit is a client-side JavaScript library that provides caching and offline support at the HTTP request layer. This support is transparent to the user and is done through the Fetch API and an XHR adapter. HTTP requests made while the client device is offline are captured for replay when connection to the server is restored. Additional capabilities include a persistent storage layer, synchronization manager, binary data support and various configuration APIs for customizing the default behavior. This framework can be used in both ServiceWorker and non-ServiceWorker contexts within web and hybrid mobile apps.
44

@@ -58,16 +58,16 @@ If your app uses [RequireJS](http://www.requirejs.org/ "RequireJS"), update the
5858
```javascript
5959
requirejs.config({
6060
paths: {
61-
'persist' : 'js/libs/persist/v1.2.2/min'
61+
'persist' : 'js/libs/persist/v1.2.3/min'
6262

6363
// Other path mappings here
6464
}
6565
```
66-
For Oracle JET apps, also open `appDir/src/js/main-release-paths.json` and add the `'persist' : 'js/libs/persist/v1.2.2/min'` entry to the list of paths.
66+
For Oracle JET apps, also open `appDir/src/js/main-release-paths.json` and add the `'persist' : 'js/libs/persist/v1.2.3/min'` entry to the list of paths.
6767
6868
You can choose the name of the paths prefix. That is, you can use a different value to the ‘persist’ value shown in the examples.
6969
70-
It is recommended to add the version number as a convention in your application build step such as `'persist' : 'js/libs/persist/v1.2.2/min'`.
70+
It is recommended to add the version number as a convention in your application build step such as `'persist' : 'js/libs/persist/v1.2.3/min'`.
7171
7272
Versions of the toolkit are also available on CDN under the latest JET release. e.g.
7373
@@ -91,7 +91,7 @@ And again, if you are using RequireJS, you will need to map paths for these pack
9191
paths: {
9292
'pouchdb': 'js/libs/pouchdb-6.3.4',
9393
'pouchfind': 'js/libs/pouchdb.find',
94-
'persist' : 'js/libs/persist/v1.2.2/min'
94+
'persist' : 'js/libs/persist/v1.2.3/min'
9595

9696
// Other path mappings here
9797
}

USAGE.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# offline-persistence-toolkit 1.2.2 #
1+
# offline-persistence-toolkit 1.2.3 #
22

33
# Introduction #
44

docs/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ <h3> </h3>
4343

4444

4545
<section>
46-
<article><h1>offline-persistence-toolkit 1.2.2</h1><h2>Introduction</h2><p>This document provides a reference to the offline-persistence-toolkit's JavaScript API. Links to JSDoc for each class can be found to the right.</p>
46+
<article><h1>offline-persistence-toolkit 1.2.3</h1><h2>Introduction</h2><p>This document provides a reference to the offline-persistence-toolkit's JavaScript API. Links to JSDoc for each class can be found to the right.</p>
4747
<p>The toolkit's <a href="https://github.com/oracle/offline-persistence-toolkit/" title="README">README</a> file provides an overview of the toolkit’s capabilities. Having read that document, you can find more information about how to implement a range of common and advanced use cases with the toolkit in the
4848
<a href="https://github.com/oracle/offline-persistence-toolkit/blob/master/USAGE.md" title="Usage guide">Usage guide</a>.</p>
4949
<h2>License</h2><p>Copyright (c) 2017 Oracle and/or its affiliates The Universal Permissive License (UPL), Version 1.0.</p></article>

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@oracle/offline-persistence-toolkit",
33
"title": "Offline Persistence Toolkit",
4-
"version": "1.2.2",
4+
"version": "1.2.3",
55
"description": "Offline Persistence Toolkit by Oracle Corp.",
66
"author": "oraclejet",
77
"license": "UPL-1.0",

src/impl/keyValuePersistenceStore.js

+1-18
Original file line numberDiff line numberDiff line change
@@ -197,24 +197,7 @@ define(["../PersistenceStore", "./storageUtils", "./logger"],
197197
if (!fieldsExpression) {
198198
returnObject = itemData.value;
199199
} else {
200-
returnObject = {};
201-
for (var index = 0; index < fieldsExpression.length; index++) {
202-
var currentObject = returnObject;
203-
var currentItemDataValue = itemData;
204-
var field = fieldsExpression[index];
205-
var paths = field.split('.');
206-
for (var pathIndex = 0; pathIndex < paths.length; pathIndex++) {
207-
currentItemDataValue = currentItemDataValue[paths[pathIndex]];
208-
if (!currentObject[paths[pathIndex]] && pathIndex < paths.length - 1) {
209-
currentObject[paths[pathIndex]] = {};
210-
}
211-
if (pathIndex === paths.length - 1) {
212-
currentObject[paths[pathIndex]] = currentItemDataValue;
213-
} else {
214-
currentObject = currentObject[paths[pathIndex]];
215-
}
216-
}
217-
}
200+
returnObject = storageUtils.assembleObject(itemData, fieldsExpression);
218201
}
219202
return returnObject;
220203
};

src/impl/pouchDBPersistenceStore.js

+20-13
Original file line numberDiff line numberDiff line change
@@ -160,22 +160,24 @@ define(["../PersistenceStore", "../impl/storageUtils", "pouchdb", "./logger"],
160160
PouchDBPersistenceStore.prototype.find = function (findExpression) {
161161
logger.log("Offline Persistence Toolkit pouchDBPersistenceStore: find() for expression: " + JSON.stringify(findExpression));
162162
var self = this;
163-
var modifiedFind = self._prepareFind(findExpression);
164163

164+
findExpression = findExpression || {};
165+
165166
// if the find plugin is installed
166167
if (self._db.find) {
168+
var modifiedFind = self._prepareFind(findExpression);
167169
return self._db.find(modifiedFind).then(function (result) {
168170
if (result && result.docs && result.docs.length) {
169171
var promises = result.docs.map(self._findResultCallback(modifiedFind.fields), self);
170172
return Promise.all(promises);
171173
} else {
172-
return Promise.resolve([]);
174+
return [];
173175
}
174176
}).catch(function (finderr) {
175177
if (finderr.status === 404 && finderr.message === 'missing') {
176-
return Promise.resolve([]);
178+
return [];
177179
} else {
178-
return Promise.reject(finderr);
180+
throw finderr;
179181
}
180182
});
181183
} else {
@@ -196,7 +198,13 @@ define(["../PersistenceStore", "../impl/storageUtils", "pouchdb", "./logger"],
196198

197199
if (satisfiedRows.length) {
198200
var fixDocPromises = satisfiedRows.map(function(row) {
199-
return self._fixBinaryValue(row.doc);
201+
return self._fixBinaryValue(row.doc).then(function(fixedDoc){
202+
if (findExpression.fields) {
203+
return storageUtils.assembleObject(fixedDoc, findExpression.fields);
204+
} else {
205+
return fixedDoc.value;
206+
}
207+
});
200208
});
201209
return Promise.all(fixDocPromises);
202210
} else {
@@ -344,27 +352,26 @@ define(["../PersistenceStore", "../impl/storageUtils", "pouchdb", "./logger"],
344352
// understood by pouchdb find plugin.
345353
PouchDBPersistenceStore.prototype._prepareFind = function (findExpression) {
346354

347-
var modifiedExpression = findExpression || {};
355+
var modifiedExpression = {};
348356

349357
// ideally we should allow sort, but PouchDB-find-plugin
350-
// has bug on that, so disable sorting at this time.
351-
var sortFieldsArray = modifiedExpression.sort;
352-
if (sortFieldsArray !== undefined) {
353-
delete modifiedExpression.sort;
354-
}
358+
// has bug on that, so disable sorting at this time by
359+
// not copy sort from original expression.
355360

356361
// selector is required from pouchdb.find plugin, thus create a selector
357362
// property if no selector is explicitely defined in findExpression. The
358363
// default selector is {'_id': {$gt: null}} which selects everything.
359-
var selector = modifiedExpression.selector;
364+
var selector = findExpression.selector;
360365
if (!selector) {
361366
modifiedExpression.selector = {
362367
'_id': {'$gt': null}
363368
};
369+
} else {
370+
modifiedExpression.selector = selector;
364371
}
365372

366373
// our key attribute maps to pouchdb documents' _id field.
367-
var fields = modifiedExpression.fields;
374+
var fields = findExpression.fields;
368375
if (fields && fields.length) {
369376
var modifiedFields = fields.map(function (x) {
370377
if (x === 'key') {

src/impl/storageUtils.js

+43-1
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,51 @@ define(['./logger'], function (logger) {
297297
return returnValue;
298298
};
299299

300+
/**
301+
* Helper function that constructs an object out from value
302+
* based on fields.
303+
* @method
304+
* @name assembleObject
305+
* @param {object} value The original object to construct the return object
306+
* from.
307+
* @param {Array} fields An array of property names whose values
308+
* should be included in the final contructed
309+
* return object.
310+
* @returns {object} the object that contains all the properties defined
311+
* in fields array, the corresponding property
312+
* value is obtained from value.
313+
*/
314+
function assembleObject (value, fields) {
315+
var returnObject;
316+
if (!fields) {
317+
returnObject = value;
318+
} else {
319+
returnObject = {};
320+
for (var index = 0; index < fields.length; index++) {
321+
var currentObject = returnObject;
322+
var currentItemDataValue = value;
323+
var field = fields[index];
324+
var paths = field.split('.');
325+
for (var pathIndex = 0; pathIndex < paths.length; pathIndex++) {
326+
currentItemDataValue = currentItemDataValue[paths[pathIndex]];
327+
if (!currentObject[paths[pathIndex]] && pathIndex < paths.length - 1) {
328+
currentObject[paths[pathIndex]] = {};
329+
}
330+
if (pathIndex === paths.length - 1) {
331+
currentObject[paths[pathIndex]] = currentItemDataValue;
332+
} else {
333+
currentObject = currentObject[paths[pathIndex]];
334+
}
335+
}
336+
}
337+
}
338+
return returnObject;
339+
};
340+
300341
return {
301342
satisfy: satisfy,
302-
getValue: getValue
343+
getValue: getValue,
344+
assembleObject: assembleObject
303345
};
304346
});
305347

test/storage/storagetest.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ define(['persist/persistenceStoreManager', 'persist/localPersistenceStoreFactory
77

88
module('storagetest');
99

10-
// helper function that compares expected result set with the actual result
10+
// helper function that compares expected result set with the actxual result
1111
// set. Return true if they match, false otherwise. The expected/actual
1212
// result set are both arrays. They should contain the same number of
1313
// element, and each element should match. The order of the element in the
@@ -81,18 +81,27 @@ define(['persist/persistenceStoreManager', 'persist/localPersistenceStoreFactory
8181
if (!expected && !actual) {
8282
return true;
8383
} else if (expected && actual) {
84-
if (typeof(expected) === 'object') {
84+
if (typeof(expected) === 'object' && typeof(actual) === 'object') {
85+
var keys = [];
8586
for (var key in expected) {
8687
if (expected.hasOwnProperty(key)) {
88+
keys.push(key);
8789
var expectedValue = expected[key];
8890
var actualValue = actual[key];
8991
if (!matchObject(expectedValue, actualValue)) {
9092
return false;
9193
}
9294
}
9395
}
96+
for (var actualKey in actual) {
97+
if (actual.hasOwnProperty(actualKey)) {
98+
if (keys.indexOf(actualKey) < 0) {
99+
return false;
100+
}
101+
}
102+
}
94103
return true;
95-
} else{
104+
} else {
96105
return (expected == actual);
97106
}
98107
} else {

0 commit comments

Comments
 (0)