API Reference

BaseDAO API

TL;DR

Universal data access object based on Prisma: unified permission checks, validation, hooks, soft delete, and selects joins.


๐Ÿ‘€ Usage Overview

import { BaseDAO } from '@/app/(admin)/actions/dao/base'

const dao = new BaseDAO({
  modelName: 'role',
  tableName: 'roles',                 // Required for selects joins
  primaryKey: 'id',
  softDelete: true,
  fields: {
    creatable: ['name', 'remark'],
    updatable: ['name', 'remark'],
    searchable: ['name'],
  },
  query: { defaultSort: { createdAt: 'desc' }, defaultPageSize: 20 },
  validation: {
    name: { required: true, minLength: 2, maxLength: 50 },
  },
  uniqueFields: ['name'],
  hooks: {
    beforeCreate: async (data) => data,
    afterCreate: async (data, result) => {},
  },
  transforms: {
    input: (data) => data,
    output: (record) => record,
  },
})

๐Ÿ“ฆ Constructor Parameters

new BaseDAO({
  modelName: string,
  tableName?: string,
  primaryKey?: string,           // Default 'id'
  softDelete?: boolean,          // Default true
  requireAdmin?: boolean,
  fields?: { creatable?: string[]; updatable?: string[]; searchable?: string[] },
  query?: { defaultSort?: Record<string,'asc'|'desc'>; defaultPageSize?: number; baseFilter?: object; foreignDB?: any[] },
  validation?: Record<string, any>,
  uniqueFields?: string[],
  hooks?: Record<string, Function>,
  transforms?: { input?: (d)=>any; output?: (r)=>any },
})

๐Ÿงฉ Key Capabilities

  • Permissions: Default checks admin access; requires admin role when requireAdmin: true;
  • Validation: validation + uniqueFields + custom validators;
  • Soft Delete: When softDelete: true, deletedAt is automatically filtered and written;
  • Joins: Supports two query modes
    • Prisma include (native)
    • selects (tableName + foreignDB, including array field relations)
  • Transforms: transforms.input/output for input/output processing;
  • Hooks: before/afterCreate|Update|Delete|Batch*.

๐Ÿงช Common Methods

getList(params)
getDetail(id, options?)
create(data)
update(id, data)
delete(id)
batchUpdate(ids, data)
batchDelete(ids)
count(where?)
sum(field, where?)
max(field, where?)
min(field, where?)
avg(field, where?)
getAll(where?, orderBy?)

List Query (Auto Pagination)

Input parameters (excerpt):

  • pageIndex, pageSize
  • sortJson or sortArr
  • include (Prisma) / foreignDB (selects)
  • filters / whereJson / search

Returns: { success, data, total, pageIndex, pageSize, totalPages }

Detail Query

  • Prefer options.foreignDB for selects joins;
  • Otherwise use Prisma include;
  • Unified handling of Decimal/BigInt โ†’ number serialization.

โœ… Recommendations

  • Always set tableName when joins are needed;
  • Write search fields to fields.searchable to avoid complex where assembly;
  • All write operations should be whitelisted in fields.creatable/updatable;
  • Business validation goes in validation + hooks.

  • createCrudActions: Quickly export Actions with permissions based on DAO โ†’ api/CRUD_HELPER
  • wrapAction: Unified authentication and logging โ†’ api/WRAP_ACTION