added template support for filter expression

This commit is contained in:
Michael Kolb 2023-12-27 14:59:19 +01:00
parent 4980181634
commit a33c6da509
8 changed files with 59 additions and 35 deletions

View file

@ -1,28 +1,29 @@
<script type="text/javascript">
RED.nodes.registerType('grist-records',{
RED.nodes.registerType('grist-records', {
name: "Get records",
category: 'grist',
color: '#00bb00',
defaults: {
server:{value:"", type:"grist-server",required:true},
document: {value:"",type:"grist-document",required:true},
tableId: {value:"",required:true},
filter: {value:"",required:false}
server: { value: "", type: "grist-server", required: true },
document: { value: "", type: "grist-document", required: true },
tableId: { value: "", required: true },
filter: { value: "", required: false }
},
inputs:1,
outputs:1,
inputs: 1,
outputs: 1,
icon: "font-awesome/fa-table",
label: function() {
return this.tableId || "records";
label: function () {
return this.tableId || "Get records";
},
oneditprepare: function () {
$("#node-input-filter").typedInput({
type:"json",
types:["json"]
type: "json",
types: ["json", "str"]
})
}
});
</script>
<script type="text/html" data-template-name="grist-records">
@ -47,5 +48,16 @@
</script>
<script type="text/html" data-help-name="grist-records">
<p>Retrieves records from a grist table</p>
<p>Retrieves records from a grist table.</p>
<h3>Inputs</h3>
<p>
The input message object is available for the <code>filter</code> property via mustache template syntax.
</p>
<h3>Outputs</h3>
<p>
Array of grist rows (records).
</p>
<h3>Attributes</h3>
<p>
<code>filter</code> JSON object with filter definition. Supports mustache syntax for templates.</p>
</script>

View file

@ -1,24 +1,25 @@
const {GristDocAPI} = require('grist-api');
module.exports = function(RED) {
const { GristDocAPI } = require('grist-api');
const mustache = require('mustache');
module.exports = function (RED) {
function RecordsNode(config) {
RED.nodes.createNode(this,config);
RED.nodes.createNode(this, config);
let node = this;
this.document = RED.nodes.getNode(config.document);
this.server = RED.nodes.getNode(config.server);
this.table = config.tableId
this.filter = config.filter
node.on('input', async function(msg, send, done) {
const protocol=this.server.tlsEnabled === true ? "https" : "http";
const url=protocol+"://"+this.server.hostname+":"+this.server.port;
const filter=this.filter && this.filter !== "" ? JSON.parse(this.filter) : undefined
const api = new GristDocAPI(this.document.docid,{apiKey:this.server.apiKey,server:url});
api.fetchTable(this.table,filter).then(data => {
node.send({payload:data,topic:this.table})
}).catch(reason => done(reason,"Failed to perform grist request to "+url));
node.on('input', async function (msg, send, done) {
const protocol = this.server.tlsEnabled === true ? "https" : "http";
const url = protocol + "://" + this.server.hostname + ":" + this.server.port;
const filter = this.filter && this.filter !== "" ? JSON.parse(mustache.render(this.filter, { msg })) : undefined
node.log(`filter evaluated to: ${JSON.stringify(filter)}`)
const api = new GristDocAPI(this.document.docid, { apiKey: this.server.apiKey, server: url });
api.fetchTable(this.table, filter).then(data => {
node.send({ payload: data, topic: this.table })
}).catch(reason => done(reason, "Failed to perform grist request to " + url));
});
}
RED.nodes.registerType("grist-records",RecordsNode);
RED.nodes.registerType("grist-records", RecordsNode);
}