API 参考
createCrudActions API
TL;DR
一行创建标准 CRUD Actions,自动接好权限/日志:
const { getList, getDetail, create, update, delete: del } = createCrudActions(config)。
⚙️ 导出项
- createCrudActions(config, options?)
- createReadOnlyActions(config, options?)
- extendCrudActions(base, extensions)
👀 快速上手
import { createCrudActions } from '@/lib/core/crud-helper'
export const {
getList: sysGetRoleList,
getDetail: sysGetRoleDetail,
create: sysCreateRole,
update: sysUpdateRole,
delete: sysDeleteRole,
batchDelete: sysBatchDeleteRole,
} = createCrudActions({
modelName: 'role',
tableName: 'roles', // selects 连表必填
primaryKey: 'id',
softDelete: true,
fields: {
creatable: ['name', 'remark'],
updatable: ['name', 'remark'],
searchable: ['name'],
},
query: {
defaultSort: { createdAt: 'desc' },
defaultPageSize: 20,
foreignDB: [], // 需要连表时配置
},
validation: { // auto-schema 校验
name: { required: true, minLength: 2, maxLength: 50 },
},
uniqueFields: ['name'],
})🔧 参数
createCrudActions(
config: {
modelName: string
tableName?: string // selects 查询必填
primaryKey?: string // 默认 'id'
softDelete?: boolean // 默认 true
requireAdmin?: boolean // 仅 admin 可用
fields?: {
creatable?: string[]
updatable?: string[]
searchable?: string[]
}
query?: {
defaultSort?: Record<string,'asc'|'desc'>
defaultPageSize?: number
baseFilter?: object
include?: object // Prisma include
foreignDB?: any[] // selects 连表
}
validation?: Record<string, any> // auto-schema 规则
uniqueFields?: string[]
hooks?: Record<string, Function>
transforms?: { input?: (d)=>any, output?: (r)=>any }
},
options?: { prefix?: 'sys'|'auth'|'pub', enableBatch?: boolean }
): {
getList, getDetail, create, update, delete, batchUpdate?, batchDelete?, _dao
}📌 行为
- 生成的函数名包含前缀(默认
sys*),即自动进入后台权限与 RBAC 检查; - getDetail 默认使用
config.query.foreignDB连表; - create/update/delete 自动记录操作日志;
- 批量操作由
options.enableBatch控制(默认 true)。
🧪 只读 Actions
import { createReadOnlyActions } from '@/lib/core/crud-helper'
export const { getList, getDetail } = createReadOnlyActions({
modelName: 'actionLog',
query: { defaultSort: { createdAt: 'desc' } },
})