Using the Nile-JS SDK DB client
db
object is used to query the database. It is designed as a tenant-aware connection pool.
When nile.tenantId
is set, the db
object will use the correct tenant’s virtual database.
query
method is used to query the database. This is the main method exposed by the db
object.
sql
: The SQL query to execute.params
: The parameters to use in the query.nile.tenantId
, this will be a cross-tenant query.
If you set nile.withContext({ tenantId })
, the query will run against the tenant’s virtual database.
When inserting data into a tenant-aware table, you need to provide the tenant id - and it has to belong to an existing tenant.
If you set nile.withContext({ tenantId })
, the tenant id must match the tenant id in the table.
In the example below, we also use parameter substitution to prevent SQL injection.
nile.db
is a context-less way to query the database. nile.query
uses context when querying the database, if the context is setclient
method is used to get a client from the database connection pool. This can be useful if you want to run several queries on the same connection.
client.release()
to release it back to the pool.client
to run a transaction - a group of queries that are executed as a single unit.
If any of the queries fail, the transaction is rolled back.
If all queries succeed, the transaction is committed.
The client
object guarantees that all queries are executed on the same connection.
It is then released back to the pool after the transaction is committed or rolled back.