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

Custom methods on model? #53

Closed
fridays opened this issue Oct 1, 2016 · 4 comments
Closed

Custom methods on model? #53

fridays opened this issue Oct 1, 2016 · 4 comments
Labels

Comments

@fridays
Copy link

fridays commented Oct 1, 2016

Hi, this project looks great!

Is it possible to add custom methods to a model, something like the following?

userModel.methods.getFullName = function() {
    return this.fname + ' ' + this.lname;
}
@ummjackson
Copy link

+1, would love to know if this is in the future of Obey.js 👍

@ummjackson
Copy link

Just found https://github.com/node-modli/modli created by the same maintainers as Obey 💯

@ksafranski
Copy link
Contributor

There isn't any built-in way to do this as it stands, however, as @ummjackson shared, Modli (built by our team) is a full CRUD solution which extends on Obey to allow for doing things like this.

Given getFullName is more a function of a data read action, and not so much one of validation, I would see it better placed outside the scope of Obey. You could do something as simple as:

const user = {
  schema: obey.model({
    fname: { type: 'string' },
    lname: { type: 'string' }
  }),
  getFullName: (data) => {
    return data.fname + ' ' + data.lname
  },
  read: () => {
    return db.read({...})
  }
  /* ...other custom methods... */
}

The above is essentially what Modli does, just creating a model object with other methods associated, then on create and update we just run the data through the schema's validate method beforehand.

@ksafranski
Copy link
Contributor

I'm willing to reopen this if there's a sound argument for including this functionality, or simply to discuss further. However, for now I'm going to close it.

@ksafranski ksafranski self-assigned this Oct 4, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants