Files
Kokorone/.agents/skills/prisma-mongodb-upgrade/references/client-api-mapping.md
T
Jeffery f44200f543 feat: 完成 B 群組 — 資料層與核心領域模型
- 導入 Prisma 7.9.1 + SQLite(better-sqlite3 driver adapter),prisma.config.ts 管理連線設定
- 完整資料模型:User/Work/Character/CharacterAlias/EpisodicMemory/SemanticMemory/
  ProceduralRule/EmotionState/Relationship/SentimentLedgerEntry
- packages/db:Prisma Client 存取層(ESM,因 Prisma 7 產出的 generated client 僅支援 ESM)
- apps/api 隨之改為 ESM 以相容 packages/db
- packages/shared:新增角色/記憶/情緒/關係共用型別,api 與 web 皆從此匯入
- prisma/seed.ts:建立測試使用者與元氣型測試角色,含完整關係帳本與記憶種子資料
- apps/api:新增 GET /characters
- scripts/smoke/B.mjs:驗證資料表齊全、API 讀出種子角色、關係與記憶可寫入讀出
- 保留 Prisma 官方隨 CLI 附的 agent skill 文件(.agents/skills 等),供後續群組查閱

npm run restart && npm run smoke -- B 皆通過(B-V),A 群組冒煙測試無回歸。
2026-08-13 09:43:15 +08:00

4.0 KiB

client-api-mapping

How v6 Prisma Client calls map to Prisma Next's Mongo client — names map, parity does not.

Priority

CRITICAL

Why It Matters

The v6 and Next client APIs look superficially similar, but none of the v6 MongoDB raw methods exist under their old names, aggregation moved to a different lane entirely, and transactions go through the driver rather than a façade wrapper. Assuming parity produces code that does not compile — or, in the transactions case, code that silently loses atomicity.

The mapping

v6 call Prisma Next equivalent Notes
prisma.user.findMany(...) db.orm.users.where(...).all() Fluent ORM lane; storage-name keys (see schema-contract-mapping.md)
prisma.user.findFirst(...) db.orm.users.where(...).first()
create / update / upsert / delete / updateMany / deleteMany create / update / upsert / delete / updateAll / deleteAll on db.orm.<collection> See Prisma Next's prisma-next-queries skill
prisma.user.aggregate(...), groupBy(...) No ORM equivalent. Use the typed aggregation-pipeline builder: db.query.from(...).match(...).group(...).build() Prisma Next's prisma-next-queries skill covers the builder lane
$runCommandRaw(...) (v6 docs) Name does not exist in Next. Raw lane is mongoRaw(...) → a raw collection with aggregate, insertOne/Many, updateOne/Many, deleteOne/Many, findOneAndUpdate/Delete. For arbitrary database commands, use the underlying mongodb driver directly — it is a user-supplied peer dependency and fully accessible Check the installed version's raw surface
<model>.findRaw(...) (v6 docs) mongoRaw(...) collection reads (e.g. aggregate with a $match stage) No direct findRaw name
<model>.aggregateRaw(...) (v6 docs) mongoRaw(...).aggregate(...) or the typed pipeline builder
$transaction(...) — works on v6 with a replica set (v6 docs) The façade does not wrap db.transaction(...) yet, but the underlying mongodb driver is directly available (user-supplied peer dependency): multi-document atomicity works today via driver sessions (client.startSession() / session.withTransaction(...)) on a replica set A façade wrapper is expected soon; this row will be updated when it merges
$connect / $disconnect connect() / close() on the Mongo façade client

Bad

// Assuming v6 names exist in Prisma Next:
await db.user.$runCommandRaw({ collStats: 'users' }); // no such method
await db.transaction(async (tx) => { ... });          // no such method on the Mongo façade

Good

// Raw lane under its Next name:
const raw = mongoRaw(db);
await raw.users.aggregate([{ $match: { status: 'active' } }]);

// Aggregation through the typed pipeline builder:
const stats = await db.query.from('users').group({ _id: '$role', n: { $count: {} } }).build();

// Multi-document atomicity today: the mongodb driver (a direct dependency of the
// project) exposes sessions and transactions as usual:
const session = mongoClient.startSession();
await session.withTransaction(async () => {
  // ...writes...
});

References