Page MenuHomeSealhub

list.ts
No OneTemporary

import { curryImportPath } from "../utils/import-path";
import prompts = require("prompts");
import { listCollections } from "../utils/list-collections";
export const listTemplate = async (
action_name: string,
newfilefullpath: string
): Promise<string> => {
const response = await prompts({
type: "autocomplete",
name: "collection",
message: "Which sealious collection do you want to list?",
choices: (
await listCollections()
).map((collection) => ({
title: collection,
value: collection,
})),
});
const collection = response.collection as string;
const uppercase_collection =
collection[0].toLocaleUpperCase() + collection.slice(1);
const rel = curryImportPath(newfilefullpath);
return `import { Context } from "koa";
import { CollectionItem } from "sealious";
import { tempstream } from "tempstream";
import { ${uppercase_collection} } from "${rel(
"src/back/collections/collections"
)}";
import html from "${rel("src/back/html.ts")}";
import { SealiousItemListPage, BaseListPageFields } from "@sealcode/sealgen";
export const actionName = "${action_name}";
const filterFields = {};
export default new (class ${action_name}Page extends SealiousItemListPage<
typeof ${uppercase_collection},
typeof BaseListPageFields
> {
fields = BaseListPageFields;
filterFields = filterFields;
filterControls = [];
async render(ctx: Context) {
return html(
ctx,
"${uppercase_collection}",
tempstream/* HTML */ \`<div>
<h2>${uppercase_collection} List</h2>
<table>
<thead>
<th>id</th>
\${Object.keys(${uppercase_collection}.fields).map(
(fieldname) => \`<th>\${fieldname}</th>\`
)}
</thead>
<tbody>
\${super.render(ctx)}
</tbody>
</table>
</div>\`
);
}
async renderItem(_: Context, item: CollectionItem<typeof ${uppercase_collection}>) {
return tempstream\`<tr><td>\${item.id}</td>\${Object.keys(${uppercase_collection}.fields).map(
(fieldname: keyof typeof ${uppercase_collection}["fields"]) =>
tempstream\`<td>\${item.get(fieldname)}</td>\`
)}</tr>\`;
}
})(${uppercase_collection});
`;
};

File Metadata

Mime Type
text/html
Expires
Wed, May 7, 19:50 (3 h, 14 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
628611
Default Alt Text
list.ts (2 KB)

Event Timeline