collectFilterablePaths 递归收集标了 filterable 的字段(object 子/tab 分支子)连同点路径; buildQuery 白名单从顶层字段扩成路径(点路径本就是 mongoose 嵌套查询,翻译逻辑不变)。 array 下钻、tab 判别值过滤留后续。
477 lines
21 KiB
TypeScript
477 lines
21 KiB
TypeScript
import {
|
|
IFieldConfig, IFieldTypeHandler, IShrimpQuery, IShrimpCondition, IShrimpPopulate,
|
|
IPageQuery, IPageResult, ITableCrudProto, ILabel, ShrimpConfig, ShrimpError, ShrimpFieldTypes,
|
|
} from "../IShrimpType"
|
|
import { Shrimp } from "./Shrimp"
|
|
import { ShrimpModule, IShrimpModuleOptions } from "./ShrimpModule"
|
|
import type { ShrimpCrud } from "./ShrimpCrud"
|
|
import type { RootShrimpPower } from "./ShrimpPower"
|
|
|
|
/** 内置字段类型处理器 */
|
|
export const defaultTypeHandlers: Record<string, IFieldTypeHandler> = {
|
|
[ShrimpFieldTypes.Password]: {
|
|
reqFilter: async (value) => {
|
|
if (typeof value === 'string' && value.length > 0) {
|
|
return Shrimp.passwordHasher.hash(value)
|
|
}
|
|
return value
|
|
},
|
|
resFilter: () => "***",
|
|
},
|
|
[ShrimpFieldTypes.Date]: {
|
|
defaultValue: () => Date.now(),
|
|
},
|
|
[ShrimpFieldTypes.Number]: {
|
|
defaultValue: 0,
|
|
},
|
|
[ShrimpFieldTypes.Boolean]: {
|
|
defaultValue: false,
|
|
},
|
|
[ShrimpFieldTypes.Tab]: {
|
|
// tab 是结构化字段:库里/响应/编辑全程是对象,只在"给人看"的边界(import/export/label)走字符串。
|
|
reqFilter: (value, field) => {
|
|
if (value == null || value === "") return undefined
|
|
if (typeof value === "string") {
|
|
try {
|
|
return (field.parse ?? JSON.parse)(value)
|
|
} catch {
|
|
throw new ShrimpError({ t: 'shrimp.invalidJson' })
|
|
}
|
|
}
|
|
return value
|
|
},
|
|
},
|
|
}
|
|
|
|
export class ShrimpTableModule<T, P extends ITableCrudProto<T> = ITableCrudProto<T>> extends ShrimpModule<P> implements ITableCrudProto<T> {
|
|
fields: IFieldConfig[] = []
|
|
|
|
constructor(public parent: Shrimp | ShrimpModule, moduleKey: string, public dataSource: ShrimpCrud<T>, fields?: IFieldConfig[], label?: ILabel, power?: RootShrimpPower, options?: IShrimpModuleOptions) {
|
|
super(parent, moduleKey, label || { t: moduleKey }, power, options)
|
|
if (fields) {
|
|
this.fields = fields
|
|
}
|
|
this.root.crudRegistry.set(moduleKey, dataSource)
|
|
}
|
|
|
|
getFields(): IFieldConfig[] {
|
|
return this.fields
|
|
}
|
|
|
|
static typeHandlers: Map<string, IFieldTypeHandler> = new Map(Object.entries(defaultTypeHandlers))
|
|
|
|
static registerTypeHandler(type: string, handler: IFieldTypeHandler) {
|
|
this.typeHandlers.set(type, handler)
|
|
}
|
|
|
|
private getTypeHandler(field: IFieldConfig): IFieldTypeHandler | undefined {
|
|
return ShrimpTableModule.typeHandlers.get(field.type)
|
|
}
|
|
|
|
/** 前置验证 */
|
|
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)
|
|
|
|
if (field.required && isEmpty) {
|
|
return field.rules?.find(r => r.required)?.message || { t: 'shrimp.fieldRequired', params: { field: field.label } }
|
|
}
|
|
|
|
if (isEmpty) return null
|
|
|
|
if (Array.isArray(field.options)) {
|
|
const validValues = new Set(field.options.map(o => o.value))
|
|
const values = field.multiple && Array.isArray(value) ? value : [value]
|
|
for (const v of values) {
|
|
if (!validValues.has(v)) {
|
|
return field.rules?.find(r => r.message)?.message || { t: 'shrimp.fieldInvalid', params: { field: field.label } }
|
|
}
|
|
}
|
|
}
|
|
|
|
// multiple 字段值是数组,类型校验需逐元素进行(与上面 options 校验保持一致);
|
|
// 非 multiple 则整体校验单值。否则数组会被标量类型分支整体判为类型错误。
|
|
const checkType = (v: any): boolean => {
|
|
switch (field.type) {
|
|
case ShrimpFieldTypes.String: return typeof v === 'string'
|
|
case ShrimpFieldTypes.Number: return typeof v === 'number' && !Number.isNaN(v)
|
|
case ShrimpFieldTypes.Boolean: return typeof v === 'boolean'
|
|
case ShrimpFieldTypes.Date: return typeof v === 'number' || v instanceof Date || !isNaN(Date.parse(v))
|
|
default: return true // 非内置标量类型(relation/json/自定义等)不在此校验
|
|
}
|
|
}
|
|
const typeTargets = field.multiple && Array.isArray(value) ? value : [value]
|
|
for (const v of typeTargets) {
|
|
if (!checkType(v)) return { t: 'shrimp.fieldTypeError', params: { field: field.label } }
|
|
}
|
|
|
|
if (field.rules) {
|
|
for (const rule of field.rules) {
|
|
const msg = rule.message || { t: 'shrimp.fieldInvalid', params: { field: field.label } }
|
|
|
|
if (rule.min !== undefined) {
|
|
if (typeof value === 'number' && value < rule.min) return { t: 'shrimp.fieldMin', params: { field: field.label, min: rule.min } }
|
|
if (typeof value === 'string' && value.length < rule.min) return { t: 'shrimp.fieldMinLength', params: { field: field.label, min: rule.min } }
|
|
if (Array.isArray(value) && value.length < rule.min) return { t: 'shrimp.fieldMinLength', params: { field: field.label, min: rule.min } }
|
|
}
|
|
|
|
if (rule.max !== undefined) {
|
|
if (typeof value === 'number' && value > rule.max) return { t: 'shrimp.fieldMax', params: { field: field.label, max: rule.max } }
|
|
if (typeof value === 'string' && value.length > rule.max) return { t: 'shrimp.fieldMaxLength', params: { field: field.label, max: rule.max } }
|
|
if (Array.isArray(value) && value.length > rule.max) return { t: 'shrimp.fieldMaxLength', params: { field: field.label, max: rule.max } }
|
|
}
|
|
|
|
if (rule.pattern) {
|
|
const regex = new RegExp(rule.pattern)
|
|
if (!regex.test(String(value))) return msg
|
|
}
|
|
}
|
|
}
|
|
|
|
return null
|
|
}
|
|
|
|
private async prepareAddItem(item: Record<string, any>): Promise<Record<string, any>> {
|
|
const result = { ...item }
|
|
for (const field of this.fields) {
|
|
if (field.editable === "readonly") {
|
|
delete result[field.key]
|
|
continue
|
|
}
|
|
|
|
const handler = this.getTypeHandler(field)
|
|
|
|
if (result[field.key] === undefined) {
|
|
const dv = field.defaultValue !== undefined ? field.defaultValue : handler?.defaultValue
|
|
if (dv !== undefined) {
|
|
result[field.key] = typeof dv === 'function' ? (dv as () => any)() : dv
|
|
}
|
|
}
|
|
|
|
if (handler?.reqFilter && result[field.key] !== undefined) {
|
|
result[field.key] = await handler.reqFilter(result[field.key], field, result)
|
|
}
|
|
|
|
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)
|
|
if (err) throw new ShrimpError(err)
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
private async prepareEditItem(item: Record<string, any>): Promise<Record<string, any>> {
|
|
const result = { ...item }
|
|
for (const field of this.fields) {
|
|
if (field.editable === "readonly" || field.editable === "addOnly") {
|
|
delete result[field.key]
|
|
continue
|
|
}
|
|
|
|
if (result[field.key] === undefined) {
|
|
delete result[field.key]
|
|
continue
|
|
}
|
|
|
|
const handler = this.getTypeHandler(field)
|
|
|
|
if (handler?.reqFilter) {
|
|
result[field.key] = await handler.reqFilter(result[field.key], field, result)
|
|
}
|
|
|
|
await this.validateNode(field, result[field.key], [field.key], result)
|
|
|
|
if (handler?.validate) {
|
|
const err = await handler.validate(result[field.key], field, result)
|
|
if (err) throw new ShrimpError(err)
|
|
}
|
|
}
|
|
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)
|
|
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) {
|
|
const handler = this.getTypeHandler(field)
|
|
if (handler?.resFilter && result[field.key] !== undefined) {
|
|
result[field.key] = await handler.resFilter(result[field.key], field, result)
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
private async prepareResponseItems(items: any[]): Promise<any[]> {
|
|
if (items.length === 0) return items
|
|
return Promise.all(items.map(item => this.processResItem(item)))
|
|
}
|
|
|
|
private async prepareResponseItem(item: any): Promise<any> {
|
|
if (!item) return item
|
|
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 }
|
|
for (const key of keys) {
|
|
delete result[key]
|
|
}
|
|
return result
|
|
}
|
|
|
|
private filterHidden(item: any): any {
|
|
// __v 是 mongoose 版本键,前端无用,统一剥掉(连同 hidden 字段)
|
|
return this.excludeKeys(item, ["__v", ...this.fields.filter(f => f.hidden).map(f => f.key)])
|
|
}
|
|
|
|
/** 递归收集标了 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 buildQuery(query?: IPageQuery & any): IShrimpQuery {
|
|
const { keyword, page, pageSize, sortBy, sortOrder, filter, ownerId } = query || {}
|
|
const p = page || 1
|
|
const ps = pageSize || ShrimpConfig.defaultPageSize
|
|
const shrimpQuery: IShrimpQuery = {
|
|
offset: (p - 1) * ps,
|
|
limit: ps,
|
|
}
|
|
|
|
if (filter && typeof filter === 'object') {
|
|
// 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
|
|
if (Array.isArray(value) && value.length === 0) continue
|
|
if (typeof value === 'string' && value === '') continue
|
|
const obj = value as any
|
|
if (typeof obj === 'object' && !Array.isArray(obj) && obj.min === undefined && obj.max === undefined) continue
|
|
|
|
if (Array.isArray(value)) {
|
|
where[key] = { in: value }
|
|
} else if (typeof value === 'object') {
|
|
const cond: IShrimpCondition = {}
|
|
if (obj.min !== undefined) cond.gte = obj.min
|
|
if (obj.max !== undefined) cond.lte = obj.max
|
|
where[key] = cond
|
|
} else if (typeof value === 'boolean') {
|
|
where[key] = { eq: value }
|
|
} else if (typeof value === 'string' && stringLikeTypes.has(field.type)) {
|
|
where[key] = { like: value }
|
|
} else {
|
|
where[key] = { eq: value }
|
|
}
|
|
}
|
|
if (Object.keys(where).length > 0) shrimpQuery.where = where
|
|
}
|
|
|
|
// 子表场景: ownerId + ownerKey 强制注入过滤
|
|
if (ownerId != null && ownerId !== '' && this.ownerKey) {
|
|
shrimpQuery.where = shrimpQuery.where || {}
|
|
shrimpQuery.where[this.ownerKey] = { eq: ownerId }
|
|
}
|
|
|
|
if (keyword) {
|
|
const stringLikeTypes = new Set(['string', 'textarea'])
|
|
const searchFields = this.fields
|
|
.filter(f => f.searchable && stringLikeTypes.has(f.type))
|
|
.map(f => f.key)
|
|
if (searchFields.length > 0) {
|
|
shrimpQuery.orWhere = searchFields.map(key => ({ [key]: { like: keyword } }))
|
|
}
|
|
}
|
|
|
|
if (sortBy) {
|
|
shrimpQuery.order = [[sortBy, sortOrder === 'desc' ? 'desc' : 'asc']]
|
|
}
|
|
|
|
return shrimpQuery
|
|
}
|
|
|
|
async list(query?: IPageQuery & any): Promise<IPageResult<T>> {
|
|
const shrimpQuery = this.buildQuery(query)
|
|
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
|
|
}
|
|
|
|
async get(id: string): Promise<T | null> {
|
|
const item = await this.dataSource.get(id)
|
|
if (item) {
|
|
const processed = await this.prepareResponseItem(item)
|
|
return this.filterHidden(processed) as T
|
|
}
|
|
return item
|
|
}
|
|
|
|
async add(item: Omit<T, '_id'>, ownerId?: string): Promise<T> {
|
|
if (ownerId && this.ownerKey) {
|
|
(item as any)[this.ownerKey] = ownerId
|
|
}
|
|
const prepared = await this.prepareAddItem(item as any)
|
|
const result = await this.dataSource.add(prepared as Omit<T, '_id'>)
|
|
const processed = await this.prepareResponseItem(result)
|
|
return this.filterHidden(processed) as T
|
|
}
|
|
async edit(id: string, item: Partial<T>): Promise<T | null> {
|
|
const prepared = await this.prepareEditItem(item as any)
|
|
const result = await this.dataSource.edit(id, prepared as Partial<T>)
|
|
if (result) {
|
|
const processed = await this.prepareResponseItem(result)
|
|
return this.filterHidden(processed) as T
|
|
}
|
|
return result
|
|
}
|
|
async delete(id: string): Promise<boolean> {
|
|
return await this.dataSource.delete(id)
|
|
}
|
|
async clear(query?: IPageQuery & any): Promise<boolean> {
|
|
const hasFilter = query && (
|
|
(query.keyword) ||
|
|
(query.filter && typeof query.filter === 'object' && Object.keys(query.filter).length > 0)
|
|
)
|
|
if (hasFilter) {
|
|
const shrimpQuery = this.buildQuery(query)
|
|
delete shrimpQuery.offset
|
|
delete shrimpQuery.limit
|
|
const result = await this.dataSource.list(shrimpQuery)
|
|
for (const item of result.list as any[]) {
|
|
await this.dataSource.delete(item._id)
|
|
}
|
|
return true
|
|
}
|
|
return await this.dataSource.clear()
|
|
}
|
|
async export(query?: IPageQuery & any): Promise<T[]> {
|
|
const shrimpQuery = this.buildQuery(query)
|
|
delete shrimpQuery.offset
|
|
delete shrimpQuery.limit
|
|
const result = await this.dataSource.list(shrimpQuery)
|
|
result.list = await this.prepareResponseItems(result.list)
|
|
// 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
|
|
for (const raw of data) {
|
|
const item = { ...raw } as Record<string, any>
|
|
if (ownerId && this.ownerKey) item[this.ownerKey] = ownerId
|
|
const id = item._id ? String(item._id) : undefined
|
|
if (id) {
|
|
const existing = await this.dataSource.get(id)
|
|
if (existing) {
|
|
const prepared = await this.prepareEditItem(item)
|
|
await this.dataSource.edit(id, prepared as Partial<T>)
|
|
continue
|
|
}
|
|
}
|
|
const prepared = await this.prepareAddItem(item)
|
|
await this.dataSource.add(prepared as Omit<T, '_id'>)
|
|
}
|
|
}
|
|
|
|
initCrud() {
|
|
return this
|
|
.addEvent("list", this.power)
|
|
.addEvent("get", this.power)
|
|
.addEvent("add", { t: "power.add" })
|
|
.addEvent("edit", { t: "power.edit" })
|
|
.addEvent("delete", { t: "power.delete" })
|
|
}
|
|
}
|