Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do sequelize getter and setters work?

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.

  1. What does it mean by 'protect'? Against what?

  2. 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!

like image 397
user2316667 Avatar asked Feb 22 '14 04:02

user2316667


People also ask

Does Sequelize automatically CREATE TABLE?

Automatically creates tables in MySQL database if they don't exist by calling await sequelize.

How do you use Sequelize models?

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.

How do I set default value in Sequelize model?

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.

How do I use Sync in 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.


1 Answers

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.

like image 106
Jan Aagaard Meier Avatar answered Sep 18 '22 11:09

Jan Aagaard Meier