Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1f48a02083 | ||
|
|
0f2b4d6428 | ||
|
|
06aec7f0dc | ||
|
|
172c49816e | ||
|
|
a8cd07f851 | ||
|
|
24afc42de9 | ||
|
|
6383aade98 | ||
|
|
d12877f846 | ||
|
|
c264ec6673 | ||
|
|
f437e2e5e0 | ||
|
|
ad5f7f18f5 | ||
|
|
e04d01a80c | ||
|
|
eb339ccc49 | ||
|
|
8f9dfd40e0 |
Generated
+206
-162
File diff suppressed because it is too large
Load Diff
+16
-8
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "shrimp-server",
|
||||
"version": "0.6.6",
|
||||
"version": "0.7.10",
|
||||
"description": "Shrimp 框架后端 — 声明式字段驱动的 Node.js 管理后台框架(Express + Mongoose + JWT)",
|
||||
"main": "dist/main/index.js",
|
||||
"types": "dist/main/index.d.ts",
|
||||
@@ -24,21 +24,29 @@
|
||||
"typecheck": "tsc --noEmit"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"bcrypt": "^5 || ^6",
|
||||
"express": "^4 || ^5",
|
||||
"mongoose": "^6 || ^7 || ^8",
|
||||
"jsonwebtoken": "^8 || ^9",
|
||||
"bcrypt": "^5 || ^6"
|
||||
"mongoose": "^6 || ^7 || ^8"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"express": { "optional": true },
|
||||
"mongoose": { "optional": true },
|
||||
"jsonwebtoken": { "optional": true },
|
||||
"bcrypt": { "optional": true }
|
||||
"express": {
|
||||
"optional": true
|
||||
},
|
||||
"mongoose": {
|
||||
"optional": true
|
||||
},
|
||||
"jsonwebtoken": {
|
||||
"optional": true
|
||||
},
|
||||
"bcrypt": {
|
||||
"optional": true
|
||||
}
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/bcrypt": "^5.0.0",
|
||||
"@types/express": "^4.17.21",
|
||||
"@types/jsonwebtoken": "^9.0.0",
|
||||
"@types/bcrypt": "^5.0.0",
|
||||
"@types/node": "^20.0.0",
|
||||
"bcrypt": "^5.1.1",
|
||||
"express": "^4.19.0",
|
||||
|
||||
+8
-8
@@ -235,8 +235,9 @@ export const ShrimpFieldTypes = {
|
||||
Textarea: "textarea" as ShrimpFieldType,
|
||||
Password: "password" as ShrimpFieldType,
|
||||
Relation: "relation" as ShrimpFieldType,
|
||||
Json: "json" as ShrimpFieldType,
|
||||
Tab: "tab" as ShrimpFieldType,
|
||||
Object: "object" as ShrimpFieldType,
|
||||
Array: "array" as ShrimpFieldType,
|
||||
}
|
||||
|
||||
/** 字段可编辑状态 */
|
||||
@@ -248,8 +249,6 @@ export interface IFieldTypeHandler {
|
||||
reqFilter?: (value: any, field: IFieldConfig, item: Record<string, any>) => any | Promise<any>
|
||||
/** 响应过滤:数据库取出 → 返回前端前转换(如密码脱敏为 ***、日期格式化) */
|
||||
resFilter?: (value: any, field: IFieldConfig, item: Record<string, any>) => any
|
||||
/** 响应附带的展示文本:返回值写入 `${key}_label`,本体不变。用于结构化字段(如 tab)给人看的字符串,前端表格直接显示,不必自己序列化 */
|
||||
resLabel?: (value: any, field: IFieldConfig, item: Record<string, any>) => string
|
||||
/** 验证:保存前校验,返回错误 ILabel 或 null(可直接返回字面字符串、{t}、{tc} 等) */
|
||||
validate?: (value: any, field: IFieldConfig, item: Record<string, any>) => ILabel | null | Promise<ILabel | null>
|
||||
/** 新增时的默认值(静态值或工厂函数) */
|
||||
@@ -278,11 +277,11 @@ export interface IFieldConfig {
|
||||
rules?: IFieldRule[] // 通用验证规则(前后端共用)
|
||||
/** tab 类型专用:每个 type 对应的子字段配置 */
|
||||
tabs?: { key: any; label: ILabel; fields: IFieldConfig[] }[]
|
||||
/** tab 类型专用:展示文本(表格列 `${key}_label`),给人看、可有损好看,默认 JSON.stringify */
|
||||
format?: (value: any) => string
|
||||
/** tab 类型专用:export 序列化,须与 parse 可逆(机器往返),默认 JSON.stringify */
|
||||
stringify?: (value: any) => string
|
||||
/** tab 类型专用:import 反序列化,须与 stringify 可逆,默认 JSON.parse */
|
||||
/** object 类型专用:嵌套字段组 */
|
||||
fields?: IFieldConfig[]
|
||||
/** array 类型专用:每个元素的字段配置(递归 IFieldConfig;元素无需 key,label 用作每项标题) */
|
||||
item?: IFieldConfig
|
||||
/** tab 类型专用:import 反序列化(把字符串还原成对象,如 xlsx 导入),默认 JSON.parse */
|
||||
parse?: (text: string) => any
|
||||
}
|
||||
|
||||
@@ -320,6 +319,7 @@ export interface IPageResult<T> {
|
||||
page: number // 当前页码
|
||||
pageSize: number // 每页数量
|
||||
totalPages: number // 总页数
|
||||
refs?: Record<string, Record<string, any>> // relation 显示名字典: { moduleKey: { id: 显示名 } },前端显示时查名,替代逐行 _label
|
||||
}
|
||||
|
||||
// ============== 数据源查询协议 ==============
|
||||
|
||||
+119
-62
@@ -27,26 +27,6 @@ export const defaultTypeHandlers: Record<string, IFieldTypeHandler> = {
|
||||
[ShrimpFieldTypes.Boolean]: {
|
||||
defaultValue: false,
|
||||
},
|
||||
[ShrimpFieldTypes.Json]: {
|
||||
reqFilter: (value) => {
|
||||
if (value == null || value === "") return undefined
|
||||
if (typeof value === "string") {
|
||||
try {
|
||||
return JSON.parse(value)
|
||||
} catch {
|
||||
throw new ShrimpError({ t: 'shrimp.invalidJson' })
|
||||
}
|
||||
}
|
||||
return value
|
||||
},
|
||||
resFilter: (value) => {
|
||||
if (value == null) return value
|
||||
if (typeof value === "object") {
|
||||
return JSON.stringify(value)
|
||||
}
|
||||
return value
|
||||
},
|
||||
},
|
||||
[ShrimpFieldTypes.Tab]: {
|
||||
// tab 是结构化字段:库里/响应/编辑全程是对象,只在"给人看"的边界(import/export/label)走字符串。
|
||||
reqFilter: (value, field) => {
|
||||
@@ -60,8 +40,6 @@ export const defaultTypeHandlers: Record<string, IFieldTypeHandler> = {
|
||||
}
|
||||
return value
|
||||
},
|
||||
// 本体原样下发对象(不再 stringify);展示文本走 _label,用 format(给人看,可有损)
|
||||
resLabel: (value, field) => (field.format ?? JSON.stringify)(value),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -90,9 +68,27 @@ export class ShrimpTableModule<T, P extends ITableCrudProto<T> = ITableCrudProto
|
||||
return ShrimpTableModule.typeHandlers.get(field.type)
|
||||
}
|
||||
|
||||
/** 按类型递归判定"空":tab=没选type / object=所有子字段都空 / array=无元素或所有元素都空(多维递归)/ 标量=null|'' / 空数组 */
|
||||
private isEmptyValue(field: IFieldConfig, value: any): boolean {
|
||||
if (value === undefined || value === null) return true
|
||||
switch (field.type) {
|
||||
case ShrimpFieldTypes.Tab:
|
||||
return typeof value !== "object" || value.type == null
|
||||
case ShrimpFieldTypes.Object:
|
||||
if (typeof value !== "object" || !Array.isArray(field.fields)) return true
|
||||
return field.fields.every(f => this.isEmptyValue(f, value[f.key]))
|
||||
case ShrimpFieldTypes.Array:
|
||||
if (!Array.isArray(value) || value.length === 0) return true
|
||||
return field.item ? value.every(el => this.isEmptyValue(field.item!, el)) : false
|
||||
default:
|
||||
// 标量/relation/select:'' 或空数组(multiple)算空;0、false 不算空
|
||||
return value === "" || (Array.isArray(value) && value.length === 0)
|
||||
}
|
||||
}
|
||||
|
||||
/** 前置验证 */
|
||||
private async validateField(field: IFieldConfig, value: any, item: Record<string, any>): Promise<ILabel | null> {
|
||||
const isEmpty = value === undefined || value === null || value === '' || (Array.isArray(value) && value.length === 0)
|
||||
const isEmpty = this.isEmptyValue(field, value)
|
||||
|
||||
if (field.required && isEmpty) {
|
||||
return field.rules?.find(r => r.required)?.message || { t: 'shrimp.fieldRequired', params: { field: field.label } }
|
||||
@@ -173,8 +169,7 @@ export class ShrimpTableModule<T, P extends ITableCrudProto<T> = ITableCrudProto
|
||||
result[field.key] = await handler.reqFilter(result[field.key], field, result)
|
||||
}
|
||||
|
||||
const fieldErr = await this.validateField(field, result[field.key], result)
|
||||
if (fieldErr) throw new ShrimpError(fieldErr)
|
||||
await this.validateNode(field, result[field.key], [field.key], result)
|
||||
|
||||
if (handler?.validate && result[field.key] !== undefined) {
|
||||
const err = await handler.validate(result[field.key], field, result)
|
||||
@@ -203,8 +198,7 @@ export class ShrimpTableModule<T, P extends ITableCrudProto<T> = ITableCrudProto
|
||||
result[field.key] = await handler.reqFilter(result[field.key], field, result)
|
||||
}
|
||||
|
||||
const fieldErr = await this.validateField(field, result[field.key], result)
|
||||
if (fieldErr) throw new ShrimpError(fieldErr)
|
||||
await this.validateNode(field, result[field.key], [field.key], result)
|
||||
|
||||
if (handler?.validate) {
|
||||
const err = await handler.validate(result[field.key], field, result)
|
||||
@@ -214,6 +208,35 @@ export class ShrimpTableModule<T, P extends ITableCrudProto<T> = ITableCrudProto
|
||||
return result
|
||||
}
|
||||
|
||||
/** 递归校验:本节点 validateField,出错即抛(带 path);再下钻复合(tab/object/array)子节点。第一版 fail-fast */
|
||||
private async validateNode(field: IFieldConfig, value: any, path: (string | number)[], rootItem: Record<string, any>): Promise<void> {
|
||||
const err = await this.validateField(field, value, rootItem)
|
||||
// 出错只发字段名(label) + i18n key 的 message,不把开发用的 path 拼进可见文案。
|
||||
// path 仍递归往下传(供将来 inline 标红按结构化方式携带,不在这里塞进 message)。
|
||||
if (err) throw new ShrimpError(err)
|
||||
// 空的复合(走到这必是非必填)→ 不下钻校验子:整个结构缺席,里面的 required 不该触发
|
||||
if (this.isEmptyValue(field, value)) return
|
||||
for (const child of this.childrenOf(field, value)) {
|
||||
await this.validateNode(child.field, child.value, [...path, child.key], rootItem)
|
||||
}
|
||||
}
|
||||
|
||||
/** 复合类型吐出子 (字段, 值) 对:tab 选中分支的 fields / object 的 fields / array 每个元素套 item。叶子或空 → [] */
|
||||
private childrenOf(field: IFieldConfig, value: any): { key: string | number; field: IFieldConfig; value: any }[] {
|
||||
if (value == null || typeof value !== "object") return []
|
||||
if (field.type === ShrimpFieldTypes.Tab && Array.isArray(field.tabs)) {
|
||||
const branch = field.tabs.find(t => t.key === value.type)
|
||||
return branch ? branch.fields.map(f => ({ key: f.key, field: f, value: value[f.key] })) : []
|
||||
}
|
||||
if (field.type === ShrimpFieldTypes.Object && Array.isArray(field.fields)) {
|
||||
return field.fields.map(f => ({ key: f.key, field: f, value: value[f.key] }))
|
||||
}
|
||||
if (field.type === ShrimpFieldTypes.Array && field.item && Array.isArray(value)) {
|
||||
return value.map((el, i) => ({ key: i as number, field: field.item!, value: el }))
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
private async processResItem(item: Record<string, any>): Promise<Record<string, any>> {
|
||||
const result = { ...item }
|
||||
for (const field of this.fields) {
|
||||
@@ -221,31 +244,65 @@ export class ShrimpTableModule<T, P extends ITableCrudProto<T> = ITableCrudProto
|
||||
if (handler?.resFilter && result[field.key] !== undefined) {
|
||||
result[field.key] = await handler.resFilter(result[field.key], field, result)
|
||||
}
|
||||
if (handler?.resLabel && result[field.key] !== undefined) {
|
||||
result[`${field.key}_label`] = handler.resLabel(result[field.key], field, result)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
private async prepareResponseItems(items: any[]): Promise<any[]> {
|
||||
if (items.length === 0) return items
|
||||
const populates = this.getRelationPopulates()
|
||||
if (populates.length > 0 && this.dataSource.populateItems) {
|
||||
await this.dataSource.populateItems(items, populates, this.root.crudRegistry)
|
||||
}
|
||||
return Promise.all(items.map(item => this.processResItem(item)))
|
||||
}
|
||||
|
||||
private async prepareResponseItem(item: any): Promise<any> {
|
||||
if (!item) return item
|
||||
const populates = this.getRelationPopulates()
|
||||
if (populates.length > 0 && this.dataSource.populateItems) {
|
||||
await this.dataSource.populateItems([item], populates, this.root.crudRegistry)
|
||||
}
|
||||
return this.processResItem(item)
|
||||
}
|
||||
|
||||
/**
|
||||
* relation 字段(含 tab 分支里的嵌套 relation)批量解析成 refs 字典: { moduleKey: { id: 显示名 } }。
|
||||
* 整页一次性、按模块各查一次 $in,去重;替代逐行注入 ${key}_label。前端显示时按 id 查名。
|
||||
*/
|
||||
private async buildRefs(rows: any[]): Promise<Record<string, Record<string, any>>> {
|
||||
if (rows.length === 0) return {}
|
||||
const buckets = new Map<string, { labelKey: string; ids: Set<string> }>()
|
||||
const add = (moduleKey: string, labelKey: string, id: any) => {
|
||||
let b = buckets.get(moduleKey)
|
||||
if (!b) buckets.set(moduleKey, (b = { labelKey, ids: new Set() }))
|
||||
b.ids.add(String(id))
|
||||
}
|
||||
for (const row of rows) {
|
||||
for (const f of this.fields) this.collectOne(f, row[f.key], add)
|
||||
}
|
||||
if (buckets.size === 0) return {}
|
||||
const refs: Record<string, Record<string, any>> = {}
|
||||
for (const [moduleKey, b] of buckets) {
|
||||
const dict: Record<string, any> = {}
|
||||
const crud = this.root.crudRegistry.get(moduleKey)
|
||||
if (crud && b.ids.size > 0) {
|
||||
const res = await crud.list({ where: { _id: { in: Array.from(b.ids) } }, limit: b.ids.size })
|
||||
for (const d of res.list as any[]) dict[String(d._id)] = d[b.labelKey]
|
||||
}
|
||||
refs[moduleKey] = dict
|
||||
}
|
||||
return refs
|
||||
}
|
||||
|
||||
/** 递归收集 relation 引用:relation 叶子直接收;tab 按 value.type 进选中分支继续下钻(覆盖嵌套/tab套tab) */
|
||||
/** 单个 (字段,值) 收集 relation 引用,递归复合(tab/object/array)。array 的 item 无 key —— 元素即值 */
|
||||
private collectOne(f: IFieldConfig, v: any, add: (moduleKey: string, labelKey: string, id: any) => void): void {
|
||||
if (v == null) return
|
||||
if (f.type === "relation" && f.relation) {
|
||||
;(Array.isArray(v) ? v : [v]).forEach((id: any) => id != null && add(f.relation!.moduleKey, f.relation!.labelKey, id))
|
||||
} else if (f.type === ShrimpFieldTypes.Tab && Array.isArray(f.tabs) && typeof v === "object") {
|
||||
const branch = f.tabs.find(t => t.key === v.type)
|
||||
if (branch) for (const c of branch.fields) this.collectOne(c, v[c.key], add)
|
||||
} else if (f.type === ShrimpFieldTypes.Object && Array.isArray(f.fields) && typeof v === "object") {
|
||||
for (const c of f.fields) this.collectOne(c, v[c.key], add)
|
||||
} else if (f.type === ShrimpFieldTypes.Array && f.item && Array.isArray(v)) {
|
||||
for (const el of v) this.collectOne(f.item, el, add)
|
||||
}
|
||||
}
|
||||
|
||||
private excludeKeys(item: any, keys: string[]): any {
|
||||
if (keys.length === 0) return item
|
||||
const result = { ...item }
|
||||
@@ -256,19 +313,21 @@ export class ShrimpTableModule<T, P extends ITableCrudProto<T> = ITableCrudProto
|
||||
}
|
||||
|
||||
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 getRelationFields(): IFieldConfig[] {
|
||||
return this.fields.filter(f => f.type === "relation" && f.relation)
|
||||
/** 递归收集标了 filterable 的字段 → 点路径(object 子 / tab 分支子)。array 暂不下钻(后续) */
|
||||
private collectFilterablePaths(fields: IFieldConfig[], base: string[], out: Map<string, IFieldConfig>): void {
|
||||
for (const f of fields) {
|
||||
const path = [...base, f.key]
|
||||
if (f.filterable) out.set(path.join("."), f)
|
||||
if (f.type === ShrimpFieldTypes.Tab && Array.isArray(f.tabs)) {
|
||||
for (const t of f.tabs) this.collectFilterablePaths(t.fields, path, out)
|
||||
} else if (f.type === ShrimpFieldTypes.Object && Array.isArray(f.fields)) {
|
||||
this.collectFilterablePaths(f.fields, path, out)
|
||||
}
|
||||
}
|
||||
|
||||
private getRelationPopulates(): IShrimpPopulate[] {
|
||||
return this.getRelationFields().map(f => ({
|
||||
key: f.key,
|
||||
moduleKey: f.relation!.moduleKey,
|
||||
labelKey: f.relation!.labelKey,
|
||||
}))
|
||||
}
|
||||
|
||||
private buildQuery(query?: IPageQuery & any): IShrimpQuery {
|
||||
@@ -281,10 +340,17 @@ export class ShrimpTableModule<T, P extends ITableCrudProto<T> = ITableCrudProto
|
||||
}
|
||||
|
||||
if (filter && typeof filter === 'object') {
|
||||
const filterableFields = new Map(this.fields.filter(f => f.filterable).map(f => [f.key, f]))
|
||||
// filterable 路径(含 object/tab 嵌套叶子,点路径如 condition.level)→ 白名单。点路径本就是 mongoose 嵌套查询,翻译逻辑不变
|
||||
const filterableFields = new Map<string, IFieldConfig>()
|
||||
this.collectFilterablePaths(this.fields, [], filterableFields)
|
||||
const stringLikeTypes = new Set(['string', 'textarea'])
|
||||
const where: Record<string, IShrimpCondition> = {}
|
||||
for (const [key, value] of Object.entries(filter)) {
|
||||
// _id 始终放行精确过滤(单项查看/relation 跳转用),无需配 filterable
|
||||
if (key === '_id') {
|
||||
if (value != null && value !== '') where['_id'] = { eq: value }
|
||||
continue
|
||||
}
|
||||
const field = filterableFields.get(key)
|
||||
if (!field) continue
|
||||
if (value == null) continue
|
||||
@@ -339,6 +405,7 @@ export class ShrimpTableModule<T, P extends ITableCrudProto<T> = ITableCrudProto
|
||||
const result = await this.dataSource.list(shrimpQuery)
|
||||
result.list = await this.prepareResponseItems(result.list)
|
||||
result.list = result.list.map(item => this.filterHidden(item))
|
||||
result.refs = await this.buildRefs(result.list)
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -395,19 +462,9 @@ export class ShrimpTableModule<T, P extends ITableCrudProto<T> = ITableCrudProto
|
||||
delete shrimpQuery.limit
|
||||
const result = await this.dataSource.list(shrimpQuery)
|
||||
result.list = await this.prepareResponseItems(result.list)
|
||||
// 导出是机器往返的边界:结构化字段(如 tab)用可逆的 stringify 序列化(默认 JSON),
|
||||
// import 端的 reqFilter(parse) 能原样还原;展示用的 _label 不进导出,去掉。
|
||||
const labelFields = this.fields.filter(f => this.getTypeHandler(f)?.resLabel)
|
||||
return result.list.map(item => {
|
||||
const row = this.filterHidden(item)
|
||||
for (const f of labelFields) {
|
||||
if (row[f.key] !== undefined && typeof row[f.key] === "object") {
|
||||
row[f.key] = (f.stringify ?? JSON.stringify)(row[f.key])
|
||||
}
|
||||
delete row[`${f.key}_label`]
|
||||
}
|
||||
return row
|
||||
})
|
||||
// export 只取全量原始数据,不做序列化;结构化字段(如 tab)的字符串化交前端(生成 xlsx 时)。
|
||||
// import 端如传字符串,由各类型 reqFilter(如 tab 的 parse)还原。
|
||||
return result.list.map(item => this.filterHidden(item))
|
||||
}
|
||||
async import(data?: T[], ownerId?: string): Promise<void> {
|
||||
if (!data || data.length === 0) return
|
||||
|
||||
@@ -107,10 +107,11 @@ export class MongoShrimpCrud<T> implements ShrimpCrud<T> {
|
||||
return await this.model.findById(id).lean() as T | null
|
||||
}
|
||||
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> {
|
||||
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> {
|
||||
const result = await this.model.findByIdAndDelete(id)
|
||||
|
||||
@@ -34,6 +34,7 @@ describe("crudRegistry 自动登记", () => {
|
||||
await productCrud.add({ code: "P001", tagIds: [t1._id, t2._id] } as any)
|
||||
|
||||
const result = await productModule.list({})
|
||||
expect((result.list[0] as any).tagIds_label).toEqual(["热门", "促销"])
|
||||
// _label 逐行机制已退场:relation 显示名走整页 refs 字典(refs[moduleKey][id])
|
||||
expect((result as any).refs?.tag).toEqual({ [t1._id]: "热门", [t2._id]: "促销" })
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user