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

100% Statements 129/129
95% Branches 19/20
100% Functions 7/7
100% Lines 129/129

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 1301x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 8x 8x 8x 8x 2x 8x 2x 2x 2x 2x 2x 2x 8x 4x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12x 12x 1x 4x 4x 1x 11x 11x 6x 6x 6x 6x 6x 4x 4x 6x 4x 4x 3x 4x 3x 3x 1x 1x 1x 1x 1x  
import {
    checkSubject,
    type PrimitiveContext,
    type PrimitiveDescriptor,
    type PrimitiveFile,
} from '../engine.ts';
import {insertMapEntry} from '../merge.ts';
import {readGenerated} from './compose.ts';
 
/** `schema` — declarative tables, their registration order, and procedures. */
 
/** The TypeBox table factory for one entity. */
function schemaTableSource(subject: string, object: string): string {
    return `import {schema} from '@feasibleone/blong';
 
/**
 * ${subject}.${object} table.
 *
 * Every file under meta/type/ contributes to schema[${subject}], so this file can
 * sit alongside schema.ts without displacing the tables declared there.
 *
 * Constraint shapes are objects keyed by column (\`unique: {name: {}}\`), not
 * arrays — the array form silently produces an index over a column named '0'.
 */
export default schema(async ({lib: {type}}) => ({
    ${object}: type.Object(
        {
            ${object}Id: type.increment(),
            ${object}Name: type.stringNotNull({maxLength: 50}),
            ${object}Status: type.stringNotNull({maxLength: 20}),
            createdAt: type.dateTimeNull(),
        },
        {
            constraints: {
                unique: {
                    ${object}Name: {},
                },
            },
        },
    ),
}));
`;
}
 
/** The table registry (`meta/db/db.ts`) — creation order for the whole realm. */
function schemaRegisterSource(subject: string, object: string): string {
    return `import {handler} from '@feasibleone/blong';
 
const _schemaDir = new URL('./schema/', import.meta.url).pathname;
 
export default handler(() => ({
    config: {
        schema: {
            // Creation order: a table must come after the ones it references.
            tables: {'${subject}.${object}': 2},
            procedurePaths: [_schemaDir],
        },
    },
}));
`;
}
 
/** A stored-procedure skeleton for one entity. */
function schemaProcedureSource(subject: string, object: string): string {
    return `-- ${subject}.${object} helper procedure.
-- Drop the leading underscore from the filename to also bind it as an API method.
CREATE PROCEDURE ${subject}_${object}Refresh()
BEGIN
    SELECT 1;
END;
`;
}
 
/** The files a `schema` kind produces against an empty target. */
function schemaFiles(ctx: PrimitiveContext): PrimitiveFile[] {
    const {subject, object} = ctx;
    switch (ctx.kind) {
        case 'register':
            return [{path: 'meta/db/db.ts', content: schemaRegisterSource(subject, object)}];
        case 'procedure':
            return [
                {
                    path: `meta/db/schema/${object}.sql`,
                    content: schemaProcedureSource(subject, object),
                },
            ];
        default:
            return [{path: 'meta/type/schema.ts', content: schemaTableSource(subject, object)}];
    }
}
 
const schema: PrimitiveDescriptor = {
    id: 'schema',
    title: 'Database schema',
    skill: 'blong-schema',
    summary:
        'Declarative tables (meta/type/schema.ts), registration order (meta/db/db.ts) and ' +
        'stored procedures (meta/db/schema/*.sql).',
    kinds: ['table', 'register', 'procedure'],
    defaultKind: 'table',
    roots: ['meta/type', 'meta/db'],
    check(ctx) {
        return checkSubject(ctx.subject);
    },
    files(ctx) {
        return schemaFiles(ctx);
    },
    compose({host, root, context}) {
        const {subject, object} = context;
        if (context.kind === 'table') {
            // Every `meta/type/*.ts` in a realm merges into `schema[<realm>]`, so a
            // second file is additive — the tables declared in schema.ts survive.
            // Adding a table therefore never has to rewrite the shared file.
            const shared = readGenerated(host, root, 'meta/type/schema.ts');
            if (!shared) return schemaFiles(context);
            return [{path: `meta/type/${object}.ts`, content: schemaTableSource(subject, object)}];
        }
        if (context.kind === 'register') {
            const existing = readGenerated(host, root, 'meta/db/db.ts');
            if (!existing) return schemaFiles(context);
            const merged = insertMapEntry(existing, 'tables', `${subject}.${object}`, 2);
            if (merged === undefined) return schemaFiles(context);
            return [{path: 'meta/db/db.ts', content: merged}];
        }
        return schemaFiles(context);
    },
};
 
export default schema;