@proofkit/fmodata
Version:
FileMaker OData API client
160 lines (159 loc) • 5.72 kB
JavaScript
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, runLayerResult } from "../effect.js";
import { getTableName } from "../orm/table.js";
import { getAcceptHeader } from "../types.js";
import { mergeMutationExecuteOptions, resolveMutationTableId, buildMutationUrl, extractAffectedRows } from "./builders/mutation-helpers.js";
import { normalizeDatabasePath } from "./database-name.js";
import { parseErrorResponse } from "./error-parser.js";
import { QueryBuilder } from "./query/query-builder.js";
import { createClientRuntime } from "./runtime.js";
class DeleteBuilder {
constructor(config) {
__publicField(this, "table");
__publicField(this, "layer");
__publicField(this, "config");
this.table = config.occurrence;
const runtime = createClientRuntime(config.layer);
this.layer = runtime.layer;
this.config = runtime.config;
}
/**
* Delete a single record by ID
*/
byId(id) {
return new ExecutableDeleteBuilder({
occurrence: this.table,
layer: this.layer,
mode: "byId",
recordLocator: id
});
}
/**
* Delete a single record by ROWID
*/
byRowId(rowId) {
return new ExecutableDeleteBuilder({
occurrence: this.table,
layer: this.layer,
mode: "byId",
recordLocator: { ROWID: rowId }
});
}
/**
* Delete records matching a filter query
* @param fn Callback that receives a QueryBuilder for building the filter
*/
where(fn) {
const queryBuilder = new QueryBuilder({
occurrence: this.table,
layer: this.layer
});
const configuredBuilder = fn(queryBuilder);
return new ExecutableDeleteBuilder({
occurrence: this.table,
layer: this.layer,
mode: "byFilter",
queryBuilder: configuredBuilder
});
}
}
class ExecutableDeleteBuilder {
constructor(config) {
__publicField(this, "table");
__publicField(this, "mode");
__publicField(this, "recordLocator");
__publicField(this, "queryBuilder");
__publicField(this, "layer");
__publicField(this, "config");
this.table = config.occurrence;
this.layer = config.layer;
this.mode = config.mode;
this.recordLocator = config.recordLocator;
this.queryBuilder = config.queryBuilder;
this.config = createClientRuntime(this.layer).config;
}
execute(options) {
const mergedOptions = mergeMutationExecuteOptions(
options,
this.config.useEntityIds,
this.config.includeSpecialColumns
);
const { method: _method, body: _body, ...requestOptions } = mergedOptions;
const useEntityIds = mergedOptions.useEntityIds ?? this.config.useEntityIds;
const tableId = resolveMutationTableId(this.table, useEntityIds, "ExecutableDeleteBuilder");
const url = buildMutationUrl({
databaseName: this.config.databaseName,
tableId,
tableName: getTableName(this.table),
mode: this.mode,
recordLocator: this.recordLocator,
queryBuilder: this.queryBuilder,
useEntityIds,
builderName: "ExecutableDeleteBuilder"
});
const pipeline = Effect.gen(this, function* () {
const response = yield* requestFromService(url, {
...requestOptions,
method: "DELETE"
});
const deletedCount = extractAffectedRows(response, void 0, 0, "deletedCount");
return { deletedCount };
});
return runLayerResult(this.layer, pipeline, "fmodata.delete", {
"fmodata.table": getTableName(this.table)
});
}
// biome-ignore lint/suspicious/noExplicitAny: Request body can be any JSON-serializable value
getRequestConfig() {
const tableId = resolveMutationTableId(this.table, this.config.useEntityIds, "ExecutableDeleteBuilder");
const url = buildMutationUrl({
databaseName: this.config.databaseName,
tableId,
tableName: getTableName(this.table),
mode: this.mode,
recordLocator: this.recordLocator,
queryBuilder: this.queryBuilder,
useEntityIds: this.config.useEntityIds,
builderName: "ExecutableDeleteBuilder"
});
return {
method: "DELETE",
url
};
}
toRequest(baseUrl, options) {
const config = this.getRequestConfig();
const fullUrl = `${baseUrl}${normalizeDatabasePath(config.url, {
normalizeDatabaseName: (options == null ? void 0 : options.normalizeDatabaseName) ?? this.config.normalizeDatabaseName
})}`;
return new Request(fullUrl, {
method: config.method,
headers: {
Accept: getAcceptHeader(options == null ? void 0 : options.includeODataAnnotations)
}
});
}
async processResponse(response, _options) {
if (!response.ok) {
const tableName = getTableName(this.table);
const error = await parseErrorResponse(response, response.url || `/${this.config.databaseName}/${tableName}`);
return { data: void 0, error };
}
const text = await response.text();
if (!text || text.trim() === "") {
const deletedCount2 = extractAffectedRows(void 0, response.headers, 1, "deletedCount");
return { data: { deletedCount: deletedCount2 }, error: void 0 };
}
const rawResponse = JSON.parse(text);
const deletedCount = extractAffectedRows(rawResponse, response.headers, 0, "deletedCount");
return { data: { deletedCount }, error: void 0 };
}
}
export {
DeleteBuilder,
ExecutableDeleteBuilder
};
//# sourceMappingURL=delete-builder.js.map