rawsql-ts
Version:
[beta]High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
133 lines (132 loc) • 4.44 kB
TypeScript
/**
* Schema Manager for rawsql-ts
* Provides unified schema definition and automatic conversion to various formats
* Eliminates code duplication and provides type-safe schema management
*/
import type { JsonMapping } from '../transformers/PostgresJsonQueryBuilder';
/**
* Database column metadata for schema mapping
*/
export interface ColumnDefinition {
/** Column name in database */
name: string;
/** Primary key indicator - used for UPDATE/DELETE query WHERE conditions */
isPrimaryKey?: boolean;
/** Foreign key reference */
foreignKey?: {
table: string;
column: string;
};
/** Alias for JSON output (useful for avoiding conflicts) */
jsonAlias?: string;
}
/**
* Table relationship definition
*/
export interface RelationshipDefinition {
/** Type of relationship */
type: 'object' | 'array';
/** Target table name */
table: string;
/** Property name in JSON output */
propertyName: string;
/** Optional: Override target table's primary key */
targetKey?: string;
}
/**
* Complete table schema definition that users write
*/
export interface TableDefinition {
/** Table name in database */
name: string;
/** Human-readable entity name */
displayName?: string;
/** Column definitions */
columns: Record<string, ColumnDefinition>;
/** Relationships with other tables */
relationships?: RelationshipDefinition[];
}
/**
* Schema registry containing all table definitions
*/
export interface SchemaRegistry {
[tableName: string]: TableDefinition;
}
/**
* Central schema management utility for rawsql-ts
* Converts user-defined schemas to various internal formats
*/
export declare class SchemaManager {
private schemas;
constructor(schemas: SchemaRegistry);
/**
* Validate schema definitions for consistency
* Ensures each table has a primary key (required for UPDATE/DELETE operations)
* and validates relationship references
*/
private validateSchemas;
/**
* Get table column names for SqlParamInjector TableColumnResolver
* @param tableName Name of the table
* @returns Array of column names
*/
getTableColumns(tableName: string): string[];
/**
* Create TableColumnResolver function for SqlParamInjector
* @returns Function compatible with SqlParamInjector
*/
createTableColumnResolver(): (tableName: string) => string[];
/**
* Generate JSON mapping configuration for PostgresJsonQueryBuilder
* @param rootTableName Root table for the JSON structure
* @returns JSON mapping configuration
*/
createJsonMapping(rootTableName: string): JsonMapping;
/**
* Get all table names in the schema
* @returns Array of table names
*/
getTableNames(): string[];
/**
* Get table definition by name
* @param tableName Name of the table
* @returns Table definition or undefined
*/
getTable(tableName: string): TableDefinition | undefined;
/**
* Get primary key column name for a table
* Used by QueryBuilder.buildUpdateQuery for WHERE clause conditions
* @param tableName Name of the table
* @returns Primary key column name or undefined
*/
getPrimaryKey(tableName: string): string | undefined;
/**
* Get foreign key relationships for a table
* @param tableName Name of the table
* @returns Array of foreign key relationships
*/
getForeignKeys(tableName: string): Array<{
column: string;
referencedTable: string;
referencedColumn: string;
}>;
}
/**
* Create a SchemaManager instance from schema definitions
* @param schemas Schema registry object
* @returns SchemaManager instance
*/
export declare function createSchemaManager(schemas: SchemaRegistry): SchemaManager;
/**
* Create TableColumnResolver function from schema definitions
* @param schemas Schema registry object
* @returns TableColumnResolver function for SqlParamInjector
*/
export declare function createTableColumnResolver(schemas: SchemaRegistry): (tableName: string) => string[];
/**
* Create JSON mapping from schema definitions
* @param schemas Schema registry object
* @param rootTableName Root table name
* @returns JSON mapping for PostgresJsonQueryBuilder
*/
export declare function createJsonMappingFromSchema(schemas: SchemaRegistry, rootTableName: string): JsonMapping;