BYO Database
Nitric currently has out of the box preview support for PostgreSQL databases, however Nitric allows you to use whatever tooling and ORMs you prefer for directly interfacing with your database. If you want to use a database that isn't managed by Nitric, we have the following recommendations:
- For local development, set up a container that runs alongside your Nitric processes.
- For a production environment, use any of the database services offered by your preferred cloud provider:
PostgreSQL
Image
For a local PostgreSQL instance, we recommend the official docker image. It has the following configuration variables:
POSTGRES_USERPOSTGRES_PASSWORDPOSTGRES_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=rootPOSTGRES_PASSWORD=rootPOSTGRES_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: postgresrestart: alwaysenvironment:- POSTGRES_USER=$POSTGRES_USER- POSTGRES_PASSWORD=$POSTGRES_PASSWORD- POSTGRES_DB=$POSTGRES_DBports:- '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_PASSWORDMYSQL_USERMYSQL_PASSWORDMYSQL_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 errconsole.log('Connected!')})
You can then create a docker compose file to simplify running your docker container each time.
version: '3.6'services:mysql:image: mysqlrestart: alwaysenvironment:- MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASSWORD- MYSQL_USER=$MYSQL_USER- MYSQL_PASSWORD=$MYSQL_PASSWORD- MYSQL_DATABASE=$MYSQL_DATABASEports:- '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_USERNAMEMONGO_INITDB_ROOT_PASSWORDMONGO_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: mongodbrestart: alwaysenvironment:- MONGO_INITDB_ROOT_USERNAME=$MONGO_INITDB_ROOT_USERNAME- MONGO_INITDB_ROOT_PASSWORD=$MONGO_INITDB_ROOT_PASSWORD- MONGO_INITDB_DATABASE=$MONGO_INITDB_DATABASEports:- '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.
Have feedback on this page?
Open GitHub Issue