mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 20:37:21 +00:00
72 lines
1.8 KiB
TypeScript
72 lines
1.8 KiB
TypeScript
import { type Static, Type } from "core/utils";
|
|
import { Field, baseFieldConfigSchema } from "data";
|
|
|
|
export const mediaFieldConfigSchema = Type.Composite([
|
|
Type.Object({
|
|
entity: Type.String(), // @todo: is this really required?
|
|
min_items: Type.Optional(Type.Number()),
|
|
max_items: Type.Optional(Type.Number()),
|
|
mime_types: Type.Optional(Type.Array(Type.String())),
|
|
}),
|
|
baseFieldConfigSchema,
|
|
]);
|
|
|
|
export type MediaFieldConfig = Static<typeof mediaFieldConfigSchema>;
|
|
|
|
export type MediaItem = {
|
|
id: number;
|
|
path: string;
|
|
mime_type: string;
|
|
size: number;
|
|
scope: number;
|
|
etag: string;
|
|
modified_at: Date;
|
|
folder: boolean;
|
|
};
|
|
|
|
export class MediaField<
|
|
Required extends true | false = false,
|
|
TypeOverride = MediaItem[],
|
|
> extends Field<MediaFieldConfig, TypeOverride, Required> {
|
|
override readonly type = "media";
|
|
|
|
constructor(name: string, config: Partial<MediaFieldConfig>) {
|
|
// field must be virtual, as it doesn't store a reference to the entity
|
|
super(name, { ...config, fillable: ["update"], virtual: true });
|
|
}
|
|
|
|
protected getSchema() {
|
|
return mediaFieldConfigSchema;
|
|
}
|
|
|
|
getMaxItems(): number | undefined {
|
|
return this.config.max_items;
|
|
}
|
|
|
|
getMinItems(): number | undefined {
|
|
return this.config.min_items;
|
|
}
|
|
|
|
override schema() {
|
|
return undefined;
|
|
}
|
|
|
|
override toJsonSchema() {
|
|
// @todo: should be a variable, since media could be a diff entity
|
|
const $ref = "../schema.json#/properties/media";
|
|
const minItems = this.config?.min_items;
|
|
const maxItems = this.config?.max_items;
|
|
|
|
if (maxItems === 1) {
|
|
return { $ref } as any;
|
|
} else {
|
|
return {
|
|
type: "array",
|
|
items: { $ref },
|
|
minItems,
|
|
maxItems,
|
|
};
|
|
}
|
|
}
|
|
}
|