@variablesoftware/mock-d1
Version:
🎛️🗂️🧠 Mock D1 Database implementation for testing Cloudflare Workers
54 lines (53 loc) • 2.04 kB
JavaScript
/**
* @file engine/tableUtils/tableLookup.ts
* @description Utilities for D1-like table and column lookup (case-insensitive, D1-compatible).
*/
import { log } from '@variablesoftware/logface';
/**
* Finds the canonical table key in the database Map, case-insensitively (D1-compatible).
* @param db - The database Map.
* @param tableName - The table name to look up.
* @returns The canonical table key if found, or undefined.
*/
export function findTableKey(db, tableName) {
if (process.env.DEBUG || process.env.MOCK_D1_DEBUG) {
log.debug('[findTableKey] called', { dbKeys: Array.from(db.keys()), tableName });
}
const lower = tableName.toLowerCase();
for (const key of db.keys()) {
if (key.toLowerCase() === lower) {
if (process.env.DEBUG || process.env.MOCK_D1_DEBUG) {
log.debug('[findTableKey] match', { key, tableName });
}
return key;
}
}
if (process.env.DEBUG || process.env.MOCK_D1_DEBUG) {
log.debug('[findTableKey] not found', { tableName });
}
return undefined;
}
/**
* Finds the canonical column key in a schema, case-insensitively (D1-compatible).
* @param columns - The schema columns array.
* @param columnName - The column name to look up.
* @returns The canonical column key if found, or undefined.
*/
export function findColumnKey(columns, columnName) {
if (process.env.DEBUG || process.env.MOCK_D1_DEBUG) {
log.debug('[findColumnKey] called', { columns, columnName });
}
const lower = columnName.toLowerCase();
for (const col of columns) {
if ((col.quoted ? col.name : col.name.toLowerCase()) === lower) {
log.debug('[findColumnKey] match', { col, columnName });
return col.name;
}
if (col.name.toLowerCase() === lower) {
log.debug('[findColumnKey] match', { col, columnName });
return col.name;
}
}
log.debug('[findColumnKey] not found', { columnName });
return undefined;
}