Data
MongoDB, no ORM
The official driver straight onto the startup context: the Db is handed to the service, the MongoClient is closed on stop, and zod at the edges replaces the schema the database doesn't have.
Install#
bun add mongodb zod @rhythmjs/rhythm @rhythmjs/router @rhythmjs/middlewareThe database module#
A small module connects at startup and puts the client and its Db on the context; the cleanup closes the client when the app stops. Being schemaless, there is no migration step: the startup callback is also where the collection's index is ensured.
// src/database.ts
import { Rhythm, decorate } from "@rhythmjs/rhythm";
import { MongoClient } from "mongodb";
const mongoUrl = process.env.MONGODB_URL ?? "mongodb://localhost:27017";
const dbName = process.env.MONGODB_DB ?? "notes_mongodb";
export const databaseModule = new Rhythm({ name: "database" }).register(
decorate(async () => {
const client = new MongoClient(mongoUrl);
await client.connect();
const db = client.db(dbName);
await db.collection("notes").createIndex({ createdAt: -1 });
return { client, db };
}),
({ client }) => client.close(),
);The notes module includes it, picks db, and decorates the service; the app module includes the notes module and mounts the router:
// src/notes/notes.module.ts
export const notesModule = new Rhythm({ name: "notes" })
.register(include(databaseModule, ({ db }) => ({ db })))
.register(decorate(({ db }) => ({ notesService: createNotesService(db) })));// src/app.module.ts
export const appModule = new Rhythm({ name: "app" })
.register(include(notesModule, ({ notesService }) => ({ notesService })))
.use(mount(notesController));main.ts serves with Bun.serve({ port, fetch: toFetchHandler(appModule) }) and calls appModule.stop() on shutdown, which closes the client.
Documents in, DTOs out#
Driver types stop at the service boundary. Internally documents carry _id: ObjectId; outward the service maps them to id: string DTOs, and an ObjectId.isValid guard turns malformed ids into a plain not-found instead of a thrown BSONError. findOneAndUpdate with returnDocument: "after" makes the update a single round trip.
// src/notes/notes.service.ts
function toObjectId(id: string): ObjectId | undefined {
return ObjectId.isValid(id) ? new ObjectId(id) : undefined;
}
export function createNotesService(db: Db) {
const collection: Collection<NoteDocument> = db.collection("notes");
return {
async list() {
const docs = await collection.find().sort({ createdAt: -1 }).toArray();
return docs.map(toNote);
},
async get(id: string) {
const _id = toObjectId(id);
if (!_id) return null;
const doc = await collection.findOne({ _id });
return doc && toNote(doc);
},
async create(input: CreateNoteInput) {
const now = new Date();
const doc: NoteDocument = { _id: new ObjectId(), ...input, createdAt: now, updatedAt: now };
await collection.insertOne(doc);
return toNote(doc);
},
async update(id: string, patch: UpdateNoteInput) {
const _id = toObjectId(id);
if (!_id) return null;
const doc = await collection.findOneAndUpdate(
{ _id },
{ $set: { ...patch, updatedAt: new Date() } },
{ returnDocument: "after" },
);
return doc && toNote(doc);
},
async remove(id: string) {
const _id = toObjectId(id);
if (!_id) return false;
const result = await collection.deleteOne({ _id });
return result.deletedCount > 0;
},
};
}zod is the schema#
With no database schema, the zod contracts in src/notes/notes.schema.ts do the guarding: validate("body", createNoteSchema) keeps malformed documents out of the collection, and intercept(notesResponseSchema) keeps stray document fields from leaking into responses. The module wiring is the shared pattern from the overview page, with filter() on the router.
// src/notes/notes.schema.ts
export const createNoteSchema = z.object({
title: z.string().trim().min(1, "title must be a non-empty string"),
content: z.string().default(""),
});
export const noteSchema = z.object({
id: z.string(),
title: z.string(),
content: z.string(),
createdAt: z.iso.datetime(),
updatedAt: z.iso.datetime(),
});
export const notesResponseSchema = z.union([noteSchema, z.array(noteSchema)]);Where to next#
The relational recipes: Drizzle, Prisma, and MikroORM. The full app is examples/mongodb.