node-red-contrib-filemaker
Version:
Node-RED FileMaker nodes. These nodes use the FileMaker Data API to connect with a FileMaker database.
178 lines (174 loc) • 7.03 kB
JavaScript
const _ = require("lodash");
const { Filemaker } = require("fms-api-client");
const { connect } = require("marpat");
function configurationNode(RED) {
if (!global.MARPAT) global.MARPAT = {};
function Client(n) {
RED.nodes.createNode(this, n);
const { concurrency, id, timeout, ssl } = n;
const store = _.get(RED.settings, "marpat.url", "nedb://memory");
const options = _.get(RED.settings, "marpat.options", {});
if (!global.MARPAT.CONNECTION) {
global.MARPAT.CONNECTION = new Promise((resolve, reject) =>
connect(store, options)
.then(db => {
this.client = Filemaker.findOne({ name: id })
.then(client => {
if (!client) {
this.emit("status", { connected: true, message: "Ready" });
return Filemaker.create(
Object.assign(
{
name: id,
concurrency: parseInt(concurrency) || 1,
timeout: parseInt(timeout) || 0
},
{
database: this.credentials.database,
server: this.credentials.server,
user: this.credentials.username,
password: this.credentials.password
},
!ssl ? { agent: { rejectUnauthorized: false } } : {}
)
).save();
} else {
client.agent.connection.starting = false;
client.agent.connection.queue = [];
client.agent.connection.pending = [];
if (
client.agent.connection.credentials.password !==
this.credentials.password
) {
client.agent.connection.credentials.password = this.credentials.password;
client.agent.connection.sessions = [];
}
if (
client.agent.connection.credentials.user !==
this.credentials.username
) {
client.agent.connection.credentials.user = this.credentials.username;
client.agent.connection.sessions = [];
}
if (
client.agent.connection.server !== this.credentials.server
) {
client.agent.connection.server = this.credentials.server;
client.agent.connection.sessions = [];
}
if (
client.agent.connection.database !==
this.credentials.database
) {
client.agent.connection.database = this.credentials.database;
client.agent.connection.sessions = [];
}
if (!ssl) {
client.agent.agent = { rejectUnauthorized: false };
}
client.agent.timeout = parseInt(timeout) || 0;
client.agent.concurrency = parseInt(concurrency) || 1;
this.emit("status", { connected: true, message: "Ready" });
return client.save();
}
})
.catch(error => {
this.emit("status", {
connected: false,
message: error.message
});
return error;
});
resolve(db);
})
.catch(error => reject(error))
);
this.connection = global.MARPAT.CONNECTION;
} else {
this.connection = global.MARPAT.CONNECTION;
global.MARPAT.CONNECTION.then(db => {
this.client = Filemaker.findOne({ name: id })
.then(client => {
if (!client) {
this.emit("status", { connected: true, message: "Ready" });
return Filemaker.create(
Object.assign(
{
name: id,
concurrency: parseInt(concurrency) || 1,
timeout: parseInt(timeout) || 0
},
{
database: this.credentials.database,
server: this.credentials.server,
user: this.credentials.username,
password: this.credentials.password
},
!ssl ? { agent: { rejectUnauthorized: false } } : {}
)
).save();
} else {
client.agent.connection.starting = false;
client.agent.connection.queue = [];
client.agent.connection.pending = [];
if (
client.agent.connection.credentials.password !==
this.credentials.password
) {
client.agent.connection.credentials.password = this.credentials.password;
client.agent.connection.sessions = [];
}
if (
client.agent.connection.credentials.user !==
this.credentials.username
) {
client.agent.connection.credentials.user = this.credentials.username;
client.agent.connection.sessions = [];
}
if (client.agent.connection.server !== this.credentials.server) {
client.agent.connection.server = this.credentials.server;
client.agent.connection.sessions = [];
}
if (
client.agent.connection.database !== this.credentials.database
) {
client.agent.connection.database = this.credentials.database;
client.agent.connection.sessions = [];
}
if (!ssl) {
client.agent.agent = { rejectUnauthorized: false };
}
if (client.agent.agent && ssl) {
client.agent.agent = { rejectUnauthorized: true };
}
client.agent.timeout = parseInt(timeout) || 0;
client.agent.concurrency = parseInt(concurrency) || 1;
this.emit("status", { connected: true, message: "Ready" });
return client.save();
}
})
.catch(error => {
this.emit("status", { connected: false, message: error.message });
return error;
});
});
}
this.on("close", function(done) {
this.client
? this.client
.then(client => client.save())
.then(client => done())
.catch(error => done())
: done();
});
}
RED.nodes.registerType("dapi-client", Client, {
credentials: {
server: { type: "text" },
database: { type: "text" },
username: { type: "text" },
password: { type: "password" }
}
});
}
module.exports = configurationNode;