Creating a job board with Node.js and FaunaDB - Part 1
Tags: fauna, node, javascript
Published: 2020-12-06 02:50:01

In the last couple days, I've built the back-end for a to-do list with FaunaDB, and it got me really interested, so I've decided to explore more about this database and build something a little more complex.

My idea here is to build the back-end for something like Angelist, Indeed or Linkedin('s jobs board)

---

### Notes


* This post in particular is a translation of a post I've done some days ago, so any feedback here won't affect the next post (cause it's already written)

* This posts will literally be a documentation of my studies, so I will probably be wrong about the way I do things a lot of times, I would really appreciate some feedback when that happens.

* This chould change as I go, but I have no intention of create authentication for this project.


---

### Content

* [About the app](#c-1)
Explaining what will be built and the business requirements for it

* [Data modelling](#c-2)
A section talking about the database's tables and their fields, and also a pseudo-code of how their classes will look like

* [Creating the database](#c-3)
Database, table and document creation proccess 

* [Creating the project and installing dependencies](#c-4)
Exactly what the title says (I love descriptive names, hope they are good for article writing as well)

* [Structuring the project](#c-5)
Section about the file structure I will use in the project

* [Beginning the app](#c-6)
Coding the first files in the app 

Notoriously what is not in this post are the modules, this first article took me way more time than I thought it would, so I will start the modules in the future articles

---

### About the app <a name="c-1"></a>

#### Users

This app will have two users in mind

* **Companies** who wanna hire people
* **Candidates** who want to be hired

#### Functionalities

* Registration, for each user type.
* Job offer creation.
* Notifying candidates when there is a new job offer that they fulfill the requisites for.
* A match system that will share their information if both the company and the candidate see each other as a fit.
* Search for candidates to search for job offers and companies and for companies to search for candidates.

---

### Data modelling <a name="c-2"></a>

First of all, I think it's important to say that I thought this would be the hardest part, for my experience with both SQL and NoSQL, it's very easy to get the data modelling wrong and create a lot of rework for yourself in the future.

However, while I was writing this post, I found another post here in Dev.to (which will be linked in the end) about data modelling in FaunaDB, and it happens that it's possible to easily restructure the data models while the database is already being used.

Alright, about the models;

So far, I've thought about four models:

* Company
* Candidate
* Job post
* Skill

Companies and candidates will be our users.

Job posts will be the posts companies will make.

Skills will be in the candidates profiles and in the job offerings and requisites.

One by one, I will talk about the fields for each model and create a pseudo code of how I think the class will look like.

#### Company

Companies will need a identification (name and email), they will need a description (kind of a bio), and links for their websites and social media

```
Company {
  name: "fake company",
  email: "fake@compa.ny",
  description: "we do not exist",
  social_media: [
    { name: "twitter", link: "https://twitter.com/fakecompany" },
   . . .  
  ]  
}
```

#### Candidate

As the company, candidates will need identification (name and email), as well as links for their social media acounts. Besides that, candidates will need a bio and a list of skills

This list of skills will be a list of references to the skill model and a level of proficiency and that skill

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

#### Job posts

Job posts will have to be associated with a company, will also need a title, a description, a list of pre-requisites and a list of candidates

```
JobPosting {
  title: "Senior fake position",
  company_ref: "123123123",
  description: "we need a ninja fake employee",
  requisites: [
    { skill_ref: "123123", experience: "medium" },
   . . .  
  ],
  applicants: [
    { candidate_ref:"1234321" },
    . . .
  ]
}
```
#### Skills

Skill will only need a name. Their purpose is only to be referenced by other models (candidates and job posts)

```
Skill{
  name: "journalism"
}
```

---

### Creating the database <a name="c-3"></a>

I will be using the free version of Fauna for this study, you can get it in [this link](https://dashboard.fauna.com/accounts/register).
In this account, I will create a database: 

![Creating the database](https://miro.medium.com/max/814/1*xcGe4MgtLhiEkPPdIj2b0w.png)


And inside it a table for each of the models I've talked about:

![Creating a table](https://miro.medium.com/max/875/1*Kob1AWhAHTzssWqp-dKGCg.png)


And then insert a document in each of the tables:

![Creating the document](https://miro.medium.com/max/875/1*C7ySEnHpshor79kMLSiLMg.png)

---

### Creating the project and installing dependencies <a name="c-4"></a>

First thing, I will create a folder and initialize a node project inside of it:

```
npm init -y
```

After that I will install Express, the body-parser, Fauna's driver and dotenv with the following commands:

```
npm install express
npm install body-parser
npm install faunadb
npm install dotenv

```

A quick explanation about each of those:

* Express: server framework, I will use it for the routes and some middleware.
* Body-parser: A middle for express that makes it easy to get a json from the requests.
* Faunadb: driver I will use to connect with my database.
* dotenv: A library that gets my .env file and makes it's information acessible in the code.

---

### Structuring the project <a name="c-5"></a>

I will organize this project's structure by modules, being one module for each of the models I've created during the modelling. I usually use this structure because it's clean and easy to navigate.

(but if I am being honest, it's because I've read [this github](https://github.com/elsewhencode/project-guidelines#6-structure-and-naming) and got convinced to follow some of their standards)

So the project will look like this:

In the root, I will keep files that apply to every module, and then each module will have their own routes, controllers and utils files

---

### Beginning the app <a name="c-6"></a>

First thing, I will create a index.js and throw all the information about express a initialize the body-parser 

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

It's considered a good practice to separate the server initialization from the index file, so I will possibly change this in the future, but for now, I will just keep it this way.

After that, I will create a file called db.js and write the Fauna initialization there:

![](https://miro.medium.com/max/736/1*ufRuYsevXsAwao7_Q5i-SA.png)

What this code is doing, is creating a new Fauna client and exporting it and also the query from Fauna, so I can create queries in other files.

After that, I will create the file routes.js, where I will keep a reference for the other modules:

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

I've left a comment in there for how I imagine the reference for each module will be done.

After that, I've added the reference to the router in index:
![](https://miro.medium.com/max/875/1*FlQNnQbOImSbcAAv-dXZxg.png)

---

### Conclusion

Well, with that I have the structure I will need to start writing the modules.


This is the repository for the project:


https://github.com/ciochetta/faunajobboard


Link I've used to study: 

{% post https://dev.to/b_bot/modelling-data-in-faunadb-a-primer-51d3 %}