You can use Nile with any of Drizzle’s Postgres drivers, we’ll be showing the use of node-postgres below.
Step 1 - Install packages
npm
yarn
pnpm
bun
Step 2 - Initialize the driver and make a query
If you need to provide your existing driver:
Connecting to a virtual tenant database
Nile provides virtual tenant databases, when you set the tenant context, Nile will direct your queries to the virtual database for this particular tenant and all queries will apply to that tenant (i.e. select * from table will result records only for this tenant).
In order to set the tenant context, we wrap each query in a transaction that sets the appropriate tenant context before running the transaction.
The tenant ID can simply be passed into the wrapper as an argument:
If you are using a web framwork that supports it, you can set up AsyncLocalStorage and use middleware to populate it with the tenant ID. In this case, your Drizzle client setup will be:
And then, configure a middleware to populate the the AsyncLocalStorage and use tenantDB method when handling requests: