Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Adam Miedema
Adam Miedema

Posted on • Edited on

     

Setting up Adonis v5 with PostgreSQL

In this multi-part tutorial, we'll create and deploy a multi-server architecture web application.

Part 1:Creating a front-end using NuxtJS and TailwindCSS
Part 2: Setting up Adonis v5 with PostgreSQL
Part 3:Creating a REST API using Adonis v5
Part 4:Connecting Nuxt front-end to Adonis 5 API
Part 5:Deploying a multi-server app with Cleavr

Frameworks and tools used

Part 2 - Setting up Adonis v5 with PostgreSQL

AdonisJS version 5 is now available to preview and brings about a huge overhaul as it is completely re-written in Typescript. We'll be using version 5 in this tutorial and you can check out thenew documentation here.

Install AdonisJS v5 on your local machine

We'll first install our Adonis project via the terminal by running the following command.

yarn create adonis-ts-app movieapi

Swap outmovieapi if you want to name your project something else.

When asked, select the option forAPI Server as we will be creating an API only.

Once the installation is complete, you'll be prompted to run the following commands.

cd movieapinode ace serve --watch

This will open up themovieapi directory and then serve our starter project.

Your app will be served on your localhost, copy and paste the<address>:<port number> from the terminal and paste into your web browser.

If you see the following appear on your browser, then you're off to a good start!

{"hello":"world"}

Install PostgreSQL drivers for AdonisJS v5

We'll now work on installing database drivers for PostgreSQL. You may choose to use a different database type. If so, follow theinstructions for the database type you want to use.

No matter the database type you choose, you'll first need to installlucid.

cd movieapiyarn add @adonisjs/lucid@alpha

Next, run:

node ace invoke @adonisjs/lucid

You'll then be asked which database drivers to install. I'll select PostgreSQL.

Lastly, let's finish up configuring the Postgres driver.

npm i pg

Check the database connection between Adonis and Postgres

First, make sure you have an instance of PostgreSQL running on your machine.

DBngin offers a quick and easy way to manage local database servers. I'll use it to run a PostgreSQL server on my machine, and then I'll connect to it via TablePlus and add a new database namedmovies.

Now, open up themovieapi project in IntelliJ and navigate to thedatabase.ts file to configure database connections.

pg:{client:'pg',connection:{host:Env.get('DB_HOST','127.0.0.1')asstring,port:Number(Env.get('DB_PORT',5432)),user:Env.get('DB_USER','postgres')asstring,password:Env.get('DB_PASSWORD','')asstring,database:Env.get('DB_NAME','movies')asstring,},healthCheck:true,},

Note -postgres is typically the default username for new PostgreSQL installations.

Also, we'll want to make sure thathealthCheck is set totrue as Adonis only attempts to establish a connection with the database when queries are ran. And, using the health check feature is a quick way to test the database connection.

From the configs above, you can see that Adonis will first check to see if the variables exist in the .env file; so, we'll need to make sure we change the defaults in the .env file as well.

DB_USER=postgresDB_PASSWORD=DB_NAME=movies

Now, let's openroutes.ts, import health check, and then add a health check route.

importHealthCheckfrom'@ioc:Adonis/Core/HealthCheck'Route.get('health',async({response})=>{constreport=awaitHealthCheck.getReport()returnreport.healthy?response.ok(report):response.badRequest(report)})

We can now check the connection. You may need to re-runnode ace serve --watch if the process stopped.

Also, I received an error at this step and had to installproxy-addr to continue. If you get this error as well, just run the following command and then re-serve the project.

npm install proxy-addr

Open up your browser and append the url with/health to see the results of the health check. If you've successfully established a connection with your database, you'll see the following message:

{"healthy":true,"report":{"env":{"displayName":"Node Env Check","health":{"healthy":true}},"appKey":{"displayName":"App Key Check","health":{"healthy":true}},"lucid":{"displayName":"Database","health":{"healthy":true,"message":"All connections are healthy"},"meta":[{"connection":"pg","message":"Connection is healthy","error":null}]}}}

Now that our Adonis and PostgreSQL connection is setup and is working, we'll continue creating our API inpart 3.


Following along? View Part 2 progress on GitHub athttps://github.com/armgitaar/moviesapi.

Watch the full tutorial series uninterrupted on Youtube.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Keep moving forward.
  • Joined

More fromAdam Miedema

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp