All files / core/blong-kukum/primitives model.ts

100% Statements 179/179
100% Branches 5/5
100% Functions 2/2
100% Lines 179/179

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 1801x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x  
import {capitalize, checkObject, checkSubject, type PrimitiveDescriptor} from '../engine.ts';
import {seedRowName} from './shared.ts';
 
/** `model` — the IModelSpec that drives the generated CRUD pages. */
 
const model: PrimitiveDescriptor = {
    id: 'model',
    title: 'Model spec / fixture',
    skill: 'blong-model',
    summary:
        'IModelSpec driving Browse/New/Open/Report pages (meta/model) and mock fixtures (meta/fixture).',
    kinds: ['model', 'fixture'],
    defaultKind: 'model',
    roots: ['meta/model', 'meta/fixture'],
    check(ctx) {
        return [...checkSubject(ctx.subject), ...checkObject(ctx.object)];
    },
    files(ctx) {
        const Entity = capitalize(ctx.object);
        /**
         * Matches the `blong-kopi` template naming (`$subject$ObjectModel.ts`), so
         * regenerating a model replaces the scaffolded one instead of leaving two
         * models claiming the same entity.
         */
        const Name = `${ctx.subject}${Entity}`;
        /**
         * The model system resolves `${...}` placeholders in toolbar params.
         * They are assembled here rather than written literally so the
generated
         * spec keeps them verbatim without template-literal escaping.
         */
        const current = '$' + '{current}';
        const idPlaceholder = '$' + '{' + ctx.object + 'Id}';
        // The scaffolding template ships this file too (`blong-kopi/meta/fixture/$subjectFixture.ts`),
        // and a scaffolded realm receives that copy verbatim. `engine.test.ts` compares the two
        // structurally — handler name, the keyed object, and the row fields this descriptor
        // declares — because the template's rows are its own sample data (they mirror its test
        // seed, and its Open story opens record 101); the comments differ on purpose.
        //
        // The rows carry the same ids and names as the seed template, plus a `(fixture)`
        // marker: the Storybook toolbar serves the same stories from either source, so the
        // Open story has to find its record in both, and the marker is what makes the
        // Backend item's effect visible.
        if (ctx.kind === 'fixture') {
            return [
                {
                    path: `meta/fixture/${ctx.subject}Fixture.ts`,
                    content: `import {handler} from '@feasibleone/blong';
 
/**
 * meta/fixture/${ctx.subject}Fixture.ts — sample rows for Storybook.
 *
 * A plain \`handler()\`, named ${ctx.subject}Fixture: the mock adapter matches
 * this folder's group (${ctx.subject}.fixture) and reads the data by calling
 * \`blong.handler['${ctx.subject}Fixture']\`. It must NOT use the \`fixture()\`
 * factory — that one describes mock OpenAPI subjects/dropdowns, a different
 * shape entirely.
 *
 * \`browser.ts\` has to glob \`meta/fixture/**/*.ts\`, or the handler never
 * reaches the adapter and every story renders empty.
 *
 * The rows mirror \`meta/dbTest/${ctx.subject}${Entity}Merge.yaml\` — same ids, same
 * names plus a \`(fixture)\` marker — because a story must load in both modes: this
 * file feeds the Storybook mock path and the seed feeds the database path, and the
 * marker is what makes the toolbar's Backend item visibly do something.
 */
export default handler(
    () =>
        async function ${ctx.subject}Fixture() {
            return {
                '${ctx.subject}.${ctx.object}': [
                    {
                        ${ctx.object}Id: 101,
                        ${ctx.object}Name: '${seedRowName(ctx.object)} (fixture)',
                        ${ctx.object}Status: 'draft',
                    },
                    {
                        ${ctx.object}Id: 102,
                        ${ctx.object}Name: 'Sample ${Entity} Two (fixture)',
                        ${ctx.object}Status: 'sent',
                    },
                ],
            };
        },
);
`,
                },
            ];
        }
        return [
            {
                path: `meta/model/${Name}Model.ts`,
                content: `import {model} from '@feasibleone/blong';
 
/**
 * meta/model/${Name}Model.ts — model spec for the \`${ctx.object}\` entity.
 *
 * Drives the auto-generated Browse/New/Open pages. \`public: true\` exposes the
 * standard CRUD operations on the gateway, validated by blong-server's
 * subject.validation — no manual gateway override is needed.
 *
 * The field names here must match meta/type/${ctx.subject}.ts: the Playwright
 * helpers drive this page by name, so a rename has to happen in both places.
 */
export default model(
    () =>
        async function ${Name}Model() {
            return {
                subject: '${ctx.subject}',
                object: '${ctx.object}',
                objectTitle: '${Entity}',
                public: true,
                nameField: '${ctx.object}.${ctx.object}Name',
                schema: {
                    properties: {
                        ${ctx.object}: {
                            properties: {
                                ${ctx.object}Name: {title: 'Name', filter: true, sort: true},
                                ${ctx.object}Status: {
                                    title: 'Status',
                                    widget: {
                                        options: [
                                            {value: 'draft', label: 'Draft'},
                                            {value: 'sent', label: 'Sent'},
                                            {value: 'paid', label: 'Paid'},
                                            {value: 'void', label: 'Void'},
                                        ],
                                    },
                                },
                            },
                            widget: {columns: ['${ctx.object}Name', '${ctx.object}Status']},
                        },
                    },
                },
                cards: {
                    browse: {label: '${Entity}s', widgets: ['${ctx.object}']},
                    edit: {
                        label: '${Entity} Details',
                        widgets: ['${ctx.object}.${ctx.object}Name', '${ctx.object}.${ctx.object}Status'],
                    },
                },
                browser: {
                    title: '${Entity}s',
                    icon: 'pi pi-file',
                    toolbar: [
                        {
                            label: 'Create',
                            icon: 'pi pi-plus',
                            action: 'component/${ctx.subject}.${ctx.object}.new',
                            permission: '${ctx.subject}${Entity}Add',
                        },
                        {
                            label: 'Edit',
                            icon: 'pi pi-pencil',
                            enabled: 'current',
                            method: 'component/${ctx.subject}.${ctx.object}.open',
                            params: '${current}',
                        },
                        {
                            label: 'Delete',
                            icon: 'pi pi-trash',
                            enabled: 'selected',
                            confirm: 'Delete selected ${ctx.object}?',
                            method: '${ctx.subject}.${ctx.object}.remove',
                            refresh: true,
                            params: {${ctx.object}Id: '${idPlaceholder}'},
                        },
                    ],
                },
            };
        },
);
`,
            },
        ];
    },
};
 
export default model;