@illusionforge/node-red-contrib-directus 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/directus-config.html +23 -0
- package/directus-config.js +8 -0
- package/directus-items.html +91 -0
- package/directus-items.js +90 -0
- package/icons/directus.io.svg +1 -0
- package/package.json +28 -0
package/README.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
<script type="text/javascript">
|
|
2
|
+
RED.nodes.registerType('directus-config',{
|
|
3
|
+
category: 'config',
|
|
4
|
+
defaults: {
|
|
5
|
+
host: {value:"https://localhost:8055",required:true},
|
|
6
|
+
token: {value:"",required:false}
|
|
7
|
+
},
|
|
8
|
+
label: function() {
|
|
9
|
+
return this.host;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
</script>
|
|
13
|
+
|
|
14
|
+
<script type="text/html" data-template-name="directus-config">
|
|
15
|
+
<div class="form-row">
|
|
16
|
+
<label for="node-config-input-host"><i class="fa fa-bookmark"></i> Host</label>
|
|
17
|
+
<input type="text" id="node-config-input-host">
|
|
18
|
+
</div>
|
|
19
|
+
<div class="form-row">
|
|
20
|
+
<label for="node-config-input-token"><i class="fa fa-bookmark"></i> Token</label>
|
|
21
|
+
<input type="password" id="node-config-input-token">
|
|
22
|
+
</div>
|
|
23
|
+
</script>
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
<script type="text/javascript">
|
|
2
|
+
RED.nodes.registerType('directus-items', {
|
|
3
|
+
category: 'Directus',
|
|
4
|
+
color: '#64f',
|
|
5
|
+
defaults: {
|
|
6
|
+
name: { value: '' },
|
|
7
|
+
server: {value:'', type:"directus-config"},
|
|
8
|
+
collection: { value: '' },
|
|
9
|
+
method: { value: 'GET' },
|
|
10
|
+
mode: { value: 'single' }
|
|
11
|
+
},
|
|
12
|
+
inputs: 1,
|
|
13
|
+
outputs: 2,
|
|
14
|
+
inputLabels: ['Input'],
|
|
15
|
+
outputLabels: ['Success', 'Error'],
|
|
16
|
+
icon: 'directus.io.svg',
|
|
17
|
+
label() {
|
|
18
|
+
return this.collection+ ' - ' + this.method + (this.mode === "single" ? " Single Item" : " Multiple Items") || this.name + ' - ' + this.method + (this.mode === "single" ? " Single Item" : " Multiple Items") || 'Directus Collections';
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
</script>
|
|
22
|
+
|
|
23
|
+
<script type="text/html" data-template-name="directus-items">
|
|
24
|
+
<div class="form-row">
|
|
25
|
+
<label for="node-input-server"><i class="fa fa-tag"></i> Directus Configuration</label>
|
|
26
|
+
<input type="directus-config" id="node-input-server" placeholder="Directus Configuration" />
|
|
27
|
+
</div>
|
|
28
|
+
<div class="form-row">
|
|
29
|
+
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
|
30
|
+
<input type="text" id="node-input-name" placeholder="Name" />
|
|
31
|
+
</div>
|
|
32
|
+
<div class="form-row">
|
|
33
|
+
<label for="node-input-collection"><i class="fa fa-tag"></i> Collection</label>
|
|
34
|
+
<input type="text" id="node-input-collection" placeholder="Collection" />
|
|
35
|
+
</div>
|
|
36
|
+
<div class="form-row">
|
|
37
|
+
<label for="node-input-method"><i class="fa fa-tasks"></i> Methode</label>
|
|
38
|
+
<select id="node-input-method">
|
|
39
|
+
<option value="GET">Read Data (GET)</option>
|
|
40
|
+
<option value="POST">Create Data (POST)</option>
|
|
41
|
+
<option value="PATCH">Update Data (PATCH)</option>
|
|
42
|
+
<option value="DELETE">Delete Data (DELETE)</option>
|
|
43
|
+
</select>
|
|
44
|
+
</div>
|
|
45
|
+
<div class="form-row">
|
|
46
|
+
<label for="node-input-mode"><i class="fa fa-tasks"></i> Mode</label>
|
|
47
|
+
<select id="node-input-mode">
|
|
48
|
+
<option value="single">Single Item</option>
|
|
49
|
+
<option value="multiple">Multiple Items</option>
|
|
50
|
+
</select>
|
|
51
|
+
</div>
|
|
52
|
+
</script>
|
|
53
|
+
|
|
54
|
+
<script type="text/markdown" data-help-name="directus-items">
|
|
55
|
+
Directus Items Node
|
|
56
|
+
|
|
57
|
+
### Inputs
|
|
58
|
+
|
|
59
|
+
: payload (object or array) : The data to be sent to Directus for POST, PATCH, or DELETE operations. For GET operations, this can be used to specify query parameters.
|
|
60
|
+
: itemId (string) : The ID of the item to be updated for PATCH operations.
|
|
61
|
+
: query (object) : The query parameters for GET, PATCH, or DELETE operations in multiple mode.
|
|
62
|
+
|
|
63
|
+
### Outputs
|
|
64
|
+
1. Standard output
|
|
65
|
+
: payload (object) : The response from Directus if the operation was successful.
|
|
66
|
+
2. Standard output
|
|
67
|
+
: payload (object) : The error details if there was an error during the operation.
|
|
68
|
+
|
|
69
|
+
This node allows you to interact with Directus collections. You can perform various operations such as reading, creating, updating, and deleting data from your Directus collections.
|
|
70
|
+
|
|
71
|
+
### Configuration
|
|
72
|
+
- **Directus Configuration**: Select the Directus configuration you want to use for this node.
|
|
73
|
+
- **Name**: A name for the node (optional).
|
|
74
|
+
- **Collection**: The name of the Directus collection you want to interact with.
|
|
75
|
+
- **Method**: The HTTP method to use for the operation (GET, POST, PATCH, DELETE).
|
|
76
|
+
- **Mode**: Choose between "Single Item" or "Multiple Items" mode.
|
|
77
|
+
|
|
78
|
+
### Input
|
|
79
|
+
The node accepts input messages that can contain data to be sent to Directus for POST, PATCH, or DELETE operations. For GET operations, the input can be used to specify query parameters.
|
|
80
|
+
## Output
|
|
81
|
+
- **Success**: The output will contain the response from Directus if the operation was successful.
|
|
82
|
+
- **Error**: If there was an error during the operation, the error details will be sent to this output.
|
|
83
|
+
|
|
84
|
+
### Methods
|
|
85
|
+
- **GET**: Retrieve data from the specified collection. The output will contain the retrieved data. You can use query parameters `msg.query` in the input message to filter, sort, or paginate the results.
|
|
86
|
+
- **POST**: Create a new item in the specified collection. The input message should contain the data for the new item in the payload. The output will contain the response from Directus, including the ID of the newly created item.
|
|
87
|
+
- **PATCH**: Update an existing item in the specified collection. The input message should contain the ID `msg.itemId` of the item to be updated and the data to be updated in the `msg.payload`. In multiple mode, the input message should contain a `msg.query` and data in the `msg.payload`. The output will contain the response from Directus, including the updated item details.
|
|
88
|
+
- **DELETE**: Delete an existing item from the specified collection. The input message should contain the `msg.payload` ID of the item or an array of IDs to be deleted in the payload. Alternatively a `msg.query` can be used, to find specific items to delete. The output will contain the response from Directus, confirming the deletion.
|
|
89
|
+
For more information on how to use this node, please refer to the Directus API documentation (https://directus.io/docs/api/items) and the Node-RED documentation.
|
|
90
|
+
|
|
91
|
+
</script>
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
const { createDirectus, rest, readItem, readItems, updateItem, updateItems, createItem, createItems, deleteItem, deleteItems, staticToken, serverPing } = require('@directus/sdk');
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
module.exports = function(RED) {
|
|
6
|
+
function DirectusItemsNode(config) {
|
|
7
|
+
RED.nodes.createNode(this, config);
|
|
8
|
+
|
|
9
|
+
this.serverConfig = RED.nodes.getNode(config.server);
|
|
10
|
+
this.collection = config.collection;
|
|
11
|
+
|
|
12
|
+
var node = this;
|
|
13
|
+
|
|
14
|
+
const client = createDirectus(this.serverConfig.host)
|
|
15
|
+
.with(staticToken(this.serverConfig.token))
|
|
16
|
+
.with(rest());
|
|
17
|
+
|
|
18
|
+
node.status({fill:"yellow", shape:"dot", text:"healthcheck..."});
|
|
19
|
+
|
|
20
|
+
const result = async function() { await client.request(serverPing()) };
|
|
21
|
+
|
|
22
|
+
result().finally(() => {
|
|
23
|
+
node.status({fill:"green", shape:"dot", text:"ready"});
|
|
24
|
+
}).catch((error) => {
|
|
25
|
+
node.status({fill:"red", shape:"dot", text:"error"});
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
node.on('input', async function(msg) {
|
|
29
|
+
node.status({fill:"blue", shape:"dot", text:"fetching..."});
|
|
30
|
+
const collection = msg.collection || config.collection;
|
|
31
|
+
const method = msg.method || config.method;
|
|
32
|
+
const mode = msg.mode || config.mode;
|
|
33
|
+
|
|
34
|
+
let response = null;
|
|
35
|
+
|
|
36
|
+
try {
|
|
37
|
+
switch (method) {
|
|
38
|
+
case 'GET':
|
|
39
|
+
if(mode === 'single') {
|
|
40
|
+
response = await client.request(
|
|
41
|
+
readItem(collection, msg.itemId || msg.payload, msg.query || {})
|
|
42
|
+
);
|
|
43
|
+
} else {
|
|
44
|
+
response = await client.request(
|
|
45
|
+
readItems(collection, msg.query || msg.payload || {
|
|
46
|
+
limit: -1,
|
|
47
|
+
fields: ['*']
|
|
48
|
+
})
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
break;
|
|
52
|
+
case 'POST':
|
|
53
|
+
if(mode === 'single') {
|
|
54
|
+
response = await client.request(createItem(collection, msg.payload, msg.query || {}));
|
|
55
|
+
} else {
|
|
56
|
+
response = await client.request(createItems(collection, msg.payload, msg.query || {}));
|
|
57
|
+
}
|
|
58
|
+
break;
|
|
59
|
+
case 'PATCH':
|
|
60
|
+
if(mode === 'single') {
|
|
61
|
+
response = await client.request(updateItem(collection, msg.itemId, msg.payload, msg.query || {}));
|
|
62
|
+
} else {
|
|
63
|
+
response = await client.request(updateItems(collection, msg.query, msg.payload));
|
|
64
|
+
}
|
|
65
|
+
break;
|
|
66
|
+
case 'DELETE':
|
|
67
|
+
if(mode === 'single') {
|
|
68
|
+
response = await client.request(deleteItem(collection, msg.payload));
|
|
69
|
+
} else {
|
|
70
|
+
response = await client.request(deleteItems(collection, msg.query || msg.payload));
|
|
71
|
+
}
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
msg.payload = response;
|
|
76
|
+
node.send([msg, null]);
|
|
77
|
+
node.status({fill:"green", shape:"dot", text:"idle"});
|
|
78
|
+
|
|
79
|
+
} catch (error) {
|
|
80
|
+
msg.payload = error;
|
|
81
|
+
node.status({fill:"red", shape:"dot", text:"error"});
|
|
82
|
+
node.send([null, msg]);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
node.status({fill:"green", shape:"dot", text:"idle"});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
RED.nodes.registerType("directus-items", DirectusItemsNode);
|
|
90
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<svg viewBox="0 0 90 90" xmlns="http://www.w3.org/2000/svg"><g fill="none" fill-rule="evenodd"><path fill="#6e84f000" fill-rule="nonzero" d="M0 0h90v90H0z"/><path d="M65.962 50.504a5.107 5.107 0 0 1-.833-.266 1.915 1.915 0 0 1-.592-.398c.161-1.41 0-2.632.135-4.015.537-5.37 3.953-3.669 7.018-4.546 1.91-.532 3.819-1.622 4.303-3.856a39.29 39.29 0 0 0-6.992-6.354c-8.31-5.85-19.174-8.189-29.043-6.567a14.85 14.85 0 0 0 6.527 5.982s-2.674 0-4.967-1.693c-.672.266-2.017.79-2.663 1.108 5.244 4.972 13.446 5.53 19.336 1.064-.027.053-.538.824-1.157 4.04-1.371 6.86-5.325 6.329-10.219 4.6-10.165-3.642-15.758-.265-20.841-7.178a4.63 4.63 0 0 0-2.393 4.041c0 1.728.968 3.19 2.366 3.988.763-1 1.106-1.286 2.437-1.286-2.06 1.155-2.303 2.164-3.19 4.956-1.076 3.376-.619 6.832-5.647 7.736-2.663.133-2.609 1.915-3.577 4.573-1.21 3.457-2.824 4.84-5.97 8.11 1.29 1.542 2.636 1.728 4.007 1.17 2.824-1.17 5.002-4.786 7.046-7.126 2.285-2.606 7.771-1.489 11.913-4.041 2.85-1.729 4.249-4.068 2.366-8.03a7.8 7.8 0 0 1 2.044 4.786c4.787-.611 11.187 5.158 17.023 6.115a10.29 10.29 0 0 1-1.425-2.366c-.673-1.595-.888-3.058-.753-4.334.537 3.164 3.764 7.232 8.955 7.604 1.317.106 2.77-.053 4.275-.505 1.802-.532 3.47-1.223 5.46-.85 1.479.265 2.85 1.01 3.71 2.26 1.291 1.86 4.115 2.259 5.379-.027-2.85-7.365-10.703-7.844-14.038-8.695Z" fill="#FFF"/></g></svg>
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@illusionforge/node-red-contrib-directus",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"publishConfig": {
|
|
5
|
+
"access": "public"
|
|
6
|
+
},
|
|
7
|
+
"description": "A node collection for Node-RED to interact with Directus, a headless CMS. This package provides nodes to perform CRUD operations on Directus collections and items.",
|
|
8
|
+
"main": "index.js",
|
|
9
|
+
"author": {
|
|
10
|
+
"name": "Erik Konrad",
|
|
11
|
+
"email": "e_konrad84@yahoo.de"
|
|
12
|
+
},
|
|
13
|
+
"node-red": {
|
|
14
|
+
"version": ">=4.0.0",
|
|
15
|
+
"nodes": {
|
|
16
|
+
"directus-items": "directus-items.js",
|
|
17
|
+
"directus-config": "directus-config.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [ "node-red" ],
|
|
24
|
+
"license": "ISC",
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@directus/sdk": "^21.1.0"
|
|
27
|
+
}
|
|
28
|
+
}
|