This is the fourth part of a series of posts documenting my studies with FaunaDB, these are the links for the first three posts:
Creating a job board with Node.js and FaunaDB - Part 1
Luis Felipe Ciochetta ・ Dec 6 '20
Creating a job board with Node.js and FaunaDB - Part 2
Luis Felipe Ciochetta ・ Dec 8 '20
Creating a job board with Node.js and FaunaDB - Part 3
Luis Felipe Ciochetta ・ Dec 9 '20
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:
Creating the module structure
*Insert Candidate:
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
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";
Insert Candidate
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.
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 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:
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:
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:
Top comments (0)