Compare commits

...
2 Commits
Author SHA1 Message Date
wc24 6383aade98 fix(crud): 响应统一剥掉 mongoose 版本键 __v
filterHidden 出口加剥 __v(连同 hidden 字段),list/get/add/edit 全覆盖。
前端无用的 __v 不再下发。
2026-06-30 14:09:44 +08:00
wc24 d12877f846 fix(crud): add/edit 返回普通对象(toObject/lean),不再抖出 mongoose Document 内部
add 用 create 返回 Document、edit 用 findByIdAndUpdate 返回 Document,下游 processResItem
的 {...item} 会把 $__/_doc/$isNew 等内部属性展开、真数据埋进 _doc。改:add 转 toObject、
edit 加 .lean()(与 list/get 一致)。响应回归干净 POJO。
2026-06-30 14:06:40 +08:00
3 changed files with 6 additions and 4 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "shrimp-server", "name": "shrimp-server",
"version": "0.7.0", "version": "0.7.2",
"description": "Shrimp 框架后端 — 声明式字段驱动的 Node.js 管理后台框架(Express + Mongoose + JWT)", "description": "Shrimp 框架后端 — 声明式字段驱动的 Node.js 管理后台框架(Express + Mongoose + JWT)",
"main": "dist/main/index.js", "main": "dist/main/index.js",
"types": "dist/main/index.d.ts", "types": "dist/main/index.d.ts",
+2 -1
View File
@@ -268,7 +268,8 @@ export class ShrimpTableModule<T, P extends ITableCrudProto<T> = ITableCrudProto
} }
private filterHidden(item: any): any { private filterHidden(item: any): any {
return this.excludeKeys(item, this.fields.filter(f => f.hidden).map(f => f.key)) // __v 是 mongoose 版本键,前端无用,统一剥掉(连同 hidden 字段)
return this.excludeKeys(item, ["__v", ...this.fields.filter(f => f.hidden).map(f => f.key)])
} }
private buildQuery(query?: IPageQuery & any): IShrimpQuery { private buildQuery(query?: IPageQuery & any): IShrimpQuery {
+3 -2
View File
@@ -107,10 +107,11 @@ export class MongoShrimpCrud<T> implements ShrimpCrud<T> {
return await this.model.findById(id).lean() as T | null return await this.model.findById(id).lean() as T | null
} }
async add(item: Omit<T, '_id'>): Promise<T> { async add(item: Omit<T, '_id'>): Promise<T> {
return await this.model.create(item) const doc = await this.model.create(item)
return doc.toObject() as T // 转普通对象,否则下游 {...item} 展开的是 mongoose Document 内部($__/_doc/$isNew)
} }
async edit(id: string, item: Partial<T>): Promise<T | null> { async edit(id: string, item: Partial<T>): Promise<T | null> {
return await this.model.findByIdAndUpdate(id, item, { new: true }) return await this.model.findByIdAndUpdate(id, item, { new: true }).lean() as T | null
} }
async delete(id: string): Promise<boolean> { async delete(id: string): Promise<boolean> {
const result = await this.model.findByIdAndDelete(id) const result = await this.model.findByIdAndDelete(id)