DB Pool

Time to connect our models to our database!

Setting up the database pool#

This step is only for PostgreSQL and MySQL. SQLite users can skip to thebottom of this lesson.

With the initial db connection config set up in the previous step, we will now be able to use it to access the database, but first we have to make a database client pool available to the application.

If you don't remember what a pool is, it essentially reserves a set number of connections to the database, kept in the background ready to be used at any time. You can think of it as a queue system where you can decide how many requests can be handled at the same time.

Adding to our application#

Note for MySQL: creating a pool instance is a bit different, so revisit Module 2 if you don't fully remember how to do this.

In main.ts we can now initialize a new pool and add it to our global app scope so that we can use it throughout our application.

Note for MySQL: you simply pass in the client to the Application below as it will automatically use the first available pool instance.

Change your main.ts to the following:

Let's dissect these changes.

 

This page is a preview of Build and deploy a REST API with Deno

Start a new discussion. All notification go to the author.