banner



How To Create Model Class In Node Js

Introduction

Model classes are helpful in saving the data with multiple objects of the same class. Additionally, these are also helpful in parsing objects to JSON and JSON to object. Also creating and using the model classes are quite easier. In this article, we learn how to create a Model class in JavaScript.

However, a model class will have a constructor with parameters and in turn, each parameter will have getter and setter methods. Furthermore, we have found two different methods to create and use a model class that really works.

Moreover, we are going to create a separate javascript file of the model classes and we access them from the main file where the execution starts from. Let's say we call the file as app.js.

model class project structure
Project Structure

Method 1: Create Model Class using Class Keyword

while to describe this method we are going to create a Customer class having all the details of Customers like 'customer id', 'name', 'address' and 'phone number' as the member variables of the class. Also, there will be a constructor with the parameters to assign values to the member variables of the class. Still, there are getter and setter functions for each member variable.

This class will be named as Customer.js

Customer.js
class Customers {        constructor(CustomerId,Name,Address,PhoneNumber) {          this.customerId = CustomerId;         this.name = Name;         this.address = Address;         this.phoneNumber = PhoneNumber;       }        getCustomerId() {         return this.customerId;       }        setCustomerId(customerId) {         this.customerId = customerId       }        getName() {         return this.name;       }        setName(name) {         this.name = name       }        getAddress() {         return this.address;       }        setAddress(address) {         this.address = address       }        getPhoneNumber() {         return this.phoneNumber;       }        setPhoneNumber(phoneNumber) {         this.phoneNumber = phoneNumber       }  } module.exports.customers = Customers;

After creating the model class for the Customer, let's create an object of Customer and assign values using the constructor of the class to access the values of the member variables.

app.js
var {customers} = require('./models/Customers');  	// assign values using constructor 	var cust = new customers(12, 'Tom', 'New Jersey', '234987');  	// access the values 	custId = cust.customerId 	custName = cust.name 	custAddress = cust.address 	custPhoneNum = cust.phoneNumber 	//custName2 = cust.getName() 	console.log(' customer id: '+custId+'\n name : '+custName+'\n address: '+custAddress+'\n phone number: '+custPhoneNum);

We have just seen how to create an object using the constructions. Now we assign and access values using getter and setter methods of the Customer Model Class.

app.js
var {customers} = require('./models/Customers');  	var custObj2 = new customers()  	// set values using setter functions 	custObj2.setCustomerId(13) 	custObj2.setName("Srinivas") 	custObj2.setAddress("Bangalore") 	custObj2.setPhoneNumber("81098745")  	// get values using getter functions 	cusIdObj2 = custObj2.getCustomerId() 	cusNameObj2 = custObj2.getName() 	cusAddObj2 = custObj2.getAddress() 	cusPhoneNumObj2 = custObj2.getPhoneNumber() 	console.log('\n customer id: '+cusIdObj2+'\n name : '+cusNameObj2+'\n address: '+cusAddObj2+'\n phone number: '+cusPhoneNumObj2);        

Method 2: Create Model Class With function Keyword

In Method 1 we have created a model class in javascript with the 'class' as the keyword. Whereas in this method we create a model class using 'function' keyword. Like method 1 this function also does have constructor, getter and setter functions. Still, this model function will have member variables.

Then again we create a different Model function compared to the previous method model as Customers. We create a Model function named Students, where this will have roll number, name, group, and marks as the member variables.

Students.js
// constructor function for the Students class function Students(RollNumber,Name,Group,Marks) {     this.rollNumber = RollNumber;     this.name = Name;     this.group = Group;     this.marks = Marks; }  //Students getter and setter methods Students.prototype.setRollNumber = function(rollNumber) {     this.rollNumber = rollNumber; };  Students.prototype.getRollNumber = function() {     return this.rollNumber; }  Students.prototype.setName = function(name) {     this.name = name; };  Students.prototype.getName = function() {     return this.name; }  Students.prototype.setGroup = function(group) {     this.group = group; };  Students.prototype.getGroup = function() {     return this.group; }  Students.prototype.setMarks = function(marks) {     this.marks = marks; };  Students.prototype.getMarks = function() {     return this.marks; }  // now we export the class, so other modules can create Students objects module.exports = {     Student: Students }

Later we acmes this model class in app.js and create a object using the constructor. Afterwards we will access the values that we have assigned to the constructor.

app.js
var {Student} = require('./models/Students');  // creates some student object let stu = new Student(3456,"Manny","CSE","345");  // access values assigned using constructor. stuRollNum = stu.rollNumber stuName = stu.name stuGroup = stu.group stuMarks = stu.marks console.log('\n roll number: '+stuRollNum+'\n name : '+stuName+'\n group: '+stuGroup+'\n marks: '+stuMarks);        

Finally, we create an object and set values to the object using setter methods. In turn, we access the values from the same object using getter methods.

app.js
var stuObj2 = new Student()  // set values using setter functions stuObj2.setRollNumber(3457) stuObj2.setName("Srinivas") stuObj2.setGroup("ECE") stuObj2.setMarks("375")  // get values using getter functions stuRollNumObj2 = stuObj2.getRollNumber() stuNameObj2 = stuObj2.getName() stuGroupObj2 = stuObj2.getGroup() stuMarksObj2 = stuObj2.getMarks() console.log('\n roll number: '+stuRollNumObj2+'\n name: '+stuNameObj2+'\n group: '+stuGroupObj2+'\n marks: '+stuMarksObj2);

How To Create Model Class In Node Js

Source: https://buildcoding.com/javascript-create-model-class-with-getters-setters/

Posted by: blayowle1987.blogspot.com

0 Response to "How To Create Model Class In Node Js"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel