UNPKG

@proofkit/fmodata

Version:
141 lines (140 loc) 5.13 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 { QueryBuilder } from "./query-builder.js"; import { RecordBuilder } from "./record-builder.js"; import { InsertBuilder } from "./insert-builder.js"; import { DeleteBuilder } from "./delete-builder.js"; import { UpdateBuilder } from "./update-builder.js"; class EntitySet { constructor(config) { __publicField(this, "occurrence"); __publicField(this, "tableName"); __publicField(this, "databaseName"); __publicField(this, "context"); __publicField(this, "database"); // Database instance for accessing occurrences __publicField(this, "isNavigateFromEntitySet"); __publicField(this, "navigateRelation"); __publicField(this, "navigateSourceTableName"); this.occurrence = config.occurrence; this.tableName = config.tableName; this.databaseName = config.databaseName; this.context = config.context; this.database = config.database; } // Type-only method to help TypeScript infer the schema from occurrence static create(config) { return new EntitySet({ occurrence: config.occurrence, tableName: config.tableName, databaseName: config.databaseName, context: config.context, database: config.database }); } list() { var _a; const builder = new QueryBuilder({ occurrence: this.occurrence, tableName: this.tableName, databaseName: this.databaseName, context: this.context, databaseUseEntityIds: ((_a = this.database) == null ? void 0 : _a.isUsingEntityIds()) ?? false }); if (this.occurrence) { const defaultSelect = this.occurrence.defaultSelect; if (defaultSelect === "schema") { const schema = this.occurrence.baseTable.schema; const fields = Object.keys(schema); const uniqueFields = [...new Set(fields)]; return builder.select(...uniqueFields).top(1e3); } else if (Array.isArray(defaultSelect)) { const uniqueFields = [ ...new Set(defaultSelect) ]; return builder.select(...uniqueFields).top(1e3); } } if (this.isNavigateFromEntitySet) { builder.isNavigate = true; builder.navigateRelation = this.navigateRelation; builder.navigateSourceTableName = this.navigateSourceTableName; } return builder.top(1e3); } get(id) { var _a; const builder = new RecordBuilder({ occurrence: this.occurrence, tableName: this.tableName, databaseName: this.databaseName, context: this.context, recordId: id, databaseUseEntityIds: ((_a = this.database) == null ? void 0 : _a.isUsingEntityIds()) ?? false }); if (this.isNavigateFromEntitySet) { builder.isNavigateFromEntitySet = true; builder.navigateRelation = this.navigateRelation; builder.navigateSourceTableName = this.navigateSourceTableName; } return builder; } // Implementation insert(data, options) { var _a; const returnPref = (options == null ? void 0 : options.returnFullRecord) === false ? "minimal" : "representation"; return new InsertBuilder({ occurrence: this.occurrence, tableName: this.tableName, databaseName: this.databaseName, context: this.context, data, returnPreference: returnPref, databaseUseEntityIds: ((_a = this.database) == null ? void 0 : _a.isUsingEntityIds()) ?? false }); } // Implementation update(data, options) { var _a; const returnPref = (options == null ? void 0 : options.returnFullRecord) === true ? "representation" : "minimal"; return new UpdateBuilder({ occurrence: this.occurrence, tableName: this.tableName, databaseName: this.databaseName, context: this.context, data, returnPreference: returnPref, databaseUseEntityIds: ((_a = this.database) == null ? void 0 : _a.isUsingEntityIds()) ?? false }); } delete() { var _a; return new DeleteBuilder({ occurrence: this.occurrence, tableName: this.tableName, databaseName: this.databaseName, context: this.context, databaseUseEntityIds: ((_a = this.database) == null ? void 0 : _a.isUsingEntityIds()) ?? false }); } // Implementation navigate(relationName) { var _a; const targetOccurrence = (_a = this.occurrence) == null ? void 0 : _a.navigation[relationName]; const entitySet = new EntitySet({ occurrence: targetOccurrence, tableName: (targetOccurrence == null ? void 0 : targetOccurrence.name) ?? relationName, databaseName: this.databaseName, context: this.context }); entitySet.isNavigateFromEntitySet = true; entitySet.navigateRelation = relationName; entitySet.navigateSourceTableName = this.tableName; return entitySet; } } export { EntitySet }; //# sourceMappingURL=entity-set.js.map