ADVERTISEMENT
ADVERTISEMENT

Introduction to Lucid Models in AdonisJs

Here, we are going to learn what is Lucid Models in AdonisJs, its usages, etc.
Submitted by Radib Kar, on January 17, 2021

In the last tutorial, we learnt everything about database migration & our tables is also created if you have followed the tutorial. In this tutorial, we are going to learn about lucid models which help us to manage database relationships.

So let's get started with already created models in our boilerplate. Models are created in app/Models folder. There are by default you should see two models created like below as we had in migrations.

lucid models

So by default, we have two models created. Let's check out the user model first.

The first thing is it has imported the interface "Model" like below,

const Model = use('Model')

Then the user class extends the interface,

class User extends Model {

Then we have the below code snippet where we can see the definition for the boot method where a hook has been added, We will skip this part now, but surely discuss later in our hook tutorial section.

static boot () {
	super.boot()

	/**
	* A hook to hash the user password before saving
	* it to the database.
	*/
	this.addHook('beforeSave', async (userInstance) => {
		if (userInstance.dirty.password) {
			userInstance.password = await Hash.make(userInstance.password)
		}
	})
}

Our main focus for this tutorial is how model defines relationships. For that check this below code snippet,

/**
* A relationship on tokens is required for auth to
* work. Since features like `refreshTokens` or
* `rememberToken` will be saved inside the
* tokens table.
*
* @method tokens
*
* @return {Object}
*/
tokens () {
	return this.hasMany('App/Models/Token')
}

We need to understand what this snippet means as we will be creating similar methods to define all the database relationships. Before that you can see there is already a token model created at the location mentioned under hasMany relationship which is 'App/Models/Token'

This snippet means that a user can have multiple tokens or another way we can say that the relationship between users & tokens is "one to many". That's why in the user model, we have a function named "tokens" which returns all the tokens it has.

Now we need to define the relationship between users & projects. So a user can have many projects and thus we need to add hasMany relationship the same way as tokens() method.

To create a new method named projects() and the body should be like below,

return this.hasMany('App/Models/Project')

Yah, of course, we don’t have our Project model created which we will add in our next tutorial.

Model not only defines the relationship but also whenever we need to create a user object, we have to use this user model(we will see examples later on).

Below is some possible relationships which might help you to build something of your own,

  • hasOne - one to one
  • hasMany - one to Many
  • belongsTo - inverse of hasOne, hasMany
  • belongsToMany - inverse of many to many
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.