UNPKG

@proofkit/fmodata

Version:

FileMaker OData API client

190 lines (189 loc) 8.3 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); import { getDefaultSelect, getTableSchema, getTableColumns, getTableName, FMTable, isUsingEntityIds } from "../orm/table.js"; import { resolveTableId } from "./builders/table-utils.js"; import { CountBuilder } from "./count-builder.js"; import { DeleteBuilder } from "./delete-builder.js"; import { InsertBuilder } from "./insert-builder.js"; import { QueryBuilder } from "./query/query-builder.js"; import { RecordBuilder } from "./record-builder.js"; import { createClientRuntime } from "./runtime.js"; import { UpdateBuilder } from "./update-builder.js"; class EntitySet { constructor(config) { __publicField(this, "occurrence"); __publicField(this, "layer"); __publicField(this, "config"); __publicField(this, "logger"); __publicField(this, "database"); // Database instance for accessing occurrences __publicField(this, "isNavigateFromEntitySet"); __publicField(this, "navigateRelation"); __publicField(this, "navigateRelationEntityId"); __publicField(this, "navigateSourceTableName"); __publicField(this, "navigateSourceTableEntityId"); __publicField(this, "navigateBasePath"); // Full base path for chained navigations __publicField(this, "navigateBasePathEntityId"); this.occurrence = config.occurrence; this.database = config.database; const runtime = createClientRuntime(config.layer); this.layer = runtime.layer; this.config = runtime.config; this.logger = runtime.logger; } // Type-only method to help TypeScript infer the schema from table // biome-ignore lint/suspicious/noExplicitAny: Accepts any FMTable configuration static create(config) { return new EntitySet({ occurrence: config.occurrence, layer: config.layer, database: config.database }); } applyNavigationContext(builder) { if (this.isNavigateFromEntitySet && this.navigateRelation && this.navigateSourceTableName) { builder.navigation = { relation: this.navigateRelation, relationEntityId: this.navigateRelationEntityId, sourceTableName: this.navigateSourceTableName, sourceTableEntityId: this.navigateSourceTableEntityId, basePath: this.navigateBasePath, basePathEntityId: this.navigateBasePathEntityId }; } return builder; } list() { const builder = new QueryBuilder({ occurrence: this.occurrence, layer: this.layer }); if (this.occurrence) { const defaultSelectValue = getDefaultSelect(this.occurrence); getTableSchema(this.occurrence); if (defaultSelectValue === "schema") { const allColumns = getTableColumns(this.occurrence); const selectedBuilder = this.applyNavigationContext( this.config.includeSpecialColumns ? builder.select(allColumns, { ROWID: true, ROWMODID: true }) : builder.select(allColumns) ).top(1e3); return selectedBuilder; } if (typeof defaultSelectValue === "object") { const selectedBuilder = this.applyNavigationContext( builder.select(defaultSelectValue) ).top(1e3); return selectedBuilder; } } return this.applyNavigationContext(builder).top(1e3); } count() { const builder = new CountBuilder({ occurrence: this.occurrence, layer: this.layer }); return this.applyNavigationContext(builder); } get(locator) { const builder = new RecordBuilder({ occurrence: this.occurrence, layer: this.layer, recordLocator: locator }); if (this.occurrence) { const defaultSelectValue = getDefaultSelect(this.occurrence); getTableSchema(this.occurrence); if (defaultSelectValue === "schema") { const allColumns = getTableColumns(this.occurrence); const selectedBuilder = this.applyNavigationContext( this.config.includeSpecialColumns ? builder.select(allColumns, { ROWID: true, ROWMODID: true }) : builder.select(allColumns) ); return selectedBuilder; } if (typeof defaultSelectValue === "object" && defaultSelectValue !== null && !Array.isArray(defaultSelectValue)) { const selectedBuilder = this.applyNavigationContext( builder.select(defaultSelectValue) ); return selectedBuilder; } } return this.applyNavigationContext(builder); } // Implementation insert(data, options) { const returnPreference = (options == null ? void 0 : options.returnFullRecord) === false ? "minimal" : "representation"; return new InsertBuilder({ occurrence: this.occurrence, layer: this.layer, // biome-ignore lint/suspicious/noExplicitAny: Input type is validated/transformed at runtime data, // biome-ignore lint/suspicious/noExplicitAny: Type assertion for generic type parameter returnPreference }); } // Implementation update(data, options) { const returnPreference = (options == null ? void 0 : options.returnFullRecord) === true ? "representation" : "minimal"; return new UpdateBuilder({ occurrence: this.occurrence, layer: this.layer, // biome-ignore lint/suspicious/noExplicitAny: Input type is validated/transformed at runtime data, // biome-ignore lint/suspicious/noExplicitAny: Type assertion for generic type parameter returnPreference }); } delete() { return new DeleteBuilder({ occurrence: this.occurrence, layer: this.layer // biome-ignore lint/suspicious/noExplicitAny: Type assertion for complex generic return type }); } // Implementation // biome-ignore lint/suspicious/noExplicitAny: Accepts any FMTable configuration navigate(targetTable) { let relationName; relationName = getTableName(targetTable); if (this.occurrence && FMTable.Symbol.NavigationPaths in this.occurrence) { const navigationPaths = this.occurrence[FMTable.Symbol.NavigationPaths]; if (navigationPaths && !navigationPaths.includes(relationName)) { this.logger.warn( `Cannot navigate to "${relationName}". Valid navigation paths: ${navigationPaths.length > 0 ? navigationPaths.join(", ") : "none"}` ); } } const entitySet = new EntitySet({ occurrence: targetTable, layer: this.layer, database: this.database }); const relationEntityId = isUsingEntityIds(targetTable) ? resolveTableId(targetTable, relationName, true) : relationName; const sourceTableName = getTableName(this.occurrence); const sourceTableEntityId = isUsingEntityIds(this.occurrence) ? resolveTableId(this.occurrence, sourceTableName, true) : sourceTableName; entitySet.isNavigateFromEntitySet = true; entitySet.navigateRelation = relationName; entitySet.navigateRelationEntityId = relationEntityId; if (this.isNavigateFromEntitySet && this.navigateBasePath) { entitySet.navigateBasePath = `${this.navigateBasePath}/${this.navigateRelation}`; entitySet.navigateBasePathEntityId = `${this.navigateBasePathEntityId ?? this.navigateBasePath}/${this.navigateRelationEntityId ?? this.navigateRelation}`; entitySet.navigateSourceTableName = this.navigateSourceTableName; entitySet.navigateSourceTableEntityId = this.navigateSourceTableEntityId; } else if (this.isNavigateFromEntitySet && this.navigateRelation) { entitySet.navigateBasePath = `${this.navigateSourceTableName}/${this.navigateRelation}`; entitySet.navigateBasePathEntityId = `${this.navigateSourceTableEntityId ?? this.navigateSourceTableName}/${this.navigateRelationEntityId ?? this.navigateRelation}`; entitySet.navigateSourceTableName = this.navigateSourceTableName; entitySet.navigateSourceTableEntityId = this.navigateSourceTableEntityId; } else { entitySet.navigateSourceTableName = sourceTableName; entitySet.navigateSourceTableEntityId = sourceTableEntityId; } return entitySet; } } export { EntitySet }; //# sourceMappingURL=entity-set.js.map