@farming-labs/orm

MongoDB

MongoDB support is live through two runtime packages:

Native MongoDB example

mongo-runtime.ts
import { createOrm } from "@farming-labs/orm";
import { createMongoDriver } from "@farming-labs/orm-mongo";
import { MongoClient } from "mongodb";
import { authSchema } from "./schema";

const client = new MongoClient(process.env.MONGODB_URL!);
await client.connect();

const orm = createOrm({
  schema: authSchema,
  driver: createMongoDriver({
    db: client.db("app"),
    client,
  }),
});

Mongoose example

mongoose-runtime.ts
import { createOrm } from "@farming-labs/orm";
import { createMongooseDriver } from "@farming-labs/orm-mongoose";
import { authSchema } from "./schema";

const orm = createOrm({
  schema: authSchema,
  driver: createMongooseDriver<typeof authSchema>({
    models: {
      user: UserModel,
      profile: ProfileModel,
      session: SessionModel,
      account: AccountModel,
    },
    connection: mongoose.connection,
  }),
});

Field mapping

MongoDB support still uses the schema as the source of truth, including mapped field names.

mongo-mapped-schema.ts
const authSchema = defineSchema({
  user: model({
    table: "users",
    fields: {
      id: id().map("_id"),
      email: string(),
      emailVerified: boolean().map("email_verified"),
      createdAt: datetime().map("created_at"),
    },
  }),
});

Library code keeps using logical names like id and emailVerified. The runtime handles _id, email_verified, and other mapped fields.

Relations

Both Mongo runtimes support:

They load related documents through follow-up queries and return the same typed result shape as the other runtimes.

That does not mean MongoDB is missing native join-like features forever. A future Mongo-native aggregation / $lookup path can still be added later. The current fallback behavior just means this repo has not translated those richer native plans yet.

The live MongoDB suites also verify:

Transactions

Helpful references:


Why there are two Mongo packages

MongoDB is not a SQL backend with different syntax. It has its own query, relation, and transaction model.

That is why the repo ships:

Both use the same schema contract and typed API, but they plug into different MongoDB application stacks.

Local verification

The repo verifies both Mongo runtimes locally:

Or run both with:

terminal
pnpm test:local:mongodb