Skip to content

Commit

Permalink
getTableSchema exposed
Browse files Browse the repository at this point in the history
  • Loading branch information
shivajivarma committed Dec 17, 2023
1 parent c1e0d2f commit 2363705
Showing 1 changed file with 31 additions and 33 deletions.
64 changes: 31 additions & 33 deletions src/ORMConnection.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import { Logger } from "../deps.ts";
import {
DatabaseOperationContext,
TableDefinition,
TableDefinitionRaw,
UUID
} from "./types.ts";
import { DatabaseOperationContext, TableDefinition, TableDefinitionRaw, UUID } from "./types.ts";
import { UUIDUtils } from "./utils.ts";
import {
DatabaseConfiguration,
DatabaseConnection
} from "./core/connection/index.ts";
import { DatabaseConfiguration, DatabaseConnection } from "./core/connection/index.ts";
import Registry from "./core/Registry.ts";
import TableSchema from "./table/TableSchema.ts";
import DataType from "./data-types/DataType.ts";
Expand Down Expand Up @@ -47,6 +39,14 @@ export default class ORMConnection {
this.#operationInterceptorService = operationInterceptorService;
}

static generateRecordId(): UUID {
return UUIDUtils.generateId();
}

static validateRecordId(id: UUID): boolean {
return UUIDUtils.isValidId(id);
}

async connect(): Promise<void> {
return await this.#conn.connect();
}
Expand Down Expand Up @@ -74,13 +74,13 @@ export default class ORMConnection {
return result;
}

async defineTable(tableDefinition: TableDefinitionRaw | Function) {
if (typeof tableDefinition === "function") {
async defineTable(tableDefinitionRaw: TableDefinitionRaw | Function) {
if (typeof tableDefinitionRaw === "function") {
// @ts-ignore
tableDefinition = tableDefinition.__tableDefinition;
tableDefinitionRaw = tableDefinitionRaw.__tableDefinition;
}
const tableSchema = new TableSchema(
tableDefinition,
tableDefinitionRaw,
this.#fieldTypeRegistry,
this.#tableDefinitionRegistry
);
Expand Down Expand Up @@ -132,7 +132,7 @@ export default class ORMConnection {
} else {
const columns =
await reserved`SELECT column_name FROM information_schema.columns WHERE table_schema = ${tableSchema.getSchemaName()} AND table_name = ${tableSchema.getName()};`;
const columnNames = columns.map((column: any) => column.column_name);
const columnNames = columns.map((column: { column_name: string }) => column.column_name);
const newColumns = tableSchema
.getOwnColumnSchemas()
.filter((column) => !columnNames.includes(column.getName()));
Expand Down Expand Up @@ -170,21 +170,13 @@ export default class ORMConnection {
* @param context Context object
*/
table(nameWithSchema: string, context?: DatabaseOperationContext): Table {
const tableDefinitionStrict: TableDefinition | undefined =
this.#tableDefinitionRegistry.get(
TableSchema.getSchemaAndTableName(nameWithSchema)
);
if (typeof tableDefinitionStrict === "undefined") {
const tableSchema: TableSchema | undefined = this.getTableSchema(nameWithSchema);
if (typeof tableSchema === "undefined") {
throw new ORMError(
DatabaseErrorCode.SCHEMA_VALIDATION_ERROR,
`Table with name '${nameWithSchema}' is not defined`
);
}
const tableSchema = new TableSchema(
tableDefinitionStrict,
this.#fieldTypeRegistry,
this.#tableDefinitionRegistry
);
const queryBuilder = new Query(this.#conn.getNativeConnection());
return new Table(
queryBuilder,
Expand All @@ -196,6 +188,20 @@ export default class ORMConnection {
);
}

getTableSchema(tableName: string): TableSchema | undefined {
const tableDefinition: TableDefinition | undefined =
this.#tableDefinitionRegistry.get(
TableSchema.getSchemaAndTableName(tableName)
);
if (tableDefinition) {
return new TableSchema(
tableDefinition,
this.#fieldTypeRegistry,
this.#tableDefinitionRegistry
);
}
}

isTableDefined(tableName: string): boolean {
return this.#tableDefinitionRegistry.has(
TableSchema.getSchemaAndTableName(tableName)
Expand All @@ -212,14 +218,6 @@ export default class ORMConnection {
);
}

static generateRecordId(): UUID {
return UUIDUtils.generateId();
}

static validateRecordId(id: UUID): boolean {
return UUIDUtils.isValidId(id);
}

#getConnection(): DatabaseConnection {
if (!this.#conn) {
throw new ORMError(
Expand Down

0 comments on commit 2363705

Please sign in to comment.