Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to properly extend knex service #155

Closed
lukaszczerpak opened this issue Apr 6, 2018 · 3 comments
Closed

How to properly extend knex service #155

lukaszczerpak opened this issue Apr 6, 2018 · 3 comments

Comments

@lukaszczerpak
Copy link

lukaszczerpak commented Apr 6, 2018

Steps to reproduce

When my service class extends knex service, a newly created instance of my service is pure knex service instance.

const Service = require('feathers-knex')

class CaseService extends Service {
  testMethod() {
    console.log('Hello from test method!')
  }
}

module.exports = function init (options) {
  return new CaseService(options)
}

module.exports.CaseService = CaseService

Simple call to the test method during service initialization:

const createService = require('./case.class.js')
...
module.exports = function (app) {
...
  // Initialize our service with any options it requires
  let service1 = createService(options)
  service1.testMethod()
...

throws an error:

  service1.testMethod()
           ^

TypeError: service1.testMethod is not a function

Expected behavior

Original intention was to extend knex service class and override some functions as I need custom stuff in there.

Actual behavior

Looks like my class is totally ignored and returned instance is knex service.

System configuration

Module versions: feathers-knex: 3.0.6
NodeJS version: 8.11.1
Operating System: macOS Sierra (10.12.6)

Workaround

The following code works though (extending knex service by using builtin function extend()):

const Service = require('feathers-knex')

class CaseService {
  testMethod() {
    console.log('Hello from test method!')
  }
}

module.exports = function init (options) {
  return new Service(options).extend(new CaseService(options))
}

// disabled for safety reasons
// module.exports.CaseService = CaseService
@lukaszczerpak lukaszczerpak changed the title how to properly extend knex service How to properly extend knex service Apr 6, 2018
@daffl
Copy link
Member

daffl commented Apr 6, 2018

The service is exported by require('feathers-knex').Service so it should be const { Service } = require('feathers-knex') (see common database API Documentation).

@lukaszczerpak
Copy link
Author

Many thanks! Yeah, it works now :)

@daffl
Copy link
Member

daffl commented Apr 6, 2018

👍

@daffl daffl closed this as completed Apr 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants