hey friends welcome back to my channel i am really really excited for this video because it's been
a long requested video to do which is creating a rest api with express and we also are going to be
using fauna throughout this video you will not only learn how to build a rest api but also to
how to use fauna so you can take these skills and literally build any other projects you want which
is pretty empowering if you ask me also make sure to click the link in the description for fauna so
you can follow along too i find it's always best in tutorials to follow along before we get started
though make sure to hit that subscribe button for more tech and coding related videos shout out to
some of these subscribers here thank you for your comments feedback questions love support just for
being amazing okay let's get started i want to say a big shout out to fauna thank you for sponsoring
this video for those of you who aren't familiar with fauna it is a flexible and secure database
that is delivered through a scalable api or as a scalable api i'm really excited to be using
fauna today and you're definitely going to see more tutorials on my channel using fauna because
of its flexibility its secureness and also to how scalable it is here are some other reasons i chose
fauna one of the main things that drew me to using fauna is that it is a serverless utility so once
it's plugged in it delivers limitless capacity it's not a dbas or cluster that you have to manage
also it's secure and scalable i also really liked how it is connectionless and accessible
directly from the browser or mobile clients it's kind of a win-win you can simplify code and
ship faster with it and lastly it is a unique indexed document system that gives you the best
of both worlds for sql and nosql so as you can see there are a lot of positives to using fauna and it
is extremely developer friendly for this tutorial we are going to be using the test of time tutorial
of all tutorials which is twitter and i chose this because i think it's a really good way to
kind of like a to-do list in the sense that it's a good way to um you know use something that we are
all familiar with but also to something that can be applied to so many other projects for example
how you get tweets how you post tweets different things like that that really can be applicable to
whatever project you are working on my goal for this was that you can leave this tutorial
with the skills required and feel confident that you can take these skills and apply them to your
own project okay enough of me talking let's get started to do is open up our ide for myself i'm
using vs code and we also want to make sure we have a version of node installed to check if you
have node installed all you need to do is go node v for myself as you can see i have it installed
however if you don't have it installed i'll put up on the screen here where to go to install it
okay next thing i want to do is install the fauna extension
okay next up we need to create an empty npm project to do so we run mpm init dash y
okay now that we have that installed we need to install fauna db and express
okay you can see now we have our package.json and our node modules let's also add a source
folder with an index file and that is where we are going to write all of our source code
now we want to ensure we are able to serve all of our api
endpoints and of course we do that with express
next up we also need to require fauna so let's go
okay now we need to find a way to connect our source code to the actual fauna database
to do so we need to first of all find a client key
to find our secret key if you haven't already done so make sure to go to fauna
and create a new database for me i created twitterdb navigate to security and new key
okay now that we have fauna installed we have express installed we have node
installed let's get our server up and running to do so let's go app.listen
and now we need to choose a port let's do 30 30 but it's whatever you really want to do
and let's log something out to make sure it's running
okay let's go ahead and run this
to start the server we need to ensure that we are in our source folder and access index.js
hmm we're getting an error here let's check it out fauna db is not defined
oh as you can see i called it fauna and i'm using fauna db here let's fix that okay let's try again
perfect we are listing on 30 30. okay now that we have the server up and running i
want to talk to you a little bit about fql or fauna query language this is something we are
going to be using throughout the project especially the functions of it and i have
the dots pulled up here for fauna which i will also link down below in the description
as you can see here fona query language or fql is the native api for querying fonda
and in the documentation it goes through different parts of it from the cheat sheet
data types databases collections all the way to built-in functions
for the first fql function we are going to use is going to be get
so let's start by going const we're going to make this an object because there
is going to be more uh functions that we are adding but to start with let's add guest
okay now it's time to use express again let's set up an endpoint that will read an individual tweet
okay as i mentioned for this endpoint it's to read a single tweet or single
document and because fauna returns a promise we are going to make this async
okay and this is where the get function comes in to read a single document
and as you can see we use the ref function which we will also need to add
to reference a single document we want this case it is tweets
okay let's also make sure we add breath
there we go also we can't forget to pass in the rec params.id
this will be passed in the url
okay so before we can test it out we need to go back to fauna and add a new collection for tweets
we are back in fauna let's go to collections new collection
collection name as we mentioned in our code is tweets
next let's create a new document which will have some text in it for this text let's say hi friends
let's go ahead and save it
and copy the document id
so first we type in localhost 3030 slash tweet and then that document id and as you can see here
we have we are getting the text hi friends doesn't look pretty but we're getting it
which is a big win one thing we forgot to do which i just did was make sure you also add
collection to the different functions we are going to be using let's do something to create tweets
next up we need to use the create function
tell it what collection we want
and also pass it whatever data we want
don't forget to add create and import it
oh and you can see here i need a document and this doc let's change that document
okay now that we've created this we need to create a user's collection as
well because we are posting from a user so let's go back to fauna
as you can see i created the user's collection all i did was simply once again go to new collection
users and save i've already done this so i won't do this again but you get the idea
from here i'm going to go into indexes new index and select collection i'm going to use users
and the index i'm going to make an index name let's call this
user
name terms it's just going to be data.name now i've created a user's collection let's go into
that collection and create a new document for this document i want to have my name and email
i'm going to choose my dog's name mr mugs
and email mr mugs mugs.com of course oh and make sure as well to do not do what i just did which is
not adding strings around them or making them into strings there we go perfect okay now let's go into
indexes new index we're going to use the user's collection index name is going to be
user underscore name because the goal of this is to get the data.name so terms would be data
and let's see
okay now let's test this out actually before we even get there make sure you have um match and
index and get and select all up here as well or else of course it will not work so we just ran
again and then to test this one out though i'm going to use postman um to do localhost 3030
tweet to get the tweet okay so in postman as you can see here i have post localhost
3030 slash tweet and i already ran it but we'll run it again for you and as you can
see here we have our new text hello world see back in collections we have our hello world
i want to show you how to use functions built into fauna and this is really
great for improving code reusability and avoiding large data transfers for this
let's do the example of getting a user and their tweets let's name this function get user
yeah just get users good the role i'm going to choose is server they don't need admin permissions
and you can see here they've already kind of given an example of what it should
look like so let's just kind of structure this out currently they just have x in there but we
want to get the user and instead of using the add function we actually want to use select function
there we go and we are selecting the ref
and then actually let's kind of structure this out a little bit better
select we're going to take our wrap here
app and references function that we just created okay before we do anything we need to import call
and also too we need to import function but because function is built into javascript we
need to reference function as something else so we do that by colon and then name it something else
actually let's just do fn
okay next let's start building out the function
and we need to ensure we have paginate
and this is where we can use the function that we just built
now let's use the function we just built which is get user
and the user we are trying to get is the famous mr mugs
and now that we have it down here we can reuse this so copy
go back to our post and then user instead of doing all this can simply call the function again
okay i'm going to wrap up the tutorial here i feel like i could make this video
hours long because there is so much to do and so much that we can explore but i really
wanted to cover some of the fundamentals and basics i hope you all enjoyed going through it
and found it very valuable i linked fauna down in the description so if you haven't
already gone to check them out make sure to do so also make sure to hit that subscribe button
for more tech and coding related videos give this video a thumbs up and leave
in the comments other videos or tutorials that you would like to see thanks everyone