Creating a job board with Node.js and FaunaDB - Part 4 - Inserting nested arrays of objects
Tags: javascript, node, fauna
Published: 2020-12-10 12:50:38

This is the fourth part of a series of posts documenting my studies with FaunaDB, these are the links for the first three posts:

{% post https://dev.to/ciochetta/creating-a-job-board-with-node-js-and-faunadb-part-1-51fb %}

{% post https://dev.to/ciochetta/creating-a-job-board-with-node-js-and-faunadb-part-2-1926 %}

{% post https://dev.to/ciochetta/creating-a-job-board-with-node-js-and-faunadb-part-3-5fk %}

---

### Introduction

I finally got where I wanted to be from the start of these posts, the candidate module should be a little more complex than the previous ones;

Here, I'll need to learn how to make joins and insert references in the database as I would in a regular relational database, however, as this is NoSQL, instead of creating those n => n tables with only IDs, I will have arrays of references inside objects.

---

### Content

*[Candidate Module](#c-1): 
Creating the module structure

*[Insert Candidate](#c-2): 
I create the bulk insert function for the candidate's skills and then collect and insert the references for these skills in the candidate object

---

### Candidate Module <a name="c-1"></a>

First thing, I will copy another module's folder and add a reference in the root's route.js for this module.

Then I will proceed to change all the names to candidates and remove the content for the functions.

The URL for this module will be: "http:localhost:3000/candidates";

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/w8b18tv7ba7nj2puvf7e.png)

---

### Insert Candidate <a name="c-2"></a>

Normally, I would start a module by creating a list, but not this time, I will delete the document there is already in the database and proceed to create new documents.

I want the request for a candidate to have an array of skill names, then I will check for those names in the skills table and insert the ones that do not exist.

After that, I will insert the user in the database with an array of references for the skills he has and the level of each skill.

I will be using this object from my first post of this series as an input:

```
{
    "name": "fake candidate",
    "email": "someone@gmail.com",
    "bio": "was literally just created",
    "social_media": [
      {
        "name": "twitter",
        "link": "https://twitter.com/fake_candidate"
      }
    ],
    "skills": [
      {
        "name":"AWS",
        "experience": "advanced"
      },
      {
        "name":"Azure",
        "experience": "advanced"
      }
    ]
  }

```

When I send this, my function should insert both AWS and Azure in the skill table return their reference and replace the name in these objects for it;

So the first function that I will use in this module will actually be in the skills module, I will create this "bulk insert" function there, as it sounds like it belongs in the skills domain more than in the candidates.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/r2kvlj0kmiic2igte4fg.png)

About this function, it is first separating the new skills from the ones already on the database, after that, inserting the new skills and returning both.

Now that I have the references for each skill, I have looked around and found [this StackOverflow question](https://stackoverflow.com/questions/61104940/how-to-get-nested-documents-in-faunadb) that explains how to get the documents will be inserted in this table. 

The author says in his answer that we should probably store the entire reference query instead of the id, so that's what I did:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/sm01u043ntlm30qo00o1.png)

I am storing the query along with the experience level in that skill, and this should be the final look for the skills array in the candidate object.

I will now proceed to create the validations for the candidate object like I've done for the other models and proceed to test the insert candidate function.

It did work, this is what to object looks like in the database: 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/u27118ofqeamlblxg7pg.png)

The response to this request is huge, but I will work on that in the next post.

---

### Conclusion

The nice parts about FaunaDB are starting to show up (besides it being serverless and secure).

I am really excited to finish this module and make this joins for the candidate list, it sounds like a nice challenge

---

Repository for this project:

{% github https://github.com/ciochetta/faunajobboard no-readme %}