treojs / treo

Consistent API to IndexedDB
http://treojs.com
MIT License
258 stars 18 forks source link

Promise-plugin: denodeify not working correctly #45

Open cgrossde opened 8 years ago

cgrossde commented 8 years ago

Denodeification does not work correctly for me and when calling get() on a store I get an exception: TypeError: undefined is not a function at IDBRequest.onsuccess .... This is not the only method that fails when trying to use it as a promise. Sample code:

import treo from 'treo'
import treoPromise from 'treo/plugins/treo-promise'

// Init DB
    const schema = treo.schema()
      .version(1)
        .addStore('apps', { key: 'aid' })
        .addStore('notifications', { key: 'id' })
        .addIndex('byAppId', 'aid')
        .addIndex('byDate', 'date')

    const db = treo('pushover', schema)
      .use(treoPromise())

    this.appDB = db.store('apps')
    this.notificationDB = db.store('notifications')

function isNew(notification) {
  return this.notificationDB.get(notification.id)
      .then(function(result) {
        if (result !== undefined) {
          throw new Error('notificationExists')
        }
      })
}

I could track down the problem in the promise-plugin:

// Method name and argument count
var storeMethods = [
  ['put', 3],
  ['get', 2],
  ['del', 2],
  ['count', 1],
  ['clear', 1],
  ['batch', 2],
  ['all', 1],
];
// Later in the patch method
// Denodeification with argument count specified in the storeMethods array
object[m[0]] = denodeify(object[m[0]], m[1]);
// ...

The argument count in the array is wrong. For get it works if I use an argument count of 1. But why do you bother maintaining a list with argument counts? You could just omit the count and the denodeify() method would figure it out itself (https://github.com/then/promise/blob/0a8c481dd8e9825ffd05251658f22d64bead1315/src/node-extensions.js#L13). This solves the problem:

object[m[0]] = denodeify(object[m[0]]);

Was there a reason for manually specifying the count? Do you think I maybe ran into this problem because of something webpack does?