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

Suggested pattern to create a multi model service #86

Closed
subodhpareek18 opened this issue Feb 1, 2017 · 4 comments
Closed

Suggested pattern to create a multi model service #86

subodhpareek18 opened this issue Feb 1, 2017 · 4 comments

Comments

@subodhpareek18
Copy link

subodhpareek18 commented Feb 1, 2017

I have multiple models so I thought I could add a service for each of them. Like so:

Object.keys(models).map(key => app.use(`/api/${key}`, sequelize({ Model: db[key] })))

But this seems like an inelegant approach if I want to have consistent hooks and other behavior for all of them.

I've looked at another approach which generates a service at the time of a request:

app.use('/api/:path', (req) => sequelize({ Model: db[req.params.path] }))

The above doesn't seem to work actually, but the conceptual point stands. Even with this approach it's not very clear how to attach events to this freshly created temp instance of a service. Not even sure if the app will retain all these services or clear them out after a single usage. Plus it might not be very performant either.

I guess what I'm looking for is a parent service that can modularly connect to various models and provides a consistent surface like any other feathers service.

@marshallswain
Copy link
Member

@zusamann With the official database adapters, a service can be setup with a single model. Model relationships happen at the model level, and the service interface stays purely unaware of them.

The second example isn't a good idea with Feathers, but you can possibly make the first work. Once you have created the services, you can reference them by using app.service('path/to/service'). Add hooks by doing another map:

var commonHooks = {
  before: {}, 
  after: {}
};
Object.keys(models).map(key => app.service(`/api/${key}`).hooks(commonHooks));

@subodhpareek18
Copy link
Author

subodhpareek18 commented Feb 1, 2017

Okay makes sense, I guess it's more about the mental modeling of a rest service, treating each of the db resource as a separate service does seem like a better idea.

The approach of a single service for the entire database might make more sense with something like graphql.

@marshallswain
Copy link
Member

Cool. I'm going to close this. Please reopen if you feel like there's something more to discuss.

@ndrwptrsn
Copy link

ndrwptrsn commented Nov 19, 2019

This may be helpful. I used two endpoints and separate hooks on the same service:

const createService = require('feathers-sequelize');
const createModel = require('../../models/charge.model');
const hooks = require('./charge.hooks');
const searchHooks = require('./charge-search.hooks');
const sequelize = require('../../sequelize');

module.exports = function (app) {
  const Model = createModel(app);

  const options = {
    Model
  };

  const charge = createService(options);

  app.use('/api/v1/platform/account/:accountId/charge-search', charge);

  app.use('/localCharge', charge);

  const service = app.service('/localCharge');

  const search = app.service('/api/v1/platform/account/:accountId/charge-search');

  service.hooks(hooks);

  search.hooks(searchHooks);
};

Not exactly two services on the same model, but addresses the include issue.

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

3 participants