- Overview
- Installation
- Actions
- Tasks
- Middleware
- Satellites
- Cluster
- Cache
- Chat
- Development Mode
- File System
- Events
- WebSocket
- TCP
- HTTP
- Security
- Validation
- Logging
- Commands
- Testing
Models
- Introduction
- Data Types and Attributes
- Instance and Class Methods
- Validations
- Associations
- Model Configuration
- Queries
Instance and Class Methods
Instance methods
You can attach instance methods to a model which will be available on any record returned from a query. These are defined as functions in your model attributes.
{ |
toObject/toJSON
The toObject()
method will return the currently set model values only, without any of the instance methods attached. Useful if you want to change or remove values before sending to the client.
However we provide an even easier way to filter values before returning to the client by allowing you to override the toJSON()
method in your model.
Example of filtering a password in your model definition:
{ |
Class Methods
“Class” methods are functions available at the top level of a model. They can be called anytime after you get a model instance.
These are useful if you would like to keep model logic in the model and have reusable functions available.
// example.js - just an example model |