@msugiura/rawsql-prisma
Version:
Prisma integration for rawsql-ts - Dynamic SQL generation with type safety and hierarchical JSON serialization
187 lines • 6.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PrismaTableColumnResolver = void 0;
/**
* Prisma-specific table and column resolver
*
* This resolver uses Prisma schema information to resolve table and column names
* for SQL generation and validation.
*/
class PrismaTableColumnResolver {
constructor(schemaInfo) {
this.schemaInfo = schemaInfo;
}
/**
* Create a TableColumnResolver function for rawsql-ts
*
* @returns TableColumnResolver function
*/
createTableColumnResolver() {
return (tableName) => {
const columnNames = this.getColumnNames(tableName);
return columnNames || [];
};
}
/**
* Get all available table names
*
* @returns Array of table names
*/
getTableNames() {
return Object.values(this.schemaInfo.models).map(model => model.tableName);
}
/**
* Get all column names for a specific table
*
* @param tableName - The table name
* @returns Array of column names, or undefined if table not found
*/
getColumnNames(tableName) {
const model = this.findModelByTableName(tableName);
if (!model) {
return undefined;
}
return Object.values(model.fields).map(field => field.columnName);
}
/**
* Check if a table exists in the schema
*
* @param tableName - The table name to check
* @returns true if table exists, false otherwise
*/
hasTable(tableName) {
return this.findModelByTableName(tableName) !== undefined;
}
/**
* Check if a column exists in a specific table
*
* @param tableName - The table name
* @param columnName - The column name to check
* @returns true if column exists, false otherwise
*/
hasColumn(tableName, columnName) {
const model = this.findModelByTableName(tableName);
if (!model) {
return false;
}
return Object.values(model.fields).some(field => field.columnName === columnName);
}
/**
* Get the primary key columns for a table
*
* @param tableName - The table name
* @returns Array of primary key column names, or undefined if table not found
*/
getPrimaryKeyColumns(tableName) {
const model = this.findModelByTableName(tableName);
if (!model) {
return undefined;
}
return model.primaryKey.map(fieldName => {
const field = model.fields[fieldName];
return field ? field.columnName : fieldName;
});
}
/**
* Get the unique constraints for a table
*
* @param tableName - The table name
* @returns Array of unique constraint column arrays, or undefined if table not found
*/
getUniqueConstraints(tableName) {
const model = this.findModelByTableName(tableName);
if (!model) {
return undefined;
}
return model.uniqueConstraints.map(constraint => constraint.map(fieldName => {
const field = model.fields[fieldName];
return field ? field.columnName : fieldName;
}));
}
/**
* Get column type information
*
* @param tableName - The table name
* @param columnName - The column name
* @returns Column type information, or undefined if not found
*/
getColumnType(tableName, columnName) {
const model = this.findModelByTableName(tableName);
if (!model) {
return undefined;
}
const field = Object.values(model.fields).find(field => field.columnName === columnName);
return field === null || field === void 0 ? void 0 : field.type;
}
/**
* Check if a column is nullable
*
* @param tableName - The table name
* @param columnName - The column name
* @returns true if column is nullable, false if not nullable, undefined if not found
*/
isColumnNullable(tableName, columnName) {
const model = this.findModelByTableName(tableName);
if (!model) {
return undefined;
}
const field = Object.values(model.fields).find(field => field.columnName === columnName);
return field === null || field === void 0 ? void 0 : field.isOptional;
}
/**
* Get relation information for a table
*
* @param tableName - The table name
* @returns Array of relation information, or undefined if table not found
*/
getRelations(tableName) {
const model = this.findModelByTableName(tableName);
if (!model) {
return undefined;
}
return Object.values(model.relations).map(relation => {
const relatedModel = this.schemaInfo.models[relation.modelName];
return {
name: relation.name,
relatedTable: (relatedModel === null || relatedModel === void 0 ? void 0 : relatedModel.tableName) || relation.modelName,
type: relation.type,
foreignKeys: relation.foreignKeys.map(fieldName => {
const field = model.fields[fieldName];
return field ? field.columnName : fieldName;
}),
referencedKeys: relation.referencedFields.map(fieldName => {
const field = relatedModel === null || relatedModel === void 0 ? void 0 : relatedModel.fields[fieldName];
return field ? field.columnName : fieldName;
})
};
});
}
/**
* Find model by table name
*
* @param tableName - The table name to search for
* @returns Model information if found, undefined otherwise
*/
findModelByTableName(tableName) {
return Object.values(this.schemaInfo.models).find(model => model.tableName === tableName);
}
/**
* Get model information by model name
*
* @param modelName - The Prisma model name
* @returns Model information if found, undefined otherwise
*/
getModelInfo(modelName) {
return this.schemaInfo.models[modelName];
}
/**
* Get all model names
*
* @returns Array of model names
*/
getModelNames() {
return Object.keys(this.schemaInfo.models);
}
}
exports.PrismaTableColumnResolver = PrismaTableColumnResolver;
//# sourceMappingURL=PrismaTableColumnResolver.js.map