@variablesoftware/mock-d1
Version:
🎛️🗂️🧠 Mock D1 Database implementation for testing Cloudflare Workers
65 lines (64 loc) • 2.68 kB
JavaScript
/**
* @file helpers/mockInjectTableRows.ts
* @description Test-only utility for injecting rows into a mock D1 database table.
* @warning This is for test/mocking purposes only and should not be used in production code.
*/
import { validateRowAgainstSchema, normalizeRowToSchema } from '../engine/tableUtils/schemaUtils.js';
import { log } from '@variablesoftware/logface';
/**
* Injects rows into a table in the mock D1 database (test helper only).
* Requires explicit columns (no inference from data).
* Throws if extra columns are present in injected rows.
* @param db - The database Map.
* @param tableName - The table name.
* @param columns - The table schema columns (explicit, never inferred).
* @param rows - The rows to inject.
*/
export function injectTableRows(db, tableName, columns, rows) {
if (process.env.DEBUG || process.env.MOCK_D1_DEBUG) {
log.debug('called', { tableName, columns, rows });
}
if (!tableName || typeof tableName !== 'string' || tableName.trim() === '') {
log.debug('invalid tableName', { tableName });
throw new Error('Table name must be a non-empty string');
}
if (!Array.isArray(rows)) {
log.debug('rows not array', { rows });
throw new Error('Rows must be an array');
}
if (!Array.isArray(columns) || columns.length === 0) {
log.debug('columns not array or empty', { columns });
throw new Error('Columns must be a non-empty array');
}
const normalizedTableName = tableName.toLowerCase();
let table = db.get(normalizedTableName);
if (!table) {
log.debug('creating new table', { normalizedTableName, columns });
db.set(normalizedTableName, {
rows: [],
columns,
});
table = db.get(normalizedTableName);
}
else {
// Overwrite columns if table exists and columns differ
if (JSON.stringify(table.columns) !== JSON.stringify(columns)) {
log.debug('overwriting table columns', { normalizedTableName, columns });
table.columns = columns;
table.rows = [];
}
}
const tableRows = db.get(normalizedTableName).rows;
if (rows.length === 0) {
log.debug('clearing table rows', { normalizedTableName });
db.set(normalizedTableName, { rows: [], columns });
return;
}
for (const row of rows) {
log.debug('validating and normalizing row', { row, columns });
validateRowAgainstSchema(columns, row);
const normalizedRow = normalizeRowToSchema(columns, row);
log.debug('normalized row', { normalizedRow });
tableRows.push(normalizedRow);
}
}