All files / blong-gogo/src/adapter/schema/knex schemaMysql.ts

68.75% Statements 143/208
50% Branches 6/12
71.42% Functions 5/7
68.75% Lines 143/208

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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 2091x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x               1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                             1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                        
import {type Knex} from '@feasibleone/blong/types';
import {Type, type TFunction, type TSchema} from 'typebox';
import {attachHandlers} from './schemaTable.ts';
import {
    extractProcedureBody,
    methodId,
    normalizeSQL,
    readSqlFiles,
    snakeToCamel,
    sqlTypeToTypebox,
} from './utils.ts';
 
interface IParameter {
    PARAMETER_NAME: string;
    DATA_TYPE: string;
    PARAMETER_MODE: string;
}
 
function _dbName(knex: Knex): string | undefined {
    return (knex as unknown as {client?: {config?: {connection?: {database?: string}}}})?.client
        ?.config?.connection?.database;
}
 
async function _getParameters(knex: Knex, dbName: string, spName: string): Promise<IParameter[]> {
    return knex
        .select('PARAMETER_NAME', 'DATA_TYPE', 'PARAMETER_MODE')
        .from('information_schema.PARAMETERS')
        .where('SPECIFIC_SCHEMA', dbName)
        .where('SPECIFIC_NAME', spName)
        .whereNotNull('PARAMETER_NAME')
        .orderBy('ORDINAL_POSITION');
}
 
function _makeCallable(
    knex: Knex,
    spName: string,
    inParams: IParameter[],
): (params: Record<string, unknown>) => Promise<unknown> {
    return async (params: Record<string, unknown>) => {
        const placeholders = inParams.map(() => '?').join(', ');
        const values = inParams.map(p => params[snakeToCamel(p.PARAMETER_NAME)]);
        const result = await knex.raw(`CALL \`${spName}\`(${placeholders})`, values as string[]);
        return Array.isArray(result?.[0]) ? result[0][0] : result?.[0];
    };
}
 
/**
 * Synchronise stored procedures to the database.
 *
 * Accepts either a directory path (all `.sql` files are loaded) or an array of
 * `{name, sql}` objects.
 *
 * **Only procedures whose body differs from the current definition are
 * re-created** — so repeated startups with an unchanged schema are cheap.
 *
 * Procedures whose name starts with `_` are synced as normal — the underscore
 * prefix is a convention that marks them as private DB helpers.  They will be
 * synced but will **not** be bound as synthetic API handlers.
 */
export async function schemaProcedureSyncImpl(
    knex: Knex,
    procedures: string | Array<{name: string; sql: string}>,
): Promise<{created: string[]; skipped: string[]}> {
    const dbName = _dbName(knex);
    let definitions: Array<{name: string; sql: string}>;
    if (typeof procedures === 'string') {
        definitions = readSqlFiles(procedures);
    } else {
        definitions = procedures;
    }
    const created: string[] = [];
    const skipped: string[] = [];
    for (const {name, sql} of definitions) {
        if (dbName) {
            const existing: Array<{ROUTINE_DEFINITION: string}> = await knex
                .select('ROUTINE_DEFINITION')
                .from('information_schema.ROUTINES')
                .where('ROUTINE_SCHEMA', dbName)
                .where('ROUTINE_NAME', name)
                .where('ROUTINE_TYPE', 'PROCEDURE');
            if (existing.length > 0 && existing[0].ROUTINE_DEFINITION) {
                const storedNorm = normalizeSQL(existing[0].ROUTINE_DEFINITION);
                const newNorm = normalizeSQL(extractProcedureBody(sql));
                if (storedNorm === newNorm) {
                    skipped.push(name);
                    continue;
                }
            }
        }
        await knex.raw('DROP PROCEDURE IF EXISTS ??', [name]);
        await knex.raw(sql);
        created.push(name);
    }
    return {created, skipped};
}
 
/**
 * Discover stored procedures whose names match `namespace` in the database and
 * generate a callable handler function and TypeBox schema for each.
 *
 * Procedures starting with `_` are intentionally excluded — they are private
 * DB helpers not intended to be exposed via the API layer.
 *
 * Input parameter types are derived from `information_schema.PARAMETERS`.
 */
