Use Realm Object Database with Node.js

Will Button
InstructorWill Button

Share this video with your friends

Send Tweet
Published 6 years ago
Updated 3 years ago

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.

[00:01] 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.

[00:18] 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.

[00:41] 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.

[01:04] 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.

[01:24] 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.

[01:49] 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.

[02:13] 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.

[02:51] 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.

[03:21] 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.

[03:45] 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.

[04:08] 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.

[04:26] 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.

[04:58] 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.

[05:16] 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.

[05:34] 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.

[05:54] 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.

[06:09] 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.

[06:34] 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.

[06:52] 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.

[07:15] 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.

[07:49] 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.

~ 6 years ago

Hi, i am having problems installing Realm on a windows machine. has anyone run into problems too ?

using node version 6.2.1

node-pre-gyp ERR! Tried to download(404): -v48-win32-x64.tar.gz node-pre-gyp ERR! Pre-built binaries not found for realm@0.15.0 and node@6.2.1 (node-v48 ABI) (fa lling back to source compile with node-gyp) . . . .

Will Button
Will Buttoninstructor
~ 6 years ago

Hi Noé, Sorry to hear you are having problems installing. I have not tried it on Windows, but it appears there may be a known issue with it: As a work-around, you could try running node.js in a Vagrant image, a Docker container or use Cloud9 which provides a complete environment (and is free for public repos) -