DEV Community

Cover image for Creating a job board with Node.js and FaunaDB - Part 2
Luis Felipe Ciochetta
Luis Felipe Ciochetta

Posted on • Updated on

Creating a job board with Node.js and FaunaDB - Part 2


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:


  • Module structure and objective: Creating the files that will be part of the module and adding it to the application.
  • List skills: 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: How to add documents in Fauna. Also I create a function for validating my model.
  • Cleaning the response: In this part I've made a function that clean the results I get fro the database before sending to the user.
  • Updating skill: I go on how to update a document on Fauna

Module structure and objective

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:

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

For the list skills query, I will use this stackoverflow question as a base.

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

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

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:

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

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:

Fixing this error, the route works as expected:

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" 
Enter fullscreen mode Exit fullscreen mode

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

For inserting a skill, I will use this piece of documentation 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:

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.

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:

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:

And now to the test with postman;

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

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

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

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

Cleaning the response

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:

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

And the result was the expected:

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

And it also works as expected:

Updating Skill

The updating is mostly a copy form insertion, the difference is the query, that I will copy from this documentation and that I need to validate if the reference existis.

So, I added the validation for the reference:

And changed the query for update:

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:

And tested with postman:


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:

Top comments (0)