fms-api-client
v2.3.4
Published
A FileMaker Data API client designed to allow easier interaction with a FileMaker database from a web environment.
Downloads
183
Readme
fms-api-client
A FileMaker Data API client designed to allow easier interaction with a FileMaker database from a web environment. This client abstracts the FileMaker 17 & 18 Data API into class based methods.
Table of Contents
- License
- Installation
- Usage
- Tests
- Dependencies
- Dev Dependencies
License
MIT © Lui de la Parra
Installation
npm install --save fms-api-client
Usage
Introduction
The fms-api-client is a wrapper around the FileMaker Data API. Much :heart: to FileMaker for their work on the Data API. This client attempts to follow the terminology and conventions used by FileMaker wherever possible. The client uses a lightweight datastore to hold Data API connections. For more information about datastore configuration see the datastore connection section and the marpat project.
Each client committed to the datastore will automatically handle Data API Sessions. If required, clients can also manually open or close their FileMaker sessions by calling either the client.login()
method or the client.logout()
method. To remove a client from a datastore and log out a session call client.destroy()
. For more information on Data API Session handling see the Data API sessions section.
All client methods accept parameters as defined by FileMaker. The client also allows for an expanded parameter syntax designed to make interacting with the client easier. Scripts and portals can be defined as arrays. Find queries can be defined as either an object or an array. For more information on the syntax supported by the client see the parameter syntax section.
In addition to the expanded syntax the client will also automatically parse arrays, objects, and numbers to adhere to the requirements of the Data API. The limit
and offset
parameters can be either strings or a numbers. The client will also automatically convert limit
, find
, and offset
parameters into their underscored conterparts as needed. Additionally, if a script result is valid JSON it will be automatically parsed for you by the client.
All methods on the client return promises and each method will reject with a message and code upon encountering an error. All messages and codes follow the FileMaker Data API codes where applicable.
The client will queue concurrent requests to the Data API. The client can be configured with a concurrency setting. The client will attempt ensure that each request to the Data API is made with an valid token which is not in use. This behavior prevents request session collisions. For more information on Data API Session handling see the request queue section.
The client also provides utility modules to aid in working with FileMaker Data API Results. The provided utility modules are fieldData
, recordId
, containerData
, and transform
. These utilities will accept and return either an object or an array objects. For more information on the utility modules see the utilities section.
Parameter Syntax
The client supports the same parameter syntax as is found in the Data API Documentation. Where appropriate and useful the client also allows additional parameters.
Script Array Syntax
The custom script parameter follows the following syntax:
{
"scripts": [
{
"name": "At Mos Eisley",
"phase": "presort",
"param": "awesome bar"
},
{
"name": "First Shot",
"phase": "prerequest",
"param": "Han"
},
{
"name": "Moof Milker",
"param": "Greedo"
}
]
}
Following the Data API, the prerequest
phase occurs before executing request and sorting of records, the presort
phase after executing request and before sorting records. Not specifying a phase will run the script after the request and sorting are executed.
Note: The FileMaker script and portal syntax will override the alternative scripts and portals parameter syntax.
Portals Array Syntax
The custom portals parameter follows the following syntax:
{
"portals": [
{ "name": "planets", "limit": 1, "offset": 1 },
{ "name": "vehicles", "limit": 2 }
]
}
If a portals array is not used all portals on a queried layout will be returned.
Note: The FileMaker script and portal syntax will override the alternative scripts and portals parameter syntax.
Data Syntax
Arrays and objects are stringified before being inserted into field or portal data.
{
"data": {
"name": "Yoda",
"Vehicles::name": "The Force"
}
}
Any property not nested in a portalData
property will be moved into the fieldData
property.
Sort Syntax
The client accepts the same sort parameters as the Data API.
{
"sort": [
{ "fieldName": "name", "sortOrder": "ascend" },
{ "fieldName": "modificationTimestamp", "sortOrder": "descend" }
]
}
Query Syntax
When using the find
method a query is required. The query can either be a single json object or an array of json objects.
{
"query": [
{
"name": "Han solo",
"Vehicles::name": "Millenium Falcon"
},
{
"name": "Luke Skywalker",
"Vehicles::name": "X-Wing Starfighter"
},
{
"age": ">10000"
}
]
}
Datastore Connection
The connect method must be called before the FileMaker class is used. The connect method is not currently exposed by fms-api-client, but from the marpat dependency. marpat is a fork of camo. Thanks and love to Scott Robinson for his creation and maintenance of camo.
marpat is designed to allow the use of multiple datastores with the focus on encrypted file storage and project flexibility.
For more information on marpat and the different types of supported storage visit marpat
const { connect } = require('marpat');
connect('nedb://memory');
Excerpt from ./examples/index.js
Client Creation
After connecting to a datastore you can import and create clients. A client is created using the create method on the Filemaker class. The FileMaker class accepts an object with the following properties:
| Property | Type | Description |
| ------------- | :------------------: | :---------------------------------------------------------------------------------: |
| database | String | The FileMaker database to connect to |
| server | String | The FileMaker server to use as the host. Note: Must be an http or https Domain. |
| user | String | The FileMaker user account to be used when authenticating into the Data API |
| password | String | The FileMaker user account's password. |
| [name] | String | A name for the client. |
| [usage] | Boolean | Track Data API usage for this client. Note: Default is true
|
| [timeout] | Number | The default timeout time for requests Note: Default is 0, (no timeout) |
| [concurrency] | Number | The number of concurrent requests that will be made to FileMaker |
| [proxy] | Object | settings for a proxy server |
| [agent] | Object | settings for a custom request agent |
:warning: You should only use the agent parameter when absolutely necessary. The Data API was designed to be used on https. Deviating from the intended use should be done with caution.
const client = Filemaker.create({
name: process.env.CLIENT_NAME,
database: process.env.DATABASE,
concurrency: 3,
server: process.env.SERVER,
user: process.env.USERNAME,
password: process.env.PASSWORD,
usage: process.env.CLIENT_USAGE_TRACKING
});
Excerpt from ./examples/index.js
Note: The server must be an http or https domain.
A client can be used directly after saving it. The client.save()
method takes no arguments and will either reject with an error or resolve with a useable client. The client will automatically handle Data API session creation and expiration. Once a client is saved it will be stored on the datastore for reuse later.
return client.save();
})
.then(client => authentication(client))
.then(client => metadata(client))
.then(client => creates(client))
.then(client => duplicate(client))
.then(client => gets(client))
.then(client => lists(client))
.then(client => finds(client))
.then(client => edits(client))
.then(client => scripts(client))
.then(client => script(client))
.then(client => globals(client))
.then(client => deletes(client))
.then(client => uploads(client))
.then(client => utilities(client))
Excerpt from ./examples/index.js A client can be removed using either the
client.destroy()
method, theFilemaker.deleteOne(query)
method or theFilemaker.deleteMany(query)
method. Note Only theclient.destroy()
method will close a FileMaker session. Any client removed using the theFilemaker.deleteOne(query)
method or theFilemaker.deleteMany(query)
method will not log out before being destroyed.
Client Use
A client can be used after it is created and saved or recalled from the datastore. The Filemaker.find(query)
or Filemaker.findOne(query)
methods can be used to recall clients. The Filemaker.findOne(query)
method will return either one client or null. The Filemaker.find(query)
will return either an empty array or an array of clients. All public methods on the client return promises.
const createManyRecords = client =>
Promise.all([
client.create('Heroes', { name: 'Anakin Skywalker' }, { merge: true }),
client.create('Heroes', { name: 'Obi-Wan' }, { merge: true }),
client.create('Heroes', { name: 'Yoda' }, { merge: true })
]).then(result => log('create-many-records-example', result));
Excerpt from ./examples/create.examples.js
Results:
[
{
"name": "Anakin Skywalker",
"recordId": "1138",
"modId": "327"
},
{
"name": "Obi-Wan",
"recordId": "1138",
"modId": "327"
},
{
"name": "Yoda",
"recordId": "1138",
"modId": "327"
}
]
Request Queue
The client will automatically queue a requests to the FileMaker Data API if there are no available sessions to make the request. The client will ensure that a session is not in use or expired before it uses a session token in a request. This functionality is designed to prevent session collisions. The client will open and use a new session up to the configured concurrency limit. The concurrency limit is set when a client is created. By default the concurrency limit is one.
Data API Sessions
The client will automatically handle creating and closing Data API sessions. If a new session is required the client will authenticate and generate a new session or queue requests if the session limit has been reached.
The Data API session is also monitored, updated, and saved as the client interacts with the Data API. The client will always attempt to reuse a valid token whenever possible.
The client contains two methods related to Data API sessions. These methods are client.login()
and client.logout()
. The login method is used to start a Data API session and the logout method will end a Data API session.
Login Method
The client will automatically call the login method if it does not have a valid token. This method returns an object with a token property. This method will also save the token to the client's connection for future use.
client.login()
const login = client =>
client.login().then(result => log('client-login-example', result));
Excerpt from ./examples/authentication.examples.js
Logout Method
The logout method is used to end a Data API session. This method will also remove the current client's authentication token. The logout method accepts an optional id parameter. The logout method will use the id parameter as the session to target for ending.
client.logout(id)
const logout = client =>
client
.login()
.then(() => client.logout())
.then(result => log('client-logout-example', result));
Excerpt from ./examples/authentication.examples.js
Client Methods
Product Info
A client can get server product info. The productInfo method will return metadata about the FileMaker server the client is configured to use.
client.productInfo()
const productInfo = client =>
client.productInfo().then(result => log('product-info-example', result));
Excerpt from ./examples/metadata.examples.js Result:
{
"name": "FileMaker Data API Engine",
"buildDate": "07/05/2019",
"version": "18.0.2.217",
"dateFormat": "MM/dd/yyyy",
"timeFormat": "HH:mm:ss",
"timeStampFormat": "MM/dd/yyyy HH:mm:ss"
}
Get Databases
A client can get the databases accessible using the configured credentials. This method will return all databases hosted by the currently configured server that the client can connect to. An alternative set of credentials can be passed to this method to check what databases alternate credentials are able to access.
client.databases(credentials)
const databases = client =>
client.databases().then(result => log('databases-info-example', result));
Excerpt from ./examples/metadata.examples.js
Result:
{
"databases": [
{
"name": "fms-api-app"
},
{
"name": "node-red-app"
}
]
}
Get Layouts
The layouts method will return a list of layouts accessible by the configured client.
client.layouts()
const layouts = client =>
client.layouts().then(result => log('layouts-example', result));
Excerpt from ./examples/metadata.examples.js
Result:
{
"layouts": [
{
"name": "Parameter Storage Demo"
},
{
"name": "Chalmuns"
},
{
"name": "Card Windows",
"isFolder": true,
"folderLayoutNames": [
{
"name": "Authorization Window"
}
]
},
{
"name": "API Integration Layouts",
"isFolder": true,
"folderLayoutNames": [
{
"name": "authentication"
}
]
},
{
"name": "Base Tables",
"isFolder": true,
"folderLayoutNames": [
{
"name": "RequestLog"
},
{
"name": "SyncLog"
},
{
"name": "AuthenticationStore"
}
]
},
{
"name": "Heroes"
},
{
"name": "Transform"
},
{
"name": "Hero Search"
},
{
"name": "Hero"
},
{
"name": "Globals"
},
{
"name": "Scripts"
},
{
"name": "Logs"
},
{
"name": "Planets"
},
{
"name": "Driods"
},
{
"name": "Vehicles"
},
{
"name": "Species"
},
{
"name": "Images"
}
]
}
Get Scripts
The scripts method will return metadata for the scripts accessible by the configured client.
client.scripts()
const scripts = client =>
client
.scripts(process.env.LAYOUT)
.then(result => log('scripts-example', result));
Excerpt from ./examples/metadata.examples.js
Result:
{
"scripts": [
{
"name": "Upon Open",
"isFolder": false
},
{
"name": "FMS Triggered Script",
"isFolder": false
},
{
"name": "node-red-script",
"isFolder": false
},
{
"name": "example script",
"isFolder": false
},
{
"name": "Error Script",
"isFolder": false
},
{
"name": "Non JSON Script",
"isFolder": false
},
{
"name": "Create Droids",
"isFolder": false
},
{
"name": "Delete All Records",
"isFolder": false
},
{
"name": "Buttons",
"folderScriptNames": [
{
"name": "New Clip Record",
"isFolder": false
},
{
"name": "Update Clip Button",
"isFolder": false
},
{
"name": "Replace Clip Record Data",
"isFolder": false
},
{
"name": "Delete All Clip Records",
"isFolder": false
},
{
"name": "Push all flagged clips",
"isFolder": false
},
{
"name": "Set Clipboard",
"isFolder": false
}
],
"isFolder": true
}
]
}
Get Layout
The layout method will get metadata for the specified layout.
client.layout(layout)
| Param | Type | Description | | ------------ | ------------------- | -------------------------------------------- | | layout | String | The layout to use in the request. | | [parameters] | Object | optional request parameters for the request. |
const layout = client =>
client
.layout(process.env.LAYOUT)
.then(result => log('layout-details-example', result));
Excerpt from ./examples/metadata.examples.js
Result:
{
"fieldMetaData": [
{
"name": "name",
"type": "normal",
"displayType": "editText",
"result": "text",
"global": false,
"autoEnter": false,
"fourDigitYear": false,
"maxRepeat": 1,
"maxCharacters": 0,
"notEmpty": false,
"numeric": false,
"timeOfDay": false,
"repetitionStart": 1,
"repetitionEnd": 1
},
{
"name": "image",
"type": "normal",
"displayType": "editText",
"result": "container",
"global": false,
"autoEnter": false,
"fourDigitYear": false,
"maxRepeat": 1,
"maxCharacters": 0,
"notEmpty": false,
"numeric": false,
"timeOfDay": false,
"repetitionStart": 1,
"repetitionEnd": 1
},
{
"name": "object",
"type": "normal",
"displayType": "editText",
"result": "text",
"global": false,
"autoEnter": false,
"fourDigitYear": false,
"maxRepeat": 1,
"maxCharacters": 0,
"notEmpty": false,
"numeric": false,
"timeOfDay": false,
"repetitionStart": 1,
"repetitionEnd": 1
},
{
"name": "array",
"type": "normal",
"displayType": "editText",
"result": "text",
"global": false,
"autoEnter": false,
"fourDigitYear": false,
"maxRepeat": 1,
"maxCharacters": 0,
"notEmpty": false,
"numeric": false,
"timeOfDay": false,
"repetitionStart": 1,
"repetitionEnd": 1
},
{
"name": "height",
"type": "normal",
"displayType": "editText",
"result": "text",
"global": false,
"autoEnter": false,
"fourDigitYear": false,
"maxRepeat": 1,
"maxCharacters": 0,
"notEmpty": false,
"numeric": false,
"timeOfDay": false,
"repetitionStart": 1,
"repetitionEnd": 1
},
{
"name": "id",
"type": "normal",
"displayType": "editText",
"result": "text",
"global": false,
"autoEnter": true,
"fourDigitYear": false,
"maxRepeat": 1,
"maxCharacters": 0,
"notEmpty": false,
"numeric": false,
"timeOfDay": false,
"repetitionStart": 1,
"repetitionEnd": 1
},
{
"name": "imageName",
"type": "calculation",
"displayType": "editText",
"result": "text",
"global": false,
"autoEnter": false,
"fourDigitYear": false,
"maxRepeat": 1,
"maxCharacters": 0,
"notEmpty": false,
"numeric": false,
"timeOfDay": false,
"repetitionStart": 1,
"repetitionEnd": 1
},
{
"name": "creationAccountName",
"type": "normal",
"displayType": "editText",
"result": "text",
"global": false,
"autoEnter": true,
"fourDigitYear": false,
"maxRepeat": 1,
"maxCharacters": 0,
"notEmpty": false,
"numeric": false,
"timeOfDay": false,
"repetitionStart": 1,
"repetitionEnd": 1
},
{
"name": "creationTimestamp",
"type": "normal",
"displayType": "editText",
"result": "timeStamp",
"global": false,
"autoEnter": true,
"fourDigitYear": false,
"maxRepeat": 1,
"maxCharacters": 0,
"notEmpty": false,
"numeric": false,
"timeOfDay": false,
"repetitionStart": 1,
"repetitionEnd": 1
},
{
"name": "modificationAccountName",
"type": "normal",
"displayType": "editText",
"result": "text",
"global": false,
"autoEnter": true,
"fourDigitYear": false,
"maxRepeat": 1,
"maxCharacters": 0,
"notEmpty": false,
"numeric": false,
"timeOfDay": false,
"repetitionStart": 1,
"repetitionEnd": 1
},
{
"name": "modificationTimestamp",
"type": "normal",
"displayType": "editText",
"result": "timeStamp",
"global": false,
"autoEnter": true,
"fourDigitYear": false,
"maxRepeat": 1,
"maxCharacters": 0,
"notEmpty": false,
"numeric": false,
"timeOfDay": false,
"repetitionStart": 1,
"repetitionEnd": 1
},
{
"name": "Vehicles::name",
"type": "normal",
"displayType": "editText",
"result": "text",
"global": false,
"autoEnter": false,
"fourDigitYear": false,
"maxRepeat": 1,
"maxCharacters": 0,
"notEmpty": false,
"numeric": false,
"timeOfDay": false,
"repetitionStart": 1,
"repetitionEnd": 1
}
],
"portalMetaData": {
"Planets": [
{
"name": "Planets::name",
"type": "normal",
"displayType": "editText",
"result": "text",
"global": false,
"autoEnter": false,
"fourDigitYear": false,
"maxRepeat": 1,
"maxCharacters": 0,
"notEmpty": false,
"numeric": false,
"timeOfDay": false,
"repetitionStart": 1,
"repetitionEnd": 1
},
{
"name": "Species::name",
"type": "normal",
"displayType": "editText",
"result": "text",
"global": false,
"autoEnter": false,
"fourDigitYear": false,
"maxRepeat": 1,
"maxCharacters": 0,
"notEmpty": false,
"numeric": false,
"timeOfDay": false,
"repetitionStart": 1,
"repetitionEnd": 1
}
],
"Vehicles": [
{
"name": "Vehicles::name",
"type": "normal",
"displayType": "editText",
"result": "text",
"global": false,
"autoEnter": false,
"fourDigitYear": false,
"maxRepeat": 1,
"maxCharacters": 0,
"notEmpty": false,
"numeric": false,
"timeOfDay": false,
"repetitionStart": 1,
"repetitionEnd": 1
},
{
"name": "Vehicles::type",
"type": "normal",
"displayType": "editText",
"result": "text",
"global": false,
"autoEnter": false,
"fourDigitYear": false,
"maxRepeat": 1,
"maxCharacters": 0,
"notEmpty": false,
"numeric": false,
"timeOfDay": false,
"repetitionStart": 1,
"repetitionEnd": 1
}
]
}
}
Create Records
Using the client you can create filemaker records. To create a record specify the layout to use and the data to insert on creation. The client will automatically convert numbers, arrays, and objects into strings so they can be inserted into a filemaker field. The create method will automatically create a fieldData
property and add all data to that property if there is no fieldData property present. The client will preserve the contents of the portalData
property.
client.create(layout, data, parameters)
| Param | Type | Description | | ---------- | ------------------- | ------------------------------------------------------- | | layout | String | The layout to use when creating a record. | | data | Object | The data to use when creating a record. | | parameters | Object | The request parameters to use when creating the record. |
const createRecord = client =>
client
.create('Heroes', {
name: 'George Lucas'
})
.then(result => log('create-record-example', result));
Excerpt from ./examples/create.examples.js
Result:
{
"recordId": "1138",
"modId": "327"
}
The create method also allows you to trigger scripts when creating a record. Notice the scripts property in the following example. You can specify scripts to run using either FileMaker's script.key syntax or specify an array of in a scripts
property. The script objects should have with name
, optional phase
, and optional params
parameters. For more information see the scripts syntax example in the introduction.
const triggerScriptsOnCreate = client =>
client
.create(
'Heroes',
{ name: 'Anakin Skywalker' },
{
merge: true,
scripts: [
{ name: 'Create Droids', param: { droids: ['C3-PO', 'R2-D2'] } }
]
}
)
.then(result => log('trigger-scripts-on-create-example', result));
Excerpt from ./examples/create.examples.js
Result:
{
"name": "Anakin Skywalker",
"scriptError": "0",
"recordId": "1138",
"modId": "327"
}
File ./examples/results/trigger-scripts-on-create-example.json
Duplicate Record
The duplicate method duplicates the FileMaker record using the layout and recordId parameters passed to it.
client.duplicate(layout, recordId, parameters)
| Param | Type | Description | | ------------ | ------------------- | -------------------------------------------- | | layout | String | The layout to use in the request. | | recordId | String | The record id to target for duplication. | | [parameters] | Object | optional request parameters for the request. |
client
.duplicate('Heroes', response.data[0].recordId)
.then(result => log('duplicate-record-example', result));
Excerpt from ./examples/duplicate.examples.js
Result:
{
"recordId": "1138",
"modId": "327"
}
The duplicate method also allows you to trigger scripts when duplicating a record. Notice the scripts property in the following example. You can specify scripts to run using either FileMaker's script.key syntax or specify an array of in a scripts
property. The script objects should have with name
, optional phase
, and optional params
parameters. For more information see the scripts syntax example in the introduction.
const triggerScriptsOnCreate = client =>
client
.create(
'Heroes',
{ name: 'Anakin Skywalker' },
{
merge: true,
scripts: [
{ name: 'Create Droids', param: { droids: ['C3-PO', 'R2-D2'] } }
]
}
)
.then(result => log('trigger-scripts-on-create-example', result));
Excerpt from ./examples/create.examples.js
Result:
{
"name": "Anakin Skywalker",
"scriptError": "0",
"recordId": "1138",
"modId": "327"
}
File ./examples/results/trigger-scripts-on-create-example.json
Get Record Details
The Get method will return a specific FileMaker record based on the recordId passed to it. The recordId can be a string or a number.
client.get(layout, recordId, parameters)
| Param | Type | Description | | ------------ | ------------------- | ------------------------------------------------------------------- | | layout | String | The layout to use when retrieving the record. | | recordId | String | The FileMaker internal record ID to use when retrieving the record. | | [parameters] | Object | Parameters to add for the get query. |
client
.get('Heroes', response.data[0].recordId)
.then(result => log('get-record-example', result));
Excerpt from ./examples/get.examples.js Result:
{
"dataInfo": {
"database": "fms-api-app",
"layout": "Heroes",
"table": "Heroes",
"totalRecordCount": "1977",
"foundCount": 1,
"returnedCount": 1
},
"data": [
{
"fieldData": {
"name": "yoda",
"image": "https://some-server.com/Streaming_SSL/MainDB/E8BDBF29B9388B2F212002F7F6A7D6B8EF87A16E7E2D231EA27B8FC32147F64D?RCType=EmbeddedRCFileProcessor",
"object": "",
"array": "",
"height": "",
"id": "r2d2-c3po-l3-37-bb-8",
"imageName": "placeholder.md",
"creationAccountName": "obi-wan",
"creationTimestamp": "05/25/1977 6:00:00",
"modificationAccountName": "obi-wan",
"modificationTimestamp": "05/25/1977 6:00:00",
"Vehicles::name": ""
},
"portalData": {
"Planets": [],
"Vehicles": []
},
"recordId": "1138",
"modId": "327",
"portalDataInfo": [
{
"database": "fms-api-app",
"table": "Planets",
"foundCount": 0,
"returnedCount": 0
},
{
"database": "fms-api-app",
"table": "Vehicles",
"foundCount": 0,
"returnedCount": 0
}
]
}
]
}
List Records
You can use the client to list filemaker records. The list method accepts a layout and parameter variable. The client will automatically santize the limit, offset, and sort keys to correspond with the DAPI's requirements.
client.list(layout, parameters)
| Param | Type | Description | | ------------ | ------------------- | --------------------------------------------- | | layout | String | The layout to use when retrieving the record. | | [parameters] | Object | the parameters to use to modify the query. |
const listHeroes = client =>
client
.list('Heroes', { limit: 2 })
.then(result => log('list-records-example', result));
Excerpt from ./examples/list.examples.js
Result:
{
"dataInfo": {
"database": "fms-api-app",
"layout": "Heroes",
"table": "Heroes",
"totalRecordCount": "1977",
"foundCount": 29493,
"returnedCount": 2
},
"data": [
{
"fieldData": {
"name": "George Lucas",
"image": "https://some-server.com/Streaming_SSL/MainDB/6BA0C4191156DE2DF3036BB1ABF079B0CF8BF6F286316A5603EF789FB45C66D7.png?RCType=EmbeddedRCFileProcessor",
"object": "",
"array": "",
"height": "",
"id": "r2d2-c3po-l3-37-bb-8",
"imageName": "IMG_0001.PNG",
"creationAccountName": "obi-wan",
"creationTimestamp": "05/25/1977 6:00:00",
"modificationAccountName": "obi-wan",
"modificationTimestamp": "05/25/1977 6:00:00",
"Vehicles::name": "test"
},
"portalData": {
"Planets": [],
"Vehicles": [
{
"recordId": "1138",
"Vehicles::name": "test",
"Vehicles::type": "",
"modId": "327"
}
]
},
"recordId": "1138",
"modId": "327",
"portalDataInfo": [
{
"database": "fms-api-app",
"table": "Planets",
"foundCount": 0,
"returnedCount": 0
},
{
"database": "fms-api-app",
"table": "Vehicles",
"foundCount": 1,
"returnedCount": 1
}
]
},
{
"fieldData": {
"name": "George Lucas",
"image": "",
"object": "",
"array": "",
"height": "",
"id": "r2d2-c3po-l3-37-bb-8",
"imageName": "",
"creationAccountName": "obi-wan",
"creationTimestamp": "05/25/1977 6:00:00",
"modificationAccountName": "obi-wan",
"modificationTimestamp": "05/25/1977 6:00:00",
"Vehicles::name": ""
},
"portalData": {
"Planets": [],
"Vehicles": []
},
"recordId": "1138",
"modId": "327",
"portalDataInfo": [
{
"database": "fms-api-app",
"table": "Planets",
"foundCount": 0,
"returnedCount": 0
},
{
"database": "fms-api-app",
"table": "Vehicles",
"foundCount": 0,
"returnedCount": 0
}
]
}
]
}
Find Records
The client's find method will accept either a single object as find parameters or an array. The find method will also santize the limit, sort, and offset parameters to conform with the Data API's requirements.
client.find(layout, query, parameters)
| Param | Type | Description | | ---------- | ------------------- | ------------------------------------------- | | layout | String | The layout to use when performing the find. | | query | Object | to use in the find request. | | parameters | Object | the parameters to use to modify the query. |
const findRecords = client =>
client
.find('Heroes', [{ name: 'Anakin Skywalker' }], { limit: 1 })
.then(result => log('find-records-example', result));
Excerpt from ./examples/find.examples.js
Result:
{
"dataInfo": {
"database": "fms-api-app",
"layout": "Heroes",
"table": "Heroes",
"totalRecordCount": "1977",
"foundCount": 153,
"returnedCount": 1
},
"data": [
{
"fieldData": {
"name": "Anakin Skywalker",
"image": "",
"object": "",
"array": "",
"height": "",
"id": "r2d2-c3po-l3-37-bb-8",
"imageName": "",
"creationAccountName": "obi-wan",
"creationTimestamp": "05/25/1977 6:00:00",
"modificationAccountName": "obi-wan",
"modificationTimestamp": "05/25/1977 6:00:00",
"Vehicles::name": ""
},
"portalData": {
"Planets": [],
"Vehicles": []
},
"recordId": "1138",
"modId": "327",
"portalDataInfo": [
{
"database": "fms-api-app",
"table": "Planets",
"foundCount": 0,
"returnedCount": 0
},
{
"database": "fms-api-app",
"table": "Vehicles",
"foundCount": 0,
"returnedCount": 0
}
]
}
]
}
Edit Records
The client's edit method requires a layout, recordId, and object to use for updating the record.
client.edit(layout, recordId, data, parameters)
| Param | Type | Description | | ---------- | ------------------- | ---------------------------------------------------------------- | | layout | String | The layout to use when editing the record. | | recordId | String | The FileMaker internal record ID to use when editing the record. | | data | Object | The data to use when editing a record. | | parameters | Object | parameters to use when performing the query. |
const editRecord = client =>
client
.find('Heroes', [{ name: 'Anakin Skywalker' }], { limit: 1 })
.then(response => response.data[0].recordId)
.then(recordId => client.edit('Heroes', recordId, { name: 'Darth Vader' }))
.then(result => log('edit-record-example', result));
Excerpt from ./examples/edit.examples.js
Result:
{
"modId": "327"
}
Delete Records
The client's delete method requires a layout and a record id. The recordId can be a number or a string.
client.delete(layout, recordId, parameters)
| Param | Type | Description | | ---------- | ------------------- | ---------------------------------------------------------------- | | layout | String | The layout to use when deleting the record. | | recordId | String | The FileMaker internal record ID to use when editing the record. | | parameters | Object | parameters to use when performing the query. |
const deleteRecords = client =>
client
.find('Heroes', [{ name: 'yoda' }], { limit: 1 })
.then(response => response.data[0].recordId)
.then(recordId => client.delete('Heroes', recordId))
.then(result => log('delete-record-example', result));
Excerpt from ./examples/delete.examples.js
Result:
{}
Trigger Script
The client's script method will trigger a script. You can also trigger scripts with the create, edit, list, find, and delete methods. This method performs a list with a limit of one on the specified layout before triggering the script. this is the most lightweight request possible while still being able to trigger a script.
client.script(layout, script, param, parameters)
| Param | Type | Description | | ------------ | ------------------------------------------ | -------------------------------------- | | layout | String | The layout to use for the list request | | script | String | The name of the script | | param | Object | String | Parameter to pass to the script | | [parameters] | Object | Optional request parameters. |
const triggerScript = client =>
client
.script('Heroes', 'FMS Triggered Script', { name: 'Han' })
.then(result => log('script-trigger-example', result));
Excerpt from ./examples/script.examples.js
Result:
{
"scriptResult": {
"answer": "Han shot first"
},
"scriptError": "0"
}
Run Multiple Scripts
The client's script method will trigger a script. You can also trigger scripts with the create, edit, list, find, and delete methods. This method performs a list with a limit of one on the specified layout before triggering the script. this is the most lightweight request possible while still being able to trigger a script.
client.run(layout, scripts,parameters)
| Param | Type | Description | | ---------- | ----------------------------------------- | ------------------------------------------- | | layout | String | The layout to use for the list request | | scripts | Object | Array | The name of the script | | parameters | Object | Parameters to pass to the script | | request | Object | A request to run alongside the list method. |
const runMultipleScripts = client =>
client
.run('Heroes', [
{ name: 'FMS Triggered Script', param: { name: 'Han' } },
{ name: 'FMS Triggered Script', phase: 'presort', param: { name: 'Han' } }
])
.then(result => log('run-scripts-example', result));
Excerpt from ./examples/scripts.examples.js
Result:
{
"scriptResult.presort": {
"answer": "Han shot first"
},
"scriptResult": {
"answer": "Han shot first"
}
}
Upload Files
The upload method will upload binary data to a container. The file parameter should be either a path to a file or a buffer. If you need to set a field repetition, you can set that in parameters. If recordId is 0 or undefined a new record will be created.
client.upload(file, layout, container, recordId, parameters)
| Param | Type | Description | | ------------------ | ------------------------------------------ | ---------------------------------------------------------------------- | | file | String | The path to the file to upload. | | layout | String | The layout to use when performing the find. | | containerFieldName | String | The field name to insert the data into. It must be a container field. | | recordId | Number | String | the recordId to use when uploading the file. | | parameters | Object | parameters to use when performing the query. | | [fieldRepetition] | String | Number | The field repetition to use when inserting the file. The default is 1. |
const uploadImage = client =>
client
.upload('./assets/placeholder.md', 'Heroes', 'image')
.then(result => log('upload-image-example', result));
Excerpt from ./examples/upload.examples.js
Result:
{
"modId": "327",
"recordId": "1138"
}
You can also provide a record Id to the upload method and the file will be uploaded to that record.
client
.upload('./assets/placeholder.md', 'Heroes', 'image', recordId)
.then(result => log('upload-specific-record-example', result))
.catch(error => error),
Excerpt from ./examples/upload.examples.js
Result:
{
"modId": "327",
"recordId": "1138"
}
Set Session Globals
The globals method will set global fields for the current session.
client.globals(data, parameters)
| Param | Type | Description | | ---------- | ----------------------------------------- | ----------------------------------------------------- | | data | Object | Array | a json object containing the name value pairs to set. | | parameters | Object | parameters to use when performing the query. |
const setGlobals = client =>
client
.globals({ 'Globals::ship': 'Millenium Falcon' })
.then(result => log('set-globals-example', result));
Excerpt from ./examples/globals.examples.js
Result:
{}
Client Status
The client status method will return an object description the current client's connection status. This method will return overall client data usage, urls for pending and queued requests and current session information.
client.status()
const getStatus = client =>
client.status().then(result => log('client-status-example', result));
Excerpt from ./examples/utility.examples.js
Result:
{
"data": {
"since": "2019-09-15T15:30:43-07:00",
"in": "438 Bytes",
"out": "29.8 kB"
},
"queue": [],
"pending": [],
"sessions": [
{
"issued": "2019-09-15T15:30:44-07:00",
"expires": "2019-09-15T15:45:47-07:00",
"id": "r2d2-c3po-l3-37-bb-8",
"active": false
},
{
"issued": "2019-09-15T15:30:44-07:00",
"expires": "2019-09-15T15:45:44-07:00",
"id": "r2d2-c3po-l3-37-bb-8",
"active": true
},
{
"issued": "2019-09-15T15:30:44-07:00",
"expires": "2019-09-15T15:45:44-07:00",
"id": "r2d2-c3po-l3-37-bb-8",
"active": true
}
]
}
Client Reset
The client reset method will drop all pending and queued requests while also clearing all open sessions.
:warning: This method is experimental. It does not reject pending or queued requests before clearing them. Use with caution.
client.reset()
const resetClient = client =>
client.reset().then(result => log('client-reset-example', result));
Excerpt from ./examples/utility.examples.js
Result:
{
"message": "Client Reset"
}
Utilities
The client also provides utilities to aid in parsing and manipulating FileMaker Data. The client exports the recordId(data)
, fieldData(data)
, and transform(data, options)
to aid in transforming Data API response data into other formats. Each utility is capable of recieving either an object or a array.
Record Id Utility
The record id utility retrieves the recordId
properties for a response. This utility will return either a single string or an array of strings.
recordId(data)
| Param | Type | Description | | ----- | ----------------------------------------- | -------------------------------------------------------------------------- | | data | Object | Array | the raw data returned from a filemaker. This can be an array or an object. |
const extractRecordIdOriginal = client =>
client
.find('Heroes', { name: 'yoda' }, { limit: 2 })
.then(response => recordId(response.data))
.then(result => log('record-id-utility-example', result));
Excerpt from ./examples/utility.examples.js
Record Id Utility Results
original:
[
{
"fieldData": {
"name": "Yoda",
"image": "https://some-server.com/Streaming_SSL/MainDB/1D86123EDDA1866BDDC2962F71108F8D78EFCAE349E66934D2737E3615918167?RCType=EmbeddedRCFileProcessor",
"object": "",
"array": "",
"height": "",
"id": "r2d2-c3po-l3-37-bb-8",
"imageName": "placeholder.md",
"creationAccountName": "obi-wan",
"creationTimestamp": "05/25/1977 6:00:00",
"modificationAccountName": "obi-wan",
"modificationTimestamp": "05/25/1977 6:00:00",
"Vehicles::name": ""
},
"portalData": {
"Planets": [],
"Vehicles": []
},
"recordId": "1138",
"modId": "327",
"portalDataInfo": [
{
"database": "fms-api-app",
"table": "Planets",
"foundCount": 0,
"returnedCount": 0
},
{
"database": "fms-api-app",
"table": "Vehicles",
"foundCount": 0,
"returnedCount": 0
}
]
},
{
"fieldData": {
"name": "yoda",
"image": "",
"object": "",
"array": "",
"height": "",
"id": "r2d2-c3po-l3-37-bb-8",
"imageName": "",
"creationAccountName": "obi-wan",
"creationTimestamp": "05/25/1977 6:00:00",
"modificationAccountName": "obi-wan",
"modificationTimestamp": "05/25/1977 6:00:00",
"Vehicles::name": ""
},
"portalData": {
"Planets": [],
"Vehicles": []
},
"recordId": "1138",
"modId": "327",
"portalDataInfo": [
{
"database": "fms-api-app",
"table": "Planets",
"foundCount": 0,
"returnedCount": 0
},
{
"database": "fms-api-app",
"table": "Vehicles",
"foundCount": 0,
"returnedCount": 0
}
]
}
]
File ./examples/results/record-id-utility-original-example.json
Transformed:
["751326", "751329"]
Field Data Utility
The field data utility retrieves the fieldData
, recordId
, and modId
properties from a Data API response. The fieldData utility will merge the recordId
and modId
properties into fielData properties. This utility will not convert table::field
properties.
fieldData(data)
| Param | Type | Description | | ----- | ----------------------------------------- | -------------------------------------------------------------------------- | | data | Object | Array | The raw data returned from a filemaker. This can be an array or an object. |
const extractFieldData = client =>
client
.find('Heroes', { name: 'yoda' }, { limit: 2 })
.then(response => fieldData(response.data))
.then(result => log('field-data-utility-example', result));
Excerpt from ./examples/utility.examples.js
Field Data Utility Results
Original:
[
{
"fieldData": {
"name": "Yoda",
"image": "https://some-server.com/Streaming_SSL/MainDB/8F7489E114CFFE0FC13E8A45A2F52D77A50ABFB36B05C0858114F37EA3141634?RCType=EmbeddedRCFileProcessor",
"object": "",
"array": "",
"height": "",
"id": "r2d2-c3po-l3-37-bb-8",
"imageName": "placeholder.md",
"creationAccountName": "obi-wan",
"creationTimestamp": "05/25/1977 6:00:00",
"modificationAccountName": "obi-wan",
"modificationTimestamp": "05/25/1977 6:00:00",
"Vehicles::name": ""
},
"portalData": {
"Planets": [],
"Vehicles": []
},
"recordId": "1138",
"modId": "327",
"portalDataInfo": [
{
"database": "fms-api-app",
"table": "Planets",
"foundCount": 0,
"returnedCount": 0
},
{
"database": "fms-api-app",
"table": "Vehicles",
"foundCount": 0,
"returnedCount": 0
}
]
},
{
"fieldData": {
"name": "yoda",
"image": "",
"object": "",
"array": "",
"height": "",
"id": "r2d2-c3po-l3-37-bb-8",
"imageName": "",
"creationAccountName": "obi-wan",
"creationTimestamp": "05/25/1977 6:00:00",
"modificationAccountName": "obi-wan",
"modificationTimestamp": "05/25/1977 6:00:00",
"Vehicles::name": ""
},
"portalData": {
"Planets": [],
"Vehicles": []
},
"recordId": "1138",
"modId": "327",
"portalDataInfo": [
{
"database": "fms-api-app",
"table": "Planets",
"foundCount": 0,
"returnedCount": 0
},
{
"database": "fms-api-app",
"table": "Vehicles",
"foundCount": 0,
"returnedCount": 0
}
]
}
]
File ./examples/results/field-data-utility-original-example.json
Transformed:
[
{
"name": "Yoda",
"image": "https://some-server.com/Streaming_SSL/MainDB/27EA97DBE4B14E5BE2E09713E6C5CF75604F9458EC0885B00E436B618C798D87?RCType=EmbeddedRCFileProcessor",
"object": "",
"array": "",
"height": "",
"id": "r2d2-c3po-l3-37-bb-8",
"imageName": "placeholder.md",
"creationAccountName": "obi-wan",
"creationTimestamp": "05/25/1977 6:00:00",
"modificationAccountName": "obi-wan",
"modificationTimestamp": "05/25/1977 6:00:00",
"Vehicles::name": "",
"recordId": "1138",
"modId": "327"
},
{
"name": "yoda",
"image": "",
"object": "",
"array": "",
"height": "",
"id": "r2d2-c3po-l3-37-bb-8",
"imageName": "",
"creationAccountName": "obi-wan",
"creationTimestamp": "05/25/1977 6:00:00",
"modificationAccountName": "obi-wan",
"modificationTimestamp": "05/25/1977 6:00:00",
"Vehicles::name": "",
"recordId": "1138",
"modId": "327"
}
]
Transform Utility
The transform utility converts Data API response data by converting table::field
properties into objects. This utility will traverse the response data converting { table::field : value}
properties to { table:{ field : value } }
. This utility will also convert portalData
into arrays of objects.
The transform utility accepts three option properties. The three option properties are all booleans and true by default. The properties are convert
,fieldData
,portalData
. The convert
property toggles the transformation of table::field
properties. The fieldData
property toggles merging of fieldData to the result. The portalData
property toggles merging portalData to the result. Setting any property to false will turn that transformation off.
transform(data, parameters)
| Param | Type | Description | | ------- | ------------------- | -------------------------------------------------- | | data | Object | The data to transform. | | options | Object | transformation options to pass to transformObject. |
const transformData = client =>
client
.find('Transform', { name: 'Han Solo' }, { limit: 1 })
.then(result => transform(result.data))
.then(result => log('transform-utility-example', result));
Excerpt from ./examples/utility.examples.js
Transform Utility Results
Original:
[
{
"fieldData": {
"starships": "",
"vehicles": "",
"species": "",
"biography": "",
"birthYear": "",
"id": "r2d2-c3po-l3-37-bb-8",
"name": "Han Solo"
},
"portalData": {
"Planets": [
{
"recordId": "1138",
"Planets::name": "Coriella",
"modId": "327"
}
],
"Vehicles": [
{
"recordId": "1138",
"Vehicles::name": "Millenium Falcon",
"Vehicles::type": "Starship",
"modId": "327"
}
]
},
"recordId": "1138",
"modId": "327",
"portalDataInfo": [
{
"database": "fms-api-app",
"table": "Planets",
"foundCount": 1,
"returnedCount": 1
},
{
"database": "fms-api-app",
"table": "Vehicles",
"foundCount": 1,
"returnedCount": 1
}
]
}
]
File ./examples/results/transform-utility-original-example.json
Transformed:
[
{
"starships": "",
"vehicles": "",
"species": "",
"biography": "",
"birthYear": "",
"id": "r2d2-c3po-l3-37-bb-8",
"name": "Han Solo",
"Planets": [
{
"recordId": "1138",
"name": "Coriella",
"modId": "327"
}
],
"Vehicles": [
{
"recordId": "1138",
"name": "Millenium Falcon",
"type": "Starship",
"modId": "327"
}
],
"recordId": "1138",
"modId": "327"
}
]
Container Data Utility
The container data utility will retrieve FileMaker container data by following the links returned by the Data API. This utility will accept either a single data object or an array of objects. The utility will use the field
parameter to target container data urls in the data parameter. This utility also requires a name
parameter which will be used to target a data property that should be used as the file's name. If a name parameter is provided that is not a property or nested property in the data
parameter, the name parameter itself will be used. The destination
parameter should be either 'buffer' to indicate that an object with a file's name and buffer should be returned or the path, relative to the current working directory, where the utility should write container data to a file. This utility will also accept optional request parameters
to modify the http request.
containerData(data, field, destination, name, parameters)
| Param | Type | Description | | -------------------- | ----------------------------------------- | ----------------------------------------------------------------------------- | | data | Object | Array | The response recieved from the FileMaker DAPI. | | field | String | The container field name to target. This can be a nested property. | | destination | String | "buffer" if a buffer object should be returned or the path to write the file. | | name | String | The field to use for the file name or a static string. | | [parameters] | Object | Request configuration parameters. | | [parameters.timeout] | Number | a timeout for the request. |
const getContainerData = client =>
client
.find('Heroes', { imageName: '*' }, { limit: 1 })
.then(result =>
containerData(
result.data,
'fieldData.image',
'./assets',
'fieldData.imageName'
)
)
.then(result => log('container-data-example', result));
Excerpt from ./examples/utility.examples.js
Result:
[
{
"name": "IMG_0001.PNG",
"path": "assets/IMG_0001.PNG"
}
]
File [.