Files
cnpmcore/app/core/util/EntityUtil.ts
fengmk2 ffe723e65f refactor: use oxlint instead of eslint (#772)
say goodbye to eslint
2025-03-13 23:31:13 +08:00

54 lines
1.3 KiB
TypeScript

import ObjectID from 'bson-objectid';
import { E400 } from 'egg-errors';
import type { EntityData } from '../entity/Entity.js';
type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
export type EasyData<T extends EntityData, Id extends keyof T> = PartialBy<
T,
'createdAt' | 'updatedAt' | Id
>;
const MAX_PAGE_SIZE = 100 as const;
export interface PageOptions {
pageSize?: number;
pageIndex?: number;
}
export interface PageResult<T> {
count: number;
data: Array<T>;
}
export interface PageLimitOptions {
offset: number;
limit: number;
}
export class EntityUtil {
static defaultData<T extends EntityData, Id extends keyof T>(
data: EasyData<T, Id>,
id: Id
): T {
Reflect.set(data, id, EntityUtil.createId());
data.createdAt = data.createdAt || new Date();
data.updatedAt = data.updatedAt || new Date();
return data as T;
}
static createId(): string {
// @ts-expect-error ObjectID has no construct signatures
return new ObjectID().toHexString();
}
static convertPageOptionsToLimitOption(page: PageOptions): PageLimitOptions {
const { pageIndex = 0, pageSize = 20 } = page;
if (pageSize > MAX_PAGE_SIZE) {
throw new E400(`max page size is 100, current request is ${pageSize}`);
}
return {
offset: pageIndex * pageSize,
limit: pageSize,
};
}
}