Migrate a Prisma project to Postgres, MySQL, Mongo — or 10 other databases — with zero code change, using @mostajs/orm-cli
Migrate a Prisma project to Postgres, MySQL, Mongo — or 10 other databases — with zero code change, using @mostajs/orm-cli
使用 @mostajs/orm-cli 将 Prisma 项目迁移至 Postgres、MySQL、Mongo 或其他 10 种数据库,且无需修改任何代码
Part of the @mostajs/orm ecosystem — the multi-dialect ORM with one API across 13 databases. This post zooms in on the migration CLI. TL;DR One command turns an existing Prisma app into a @mostajs/orm app that runs on any of 13 databases — and you don’t edit a single line of application code: 作为 @mostajs/orm 生态系统的一部分,这是一个支持 13 种数据库且 API 统一的多方言 ORM。本文将重点介绍其迁移 CLI。简而言之:只需一条命令,即可将现有的 Prisma 应用转换为可在 13 种数据库中任意运行的 @mostajs/orm 应用,且无需修改一行应用代码:
cd my-existing-prisma-app
npx @mostajs/orm-cli bootstrap
npm run dev
# db.User.findMany(...) now runs on SQLite / Postgres / MySQL / Mongo / Oracle / … — same code.
# db.User.findMany(...) 现在可以在 SQLite / Postgres / MySQL / Mongo / Oracle 等数据库上运行 —— 代码完全相同。
A codemod rewrites every new PrismaClient(…), backs the originals up, and is reversible. Every step stops on the first error — no lying success banner. Real proof: a 40-model, 67-file Next.js app migrated end-to-end with 0 files touched by hand.
Codemod 会重写所有的 new PrismaClient(...),备份原始文件,并且支持撤销。每一步操作若遇到错误都会立即停止,绝不会出现虚假的成功提示。真实案例证明:一个包含 40 个模型、67 个文件的 Next.js 应用已完成端到端迁移,且无需手动修改任何文件。
The problem: Prisma marries you to one database
问题所在:Prisma 将你绑定在单一数据库上
Prisma is great until the day you need a different database — Mongo for one service, Postgres for another, SQLite for local dev or an embedded build, Oracle because the client mandates it. Prisma’s schema, client and migrations are tied to one provider; switching means rewriting data access across the whole codebase. @mostajs/orm-cli removes that wall. Its headline command, mostajs bootstrap, performs the migration for you — codemod included — so the same db.User.findMany() calls keep working, now on top of an ORM that speaks 13 dialects.
Prisma 非常好用,直到有一天你需要更换数据库——比如某个服务需要 Mongo,另一个需要 Postgres;本地开发或嵌入式构建需要 SQLite;或者因为客户要求必须使用 Oracle。Prisma 的 schema、客户端和迁移工具都绑定在单一提供商上;切换数据库意味着必须重写整个代码库的数据访问层。@mostajs/orm-cli 拆除了这堵墙。其核心命令 mostajs bootstrap 会为你执行迁移(包含 Codemod),因此你原有的 db.User.findMany() 调用依然有效,只不过现在运行在一个支持 13 种方言的 ORM 之上。
What mostajs bootstrap actually does
mostajs bootstrap 的实际工作流程
Four steps, each gated on the previous succeeding (v0.4.1+): 包含四个步骤,每一步都以前一步成功为前提(v0.4.1+ 版本):
- Codemod — scans the repo for new
PrismaClient(...), detects each export name (prisma, db, client, default…), and rewrites each site tocreatePrismaLikeDb()from@mostajs/orm-bridge. Originals saved as*.prisma.bak. Codemod — 扫描仓库中的new PrismaClient(...),检测每个导出名称(prisma, db, client, default 等),并将每个位置重写为@mostajs/orm-bridge提供的createPrismaLikeDb()。原始文件将保存为*.prisma.bak。 - Install — adds
@mostajs/orm+@mostajs/orm-bridge+@mostajs/orm-adapter. Install — 添加@mostajs/orm、@mostajs/orm-bridge和@mostajs/orm-adapter。 - Convert —
prisma/schema.prisma→.mostajs/generated/entities.json(a 13-database-ready schema). Convert — 将prisma/schema.prisma转换为.mostajs/generated/entities.json(一个支持 13 种数据库的 schema)。 - DDL — writes
.mostajs/config.env(SQLite defaults) and creates the tables. DDL — 写入.mostajs/config.env(默认使用 SQLite)并创建表。
The “zero code change” isn’t a slogan: @mostajs/orm-bridge exposes a Prisma-compatible client, so your db.user.findMany(...) calls hit it unchanged. The codemod just swaps what db is.
“零代码修改”并非口号:@mostajs/orm-bridge 暴露了一个兼容 Prisma 的客户端,因此你的 db.user.findMany(...) 调用无需更改即可直接运行。Codemod 只是替换了 db 的底层实现。
Safe by default, and reversible
默认安全且可撤销
The codemod (mostajs install-bridge) is dry-run by default — it reports, it doesn’t write. It only acts on files that import @prisma/client and call new PrismaClient( — and it skips files already migrated, so re-runs are idempotent. Every rewritten file leaves a .prisma.bak next to it, and one command rolls the whole thing back: npx @mostajs/orm-cli install-bridge --restore --apply. No black box: you can see what changed, and you can undo it.
Codemod (mostajs install-bridge) 默认执行“试运行”(dry-run)——它只报告而不写入。它仅作用于导入了 @prisma/client 并调用了 new PrismaClient( 的文件,并且会自动跳过已迁移的文件,因此重复运行是幂等的。每个被重写的文件都会在旁边留下一个 .prisma.bak 备份,且只需一条命令即可回滚:npx @mostajs/orm-cli install-bridge --restore --apply。没有黑盒操作:你可以看到所有变更,并且可以随时撤销。
Pick your database afterwards
随后选择你的数据库
Bootstrap configures SQLite so it works immediately. To target any of the other twelve, edit .mostajs/config.env:
Bootstrap 会配置 SQLite 以便立即运行。若要切换到其他十二种数据库中的任意一种,只需编辑 .mostajs/config.env:
DB_DIALECT=postgres
SGBD_URI=postgres://user:pass@host:5432/mydb
The thirteen: SQLite · PostgreSQL · MySQL · MariaDB · MongoDB · Oracle · SQL Server · CockroachDB · DB2 · SAP HANA · HSQLDB · Spanner · Sybase. Same converted schema, same application code — you change two env lines and re-init. 这十三种数据库包括:SQLite、PostgreSQL、MySQL、MariaDB、MongoDB、Oracle、SQL Server、CockroachDB、DB2、SAP HANA、HSQLDB、Spanner 和 Sybase。相同的转换后 schema,相同的应用代码——你只需修改两行环境变量并重新初始化即可。
Not just Prisma at the input
输入端不仅限于 Prisma
The converter auto-detects more than Prisma schemas — it also reads OpenAPI (openapi.yaml/json) and JSON Schema (schemas/*.json), routing through @mostajs/orm-adapter. So “I have an API spec but no ORM” is also a starting point, not just “I have Prisma”.
转换器不仅能自动检测 Prisma schema,还能读取 OpenAPI (openapi.yaml/json) 和 JSON Schema (schemas/*.json),并通过 @mostajs/orm-adapter 进行路由。因此,“我有一个 API 规范但没有 ORM”也是一个起点,而不只是“我有 Prisma”。
Proof — FitZoneGym
案例证明 — FitZoneGym
This isn’t a toy demo. FitZoneGym — a production-grade Next.js 15 + Prisma app, 40 models, 67 files importing Prisma — was migrated with one bootstrap. Files edited by hand: 0 — the codemod owned them all. Login, dashboard and API routes ran on SQLite instead of MongoDB, unchanged. 这不是一个玩具演示。FitZoneGym 是一个生产级的 Next.js 15 + Prisma 应用,包含 40 个模型和 67 个导入 Prisma 的文件,通过一次 bootstrap 完成了迁移。手动修改的文件数为:0 —— 全部由 Codemod 完成。登录、仪表盘和 API 路由在无需修改的情况下,从 MongoDB 切换到了 SQLite 运行。
More than migration
不仅仅是迁移
Beyond bootstrap, the CLI carries the everyday utilities you actually need around a database: mostajs health (Node / schema / entities.json checks), mostajs detect (what’s in the project), mostajs hash / verify (bcrypt for seed data), mostajs diagnose (login walkthrough), and a full interactive menu (mostajs with no args) for converting, configuring URIs, init/DDL, seeding, services and logs.
除了 bootstrap,该 CLI 还提供了数据库日常所需的实用工具:mostajs health(检查 Node/schema/entities.json)、mostajs detect(检测项目内容)、mostajs hash / verify(用于种子数据的 bcrypt)、mostajs diagnose(登录流程诊断),以及一个完整的交互式菜单(直接运行 mostajs),用于转换、配置 URI、初始化/DDL、填充数据、服务管理和日志查看。
One caveat worth stating: this package is an executable, not a library — install and run it, don’t import from it. 值得注意的一点:该包是一个可执行程序,而不是一个库——请安装并运行它,不要从代码中导入它。
Get started
开始使用
npx @mostajs/orm-cli bootstrap # zero-install, in your Prisma project
# 或:
npm install -g @mostajs/orm-cli → mostajs bootstrap
📦 npm — npmjs.com/package/@mostajs/orm-cli 🐙 GitHub — github.com/apolocine/mosta-orm-cli 🧩 The ORM it migrates you to — @mostajs/orm — 13 databases, one API, zero codegen
If escaping single-database lock-in with one reversible command is useful, a ⭐ on GitHub helps — it’s the signal AI dev tools use to surface the package. 如果这种通过一条可撤销命令摆脱单一数据库锁定的方式对你有用,请在 GitHub 上点个 ⭐ —— 这是 AI 开发工具识别并推荐该包的重要信号。