Facilitates MongoDB nodes in NodeScript.
NodeScript is good at connecting services on the Internet. However, most databases like MongoDB are only available on internal cluster networks.
In order to communicate to MongoDB from NodeScript you need to deploy the adapter application to your cluster.
graph LR
subgraph Cluster
adapter[MongoDB Adapter] -- private network --> MongoDB
end
subgraph NodeScript Graph
node[MongoDB Nodes] -- public internet --> adapter
end
A single adapter application is able to connect to multiple different MongoDB databases, thus typically a single adapter deployment is required to facilitate connections to all databases you need.
NodeScript MongoDB Adapter is currently available as a docker image at ghcr.io/nodescriptlang/adapter-mongodb.
Example: docker run -d -e AUTH_SECRET=<some_secret> -p 8080:8080 ghcr.io/nodescriptlang/adapter-mongodb
NodeScript MongoDB Adapter can be configured with the following environment variables:
-
AUTH_SECRET — authentication shared secret, used to restrict access from graphs to your adapter; graphs must include in
adapterUrlfield as follows:https://<AUTH_SECRET>@<hostname> -
POOL_SIZE (default: 5) - the maximum number of connections to establish to each database the adapter connects to.
-
POOL_TTL_MS (default: 60_00) - pools created that many millis ago will be recycled (this eliminates connection leaks otherwise occurring with high-throughput scenarios)
-
CONNECT_TIMEOUT_MS (default: 10_000) — the adapter will throw an error if the connection cannot be established within specified timeout.
-
SWEEP_INTERVAL_MS (default: 30_000) — the interval at which pools are checked for TTL and closed.
MongoDB Adapter acts as a thin proxy between HTTP and MongoDB driver. When it comes to configuring the compute resources it's best to keep the following in mind:
-
There is no heavy processing on the adapter side, so in a typical case the pod should be fine wuth requesting around 0.1 vCPU. Throttling CPU is not recommended.
-
The memory requirements largely depends on the payloads being sent and received. The adapter will fully buffer the responses from the database before sending them as JSON. If you don't have a solid idea about the size of the payloads, then start with requesting 400MB memory and monitor the application to see if the adjustments are necessary.
-
The memory limit should be set to roughly x2 — x4 range of the requested amount, especially if the load is uneven.
-
The adapter is built to be scaled horizontally. For serving many concurrent requests it is recommended to increase replicas count as opposed to increasing the pool size and corresponding resource requests/limits.
NodeScript MongoDB Adapter exposes the following Prometheus metrics on /metrics endpoint:
-
nodescript_mongodb_adapter_connections— counters depicting connection pool operations, further narrowed down by thetypelabel:connect— the connection successfully established, but no connection added to the pool just yetconnectionCreated— a new connection is added to the poolconnectionClosed— the pool recycles unused connectionfail— connection failed
-
nodescript_mongodb_adapter_latency— histogram with response latencies, includes the following labels:method— one of the endpoint methods (e.g.findOne,updateOne,updateMany, etc.)error— the error code, omitted if the response was successful
- Ensure your adapter and database are deployed.
- Login to Nodescript
- Either open an existing graph in your chosen workspace, or create a new one.
- Add a
Mongo DB / Connectnode in a graph, then input your adapter and Mongo DB connection urls.- These can be securely stored as secrets/variables in the workspace
Variablessection, then accessed in graphs within that workspace and connected to the node, as shown above.
- These can be securely stored as secrets/variables in the workspace
- Add a
Mongo DBquery node and connect the output of theMongo / Connectto theconnectionsocket. There are a variety of query nodes available which correspond to standard Mongo queries. - Use the node to configure your Mongo query as you would with the regular javascript mongo library.
- Running the
Mongo DBquery node will query your designated database using the connection established with your adapter.
