UNPKG

@proofkit/fmodata

Version:

FileMaker OData API client

142 lines (141 loc) 4.89 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 { Effect } from "effect"; import { requestFromService, runLayerOrThrow } from "../effect.js"; import { createClientRuntime } from "./runtime.js"; class SchemaManager { constructor(layer) { __publicField(this, "layer"); __publicField(this, "config"); const runtime = createClientRuntime(layer); this.layer = runtime.layer; this.config = runtime.config; } createTable(tableName, fields, options) { const pipeline = Effect.gen(this, function* () { return yield* requestFromService(`/${this.config.databaseName}/FileMaker_Tables`, { method: "POST", body: JSON.stringify({ tableName, fields: fields.map(SchemaManager.compileFieldDefinition) }), ...options }); }); return runLayerOrThrow(this.layer, pipeline, "fmodata.schema.createTable"); } addFields(tableName, fields, options) { const pipeline = Effect.gen(this, function* () { return yield* requestFromService(`/${this.config.databaseName}/FileMaker_Tables/${tableName}`, { method: "PATCH", body: JSON.stringify({ fields: fields.map(SchemaManager.compileFieldDefinition) }), ...options }); }); return runLayerOrThrow(this.layer, pipeline, "fmodata.schema.addFields"); } async deleteTable(tableName, options) { const pipeline = Effect.gen(this, function* () { return yield* requestFromService(`/${this.config.databaseName}/FileMaker_Tables/${tableName}`, { method: "DELETE", ...options }); }); await runLayerOrThrow(this.layer, pipeline, "fmodata.schema.deleteTable"); } async deleteField(tableName, fieldName, options) { const pipeline = Effect.gen(this, function* () { return yield* requestFromService(`/${this.config.databaseName}/FileMaker_Tables/${tableName}/${fieldName}`, { method: "DELETE", ...options }); }); await runLayerOrThrow(this.layer, pipeline, "fmodata.schema.deleteField"); } createIndex(tableName, fieldName, options) { const pipeline = Effect.gen(this, function* () { return yield* requestFromService( `/${this.config.databaseName}/FileMaker_Indexes/${tableName}`, { method: "POST", body: JSON.stringify({ indexName: fieldName }), ...options } ); }); return runLayerOrThrow(this.layer, pipeline, "fmodata.schema.createIndex"); } async deleteIndex(tableName, fieldName, options) { const pipeline = Effect.gen(this, function* () { return yield* requestFromService(`/${this.config.databaseName}/FileMaker_Indexes/${tableName}/${fieldName}`, { method: "DELETE", ...options }); }); await runLayerOrThrow(this.layer, pipeline, "fmodata.schema.deleteIndex"); } static compileFieldDefinition(field) { let type = field.type; const repetitions = field.repetitions; if (field.type === "string") { type = "varchar"; const stringField = field; if (stringField.maxLength !== void 0) { type += `(${stringField.maxLength})`; } } if (repetitions !== void 0) { type += `[${repetitions}]`; } const result = { name: field.name, type }; if (field.nullable !== void 0) { result.nullable = field.nullable; } if (field.primary !== void 0) { result.primary = field.primary; } if (field.unique !== void 0) { result.unique = field.unique; } if (field.global !== void 0) { result.global = field.global; } if (field.type === "string") { const stringField = field; if (stringField.default !== void 0) { result.default = stringField.default; } } else if (field.type === "date") { const dateField = field; if (dateField.default !== void 0) { result.default = dateField.default; } } else if (field.type === "time") { const timeField = field; if (timeField.default !== void 0) { result.default = timeField.default; } } else if (field.type === "timestamp") { const timestampField = field; if (timestampField.default !== void 0) { result.default = timestampField.default; } } else if (field.type === "container") { const containerField = field; if (containerField.externalSecurePath !== void 0) { result.externalSecurePath = containerField.externalSecurePath; } } return result; } } export { SchemaManager }; //# sourceMappingURL=schema-manager.js.map