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

Binary file not shown.

Binary file not shown.

View file

@ -1,5 +1,6 @@
<script type="text/javascript">
RED.nodes.registerType('grist-records', {
name: "Get records",
category: 'grist',
color: '#00bb00',
defaults: {
@ -12,12 +13,12 @@
outputs: 1,
icon: "font-awesome/fa-table",
label: function () {
return this.tableId || "records";
return this.tableId || "Get records";
},
oneditprepare: function () {
$("#node-input-filter").typedInput({
type: "json",
types:["json"]
types: ["json", "str"]
})
}
});
@ -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,5 +1,5 @@
const { GristDocAPI } = require('grist-api');
const mustache = require('mustache');
module.exports = function (RED) {
function RecordsNode(config) {
RED.nodes.createNode(this, config);
@ -12,7 +12,8 @@ module.exports = function(RED) {
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 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 })

View file

@ -28,6 +28,7 @@
"node": ">=14.0.0"
},
"dependencies": {
"grist-api": "^0.1.7"
"grist-api": "^0.1.7",
"mustache": "^4.2.0"
}
}

View file

@ -10,6 +10,7 @@ __metadata:
resolution: "@gorootde/node-red-grist@workspace:."
dependencies:
grist-api: ^0.1.7
mustache: ^4.2.0
languageName: unknown
linkType: soft
@ -35,12 +36,12 @@ __metadata:
linkType: hard
"follow-redirects@npm:^1.14.0":
version: 1.15.2
resolution: "follow-redirects@npm:1.15.2"
version: 1.15.3
resolution: "follow-redirects@npm:1.15.3"
peerDependenciesMeta:
debug:
optional: true
checksum: faa66059b66358ba65c234c2f2a37fcec029dc22775f35d9ad6abac56003268baf41e55f9ee645957b32c7d9f62baf1f0b906e68267276f54ec4b4c597c2b190
checksum: 584da22ec5420c837bd096559ebfb8fe69d82512d5585004e36a3b4a6ef6d5905780e0c74508c7b72f907d1fa2b7bd339e613859e9c304d0dc96af2027fd0231
languageName: node
linkType: hard
@ -56,9 +57,9 @@ __metadata:
linkType: hard
"graceful-fs@npm:^4.1.6, graceful-fs@npm:^4.2.0":
version: 4.2.10
resolution: "graceful-fs@npm:4.2.10"
checksum: 3f109d70ae123951905d85032ebeae3c2a5a7a997430df00ea30df0e3a6c60cf6689b109654d6fdacd28810a053348c4d14642da1d075049e6be1ba5216218da
version: 4.2.11
resolution: "graceful-fs@npm:4.2.11"
checksum: ac85f94da92d8eb6b7f5a8b20ce65e43d66761c55ce85ac96df6865308390da45a8d3f0296dd3a663de65d30ba497bd46c696cc1e248c72b13d6d567138a4fc7
languageName: node
linkType: hard
@ -100,6 +101,15 @@ __metadata:
languageName: node
linkType: hard
"mustache@npm:^4.2.0":
version: 4.2.0
resolution: "mustache@npm:4.2.0"
bin:
mustache: bin/mustache
checksum: 928fcb63e3aa44a562bfe9b59ba202cccbe40a46da50be6f0dd831b495be1dd7e38ca4657f0ecab2c1a89dc7bccba0885eab7ee7c1b215830da765758c7e0506
languageName: node
linkType: hard
"universalify@npm:^0.1.0":
version: 0.1.2
resolution: "universalify@npm:0.1.2"