|
13 | 13 | throw 'IndexedDB required'; |
14 | 14 | } |
15 | 15 |
|
| 16 | + var defaultMapper = function (value) { |
| 17 | + return value; |
| 18 | + }; |
| 19 | + |
16 | 20 | var CallbackList = function () { |
17 | 21 | var state, |
18 | 22 | list = []; |
|
307 | 311 | var that = this; |
308 | 312 | var modifyObj = false; |
309 | 313 |
|
310 | | - var runQuery = function ( type, args , cursorType , direction, limitRange, filters ) { |
| 314 | + var runQuery = function ( type, args , cursorType , direction, limitRange, filters , mapper ) { |
311 | 315 | var transaction = db.transaction( table, modifyObj ? transactionModes.readwrite : transactionModes.readonly ), |
312 | 316 | store = transaction.objectStore( table ), |
313 | 317 | index = indexName ? store.index( indexName ) : store, |
|
362 | 366 |
|
363 | 367 | if (matchFilter) { |
364 | 368 | counter++; |
365 | | - results.push( result ); |
| 369 | + results.push( mapper(result) ); |
366 | 370 | // if we're doing a modify, run it now |
367 | 371 | if(modifyObj) { |
368 | 372 | result = modifyRecord(result); |
|
391 | 395 | cursorType = 'openCursor', |
392 | 396 | filters = [], |
393 | 397 | limitRange = null, |
| 398 | + mapper = defaultMapper, |
394 | 399 | unique = false; |
395 | 400 |
|
396 | 401 | 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 ); |
398 | 403 | }; |
399 | 404 |
|
400 | 405 | var limit = function () { |
|
422 | 427 | desc: desc, |
423 | 428 | execute: execute, |
424 | 429 | filter: filter, |
425 | | - distinct: distinct |
| 430 | + distinct: distinct, |
| 431 | + map: map |
426 | 432 | }; |
427 | 433 | }; |
428 | 434 | var filter = function ( ) { |
|
435 | 441 | desc: desc, |
436 | 442 | distinct: distinct, |
437 | 443 | modify: modify, |
438 | | - limit: limit |
| 444 | + limit: limit, |
| 445 | + map: map |
439 | 446 | }; |
440 | 447 | }; |
441 | 448 | var desc = function () { |
|
446 | 453 | execute: execute, |
447 | 454 | filter: filter, |
448 | 455 | distinct: distinct, |
449 | | - modify: modify |
| 456 | + modify: modify, |
| 457 | + map: map |
450 | 458 | }; |
451 | 459 | }; |
452 | 460 | var distinct = function () { |
|
457 | 465 | execute: execute, |
458 | 466 | filter: filter, |
459 | 467 | desc: desc, |
460 | | - modify: modify |
| 468 | + modify: modify, |
| 469 | + map: map |
461 | 470 | }; |
462 | 471 | }; |
463 | 472 | var modify = function(update) { |
|
466 | 475 | execute: execute |
467 | 476 | }; |
468 | 477 | }; |
| 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 | + }; |
469 | 493 |
|
470 | 494 | return { |
471 | 495 | execute: execute, |
|
475 | 499 | desc: desc, |
476 | 500 | distinct: distinct, |
477 | 501 | modify: modify, |
478 | | - limit: limit |
| 502 | + limit: limit, |
| 503 | + map: map |
479 | 504 | }; |
480 | 505 | }; |
481 | 506 |
|
|
531 | 556 | var dbCache = {}; |
532 | 557 |
|
533 | 558 | var db = { |
534 | | - version: '0.8.0', |
| 559 | + version: '0.9.0', |
535 | 560 | open: function ( options ) { |
536 | 561 | var request; |
537 | 562 |
|
|
0 commit comments