interface MongoEntityMapping
Since 0.1.0

How one entity name maps onto a physical Mongo collection.

Properties

readonly
optional
collection: string

The collection name. Defaults to the entity name itself, so getRepository('users') needs no mapping at all.

readonly
optional
primaryKey: string | readonly string[]

The primary-key field name(s). Defaults to 'id'.

A scalar names a single-column key (the driver's _id maps to that one field). An array names a composite key: each named column is a top-level field on the collection (idType absent) or, when idType: 'compound', the keys of the subdocument stored under the driver's _id in the mapping's declared order (P4/P5).

readonly
optional
idType: "objectId" | "raw" | "compound"

How the collection stores its _id values.

  • 'objectId' forces the id to a driver ObjectId on read and write.
  • 'raw' forbids conversion, so a string id is passed to the driver verbatim.
  • 'compound' stores a composite key as a subdocument _id, built in the mapping's declared column order (P5 — canonical order, not caller order). Absent converts a string id that ObjectId.isValid accepts — a 24-hex string. A non-string id (a number key) is passed through verbatim, because the driver's isValid answers true for any number while its constructor rejects one.

The genuinely ambiguous collection — one whose _id values are 24-hex strings rather than ObjectIds — cannot be distinguished at runtime, so the override exists rather than being guessed.

Usage

import { type MongoEntityMapping } from "database-plugin/src/index.ts";