BYO Database

Nitric currently has out of the box preview support for SQL databases with AWS, however Nitric allows you to use whatever tooling and ORMs you prefer for directly interfacing with your database. Our recommendation for local development is to set up a container that runs alongside your Nitric processes. For a production environment, you can use any of the database services for your preferred cloud:

You can track the support for relational databases here.

PostgreSQL

Image

For a local PostgreSQL instance, we recommend the official docker image. It has the following configuration variables:

  • POSTGRES_USER
  • POSTGRES_PASSWORD
  • POSTGRES_DB

Connect

The official postgres driver is recommended for basic connections to a PostgreSQL instance. However, if you would prefer a higher abstraction, we recommend Prisma ORM. You can read our guide on using Prisma here.

import { Client } from 'pg'
const client = new Client()
await client.connect()

Run Locally

To start, make sure you have a .env file containing your environment variables.

POSTGRES_USER=root
POSTGRES_PASSWORD=root
POSTGRES_DB=my-database

You can then create a docker compose file to simplify running your docker container each time.

version: '3.6'
services:
postgres:
image: postgres
restart: always
environment:
- POSTGRES_USER=$POSTGRES_USER
- POSTGRES_PASSWORD=$POSTGRES_PASSWORD
- POSTGRES_DB=$POSTGRES_DB
ports:
- '5432:5432'

You can add a script to your package.json which will run the docker compose file.

"scripts": {
"db": "docker compose up --wait"
}

Then for any local development you can just run yarn db.

MySQL

Image

For a local MySQL instance, we recommend the official docker image. It has the following configuration variables:

  • MYSQL_ROOT_PASSWORD
  • MYSQL_USER
  • MYSQL_PASSWORD
  • MYSQL_DATABASE

Connect

The official mysql driver is recommended for basic connections to a MySQL instance. However, if you would prefer a higher abstraction, we recommend Prisma ORM. You can read our guide on using Prisma here.

import mysql from 'mysql'
var con = mysql.createConnection({
host: HOST,
user: USERNAME,
password: PASSWORD,
})
con.connect(function (err) {
if (err) throw err
console.log('Connected!')
})

You can then create a docker compose file to simplify running your docker container each time.

version: '3.6'
services:
mysql:
image: mysql
restart: always
environment:
- MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASSWORD
- MYSQL_USER=$MYSQL_USER
- MYSQL_PASSWORD=$MYSQL_PASSWORD
- MYSQL_DATABASE=$MYSQL_DATABASE
ports:
- '5432:5432'

You can add a script to your package.json which will run the docker compose file.

"scripts": {
"db": "docker compose up --wait"
}

Then for any local development you can just run yarn db.

MongoDB

Image

For a local MongoDB instance, we recommend the official docker image. It has the following configuration variables:

  • MONGO_INITDB_ROOT_USERNAME
  • MONGO_INITDB_ROOT_PASSWORD
  • MONGO_INITDB_DATABASE

Connect

The official mongodb driver is recommended for basic connections to a MongoDB instance. However, if you would prefer a higher abstraction, we recommend Prisma ORM. You can read our guide on using Prisma here.

const client = new MongoClient(
`mongodb://${USERNAME}:${PASSWORD}@${HOST}/${DATABASE}`,
)
await client.connect()

You can then create a docker compose file to simplify running your docker container each time.

version: '3.6'
services:
mongodb:
image: mongodb
restart: always
environment:
- MONGO_INITDB_ROOT_USERNAME=$MONGO_INITDB_ROOT_USERNAME
- MONGO_INITDB_ROOT_PASSWORD=$MONGO_INITDB_ROOT_PASSWORD
- MONGO_INITDB_DATABASE=$MONGO_INITDB_DATABASE
ports:
- '27017:27017'

You can add a script to your package.json which will run the docker compose file.

"scripts": {
"db": "docker compose up --wait"
}

Then for any local development you can just run yarn db.

Last updated on Jun 14, 2024