cvburgess/SQLDataSource

receive error when .cache is provided: UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'get' of undefined

Bilal-S opened this issue · 1 comments

When I provide the .cache attribute to knex query I receive this error

(node:10584) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'get' of undefined
    at MyDatabase.cacheQuery (F:\Code\menext\node_modules\datasource-sql\index.js:50:23)

The line that is not liked is this:
return this.cache.get(cacheKey).then(entry => {

I have added apollo-server-caching module added to project.
I would appreciate guidance on what I am missing here to enable cache.
Once I remove it, I can get results from DB.

This is my example class:

const { SQLDataSource } = require("datasource-sql");

const MINUTE = 60;

class MyDatabase extends SQLDataSource {
    getLists() {
        return this.knex
          .select("*")
          .from("lists")
          .where({ id: 24 })
          .limit(100)
          .cache(MINUTE);
    }
}

module.exports = MyDatabase;

When I trace this further, this part with the creation of cache store via init never gets called.

  initialize(config) {
    this.context = config.context;
    this.cache = config.cache || new InMemoryLRUCache();

    if (DEBUG && !hasLogger) {
      hasLogger = true; // Prevent duplicate loggers
      knexTinyLogger(this.db); // Add a logging utility for debugging
    }
  }

I am closing this.
Seems to be an issue with my unit-test.
When invoked in context of Apollo server the initialize() call is made by Apollo before this is called, but when in unit test I have to provide a mock.

However, this project seems dead as no one responded in nearly two weeks. Should be probably marked as abandoned.