All files / test/blong-int-adapter/mysql/adapter/sql sqlResourceSetup.ts

81.08% Statements 30/37
60% Branches 3/5
100% Functions 1/1
81.08% Lines 30/37

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 381x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 1x 1x 1x 1x               1x 1x 1x  
import {handler, type Knex} from '@feasibleone/blong';
 
/**
 * sqlResourceSetup — cleans the resource-backed / graph-edge test tables so
 * each `test.mysql.resource` run starts from a known state.
 *
 * Deletes in FK-safe order: graph edges first, then the entity rows, then the
 * `core_resource` rows created for the `sql.person` / `sql.team` types (their
 * PK is a FK → `core.resource`). The `core_type` rows are left in place —
 * `ensureType` in the adapter `add` recreates them idempotently.
 */
export default handler(
    () =>
        async function sqlResourceSetup(
            _params: Record<string, never>,
            _$meta: Record<string, unknown>,
        ): Promise<{cleaned: boolean}> {
            const qb: Knex | undefined = this.config?.context?.queryBuilder;
            if (!qb) return {cleaned: false};
            await qb('core_triple').where('predicateName', 'hasMember').del();
            for (const table of ['sql_team', 'sql_person', 'sql_ulid', 'sql_uuid']) {
                await qb(table).del();
            }
            const typeIds = await qb('core_type')
                .whereIn('typeAlias', ['sql.person', 'sql.team'])
                .select('typeId');
            if (typeIds.length) {
                await qb('core_resource')
                    .whereIn(
                        'typeId',
                        typeIds.map((r: {typeId: number}) => r.typeId),
                    )
                    .del();
            }
            return {cleaned: true};
        },
);