Sequelize: Class & Instance Methods

Mike Frey
InstructorMike Frey
Share this video with your friends

Social Share Links

Send Tweet
Published 9 years ago
Updated 5 years ago

Models in the Node.js ORM, Sequelize, can be expanded with both class level and instance level functions. This video will show you how to extend your models to add helpers or business logic, and explain the methods execution scope.

[00:01] Sequelize supports adding class and instance functions to models via the define method's options argument. Class methods are accessed from the model's definition object. I have an author model already set up. If I add a class method to it called "foo," it will be accessible by calling author.foo. Instance methods are accessed from an instance of the model.

[00:21] If I have an author instance, perhaps from assigning the result of calling author.build to the variable Mike, I'd be able to call the instance method bar by invoking Mike.bar.

[00:31] Let's take a look at some examples. We'll start with class methods.

[00:36] To add a class method to my model, I'll need to add an object as the third argument to define and specify a classMethods property, which is also an object. Each property key of the classMethods object specifies the name of the method that will be added to the class.

[00:51] If I add a property called "foo" here, it'll be accessible as author.foo. I'm going to name this one "buildFromArgs." The property value is the function that will be executed when the class method is invoked. There are no standard arguments, so I can add whatever I'd like.

[01:07] The function will execute under the context of the model definition, which means I have access to my author model via the "this" keyword. Executing under the model definitions context allows you to easily reuse the functions in many models without having to duplicate them.

[01:20] My buildFromArgs class method is just going to build and return a new instance of author from the given arguments. Now, I'm going to call the buildFromArgs function from the author object and assign it to the variable Mike. When I log the result, the unsaved object is printed to the screen.

[01:41] Next, let's take a look at instance methods. Like class methods, instance methods are added to the options argument of the define method in a property called "instanceMethods." Just like class methods, each property key of the instanceMethods object will specify the name of the method that will be added to the instance.

[01:58] The property value is the function that will be executed when the instance method is invoked. The upperCaseAndSave instance method I've just added to author will force the value of the specified field to upper case, then call save on the instance.

[02:13] It's worth pointing out that instance methods run under the context of the instance they are called from. When I call Mike.upperCaseAndSave, the "this" keyword will refer to the author instance Mike. When run and logged, the output now has an ID indicating that the data has been saved and the last name has been forced to upper case.

Prith
Prith
~ 8 years ago

implementation wise, what's the difference between class methods and instance methods? As in, in what scenarios will we be using each of them?

Mike Frey
Mike Freyinstructor
~ 8 years ago

Generally, if you want to act on a single instance, you'd create an instance method. If you want to act on multiple instances, such as, a method for helping query the database, you'd create a class method.

Markdown supported.
Become a member to join the discussionEnroll Today