Installation
PREREQUISITES
- Node.js 18.18 or later.
- A PostgreSQL database server running (15 or higher).
- (Recommended) pg_dump in your development environment (included when installing PostgreSQL).
Create a TypeScript project
INFO
You can skip this step if you already have an existing TypeScript project to work with.
Create a project directory and navigate into it:
bash
mkdir hello-monolayer
cd hello-monolayer
Then, initialize a TypeScript project:
bash
npm init -y
npm install typescript
This will create a package.json
with an install TypeScript
Add a TypeScript configuration with a new file named tsconfig.json
and the following content:
json
{
"compilerOptions": {
"target": "es2022",
"moduleResolution": "nodenext",
"module": "nodenext",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}
Add monolayer to your TypeScript project
Run the installer with the following command and follow the prompts:
bash
npx @monolayer/create-pg@latest
You will be greeted to enter the relative path to create the db
folder:
txt
┌ Welcome to monolayer!
│
◇ Where should monolayer create the `db` folder?
│ ./app
│
◇ Installed monolayer-pg and dependencies... ✓
│
◇ Creating monolayer.config.ts ✓
│
◇ Creating app/db/databases.ts ✓
│
◇ Creating app/db/schema.ts ✓
│
◇ Creating app/db/client.ts ✓
│
◇ Creating app/db/seed.ts ✓
│
◇ Adding sample environment variable to .env ✓
│
└ Done
File Structure
The installer should have installed all necesary dependencies and added the following files to your project directory:
text
🗂️ hello-monolayer
└ 📁 app (chosen directory)
└ 📁 db
├ 📄 client.ts
├ 📄 databases.ts
├ 📄 schema.ts
├ 📄 seeds.ts
├ 📄 .env
└ 📄 monolayer.ts
Generated code 🔎
ts
import { defineConfig } from "@monolayer/pg/config";
export default defineConfig({
databases: "app/db/databases.ts",
});
ts
import { defineDatabase } from "@monolayer/pg/schema";
import { dbSchema } from "./schema";
import { dbSchema } from "./seeds";
export default defineDatabase({
id: "default",
schemas: [dbSchema],
extensions: [],
camelCase: false,
seeder: dbSeed,
});
ts
import { schema } from "@monolayer/pg/schema";
export const dbSchema = schema({});
export type DB = typeof dbSchema.infer;
ts
import { CamelCasePlugin, Kysely, PostgresDialect } from "kysely";
import { Pool } from "pg";
import defaultDb from "./databases";
import { type DB } from "./schema";
export const defaultDbClient = new Kysely<DB>({
dialect: new PostgresDialect({
pool: new Pool({ connectionString: defaultDb.connectionString}),
}),
plugins: defaultDb.camelCase? [new CamelCasePlugin()] : [],
});
ts
import { sql, type Kysely } from "kysely";
import type { DB } from "./schema";
export async function dbSeed(db: Kysely<DB>) {
const currentDatabase = await sql<{
current_database: string;
}>`SELECT CURRENT_DATABASE()`.execute(db);
console.log("Current database:", currentDatabase.rows[0].current_database);
}
text
# Inserted by \`@monolayer/create-pg\`
# MONO_PG_DEFAULT_DATABASE_URL=postgresql://user:password@dbserver:5432/dbName
Configure your environment
Now, open the .env
file in the root of the project directory and:
- Uncomment the line with
MONO_PG_DEFAULT_DATABASE_URL
. - Replace the
MONO_PG_DEFAULT_DATABASE_URL
value with your database connection URL.