Page MenuHomeSealhub

crud.ts
No OneTemporary

import { Context } from "koa";
import { FormField, HTMLFunc, Mountable } from "@sealcode/sealgen";
import type Router from "@koa/router";
import { Collection, CollectionItem, ExtractFieldInput } from "sealious";
import CRUDListPage, {
ListFieldDisplayInfo,
ListFieldFilterInfo,
} from "./list.js";
import CRUDCreatePage from "./create.js";
import type { FormControl } from "@sealcode/sealgen/@types/src/forms/controls/controls.js";
type CRUDArguments<C extends Collection> = {
collection: C;
nice_collection_name: string;
fields_for_filters: ListFieldFilterInfo<C>[];
fields_for_display: ListFieldDisplayInfo<C>[];
html: HTMLFunc;
list_title?: string;
create_title?: string;
create_button_text?: string;
edit_button_text?: string;
delete_button_text?: string;
back_to_list_button_text?: string;
make_delete_question?: (item: CollectionItem<C>) => string;
edit_fields: Record<string, FormField>;
edit_controls: FormControl[];
form_value_to_sealious_value: {
[field in keyof C["fields"]]?: (
data: unknown
) => ExtractFieldInput<C["fields"][field]>;
};
};
export class CRUD<C extends Collection> extends Mountable {
collection: C;
constructor(public args: CRUDArguments<C>) {
super();
this.collection = args.collection;
}
async canAccess(ctx: Context) {
const policy = this.collection.getPolicy("list");
const response = await policy.check(ctx.$context);
return {
canAccess: response == null ? true : response?.allowed || false,
message: response?.reason || "",
};
}
mount(router: Router, base_url: string) {
const {
collection,
nice_collection_name,
list_title,
create_title,
create_button_text,
edit_button_text,
delete_button_text,
back_to_list_button_text,
make_delete_question,
html,
fields_for_display,
fields_for_filters,
edit_fields,
edit_controls,
form_value_to_sealious_value,
} = this.args;
new CRUDListPage({
collection,
fields_for_filters,
fields_for_display,
html,
title: list_title || nice_collection_name,
create_url: base_url + "create/",
edit_url: (id: string) => `${base_url}edit/${id}/`,
delete_url: (id: string) => `${base_url}delete/${id}/`,
create_button_text: create_button_text || "Create",
edit_button_text: edit_button_text || "Edit",
delete_button_text: delete_button_text || "Delete",
make_delete_question: make_delete_question,
}).mount(router, base_url);
new CRUDCreatePage({
collection,
fields: edit_fields,
controls: edit_controls,
create_title:
create_title ||
"Create " + (nice_collection_name || collection.name),
html,
base_url,
back_to_list_button_text: back_to_list_button_text || "Back",
form_value_to_sealious_value,
})
.mount(router, base_url + "create/")
.init();
}
}

File Metadata

Mime Type
text/x-java
Expires
Mon, Dec 23, 00:57 (4 h, 11 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
556731
Default Alt Text
crud.ts (2 KB)

Event Timeline