export async function schemaProcedureBindImpl(
    knex: Knex,
    namespace: string,
    schema?: string,
): Promise<{
    handlers: Record<string, (params: Record<string, unknown>) => Promise<unknown>>;
    schemas: Record<string, TFunction>;
}> {
    const dbName =
        schema ??
        (knex as unknown as {client?: {config?: {connection?: {database?: string}}}})?.client
            ?.config?.connection?.database;
    const procedures: Array<{ROUTINE_NAME: string}> = await knex
        .select('ROUTINE_NAME')
        .from('information_schema.ROUTINES')
        .where('ROUTINE_SCHEMA', dbName)
        .where('ROUTINE_TYPE', 'PROCEDURE')
        .andWhere('ROUTINE_NAME', 'like', `${namespace}%`);
    const handlers: Record<string, (params: Record<string, unknown>) => Promise<unknown>> = {};
    const schemas: Record<string, TFunction> = {};
    for (const proc of procedures) {
        const spName = proc.ROUTINE_NAME;
        if (spName.startsWith('_')) continue;
        const handlerName = snakeToCamel(spName);
        const parameters = await _getParameters(knex, dbName!, spName);
        const inParams = parameters.filter(
            p => p.PARAMETER_MODE === 'IN' || p.PARAMETER_MODE === 'INOUT',
        );
        handlers[handlerName] = _makeCallable(knex, spName, inParams);
        const paramProperties: Record<string, TSchema> = {};
        for (const p of inParams)
            paramProperties[snakeToCamel(p.PARAMETER_NAME)] = sqlTypeToTypebox(p.DATA_TYPE);
        schemas[handlerName] = Type.Function(
            [Type.Object(paramProperties)],
            Type.Promise(Type.Unknown()),
        );
    }
    return {handlers, schemas};
}
 
/**
 * Discover **all** stored procedures in the connected database and wire each as
 * a synthetic own-property handler on the adapter object so that
 * `AdapterBase.findHandler` resolves them before falling back to `exec`.
 *
 * Each handler is stored under **two keys**:
 * - `methodId(camelName)` (e.g. `"sqlitemlistactive"`) — the normalised key
 *   used by `findHandler`.
 * - `camelName` (e.g. `"sqlItemListActive"`) — allows realm handler overrides
 *   to call the synthetic version via `super.sqlItemListActive(params, $meta)`.
 *
 * Procedures whose name starts with `_` are **skipped** — they are private DB
 * helpers intentionally excluded from the API surface.
 */
export async function bindSyntheticHandlers(
    self: Record<string, unknown>,
    knex: Knex,
): Promise<void> {
    const dbName = _dbName(knex);
    if (!dbName) return;
    const procedures: Array<{ROUTINE_NAME: string}> = await knex
        .select('ROUTINE_NAME')
        .from('information_schema.ROUTINES')
        .where('ROUTINE_SCHEMA', dbName)
        .where('ROUTINE_TYPE', 'PROCEDURE');
    for (const proc of procedures) {
        const spName = proc.ROUTINE_NAME;
        if (spName.startsWith('_')) continue;
        const camelName = snakeToCamel(spName);
        const parameters = await _getParameters(knex, dbName, spName);
        const inParams = parameters.filter(
            p => p.PARAMETER_MODE === 'IN' || p.PARAMETER_MODE === 'INOUT',
        );
        const callable = _makeCallable(knex, spName, inParams);
        self[methodId(camelName)] = callable;
        self[camelName] = callable;
    }
}
 
/**
 * Bind CRUD synthetic handlers for every table in `tables`, keyed by namespace.
 * Called from `ready()` when `config.namespace` is set.
 */
export async function bindSyntheticCrud(
    self: Record<string, unknown>,
    knex: Knex,
    namespace: string,
    tables: Array<{tableName: string; definition: import('typebox').TObject}>,
): Promise<void> {
    const {schemaCrudBindImpl} = await import('./schemaTable.ts');
    for (const {tableName, definition} of tables) {
        const objectName = snakeToCamel(tableName);
        const {handlers} = await schemaCrudBindImpl(
            knex,
            namespace,
            objectName,
            definition,
            [],
            tableName,
        );
        attachHandlers(self, handlers);
    }
}