@topcs/node-red-contrib-surrealdb 0.1.0
Node-RED nodes for SurrealDB (query + live queries)
node-red-contrib-surrealdb
Node-RED nodes to interact with SurrealDB, including a Live Query node.
Features:
- Config node for connection and authentication
- Query node for arbitrary SurrealQL (with variables)
- Live query node (table or custom
LIVE SELECT) that emits change events
Install
From your Node-RED user directory:
npm install node-red-contrib-surrealdb
This package depends on surrealdb.
Nodes
surrealdb-config– connection to SurrealDB. For live queries, use a WebSocket RPC URL likews://127.0.0.1:8000/rpcand set Namespace/Database.surrealdb-query– run any SurrealQL. Put the query in the node or setmsg.query. Optional variables frommsg.vars.surrealdb-live– subscribe to live updates.- Mode "Table": subscribes via
db.live(table, cb)and emits{ action, payload }. - Mode "Custom": executes a
LIVE SELECT ...statement and keeps the subscription id. - Control with
msg.actionofstart,stop, orrestart. EnableAutostartto subscribe on deploy.
- Mode "Table": subscribes via
Example
- Add a
surrealdb-confignode pointing tows://localhost:8000/rpc, set NS/DB and user/password. - Add
SurrealDB livein Table mode with tablepersonand connect to a Debug node. - Create/update/delete rows in
person; events will stream into Node-RED.
Notes
- Live queries require a WebSocket RPC endpoint and a SurrealDB server version that supports live queries.
- The library API may differ slightly between versions of
surrealdb. If you see connection orlive()errors, updatesurrealdbto the latest version.
Query Variables
You can bind variables into SurrealQL using $name placeholders in the query. The Query node reads variables from a message field (default msg.vars). This avoids string concatenation and prevents injection.
Configure
- In the Query node, set Variables → “From msg” and Msg field →
vars(default). - Use
$varNamein your SurrealQL where you want the bound value.
- In the Query node, set Variables → “From msg” and Msg field →
SELECT example
- Query:
SELECT * FROM person WHERE age >= $min AND city = $city. - Inject msg:
msg.vars = { min: 21, city: "Rome" }
- Query:
CREATE with CONTENT
- Query:
CREATE person CONTENT $data. - Inject msg:
msg.vars = { data: { name: "Ada", age: 31 } }
- Query:
UPDATE/MERGE example
- Query:
UPDATE person:john MERGE $patch. - Inject msg:
msg.vars = { patch: { age: 33 } }
- Query:
Using msg.query
- You can override the node’s Query field by setting
msg.queryat runtime and still passmsg.varsfor variables.
- You can override the node’s Query field by setting
Notes
- Variable names are case-sensitive and must match the
$placeholderin the query. - Variables can be primitives, arrays, or objects (used with
CONTENT/MERGE). - Variables cannot replace identifiers (e.g., table or field names). For dynamic table/record operations, prefer the Record node.
- Variable names are case-sensitive and must match the
Local Development (no publish)
Develop and run this module locally in Node-RED without publishing to npm.
Prerequisites
- Install dependencies in this repo:
npm install - Have Node-RED installed (globally or via npx). Example:
npm install -g node-red
- Install dependencies in this repo:
Option 1: npm link (recommended)
- In this repo:
npm link - In your Node-RED user dir (usually
~/.node-red):npm link node-red-contrib-surrealdb - Start Node-RED (verbose helps during dev):
node-red -v - After code changes, restart Node-RED to reload nodes.
- To unlink: in
~/.node-redrunnpm unlink node-red-contrib-surrealdb, then in this repo runnpm unlink.
- In this repo:
Option 2: Local path install
- In this repo:
npm install - In
~/.node-red:npm install /absolute/path/to/node-red-contrib-surrealdb - Start or restart Node-RED. Re-run step 2 after changes (or switch to Option 1 for faster iteration).
- In this repo:
Tips
- Ensure the SurrealDB connection URL uses
ws://orwss://and points to/rpcfor live queries. - Use Node-RED debug logs: run with
-vand check the console for node status and errors.
- Ensure the SurrealDB connection URL uses
Example Flow
An importable example for testing the Live node is provided at examples/live-query-basic.json.
Import
- In Node-RED, menu → Import → select file → choose
examples/live-query-basic.json. - Open the
SurrealDB Live Demotab. - Double-click the connection node and set URL/NS/DB and credentials.
- The
SurrealDB livenode subscribes to tablepersonwith Autostart enabled. - Use the
Start/Stopinject nodes to control the subscription if needed.
- In Node-RED, menu → Import → select file → choose
Test
- Create/update/delete records in the
persontable (e.g., from SurrealQL console or another flow). - Watch events appear in the
Live OutDebug node. Messages containmsg.actionandmsg.payload.
- Create/update/delete records in the