import _ from "lodash" import EntityRow from "./EntityRow" import { PromiseExprEvaluatorRow, Schema, Row } from "@mwater/expressions" /* Implements the type of row object required by mwater-expressions' PromiseExprEvaluator. Allows expressions to be evaluated on custom table rows */ export class CustomRow implements PromiseExprEvaluatorRow { /** Custom table id (e.g. custom.xyz.abc) */ tableId: string /** Custom row */ row: Row /** schema to use */ schema?: Schema /** looks up entity. Any callbacks after first one will be ignored. * called with an entity e.g. { _id: some id, a: "abc", b: 123 } or callback null if entity not found */ getEntityById: (entityType: string, entityId: string, callback: (entity: any) => void) => void /** Get a specific row of a custom table * @param tableId: id of the custom table e.g. custom.xyz.abc * @param rowId: id of the row */ getCustomTableRow: (tableId: string, rowId: string) => Promise // Create a custom row from a row object. // Options: // row: row to create for // schema: schema to use // getEntityById(entityType, entityId, callback): looks up entity. Any callbacks after first one will be ignored. // callback: called with an entity e.g. { _id: some id, a: "abc", b: 123 } or callback null if entity not found constructor(options: { /** Custom table id (e.g. custom.xyz.abc) */ tableId: string /** Custom table row */ row: Row /** schema to use */ schema?: Schema /** looks up entity. Any callbacks after first one will be ignored. * called with an entity e.g. { _id: some id, a: "abc", b: 123 } or callback null if entity not found */ getEntityById: (entityType: string, entityId: string, callback: (entity: any) => void) => void /** Get a specific row of a custom table */ getCustomTableRow: (tableId: string, rowId: string) => Promise }) { this.tableId = options.tableId this.row = options.row this.schema = options.schema this.getEntityById = options.getEntityById this.getCustomTableRow = options.getCustomTableRow } // Gets primary key of row getPrimaryKey() { return this.row._id } // Gets the value of a column async getField(columnId: string) { // Get column (gracefully handle if no schema) if (!this.schema) { return this.row[columnId] } // Get column const column = this.schema.getColumn(this.tableId, columnId) // Get value const value = this.row[columnId] // Handle case of column not found by just returning value if (!column) { return value } // Handle null case if (value == null) { return null } return value } async followJoin(columnId: string): Promise { // If schema is not available, cannot follow joins if (!this.schema) { return null } // Get column const column = this.schema.getColumn(this.tableId, columnId) // Handle case of column not found by just returning null if (!column) { return null } // Get value const value = this.row[columnId] if (value == null) { return null } if (column.type === "id[]") { if (!column.idTable) { throw new Error(`Cannot follow join for id[] column '${columnId}' in table '${this.tableId}': idTable is not defined in schema.`) } if (!Array.isArray(value)) { console.warn(`Expected array for id[] type column ${columnId} (table ${this.tableId}) but got ${typeof value}. Value:`, value) return [] // Return empty array for malformed data } if (value.length === 0) { return [] // No IDs to follow } if (column.idTable.match(/^entities./)) { const entityType = column.idTable.substr(9) const results = await Promise.all( value.map(async (id: any) => { if (id == null || typeof id !== 'string') { if (id != null) console.warn(`Invalid ID type in array for id[] column ${columnId} (table ${this.tableId}), ID: ${id}`) return null } const entity = await new Promise(resolve => this.getEntityById(entityType, id, resolve) ) if (entity) { return new EntityRow({ entityType: entityType, entity: entity, schema: this.schema!, getEntityById: this.getEntityById }) } return null }) ) return results.filter(r => r != null) as EntityRow[] } else if (column.idTable.match(/^custom./)) { const customTableId = column.idTable const results = await Promise.all( value.map(async (id: any) => { if (id == null || typeof id !== 'string') { if (id != null) console.warn(`Invalid ID type in array for id[] column ${columnId} (table ${this.tableId}), ID: ${id}`) return null } const customRowData = await this.getCustomTableRow(customTableId, id) if (customRowData) { return new CustomRow({ tableId: customTableId, row: customRowData, schema: this.schema!, getEntityById: this.getEntityById, getCustomTableRow: this.getCustomTableRow }) } return null }) ) return results.filter(r => r != null) as CustomRow[] } else { throw new Error(`Following id[] join for column ${columnId} in table ${this.tableId} to idTable '${column.idTable}' is not supported. Must be 'entities.*' or 'custom.*'.`) } } else if (column.type == "id") { if (!column.idTable) { throw new Error(`Cannot follow join for id column '${columnId}' in table '${this.tableId}': idTable is not defined in schema.`) } if (column.idTable.match(/^entities./)) { const entityType = column.idTable.substr(9) const entity = await new Promise(resolve => this.getEntityById(entityType, value, resolve) ) if (entity) { return new EntityRow({ entityType: entityType, entity: entity, schema: this.schema!, getEntityById: this.getEntityById }) } return null } else if (column.idTable.match(/^custom./)) { const customRow = await this.getCustomTableRow(column.idTable, value) if (customRow) { return new CustomRow({ tableId: column.idTable, row: customRow, schema: this.schema!, getEntityById: this.getEntityById, getCustomTableRow: this.getCustomTableRow }) } return null } else { throw new Error(`Following id join for column ${columnId} in table ${this.tableId} to idTable '${column.idTable}' is not supported. Must be 'entities.*' or 'custom.*'.`) } } else if (column.type == "join" && column.join && column.join.type == "n-1") { // Added check for column.join if (column.join.toTable.match(/^entities./)) { const entityType = column.join.toTable.substr(9) const entity = await new Promise(resolve => this.getEntityById(entityType, value, resolve) ) if (entity) { return new EntityRow({ entityType: entityType, entity: entity, schema: this.schema!, getEntityById: this.getEntityById }) } return null } else { throw new Error(`Following legacy 'join' n-1 type for column ${columnId} in table ${this.tableId} to non-entity table '${column.join.toTable}' not supported.`) } } throw new Error(`Cannot follow join on column '${columnId}' of type '${column.type}' in table '${this.tableId}'. Supported types are 'id', 'id[]', or legacy 'join' (n-1 to entities).`) } }