Skip to content

Getting Started

Add a configuration file

Create a file named monolayer.config.ts in the root folder of your project with the following contents:

ts
import type { Configuration } from "@monolayer/sdk";

const config: Configuration = {
  // workloadsPath points to a folder where the workloads with be defined.
  // Change it to relative path of your choice inside your project.
  workloadsPath: "src/workloads",
};

export default config;

Define workloads

We'll define a PostgresDatabase workload that represents a PostgreSQL database.

ts
import { PostgreSQL } from "@monolayer/sdk";
import pg from "pg";

export const producstDb = new PostgresDatabase("products");

Launching workloads

bash
npx workload start dev


In this example, running workloads start dev will:

  1. Launch a docker container with a PostgresSQL database
  2. Create the database products if it does not exist.
  3. Write the connection string for the server to the .env file.


You can get the status of the workloads with:

bash
npx monolayer status dev

Using workloads

You can configure your database client with the connection string that's created.

ts
import { producstDb } from "src/workloads/postgres";
import pg from "pg";

// Skip if your web framework already loads the `.env` file.
import dotenv from "dotenv";
dotenv.config();

const client = new pg.Pool({
  // The environment variable is set when when you launch `monolayer status dev`
  connectionString: process.env[productsDb.connectionStringEnvVar],
});

// Querying the products database.
await producstDb.client.query("SELECT 1");