Creating a job board with Node.js and FaunaDB - Part 2
Tags: fauna, node, javascript, serverless
Published: 2020-12-08 20:57:10

### Introduction

This post is the second part of a series of posts I am writing documenting my studies with FaunaDB where I am trying to create an API for a job board.

In this specific part I will be doing the CRUD for the skills that will be used both for candidates and job postings.

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


---

### Content

* [Module structure and objective](#c-1):
Creating the files that will be part of the module and adding it to the application.
* [List skills](#c-2): 
I talk about how to list documents with Fauna and also how about I should clean the answer before sending it to the user. 
* [Add skill](#c-3): 
How to add documents in Fauna. Also I create a function for validating my model.
* [Cleaning the response](#c-4):
In this part I've made a function that clean the results I get fro the database before sending to the user.
* [Updating skill](#c-5): 
I go on how to update a document on Fauna

---

### Module structure and objective <a name="c-1"></a>

I've decided to start from the skills because it seems like the easiest one, after all, it's model will only be a name.

First of all, I will create a folder named "Skills", inside it, I will create two files, routes.js and controller.js

I would usually also create a "db.js", but I really feel like this won't be necessary, as FaunaDB doesn't need much configuration, but I could be wrong on this.

Skill will be in the url: http://localhost/skills;

I've added the skills routes in the main routes file:

![](https://miro.medium.com/max/875/1*N9n0RNaRzh_Y7udM0jio5Q.png)


For now, I will only create three functions for skills:

* List
* Add
* Update

I will not create a get function (for a single skill) because I don't see how it would be useful in this app.

---

### List Skills <a name="c-2"></a>


For the list skills query, I will use [this stackoverflow question](https://stackoverflow.com/questions/61488323/how-to-get-all-documents-from-a-collection-in-faunadb) as a base.

I've adapted the answer there and created my "listSkills" function:

![](https://miro.medium.com/max/875/1*EA0jWVU0O4RxEpm5LlPmQg.png)

Aftert that, I've created a route in the skills routes.js file:

![](https://miro.medium.com/max/875/1*r49dgM38YYoTHyetyDZQCA.png)

With that done I went into testing and realized there were an error.

I was importing the main router with the wrong name, so I've corrected it to routes.js:

![](https://miro.medium.com/max/875/1*zLulbHExPi7YZMlRrCsQGg.png)

I had also forgot to declare the variable type for my imports at the controller: 

![](https://miro.medium.com/max/875/1*OEZ-g6SB9BiS-Dv9fpSZHA.png)


This time, the server went online as expected, however, for some reason it was not responding, so I've realized that my route was returnung the list instead of responding it:

![](https://miro.medium.com/max/875/1*FbHMsfZyLYEas4ZjjZoefg.png)

Fixing this error, the route works as expected:

![](https://miro.medium.com/max/875/1*UoATieQmhtaKFEQ6jnLqPA.png)

Before I move on, I would like to clean this information and format the response as an array of this object:

```
{ 
  name: "skillName", ref: "skillId" 
}
```

However, when I went into cleaning this response, I've noticed that I am not sure how this response will look like after I add new skills, so instead I've decided to clean the response after I add more documents.

---

#### Add skill <a name="c-3"></a>


For inserting a skill, I will use [this piece of documentation](https://docs.fauna.com/fauna/current/api/fql/functions/create?lang=javascript) as a base.

I started creating a function to validate and clean the documents I receive from users, to ensure they won't pollute the database with data types I don't want or with aditional fields:

![](https://miro.medium.com/max/875/1*IEFlz-UPUnuJke3QMLay2A.png)

The validation is pretty straight forward, I only ask if there is a skill and if it has a name, and the cleaning part, I create a new object that has only the name in it and ensure it's a string.

![](https://miro.medium.com/max/875/1*GquIH8qwfWDMY8MHzAt88A.png)

So I bega to write my route and realized that my return is not talking about http status, so I've added the status to the returns:

![](https://miro.medium.com/max/875/1*bxddF6tUKqw_2Wf0F1jMjA.png)

![](https://miro.medium.com/max/875/1*J1cBBU3G_loJCZ4zeMlkYQ.png)

![](https://miro.medium.com/max/875/1*Rb0tXcCx3K249WfDZoR61A.png)

The status 400 mean that the user sent me an incorrect information and the status 200 mean everything is ok.

With that done, I've written the route like this:

![](https://miro.medium.com/max/875/1*JJGYhHBXB4VBnLK164u7vw.png)

And now to the test with postman;

First, I've sent a empty request, to test my validation:

![](https://miro.medium.com/max/875/1*P_n1pdsFIVgnEeBqGl3hDA.png)

After that, I tried with a skill object with nothing inside:

![](https://miro.medium.com/max/875/1*P-PFlLK8mC8-gwSgyDmIlA.png)

And for last, I tried with one object that should pass:

![](https://miro.medium.com/max/875/1*FQcilqbqnoJhWgYaaOtwzg.png)

The response is huge, but it's possible to see that the data was correctly inserted.

---

### Cleaning the response <a name="c-4"></a>

Now that I can insert many skills easily, I will clean the responses, so it only countains the name and the reference for that skill.

To do that, I've created this function:

![](https://miro.medium.com/max/763/1*7YlpXD6h6wXpyBFHIW7mww.png)

And passed the list I return from the database by it:

![](https://miro.medium.com/max/875/1*oJglsPOIZtnDEqKGU_o6QA.png)

And the result was the expected:

![](https://miro.medium.com/max/875/1*aVp52JZiTw49rXkQ9jzyzA.png)

With this done, I will also add this function in the insertion:

![](https://miro.medium.com/max/875/1*3yGjE5Y3O_Vmohsx5RULfw.png)

And it also works as expected: 

![](https://miro.medium.com/max/875/1*yGbGN4oj9G_3aIOmrqj30g.png)

---

### Updating Skill <a name="c-5"></a>

The updating is mostly a copy form insertion, the difference is the query, that I will copy from [this documentation](https://docs.fauna.com/fauna/current/api/fql/functions/update?lang=javascript) and that I need to validate if the reference existis.

So, I added the validation for the reference:

![](https://miro.medium.com/max/875/1*QhsKqttuRB5JBM0cHjjK-A.png)

And changed the query for update:

![](https://miro.medium.com/max/875/1*fGQ-YXasF8NIgSJEOPtz8w.png)

I will not print the entire code for the update here because it's too big and wouldn't fit the screen, but it will be on github.

With that done, I created the route:

![](https://miro.medium.com/max/875/1*8A_TKobysnTKgVRCXllYLA.png)

And tested with postman:

![](https://miro.medium.com/max/875/1*7VLgVVKSW2mUtTPWpPEUJQ.png)

---

### Conclusion

With that, the skill module is done, in the next post I will start with the other modules, that should be more fun to work with, as they will have some references and more complex things to learn.

Github repo for this project: 

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