kozhevnikov / proxymise

Chainable Promise Proxy

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

proxymise not working with Knex queries

abw opened this issue · comments

proxymise is great, thank you!

Unfortunately I've just discovered that it's not working with Knex.js queries. Here's an example which demonstrates the problem.

// $ npm install knex sqlite3 proxymise
import Knex from 'knex'
import proxymise from 'proxymise';

async function main() {
  // create Knex connection
  const knex = Knex({
    client: 'sqlite3',
    useNullAsDefault: true,
    connection: {
      filename: ':memory:',
    },
  });

  // create a users table
  await knex.raw(
    'CREATE TABLE users ( ' +
    '  id INTEGER PRIMARY KEY ASC, ' +
    '  name TEXT ' +
    ')'
  );

  // insert a row
  await knex('users').insert({
    name: 'kozhevnikov'
  });

  // knex example without proxymise()
  const row = await knex('users').select('id', 'name').where({ name: 'kozhevnikov' }).first();
  const name = row.name;
  console.log('EXPECT name: ', name);

  // works if knex method are inside proxymise()
  try {
    const prxm1 = proxymise(
      knex('users')
        .select('id', 'name')
        .where({ name: 'kozhevnikov' })
        .first()
    );
    const name1 = await prxm1.name;
    console.log('PASSED name1:', name1);
  }
  catch(error) {
    console.log('FAILED name1:', error);
  }

  // doesn't work when knex methods are outside proxymise()
  try {
    const prxm2 = proxymise(
      knex('users')
    );
    const name2 = await prxm2
      .select('id', 'name')
      .where({ name: 'kozhevnikov' })
      .first()
      .name;
    console.log('PASSED name2:', name2);
  }
  catch(error) {
    console.log('FAILED name2:', error);
  }

  knex.destroy();
}

main();

Output:

EXPECT name:  kozhevnikov
PASSED name1: kozhevnikov
FAILED name2: TypeError: Function.prototype.apply was called on undefined, which is a undefined and not a function
    at .../proxymise@1.0.2/node_modules/proxymise/index.js:27:53
    at async main (file:///.../proxymise-knex-bug.js:53:19)

I'm closing this issue as further investigation has revealed that the problem appears to be in Knex, not proxymise. It seems that Knex doesn't return "real" promises but a query object that implements a .then() method according to the Promise/A+ specification. The .then() method executes the query, which works for Knex, but it doesn't play nicely with proxymise (or other similar implementations)