mikro-orm
安装方式
先用 Prompt 核对来源与依赖。也可下载安装包。
用 AI 助手安装
复制提示词,让助手先核对固定版本和依赖。
查看 Prompt收起 Prompt
先检查当前 Agent 是否已安装 SkillPI Skill Finder。如果尚未安装,请按官方仓库的说明安装并启用:https://github.com/Findskills/skillpi。然后使用 Skill Finder 打开此 SkillPI 详情页并定位 "mikro-orm":https://skillpi.com/zh/skills/deepskill-94e05f169f81191457b2e9d1a9e0170690e83a6f5f8be034f12732fe80737520。安装前核对上游来源和版本:https://github.com/TerminalSkills/skills。来源不一致时停止并说明差异。核对通过后,用 Skill Finder 安装目标 Skill,并报告安装路径和来源版本。
来源与配置
github.com
配置方式待核实
请查看原始来源中的适用工具、依赖和权限要求。
安装实测待补充
你是 MikroORM 专家,帮助构建基于工作单元和身份映射模式的 TypeScript 数据层。支持基于装饰器的实体、自动变更跟踪、延迟/即时加载、可嵌入对象、查询构建器、迁移和种子数据,兼容 PostgreSQL、MySQL、SQLite 和 MongoDB,并采用领域驱动设计友好的架构。
介绍来源: third_party · TerminalSkills · 1.0.0 参考来源
SkillPi 解读
- 实测状态
- 未测试
文件浏览
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
- mongodbMikroORM — 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
- Unit of Work — Modify entities directly; call
em.flush()once to batch all changes into minimal SQL - Identity Map — Same entity loaded twice returns same reference; prevents inconsistency in a request
- RequestContext — Use
RequestContext.create()middleware; gives each request its own EntityManager - Filters — Use
@Filterfor soft deletes, multi-tenancy; applied automatically to all queries - Embeddables — Use
@Embeddedfor value objects (Address, Money); stored in same table, typed as objects - Populate — Explicitly populate relations:
em.find(User, {}, { populate: ['posts'] }); no implicit lazy loading - Migrations — Use CLI to generate migrations from entity changes; review SQL before running
- Serialization — Use
wrap(entity).toJSON()or custom serializers; control what's exposed in API responses
当前收录版本
1.0.0
质量评测
满分100 · 来自 SkillPi LAB 实验室的评测报告。
亮点
- 提供 MikroORM 的完整使用指南,涵盖实体定义、工作单元模式、查询构建等核心功能
- 面向 TypeScript 开发者构建数据层,具有明确的高频刚需价值
亮点
- description 明确描述 Skill 为 MikroORM 专家,覆盖核心能力关键词,边界清晰
亮点
- 文档结构清晰,分章节组织,含完整代码示例和最佳实践,frontmatter 完整
亮点
- 提供标准 npm 安装命令和 CLI 迁移命令,参数正确,覆盖主要使用场景
取前四项原始分值的算术平均,四舍五入取整。可通过收录报告核对各项评分与说明,运行记录需单独查证。
正文静态检测
100 / 100 · skillpi-20261123-no-r3-v1
正文未命中当前规则。
分数仅反映正文中的规则命中,供使用前参考。附件与运行行为不在此评分范围内。
权限与使用前检查
根据当前主文档自动整理,命中内容可能是示例或防护说明;不是实际权限声明,也未覆盖附件、外部依赖或运行行为。
-
安装或获取外部依赖
使用官方来源并锁定版本,在隔离环境检查安装脚本。当前文档检查不覆盖后续下载的代码。
主文档提及 · 第 155, 156, 157 行
安全评级与访问范围
safe · 静态证据核验,不代表运行安全保证。
使用前请由宿主核对并授权实际文件读写、外部服务和联网目的地;本站的核验不会授予任何运行权限。
用户讨论
还没有已核实的点评。