PostgreSQL

Connect DB0 to PostgreSQL
Read more in www.postgresql.org.

Usage

For this connector, you need to install pg dependency:

npm i pg @types/pg

Use postgresql connector:

import { createDatabase } from "db0";
import postgresql from "db0/connectors/postgresql";

const db = createDatabase(
  postgresql({
    bindingName: "DB",
  }),
);

Options

url

Connection URL string.

Alternatively, you can add connection configuration.

Read more in node-postgres client options.

Prisma Postgres

Prisma Postgres provides a fast, managed PostgreSQL database service. While there's no specific DB0 connector for Prisma Postgres, you can use the existing PostgreSQL connector to connect smoothly.

Create a database

You can either create an account in Prisma Data Platform and create a database manually or use create-db cli tool to create a Prisma Postgres database.

Let's create a new Prisma Postgres database using the create-db cli tool:

npx create-db

This will output something like:

┌  🚀 Creating a Prisma Postgres database
│  Provisioning a temporary database in us-east-1...
│  It will be automatically deleted in 24 hours, but you can claim it.
◇  Database created successfully!
●  Database Connection
│    Connection String:
│    postgresql://username:[email protected]:5432/postgres?sslmode=require
◆  Claim Your Database
│    Keep your database for free:
│    https://create-db.prisma.io/claim?projectID=your_project_id&utm_source=db0&utm_campaign=ppg-awareness
│    Database will be deleted on [DATE] if not claimed.

Important: Save your actual connection string to an .env file:

DATABASE_URL="postgresql://your_username:[email protected]:5432/postgres?sslmode=require"

Usage

Use the postgresql connector with your Prisma Postgres database:

import "dotenv/config";
import { createDatabase } from "db0";
import postgresql from "db0/connectors/postgresql";

const db = createDatabase(
  postgresql({
    url: process.env.DATABASE_URL,
  }),
);

Then you should be able to query your Prisma Postgres database:

import "dotenv/config";
import { createDatabase } from "db0";
import postgresql from "db0/connectors/postgresql";

const db = createDatabase(
  postgresql({
    url: process.env.DATABASE_URL!,
  }),
);

async function main() {
  console.log(await db.sql`SELECT 1`);
}

main();