mikro-orm

Act as a MikroORM expert to help build a TypeScript data layer based on Unit of Work and Identity Map patterns. Support decorator-based entities, automatic change tracking, lazy/eager loading, embeddables, query builder, migrations and seeding, compatible with PostgreSQL, MySQL, SQLite and MongoDB, and designed to be DDD-friendly.

Last repository push
GitHub Stars
219
Forks
38
Issues / PRs
7

Installation

Review the source and dependencies with a prompt. A package is available to download.

Install with an AI assistant

Copy the prompt so your assistant can review the pinned revision and dependencies first.

View promptHide prompt
Check whether SkillPI Skill Finder is installed for your current agent. If it is missing, install and enable it using the official instructions at https://github.com/Findskills/skillpi. Then use Skill Finder to locate and install "mikro-orm" from this exact SkillPI detail page: https://skillpi.com/skills/deepskill-94e05f169f81191457b2e9d1a9e0170690e83a6f5f8be034f12732fe80737520. Verify the upstream source and version against https://github.com/TerminalSkills/skills before installing. If they do not match, stop and report the mismatch. Report the installed path and source version.

Source and setup

github.com

Setup guidance pending

See the original source for supported tools, dependencies, and permissions.

Installation test pending

Act as a MikroORM expert to help build a TypeScript data layer based on Unit of Work and Identity Map patterns. Support decorator-based entities, automatic change tracking, lazy/eager loading, embeddables, query builder, migrations and seeding, compatible with PostgreSQL, MySQL, SQLite and MongoDB, and designed to be DDD-friendly.

0 Downloads

Introduction source: third_party · TerminalSkills · 1.0.0 Reference

SkillPi interpretation

Testing
Not tested

Browse files

Browse files at the source ↗

Original SKILL.md

Open original ↗
Frontmatter
name: mikro-orm
description: >-
  You are an expert in MikroORM, the TypeScript ORM built on Unit of Work and
  Identity Map patterns. You help developers build data layers with
  decorator-based entities, automatic change tracking, lazy/eager loading,
  embeddables, query builder, migrations, and seeding — supporting PostgreSQL,
  MySQL, SQLite, and MongoDB with a DDD-friendly architecture.
license: Apache-2.0
compatibility: ''
metadata:
  author: terminal-skills
  version: 1.0.0
  category: Backend Development
  tags:
    - orm
    - typescript
    - database
    - unit-of-work
    - identity-map
    - sql
    - mongodb

MikroORM — TypeScript ORM with Unit of Work

You are an expert in MikroORM, the TypeScript ORM built on Unit of Work and Identity Map patterns. You help developers build data layers with decorator-based entities, automatic change tracking, lazy/eager loading, embeddables, query builder, migrations, and seeding — supporting PostgreSQL, MySQL, SQLite, and MongoDB with a DDD-friendly architecture.

Core Capabilities

Entity Definition

import { Entity, PrimaryKey, Property, ManyToOne, OneToMany, Collection,
  Enum, Index, Unique, Embeddable, Embedded, Filter } from "@mikro-orm/core";
import { v4 } from "uuid";

@Embeddable()
class Address {
  @Property()
  street: string;

  @Property()
  city: string;

  @Property()
  country: string;
}

@Entity()
@Filter({ name: "active", cond: { deletedAt: null }, default: true })
export class User {
  @PrimaryKey()
  id: string = v4();

  @Property()
  name: string;

  @Index()
  @Unique()
  @Property()
  email: string;

  @Enum(() => UserRole)
  role: UserRole = UserRole.USER;

  @Embedded(() => Address, { nullable: true })
  address?: Address;

  @OneToMany(() => Post, (post) => post.author)
  posts = new Collection<Post>(this);

  @Property()
  createdAt: Date = new Date();

  @Property({ onUpdate: () => new Date() })
  updatedAt: Date = new Date();

  @Property({ nullable: true })
  deletedAt?: Date;
}

enum UserRole { USER = "user", ADMIN = "admin" }

@Entity()
export class Post {
  @PrimaryKey()
  id: string = v4();

