Skip to content

Commit fd06645

Browse files
committed
Implementing object mapping when querying to transform types
1 parent 6c8b64a commit fd06645

2 files changed

Lines changed: 63 additions & 9 deletions

File tree

src/db.js

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
throw 'IndexedDB required';
1414
}
1515

16+
var defaultMapper = function (value) {
17+
return value;
18+
};
19+
1620
var CallbackList = function () {
1721
var state,
1822
list = [];
@@ -307,7 +311,7 @@
307311
var that = this;
308312
var modifyObj = false;
309313

310-
var runQuery = function ( type, args , cursorType , direction, limitRange, filters ) {
314+
var runQuery = function ( type, args , cursorType , direction, limitRange, filters , mapper ) {
311315
var transaction = db.transaction( table, modifyObj ? transactionModes.readwrite : transactionModes.readonly ),
312316
store = transaction.objectStore( table ),
313317
index = indexName ? store.index( indexName ) : store,
@@ -362,7 +366,7 @@
362366

363367
if (matchFilter) {
364368
counter++;
365-
results.push( result );
369+
results.push( mapper(result) );
366370
// if we're doing a modify, run it now
367371
if(modifyObj) {
368372
result = modifyRecord(result);
@@ -391,10 +395,11 @@
391395
cursorType = 'openCursor',
392396
filters = [],
393397
limitRange = null,
398+
mapper = defaultMapper,
394399
unique = false;
395400

396401
var execute = function () {
397-
return runQuery( type , args , cursorType , unique ? direction + 'unique' : direction, limitRange, filters );
402+
return runQuery( type , args , cursorType , unique ? direction + 'unique' : direction, limitRange, filters , mapper );
398403
};
399404

400405
var limit = function () {
@@ -422,7 +427,8 @@
422427
desc: desc,
423428
execute: execute,
424429
filter: filter,
425-
distinct: distinct
430+
distinct: distinct,
431+
map: map
426432
};
427433
};
428434
var filter = function ( ) {
@@ -435,7 +441,8 @@
435441
desc: desc,
436442
distinct: distinct,
437443
modify: modify,
438-
limit: limit
444+
limit: limit,
445+
map: map
439446
};
440447
};
441448
var desc = function () {
@@ -446,7 +453,8 @@
446453
execute: execute,
447454
filter: filter,
448455
distinct: distinct,
449-
modify: modify
456+
modify: modify,
457+
map: map
450458
};
451459
};
452460
var distinct = function () {
@@ -457,7 +465,8 @@
457465
execute: execute,
458466
filter: filter,
459467
desc: desc,
460-
modify: modify
468+
modify: modify,
469+
map: map
461470
};
462471
};
463472
var modify = function(update) {
@@ -466,6 +475,21 @@
466475
execute: execute
467476
};
468477
};
478+
var map = function (fn) {
479+
mapper = fn;
480+
481+
return {
482+
execute: execute,
483+
count: count,
484+
keys: keys,
485+
filter: filter,
486+
desc: desc,
487+
distinct: distinct,
488+
modify: modify,
489+
limit: limit,
490+
map: map
491+
};
492+
};
469493

470494
return {
471495
execute: execute,
@@ -475,7 +499,8 @@
475499
desc: desc,
476500
distinct: distinct,
477501
modify: modify,
478-
limit: limit
502+
limit: limit,
503+
map: map
479504
};
480505
};
481506

@@ -531,7 +556,7 @@
531556
var dbCache = {};
532557

533558
var db = {
534-
version: '0.8.0',
559+
version: '0.9.0',
535560
open: function ( options ) {
536561
var request;
537562

tests/specs/query.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,35 @@
793793

794794
});
795795

796+
describe( 'query mapping' , function () {
797+
it( 'should allow you to transform the object being returned' , function () {
798+
var done;
799+
800+
runs(function () {
801+
var spec = this;
802+
803+
spec.server.test
804+
.query( 'age' )
805+
.lowerBound(30)
806+
.map(function (value) {
807+
return {
808+
fullName: value.firstName + ' ' + value.lastName,
809+
raw: value
810+
};
811+
})
812+
.execute()
813+
.done( function ( data ) {
814+
expect(data[0].fullName).toEqual(data[0].raw.firstName + ' ' + data[0].raw.lastName);
815+
done = true;
816+
});
817+
});
818+
819+
waitsFor(function () {
820+
return done;
821+
} , 1000 , 'timeout in atomic modify query' );
822+
});
823+
});
824+
796825
describe( 'atomic updates' , function () {
797826
it( 'should modify only data returned by query' , function () {
798827
var done;

0 commit comments

Comments
 (0)