In this article we are going to see how to use Nextjs and Kysely to easily query a database, create tables, create migrations, transactions, etc.
Installation
The first thing we need is our project created from Nextjs, for that we execute the following command:
After our project has been created, we are going to install the Kysely dependencies, in this case I will also install pg to be able to connect to a postgresql database.
Note: if you are using a database other than postgresql, you can install the driver for your database, for example for mysql it would be
npm install mysql2
and for sqlite it would bebetter-sqlite3
.
Configuration
We will use this structure to order our files:
In the db.ts
file we are going to create our database connection, in my case I will occupy (Supabase)[https://supabase.io/], but you can occupy any database you want.
Now we will have to create an .env
file in order to save our environment variables, in this case we will use the following variables:
we are also going to create a types.ts
file to define our data types
Note: in this case we are going to use
Generated
to define the data type of a column that is automatically generated, for example an ID.
These data types will help us to have a better autocompletion when writing our queries.
Migrations
Now we are going to create a migrations.ts
file in order to create our tables in the database.
In this file we have 2 parts, the first one is the up
function that is in charge of creating the table in the database, and the second part is the executeMigration
function that is in charge of executing the migrations.
We could divide these 2 parts in 2 different files, but for this example I will leave it in only one file.
Execute migrations
To execute our migrations, let’s create a script in our package.json
.
we will use bun to be able to run our typescript file, because with bun we can run typescript files without compiling them.
to install bun
we execute the following command:
and then run the following command to execute our migrations:
Execute queries
To test that everything is working correctly, I will create a new folder in the root of my project called actions
and inside this folder I will create a user.ts
file with the following content:
in this file we are exporting a function called getUsers
which is in charge of getting all the users from the database.
Note: we are using
use server
so we can run this file on the server.
Now if we go to our app/page.tsx
file and we are going to import our getUsers
function and we are going to execute it.
If everything is working correctly, we should see in our browser console an array with all the users in our database.
Conclusion
As we can see using Kysely with Nextjs is very simple, and it allows us to create queries in a simple way, create tables, create migrations, transactions, etc.
Kysely is for me the best library to create SQL queries, since being written in typescript allows us to have a better autocompletion when writing our queries, and also allows us to have a better error control.