Skip to content

Commit

Permalink
Implementing object mapping when querying to transform types
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronpowell committed Jan 21, 2014
1 parent 6c8b64a commit fd06645
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 9 deletions.
43 changes: 34 additions & 9 deletions src/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
throw 'IndexedDB required';
}

var defaultMapper = function (value) {
return value;
};

var CallbackList = function () {
var state,
list = [];
Expand Down Expand Up @@ -307,7 +311,7 @@
var that = this;
var modifyObj = false;

var runQuery = function ( type, args , cursorType , direction, limitRange, filters ) {
var runQuery = function ( type, args , cursorType , direction, limitRange, filters , mapper ) {
var transaction = db.transaction( table, modifyObj ? transactionModes.readwrite : transactionModes.readonly ),
store = transaction.objectStore( table ),
index = indexName ? store.index( indexName ) : store,
Expand Down Expand Up @@ -362,7 +366,7 @@

if (matchFilter) {
counter++;
results.push( result );
results.push( mapper(result) );
// if we're doing a modify, run it now
if(modifyObj) {
result = modifyRecord(result);
Expand Down Expand Up @@ -391,10 +395,11 @@
cursorType = 'openCursor',
filters = [],
limitRange = null,
mapper = defaultMapper,
unique = false;

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

var limit = function () {
Expand Down Expand Up @@ -422,7 +427,8 @@
desc: desc,
execute: execute,
filter: filter,
distinct: distinct
distinct: distinct,
map: map
};
};
var filter = function ( ) {
Expand All @@ -435,7 +441,8 @@
desc: desc,
distinct: distinct,
modify: modify,
limit: limit
limit: limit,
map: map
};
};
var desc = function () {
Expand All @@ -446,7 +453,8 @@
execute: execute,
filter: filter,
distinct: distinct,
modify: modify
modify: modify,
map: map
};
};
var distinct = function () {
Expand All @@ -457,7 +465,8 @@
execute: execute,
filter: filter,
desc: desc,
modify: modify
modify: modify,
map: map
};
};
var modify = function(update) {
Expand All @@ -466,6 +475,21 @@
execute: execute
};
};
var map = function (fn) {
mapper = fn;

return {
execute: execute,
count: count,
keys: keys,
filter: filter,
desc: desc,
distinct: distinct,
modify: modify,
limit: limit,
map: map
};
};

return {
execute: execute,
Expand All @@ -475,7 +499,8 @@
desc: desc,
distinct: distinct,
modify: modify,
limit: limit
limit: limit,
map: map
};
};

Expand Down Expand Up @@ -531,7 +556,7 @@
var dbCache = {};

var db = {
version: '0.8.0',
version: '0.9.0',
open: function ( options ) {
var request;

Expand Down
29 changes: 29 additions & 0 deletions tests/specs/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,35 @@

});

describe( 'query mapping' , function () {
it( 'should allow you to transform the object being returned' , function () {
var done;

runs(function () {
var spec = this;

spec.server.test
.query( 'age' )
.lowerBound(30)
.map(function (value) {
return {
fullName: value.firstName + ' ' + value.lastName,
raw: value
};
})
.execute()
.done( function ( data ) {
expect(data[0].fullName).toEqual(data[0].raw.firstName + ' ' + data[0].raw.lastName);
done = true;
});
});

waitsFor(function () {
return done;
} , 1000 , 'timeout in atomic modify query' );
});
});

describe( 'atomic updates' , function () {
it( 'should modify only data returned by query' , function () {
var done;
Expand Down

0 comments on commit fd06645

Please sign in to comment.