Create User Models
Learn how to use loopback to generate a model specifically to store user information that will eventually be used to login/register.
Last updated
Learn how to use loopback to generate a model specifically to store user information that will eventually be used to login/register.
Last updated
A data model schema is nothing more than defining the shape of our data or in other words the properties that the objects will have. Models in loopback are nothing more than us declaring the property name and its data type. This will be the structure:
AppUser Model { firstName: string, lastName: string, password: string, age: number }
Think of a loopback model as creating a class in typescript or prototype in javascript. We are defining what properties we need and what type of data each property will be. Above is an example for a model called AppUser. We defined a property called firstName, that is expecting the datatype of string; In our http post request to our backend to create an AppUser this is the object structure our backend is expecting. When sending data to loopback the properties must be exact in spelling and casing and the data type must match or it will return an error as well.
Video Instructions Here
Loopback Doc: Creating a Model
In your terminal and in the backend project folder
Now we can enter the name of the model, in our case appUser
Next up we need to select the datasource our model will be attached to, in our case the mongodb we created earlier.
The next selection is to use a default base for the model. If we select another model say, user or another pre-made model it will have all those models properties plus the ones you define later. In our case we are just selecting a User as the base. This will give us all the base functionality we needed out of the box of a loopback User model.
The next prompt we will select if the model is exposed on our backend. If we select Yes then this model can be accessed from external sources , like an http request, if we were to select no the model is only available within our loopback application. In our case we want to select Yes because we want our angular application to be able to interact with this model on our server.
Hit Enter For Yes
After exposing our model on our API, we now can pick a custom plural method, for example loopback automatically adds an s to the model name for the endpoint, so a custom endpoint will be /citys by default versus cities being the proper plural. In this case appUsers is fine. Hit Enter For Yes
Select common on the next option
Now all the basic model setup is done. You should have the image where we can start adding properties to our model. *Note pay careful attention to casing this will matter.
Our first property to add will be firstName
Now we select the data type that the property will be, in our case string, because names are strings. It is important when creating a model to understand what you need to store and the proper data type that best fits your model. Select string.
Now we have the choice of making a property required or not. If we select no and the property is not present when trying to store data it will still store it in our database. If we select Yes, then our appUser must always have a firstName property in order to be saved to the database. Hit Y then ENTER: this will make the name property required to save.
Our next option is asking us when if the model’s property is empty should loopback enter a default value? Hit ENTER for no. We do not want a default value if the name property is blank.
Repeat the above steps to add the following properties:
Note: Letter Casing will matter, ex: lastname vs lastName
lastName
Type: String
Required: Yes
Default Value: None
Type: String
Required: Yes
Default Value: None
Start your loopback project from within the final-project-bk folder
$ node .
Open the loopback api explorer app
2. We now have the appUser model and route in the explorer.
3. Open up the appUser route
The above image is with the appUser model opened in the explorer and shows us each type of request to the /appUsers route. Anything we to do with the appUser model we created will be visible here. On the right of each row is a short description of what each route will do.