Summary of question: Conceptually, what are getters and setters and why would we use them?
Excerpt from http://docs.sequelizejs.com/en/latest/docs/models-definition/?highlight=getterMethods#getters-setters:
It is possible to define 'object-property' getters and setter functions on your models, these can be used both for 'protecting' properties that map to database fields and for defining 'pseudo' properties.
What does it mean by 'protect'? Against what?
What are 'psuedo' properties?
I'm also struggling with the example code below. We appear to be setting 'title' twice. And what is the argument 'v'?
See below:
var Foo = sequelize.define('Foo', { title: { type : Sequelize.STRING, allowNull: false, } }, { getterMethods : { title : function() { /* do your magic here and return something! */ }, title_slug : function() { return slugify(this.title); } }, setterMethods : { title : function(v) { /* do your magic with the input here! */ }, } });
A concrete example instead of "do magic" would be greatly appreciated!
Automatically creates tables in MySQL database if they don't exist by calling await sequelize.
Sequelize set upInstall Sequelize database driver for the database you would like to use by running one of the commands below. Install npm package Sequelize-CLI. Create a project folder. In your project folder path, run the command below to initialize Sequelize in the folder.
When you create a Sequelize model, you can add the default value for your model by adding the defaultValue option to the column(s) definition. The defaultValue option will be used by Sequelize to define default value(s) for your SQL column(s) when you create a table using Sequelize.
A model can be synchronized with the database by calling model. sync(options) , an asynchronous function (that returns a Promise). With this call, Sequelize will automatically perform an SQL query to the database. Note that this changes only the table in the database, not the model in the JavaScript side.
pseudo properties
Would be properties that, from the perspective of the user seems like regular properties of the object, but do not exist in the database. Take for example a user object that has first names and last name fields. You could then create a fullname setter:
var foo = sequelize.define('foo', { .. }, { getterMethods: { fullName: function () { return this.getDataValue('firstName') + ' ' + this.getDataValue('lastName') } }, setterMethods: { fullName: function (value) { var parts = value.split(' ') this.setDataValue('lastName', parts[parts.length-1]) this.setDataValue('firstName', parts[0]) // this of course does not work if the user has several first names } } })
When you have a user object you can simply do
console.log(user.fullName)
To see the user's full name. The getter is then being invoked behind the scenes.
Similarily, if you define a setter method for full name you could do
user.fullName = 'John Doe'
Which would then split the passed string into two parts and save them in first and last name. (see the simplified example above)
Protect properties
@ahiipsa already provided a good example of this. Getters are called when you do user.toJSON(), so you can use getters to easily remove sensitive data before sending it to the user.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With