2.2 Creating Our Data Models
In order to build our application, we need to define the data structures our app needs. In this lesson, we'll define our MongoDB data models.
1.Introduction2 lessons, 12:18
2.Getting Started2 lessons, 20:32
3.Users and Authentication5 lessons, 54:30
4.Managing Currencies5 lessons, 46:15
5.Managing Our Portfolio5 lessons, 50:13
6.Security1 lesson, 10:49
7.Conclusion1 lesson, 00:38
2.2 Creating Our Data Models
When starting a new project, I always have a difficult time knowing where to start. And a lot of times I've just jump right in, and yeah, I find that I make a lot of mistakes that way. So I tend to start to think, okay, where would be the most logical place to start. And I think in this case it would be from the user's perspective. What is the first thing a user is going to do? Well, they're going to register. So of course in order to register, we need to store the user information in a database or at least have that capability up and running so that a user can register. So let's start there. So this is going to be our data model which is going to be on the server side. So inside of our server folder, let's create a new folder called models. And inside of there, we will create a new file called user.js, and this is going to be our user schema. So we need several things here. The first is mongoose because that's how we're going to set up our schema. So we'll bring that in but we also need schema from mongoose as well. Because every schema that we create. Well, we do so using mongoose.Schema. So we will have that and then we can define our schema. So we will have UserSchema equals and we will new up the schema constructor, then we will pass in an object that defines our schema. Now, we don't have to worry about an ID because that's automatically done. But we do need the email address for the user. So this is going to be a string. Let's also set unique to true. And just for good measure we're going to say index to true. Now frankly, I don't really know if we need to set index to true because I know that if we set unique to true that that kind of signifies that there needs to be an index build. But I'm going to leave index true because I want to make sure that there is an index there. So we have our email address. We also need a password which is going to be a string. And we also need a flag to determine if the user is an admin. Because there are going to be some administrative tasks like managing the currencies. We don't want to just say okay, we're going to support all of these currencies. We want to be able to pick and choose, okay? Let's support for. Let's support stipend. Let's support Infinex, things like that. So there is going to be some administrative capabilities so we will make sure to track that in the database. But then we also want to track the user's portfolio. That's what we are building this whole thing for which is going to be a collection of things. And one of the pieces of information is going to be the address. So if you'll remember from the very, very brief talk about blockchains, that's kinda the account. That's the account number, if you will. So that's the address, but the address isn't enough. We need to know the currency that address associated with. So the currency is something that we are also going to have to store within the databases as well. So let's call the currency and lets say CurrencySchema, because what we are doing, is we are going to be using is called an embedded document. That's how relationships are made with a document database. You embed a document inside of another one. So that's going to be a different schema that we are going to have to define before we do anything else as well. So but let's go ahead and finish our users. So we're going to say mongoose.model, we want to call this collection users and it's going to use the UserSchema. And as far as our exports is concerned I like to export two things. The schema and then the model itself, because you never know when you're going to need the schema. Like in this case, we need the CurrencySchema to denote that we are going to be storing currency information there. So we will export schema, which is going to be our UserSchema. And then, we will export to user. And since we know that we need a currency model, let's go ahead and create a new file called currency. And we will essentially do the same thing. Let's take our user and let's save that for our currency. We will of course needs to make a few changes like for example this is going to be CurrencySchema, and we don't need an email but we do need a ticker. And the ticker does need to be unique and we build the index on ticker. We do have a name so that's going to be a String, we also need the apiURL, and that is going to be a String as well and that should be it. We don't have to store balance information or anything like that because we're going to be retrieving that from the block explorer. So all we need for our currency is the ticker, the name and the apiURL that we are going to use to interact with. And so now as far as our model is concerned, we'll just call that currency. We'll call our collection, currencies. And of course our schema is going to be CurrencySchema. And we will do the same thing as far as our exports, we will exports CurrencySchema as schema and then we will export currency. So instead of our UserSchema, what we do need is our CurrencySchema, and we will require that from our newly created currency that we will save .Schema and there we go. So that should work. In fact, we can at least check the console, we can see that there are no errors, so that's a good thing. So we are ready to move on. And in the next lesson, we are going to create a setup end point. Many applications have a set of process unit gonna make sense for our's to do the same.