Explore Faunas one-to-many Relationships between Collections in the Terminal

Shadid Haque
InstructorShadid Haque
Share this video with your friends

Social Share Links

Send Tweet
Published 5 months ago
Updated 2 months ago

We'll take a moment to explore how Fauna handles one-to-many relationships inside the fauna shell.

First we'll create a Message collection.

Then we'll connect to the cloud database by running fauna shell, because I'm logged and configured this connects to the database I'm working with.

In the terminal we'll see how to create multiple messages that are associated with a room and query for those messages by room.

[00:00] Let's take a look at the relationship between collections in our application. So we already have a room collection. We're also going to have a message collection. So a room will contain many messages in our application. So there is essentially a one to many relationship between room and messages. So we're gonna go back to our [00:20] code and here under schema, we're gonna go change the main dot fsl file and we're going to add a new collection called message. Next, we're gonna run the Fauna schema push in our terminal. Then we're gonna accept the changes and [00:39] this will update our FaunaCloud and a new collection call message will get created. Next, let's go ahead and connect to our database and run some queries. So in my terminal, I'm going to execute the fauna shell command, and this will connect me to the FaunaCloud database. I can run database queries right from the terminal now. [00:59] So I'm going to get all the rooms. So I'm gonna write room dot all and then I'm going to execute this query by pressing enter. And notice that I get all the rooms that are in my database. I'm gonna press command k now to clear the console. Next, I'm going to create a new message document. So I'm [01:19] just going to write message dot create and then round bracket and squiggly bracket. And inside, we're gonna have a text field. And this field will just contain a string that says hello world. And I press enter and the new message document is created in the database. Now I can get all the messages by [01:39] executing message dot all and I'm just going to get one message because that's the only record we have in the message collection. Now let's clear the terminal again by pressing command k or alt control k if you're on Windows. Next, we're gonna write a query to get the 1st document in the room collection. So we're gonna write to room dot [01:59] all dot take and then inside the bracket 1 to get the 1st document in the room collection. And running this will return the room data with this ID. Next, we are going to create a message for this particular room. So remember, we had one to many relationship between [02:19] room and messages. So we're gonna create a couple messages for this room. So I'm gonna write message dot create, and then I'm gonna have a text field. And here in the text field, I'm gonna have a string. And then I'm going to have a room field. And this [02:38] room field will have a reference to the room document we want this message associated with. So I'm gonna write room dot by ID, and then we're gonna paste the ID of the room. And running this query creates a new message that is [02:58] associated to the specified room. So let's go ahead and create another message for this same room. And now we got 2 messages that [03:18] are associated with this one room. Next, let's take a look at how we can query all the messages in a given room. For instance, we're gonna use this room. So we're gonna be running some multiline queries in the shell. So we can enter the editor mode by typing dot editor and pressing enter. And here you [03:38] can see we have entered the editor mode, and you can run any query with control d or we can cancel out of the editor mode with the control c command. So here we're gonna write let room equals to room by id and then the id of the room and this will give us the reference to the room And then [03:58] we're gonna write message dot where and then round brackets. And inside round brackets, we're gonna have dot room equals equals room, which is the reference to the room. And running it gives us all the messages associated to that room. And [04:18] that's how you do one to many relationships in fauna.

egghead
egghead
~ 43 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today