@jd-data-limited/easy-fm
Version:
easy-fm is a Node.js module that allows you to interact with a [FileMaker database stored](https://www.claris.com/filemaker/) on a [FileMaker server](https://www.claris.com/filemaker/server/). This module interacts with your server using the [FileMaker
58 lines (57 loc) • 2.03 kB
JavaScript
/*
* Copyright (c) 2023-2024. See LICENSE file for more information
*/
import { LayoutRecordManager } from './layoutRecordManager.js';
import { FMError } from '../FMError.js';
export class Layout {
database;
name;
records = new LayoutRecordManager(this);
metadata = null;
constructor(database, name) {
this.database = database;
this.name = name;
}
get endpoint() {
return `${this.database.endpoint}/layouts/${this.name}`;
}
/**
* Executes a FileMaker script on this layout asynchronously and returns the result.
* @param {Script} script - The script to be executed.
* @returns {Promise<ScriptResult>} - A promise that resolves to the script result or rejects with an error.
*/
async runScript(script) {
let url = `${this.endpoint}/script/${encodeURIComponent(script.name)}`;
if (script.parameter)
url += '?script.param=' + encodeURIComponent(script.parameter);
const res = await this.database._apiRequestJSON(url, {
method: 'GET'
});
if (res.response && res.messages[0].code === '0') {
const error = parseInt(res.response.scriptError);
return {
scriptError: error ? new FMError(error, 200, res) : undefined,
scriptResult: res.response.scriptResult
};
}
else {
throw new FMError(res.messages[0].code, res.httpStatus, res);
}
}
/**
* Retrieves the layout metadata
*
* @returns {Promise<ApiLayoutMetadata>} The layout metadata.
* @throws {FMError} If an error occurs during the API request.
*/
async getLayoutMeta() {
if (this.metadata) {
return this.metadata;
}
const res = await this.database._apiRequestJSON(this.endpoint);
if (!res.response)
throw new FMError(res.messages[0].code, res.httpStatus, res);
this.metadata = res.response;
return this.metadata;
}
}