PostgresDatabase
Workload for PostgreSQL databases.
Description
With this workload you define PostgreSQL databases.
A PostgresDatabase
workload is initialized with a valid database name.
See examples.
Each workload has an environment variable name associated with it to hold the connection string for the database named after the workloads' databaseName
. For example:
- database name
products
:ML_PG_PRODUCTS_DATABASE_URL
.
Client
You can use any PostgreSQL database client with the workload.
See examples.
Server
By default, each PostgresDatabase
is associated to a different database server.
Development environment
A docker container for the dev environment is launched with npx monolayer start dev
You can stop it with npx monolayer stop dev
.
After the container is started:
- The environment variable with the connection string for the workload's Docker container will be written to
.env.local
. - The database will be created in the database server if it does not exist.
INFO
Check your framework documentation to see it the .env.local
file is loaded automatically.
Test environment
A docker container for the test environment is launched with npx monolayer start test
You can stop it with npx monolayer stop test
.
- The environment variable with the connection string for the workload's Docker container will be written to
.env.test.local
. - The database will be created in the database server if it does not exist.
INFO
Check your framework documentation to see it the .env.test.local
file is loaded automatically.
Examples
Workloads in the same database server
import { PostgreSQL } from "@monolayer/sdk";
import pg from "pg";
export const producsDbMain = new PostgresDatabase("products");
const client = new pg.Pool({
// Assumes the environment variable is set
connectionString: process.env[productsDb.connectionStringEnvVar],
});
client.query("SELECT 1");