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

Extending rest api functionality #121

Closed
3210jr opened this issue Sep 13, 2016 · 1 comment
Closed

Extending rest api functionality #121

3210jr opened this issue Sep 13, 2016 · 1 comment

Comments

@3210jr
Copy link

3210jr commented Sep 13, 2016

Hey @ekryski newbie questions here,

So I am (was) a die hard meteor fan, after a quick intro to feathers i think its even better. However, i'm stuck at a few places....

Using the feathers generate tool, and selecting Api and rest api using socket.io, and then selecting mongodb I get the app generated and ready to use rethink-mongoose.

Having come from meteor, i am struggling to do any checking if the document already exists so that i don't insert duplicates, performing upserts, etc.

Can you please point me to any docs that address this.

In short, Where are all the db.find(), db.update(), db.... and stuff and how can i access this from the rest apis?

@daffl
Copy link
Member

daffl commented Sep 14, 2016

The database interactions in Feathers are done through the service interface and the database adapter which all implement that interface.

Checking if an entry exists (and potential upserts) can be accomplished through Hooks which execute before or after any of the service methods runs. For example, a hook that checks for duplicate emails can look like this:

const errors = require('feathers-errors');

app.service('myservice').before({
  create(hook) {
    return app.service('myservice').find({
      query: { email: hook.data.email }
    }, hook.params).then(results => {
      if(result.data.length !== 0) {
        throw new errors.Conflict(`Email ${hook.data.email} already exists`);
      }

      return hook;
    });
  }
});

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