Nitric logo

Guides

BYO Database

Nitric does not have out of the box support for databases. This way you can 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:

AWSGCPAzure
PostgreSQLAmazon RDS for PostgreSQLCloud SQL for PostgreSQLAzure Database for PostgreSQL
MySQLAmazon RDS for MySQLCloud SQL for MySQLAzure Database for MySQL
RedisAmazon ElastiCache for RedisMemorystore for RedisAzure Cache for Redis
MongoDBMongoDB Atlas on AWSMongoDB Atlas on Google CloudMongoDB Atlas on Azure

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 MySQL 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.

# docker-compose.yml

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.

// package.json

"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.

# docker-compose.yml

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.

// package.json

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

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

Redis

Image

For a local Redis instance, we recommend the official docker image.

Connect

The node-redis client is recommended and officially supported by Redis.

import { createClient } from 'redis';

const client = createClient({
  url: `redis://${USERNAME}:${PASSWORD}@${HOST}:${PORT}/${DATABASE}`,
});

await client.connect();

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

# docker-compose.yml

version: '3.6'
services:
  redis:
    image: redis
    restart: always
    ports:
      - '6379:6379'

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

// package.json

"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.

# docker-compose.yml

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.

// package.json

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

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

Previous
Using Git Actions