@kavo/mikroorm
v0.25.0
Published
Kavo MikroORM adapter — implements @kavo/core's RepositoryAdapter over a MikroORM EntityManager.
Readme
@kavo/mikroorm
MikroORM adapter for Kavo: implements RepositoryAdapter
(EntityReader + EntityWriter) from @kavo/core over a MikroORM
EntityManager. TransactionManager is not implemented — see the
@remarks on that interface in @kavo/core.
May depend on: @kavo/core, @mikro-orm/core (peer). Never on:
@kavo/nest or any framework.
Fully implemented: CRUD, filtering/sorting/pagination (including across
relation paths), soft delete/restore/purge, and nested relation includes
(via populate). MikroORM declares no delete-marker column the way
TypeORM's @DeleteDateColumn does, so the seam reports none — but read the
soft-delete note under "Known limitations" before assuming that means soft
delete is off until you configure it.
Usage
Like @kavo/typeorm and unlike @kavo/prisma, there is nothing to declare
twice: a MikroORM entity is a real runtime class carrying its own metadata,
so the class you pass to createCrud is the identity core needs — no
marker classes, no entities list beyond the one MikroORM already has.
import { Collection, Entity, ManyToOne, OneToMany, PrimaryKey, Property, MikroORM } from "@mikro-orm/core";
import { defineConfig } from "@mikro-orm/postgresql";
import { createMikroOrmKavo } from "@kavo/mikroorm";
@Entity()
class Author {
@PrimaryKey({ type: "number" })
id!: number;
@Property({ type: "string" })
name!: string;
@OneToMany(() => Book, (book) => book.author)
books = new Collection<Book>(this);
}
const orm = await MikroORM.init(defineConfig({ dbName: "app", entities: [Author, Book] }));
const kavo = createMikroOrmKavo(orm, { caseInsensitiveFilters: true });
const authors = kavo.createCrud(Author);createInfrastructure/createMikroOrmKavo take the MikroORM instance
itself rather than an EntityManager: every adapter operation calls
orm.em.fork() to get its own manager, which is what keeps one request's
identity map out of the next one's.
caseInsensitiveFilters
ILIKE maps to MikroORM's $ilike, which only PostgreSQL supports —
SQLite, MySQL, and MongoDB pass the token through to the driver and fail
with a syntax error. MikroORM's Platform exposes nothing to detect this
from, so it is a declared setting, defaulting to false (the value that
works everywhere). Turn it on for PostgreSQL.
With it off, ILIKE translates exactly like LIKE. On SQLite that is not
even a loss — SQLite's own LIKE is already ASCII case-insensitive.
Known limitations
LIKEescapes are driver-dependent. MikroORM has no way to attach anESCAPEclause to aLIKE, so the query grammar's\escape for a literal%/_is honored only by drivers that default to backslash (PostgreSQL, MySQL). On SQLite, which has no default escape character,filter[name][like]=100\%matches the literal text100\followed by anything, rather than the string100%.- Soft-deleted related rows are pruned in memory. MikroORM's
populateWherecannot express per-level scoping: nesting a child condition makes it a predicate on the parent, so a parent with no surviving children disappears instead of coming back empty. The adapter populates everything and prunes the loaded tree, which is correct at any depth but does fetch soft-deleted related rows before discarding them. Soft-deleted roots are still excluded in SQL. IncludeNode.strategyis ignored. MikroORM resolvespopulatewith its own queries and applieslimit/offsetto the root either way, so the join/batch distinction has nothing to control here — and MikroORM'sstrategyoption is per-query rather than per-relation, so it could not express a mixed include tree anyway. Same posture as@kavo/prisma.- MikroORM's own property options win at the boundary. Rows are
converted with
wrap(entity).toObject(), so a customserializerruns before core ever sees the row. - The soft-delete marker is writable, and it is on by default. Nothing
in a MikroORM entity declares a delete column, so the adapter cannot mark
it generated — but
deletedefaults to{ field: "deletedAt", strategy: "auto" }and core matches that name against your columns, so any entity with adeletedAtproperty is soft-deletable with no config at all. The marker then sits in the derived writable projection: a plainPATCHof it soft-deletes a row, bypassingdeleteOne's already-deleted check and evenoperations: { deleteOne: false }. It cannot revive one — writes are scoped to the live set, so a soft-deleted row 404s onPUT/PATCH. This is the same hole@kavo/prismaand@kavo/mongoosehave (only@kavo/typeormescapes it, because@DeleteDateColumnis detectable and therefore markable), and the fix belongs in core. Until then, register an explicitupdate/patchschema that omits the marker. - A non-auto-increment primary key is client-writable.
@PrimaryKey() id: string = v4()carries none of MikroORM's generated flags, so aPATCHcan rewrite a row's identity. A numeric@PrimaryKey()is auto-increment and safe. Name the write schemas explicitly for any entity with a caller-assigned key. hiddenandlazyproperties are dropped from Kavo entirely. Not merely hidden from responses — excluding them from the metadata seam is what keeps them off the default filter/sort allowlists, where an invisible but filterable column is a blind extraction oracle. The trade is the one@kavo/mongoosemakes forselect: false: Kavo does not manage such a property at all, so write it through a custom operation or the ORM.- A MikroORM
@Filteris applied on top of Kavo's scoping. Kavo owns soft-delete scoping throughdelete.field; a default-on MikroORM soft-delete filter would AND a second predicate onto every query and quietly defeatwithDeleted. Use one or the other, not both. - No transactions. The
TransactionManagerseam is unbuilt across every Kavo adapter today. - Composite primary keys are refused.
buildEntityMetadataraisesKAVO_CONFIG_INVALIDfor an entity with more than one@PrimaryKey.
Soft delete and unique indexes
A soft-deleted row still occupies its unique indexes, so re-creating "the same" row after a soft delete raises a 409 conflict — the honest answer, since the value is taken. The fix is a partial unique index scoped to live rows:
CREATE UNIQUE INDEX author_email_live ON author (email) WHERE deleted_at IS NULL;Full design notes: docs/internals/architecture/17-mikroorm-adapter.md.
