All files / routes/admin/taxonomy taxonomy-strategy-factory.ts

100% Statements 3/3
100% Branches 2/2
100% Functions 1/1
100% Lines 3/3

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                                                                          105x 9x       96x    
/**
 * Taxonomy Strategy Factory
 *
 * Provides the createStrategy (getStrategyForType) factory function that selects
 * the appropriate strategy implementation for a given taxonomy type.
 *
 * @module taxonomy/taxonomy-strategy-factory
 */
 
import type { SQLiteTableWithColumns } from "drizzle-orm/sqlite-core";
import * as schema from "../../../db/schema";
import { TAXONOMY_TABLES, type TaxonomyType } from "../taxonomy.config";
import { RoomTypesStrategy } from "./room-types-strategy";
import { StandardTaxonomyStrategy } from "./standard-taxonomy-strategy";
import type { TaxonomyStrategy } from "./taxonomy-strategy.types";
 
/**
 * Factory function to get the appropriate strategy for a given taxonomy type
 * @param type - The taxonomy type (e.g., 'businessTypes', 'roomTypes', 'cities')
 * @returns The appropriate strategy instance (RoomTypesStrategy for roomTypes, StandardTaxonomyStrategy for all others)
 *
 * @example
 * ```ts
 * const strategy = getStrategyForType('roomTypes');
 * const table = strategy.getTable(); // Returns roomTypes table
 * const pkField = strategy.getPrimaryKeyField(); // Returns table.code
 *
 * const strategy2 = getStrategyForType('businessTypes');
 * const table2 = strategy2.getTable(); // Returns businessTypes table
 * const pkField2 = strategy2.getPrimaryKeyField(); // Returns table.id
 * ```
 */
export function getStrategyForType(
	type: TaxonomyType,
	// biome-ignore lint/suspicious/noExplicitAny: Drizzle ORM generic table type requires any for dynamic column access
): TaxonomyStrategy<SQLiteTableWithColumns<any>> {
	// Special case: roomTypes uses code/displayName instead of id/name
	if (type === "roomTypes") {
		return new RoomTypesStrategy(schema.roomTypes);
	}
 
	// All other taxonomy types use the standard id/name structure
	return new StandardTaxonomyStrategy(TAXONOMY_TABLES[type]);
}