Bummer! This is just a preview. You need to be signed in with a Pro account to view the entire video.
Start a free Basic trial
to watch this video
In this video we'll introduce instance methods to our models.
Sequelize 4.0 and Instance Methods
In the video, the following code is used to define the publishedAt()
and shortDescription()
instance methods on the Article
model:
'use strict';
var dateFormat = require('dateformat');
module.exports = function(sequelize, DataTypes) {
var Article = sequelize.define('Article', {
title: DataTypes.STRING,
author: DataTypes.STRING,
body: DataTypes.TEXT
}, {
classMethods: {
associate: function(models) {
// associations can be defined here
}
},
instanceMethods: {
publishedAt: function() {
return dateFormat(this.createdAt, "dddd, mmmm dS, yyyy, h:MM TT");
},
shortDescription: function(){
return this.body.length > 30 ? this.body.substr(0, 30) + "..." : this.body;
}
}
});
return Article;
};
Starting with Sequelize 4.0, class and instance methods are no longer defined via the sequelize.define()
method. Instead, you add them to the ES2015 class that's returned by the sequelize.define()
method call:
'use strict';
var dateFormat = require('dateformat');
module.exports = (sequelize, DataTypes) => {
const Article = sequelize.define('Article', {
title: DataTypes.STRING,
author: DataTypes.STRING,
body: DataTypes.TEXT
}, {});
Article.associate = function(models) {
// associations can be defined here
};
Article.prototype.publishedAt = function() {
return dateFormat(this.createdAt, "dddd, mmmm dS, yyyy, h:MM TT");
};
Article.prototype.shortDescription = function() {
return this.body.length > 30 ? this.body.substr(0, 30) + "..." : this.body;
};
return Article;
};
For more information about this change, see http://docs.sequelizejs.com/manual/tutorial/models-definition.html#expansion-of-models.
Sequelize Documentation
-
0:00
At the top of the articles.js routes file, there's the instance methods that
-
0:04
required to format dates and produce short descriptions on the article's page.
-
0:12
An instance method is a method that provides functionality for
-
0:16
an instance of a model.
-
0:18
For example, we want each instance to generate a published update and
-
0:22
a short description.
-
0:24
These functions should be invoked in the context of each model hence the name,
-
0:29
Instance Methods.
-
0:33
Let's cut these two functions and the date format dependency from the routes file.
-
0:45
And put them in the article model file.
-
0:49
Having these functions in the routes file isn't encapsulating
-
0:53
the functionality where it should be.
-
0:55
It's better suited here.
-
0:56
When you define a sequelize model, you define its attributes,
-
1:02
class methods, and instance methods.
-
1:07
A class method is a helper method that doesn't require an instance.
-
1:11
You've already used some examples of the class methods with the create and
-
1:15
build methods.
-
1:17
These are provided by sequelize.
-
1:19
They on an instance of an object but
-
1:22
they're related to the articles in general, not a given instance.
-
1:27
If you want to add functionality that doesn't require a particular instance but
-
1:31
is related to the class of objects, you can define custom class methods here.
-
1:37
Where I wanted to define instance methods though,
-
1:39
you can do that after the class method definitions.
-
2:14
We can literally move these functions into the instance methods objects.
-
2:19
And these methods will be applied to all instances of articles.
-
2:35
And that's it.
-
2:36
Before we work on the code for retrieving database records,
-
2:40
I wanted to point out this, the createdAt attribute.
-
2:44
Most ORMs create two fields in a table, createdAt and updatedAt,
-
2:49
that gets automatically updated when the model is created or updated.
-
2:54
I used createdAt to generate a publishedAt date in a nice human readable format.
-
3:00
Most ORM libraries handle these time stamps out of the box.
-
3:04
You don't need to worry about them, but they are really useful.
-
3:08
For example, when you list articles, we can order by the createdAt attribute.
-
3:14
We want them all recent articles first and that's what we'll do in the next video.
You need to sign up for Treehouse in order to download course files.
Sign up