  @Property()
  title: string;

  @Property({ type: "text" })
  body: string;

  @Property()
  published: boolean = false;

  @ManyToOne(() => User)
  author: User;

  @Property()
  createdAt: Date = new Date();
}

Unit of Work (Auto Change Tracking)

import { MikroORM, RequestContext } from "@mikro-orm/core";

const orm = await MikroORM.init({
  entities: [User, Post],
  dbName: "myapp",
  type: "postgresql",
  debug: process.env.NODE_ENV === "development",
});

// Express middleware — one EntityManager per request
app.use((req, res, next) => {
  RequestContext.create(orm.em, next);
});

// Usage — automatic change tracking
app.put("/users/:id", async (req, res) => {
  const em = orm.em;
  const user = await em.findOneOrFail(User, req.params.id);

  user.name = req.body.name;              // Just modify the entity
  user.email = req.body.email;

  await em.flush();                       // MikroORM detects changes, generates UPDATE
  res.json(user);
});

// Identity Map — same entity loaded twice returns same reference
const user1 = await em.findOne(User, "abc");
const user2 = await em.findOne(User, "abc");
console.log(user1 === user2);             // true — same object in memory

// QueryBuilder
const topAuthors = await em.createQueryBuilder(User, "u")
  .select(["u.*", "count(p.id) as post_count"])
  .leftJoin("u.posts", "p")
  .where({ role: UserRole.ADMIN })
  .groupBy("u.id")
  .orderBy({ post_count: "DESC" })
  .limit(10)
  .getResultList();

Installation

npm install @mikro-orm/core @mikro-orm/postgresql @mikro-orm/cli
npx mikro-orm migration:create
npx mikro-orm migration:up

Best Practices

  1. Unit of Work — Modify entities directly; call em.flush() once to batch all changes into minimal SQL
  2. Identity Map — Same entity loaded twice returns same reference; prevents inconsistency in a request
  3. RequestContext — Use RequestContext.create() middleware; gives each request its own EntityManager
  4. Filters — Use @Filter for soft deletes, multi-tenancy; applied automatically to all queries
  5. Embeddables — Use @Embedded for value objects (Address, Money); stored in same table, typed as objects
  6. Populate — Explicitly populate relations: em.find(User, {}, { populate: ['posts'] }); no implicit lazy loading
  7. Migrations — Use CLI to generate migrations from entity changes; review SQL before running
  8. Serialization — Use wrap(entity).toJSON() or custom serializers; control what's exposed in API responses

Current listing version

1.0.0

Verify the source revision ↗

Safety and assessment details

Quality assessment

Out of 100 · Assessment report from SkillPi LAB.

Scenario Value95/100

Highlights

  • 提供 MikroORM 的完整使用指南,涵盖实体定义、工作单元模式、查询构建等核心功能
  • 面向 TypeScript 开发者构建数据层,具有明确的高频刚需价值
Trigger Precision95/100

Highlights

  • description 明确描述 Skill 为 MikroORM 专家,覆盖核心能力关键词,边界清晰
Structure and Standards95/100

Highlights

  • 文档结构清晰,分章节组织,含完整代码示例和最佳实践,frontmatter 完整
Execution Reliability95/100

Highlights

  • 提供标准 npm 安装命令和 CLI 迁移命令,参数正确,覆盖主要使用场景
Evidence Verifiability (mean of the first four)95/100

The arithmetic mean of the first four original scores, rounded to a whole number.

Static text checks

100 / 100 · skillpi-20261123-no-r3-v1

No current rules matched the text.

This score reflects rule matches in the text and is provided for reference before use. It excludes attachments and runtime behavior.

Permissions and checks before use

Automatically summarized from this main document. Matches may be examples or defensive advice; they are not actual permission declarations. Attachments, external dependencies and runtime behavior are not covered.

These notices grant no permissions. Your host tool must still confirm file changes, secrets, external transfers or costs as needed.

Security rating and access scope

safe · Static evidence review; not a runtime safety guarantee.

Before use, confirm and authorize actual file access, external services and network destinations in your host. This review grants no runtime permissions.