Skip to main content

Databases

The database can be defined in the globals part of a configuration or the action itself. Defining it in the globals section allows any action to utilise the database. Any database config defined with a matching name will have its existing connection reused.

Options

NameTypeDescription
driverstringthe database driver (postgres, mysql, mssql, sqlite, mongodb)
conn_stringstring (optional)the full connection string to connect to a database
userstring (optional)database user
passstring (optional)database password
hoststring (optional)database host
dbnamestring (optional)name of the db
paramsarrayoptional parameters for the database
document_operationobjectrequired for NoSQL operations eg. MongoDB

note if using user, pass, host, dbname is supplied do not use conn_string

document_operation

NameTypeDescription
databasestringthe database to select
collectionstringthe collection to target
operationstringfind, findOne, findOneAndDelete, findOneAndReplace, deleteOne, deleteMany, updateOne, updateMany, insertOne, insertMany, aggregate
filterstring (optional)required for any of the above operations except aggregate, supply JSON document as a string
pipelinestring (optional)required if operation is set to aggregate, supply JSON document as a string
updatestring (optional)document as a JSON string
optionsstring (optional)options as a JSON string

Connection Strings

Below are example connection strings, please adjust the parameters to your own requirements.

  • Postgres

    • CockroachDB

    • YugabyteDB

    • Timescale

    • Any postgres compatible databases

      postgresql://username:password@host:port/dbname[?paramspec]
  • MySQL

    • MariaDB

    • TiDB

    • Any mysql compatible database

      mysql://root:college@localhost:3306/employees
  • SQLite

    sqlite:///home/my-user/db-files/Sqlite-Car_Database.db
  • MSSQL / SqlServer

    jdbc:sqlserver://localhost:1433;databaseName=AdventureWorks;user=SA;password=Passw0rd;trustServerCertificate=true;
  • MongoDB

    • Azure Cosmos DB

    • Amazon DocumentDB

Global Example

name: database
version: 0.0.1

databases:
main:
driver: postgres
conn_string: |
postgresql://a|env::POSTGRES_USER|:a|env::POSTGRES_PASS|@a|env::POSTGRES_HOST|?connect_timeout=10"

Action Example

Note the query option is for relational based database, and not used for NoSQL/Document Store type databases such as MongoDB, Azure Cosmos DB, Amazon DocumentDB.

name: database
version: 0.0.1

interfaces:
database/example:
output: http
method: POST

actions:
- name: UserList
database:
dbname: main
driver: postgres
conn_string: |
postgresql://a|env::POSTGRES_USER|:a|env::POSTGRES_PASS|@a|env::POSTGRES_HOST|?connect_timeout=10"
query: |
SELECT * FROM Users;

MongoDB Find Example

Example to send a HTTP POST with a JSON payload eg. {"name": "test"} to find a document with username called test.

MongoDB Find
loading...

MongoDB Aggregate Example

Example to send a HTTP POST with a JSON payload eg. {"starts_with": "test"} to find documents using regex and limiting the result set to 30.

MongoDB Aggregate Pipeline
loading...