Database
The Database resource lets you create and manage Prisma Postgres database resources within an alchemy project. Each database is a fully managed PostgreSQL instance that can be connected to applications.
Minimal Example
Section titled “Minimal Example”Create a basic database in a project:
import { Database } from "alchemy/prisma-postgres";
const database = await Database("my-db", { project: "prj_12345abcde",});
console.log(`Database ID: ${database.id}`);console.log(`Database Name: ${database.name}`);console.log(`Status: ${database.status}`);
Database with Project Resource
Section titled “Database with Project Resource”Create a database using a Project resource:
import { Project, Database } from "alchemy/prisma-postgres";
const project = await Project("my-app");
const database = await Database("production", { project: project,});
Database in Different Regions
Section titled “Database in Different Regions”Create databases in various regions:
import { Database } from "alchemy/prisma-postgres";
// Asia Pacificconst apDatabase = await Database("ap-db", { project: project, region: "ap-northeast-1"});
Database with Delete Protection
Section titled “Database with Delete Protection”By default, databases are not deleted when the Alchemy resource is destroyed. Enable deletion for ephemeral databases:
import { Database } from "alchemy/prisma-postgres";
const testDatabase = await Database("test-db", { project: "prj_12345abcde", region: "us-east-1", delete: true // Will delete the database on destroy});
Complete Example with Connection
Section titled “Complete Example with Connection”Create a database and connection string for your application:
import { Project, Database, Connection } from "alchemy/prisma-postgres";
const project = await Project("my-app");
const database = await Database("production", { project: project, region: "us-east-1"});
const connection = await Connection("app-connection", { database: database,});
console.log(`Connection string available at: ${connection.connectionString.unencrypted}`);
Related Resources
Section titled “Related Resources”- Project - Create projects to contain databases
- Connection - Create connection strings for databases
- Workspace - Learn about Prisma workspaces