Realm is an ACID compliant object database. In this lesson, you will learn how to install Realm, define schemas for your data, perform CRUD operations and persist your data to the filesystem.
To get started using the Realm database for Node.js, the first thing we have to do is install it. We can that with mpm install realm. Then we'll save it to our dependencies with --save. With it installed, I can enter into a Node ripple interface.
I can start using it by declaring realm as require and then specify the Realm package that we installed. Realm uses models to define your database schema, so let's define a pet schema. We're going to give that a name of pet. Then we'll define the properties for that schema.
Each pet's going to have a name which is a type of string, a species which will also be a string. We'll have an age that's an integer, and then has fur is going to be a Boolean. I'm going to define a second schema that's a person schema.
Again, we'll give it a name of person, and I'm going to define a primary key for it. I'll show you where that comes into play later in the lesson. The properties for it first will be our ID of a type integer. This is going to be the primary key that we just defined.
Primary keys can also be strings as well. They're automatically indexed. It's going to have a name property that's going to be a string, a birthday will be our type of date, and then each person can have a pet. That pet is going to be a type of pet from the pet schema.
With our schemas defined, we now need to initialize a Realm instance. We'll create a new Realm object. Then we'll pass in the schema which is going to be array. We're going to use the pet schema and the person schema. We can use the realm.write function to save data to our database.
Inside of that function, we'll call realm.create, pass in the schema that we're going to use, and then the properties for that schema. I'll create an ID of zero. We'll give this person a name of "Bob." Bob's birthday is going to be 2001 of May 1st. Bob's pet is going to have a name of Fluffy, a species of cat, age is four, and has fur is true.
I created a few more people and pets just to give us some interesting data to look at, so let's look at it now. I say we'll let people equal realm.objects of type person. We'll let pets equal realm.objects of type pet. We can take a look at the people. It shows all of the entries in the database.
We have Bob, the one that you saw me create, with his cat named Fluffy. We also have Tom who has a dog named Spot, Jim and Will with his pet goat. The interesting thing about this is I can also do pets, and it shows you the pets because it used that relational data to store the pets in the pets schema separate from the person object.
Realm also has the ability to filter, so we can do pets.filtered and then pass in our filter criteria and say species is equal to goat. It returns the matching result sets. We can do pets.filtered and say age is less than five, and we get those results back.
We can also specify multiple criteria in a filter. We can say has fur equals true and name begins with the letter F. and it returns Fluffy, our cat, who also happens to be furry. There's a sort operation so we can sort the results by age.
We also need to be able to update our data. Since we specified a primary key for our person object, we can do so by starting a write operation and then calling realm.create again, specifying person, passing our ID of zero, and change the name to Tommy. Then add this true flag on there, which tells it go ahead and modify the existing instance.
We issued a create statement, but then we issued the ID number of an ID that already existed and supplied the true flag which told it to update that existing instance. We can see that by setting new people equal to realm.objects of type person and then displaying it out.
We can see that the ID number zero, the name is now Tommy whereas before it was Bob. We can delete as well. We can day let Tom equal realm.objects of type person, filter that to name is equal to Tom. Take a look at that. There's Tom.
Then we can start a write operation again. We can call the delete method passing in the item we want to delete. Now if we take a look at our people array again, we can see that Tom has been deleted. You may have noticed that I never specified a server or a data source for saving my data.
What happens whenever I close this session? Did I just lose all of my data? The answer is no. If I do a directory listing, you'll see files and directories here starting with the name default.realm. That's where the data is actually stored.
If I start a new Node session, I can redefine my schema and re-initialize my Realm data source and then run a query against that. All of my data is still there. That's cool that it saved it, but the names are kind of generic. They don't really tells me anything about what's in that data set.
Let's not do that. Instead, let's create a new schema here. We'll give it some properties. It'll have a name that's a type of string, a species that's also a type of string, and a Boolean, whether or not it eats people, always important to know.
Now we can initialize a new Realm instance. This time, we'll provide a path object. It's going to be a relative path to wherever the code happens to be running. In this case, I'm going to put in the same directory and just save it as beasts.realm and then specify the schema like we did previously.
Let's go ahead and save something into that data schema using our write operation, pass in a function, call realm.beast.create. We'll create a type of beast. The name will be tiger, species is cat, and eat people is true. We can do a query, say let beasts equal realm.beast.objects and then pass in our type of beast, there's the beast that we created.
Then, more importantly, if I exit this and do another directory listing, you can see that the beast Realm files have now been created which gives you a little bit of insight into what's actually going to be found inside of the data there.