Toolkit for building multi-tenant applications with Next.js, Remix, Express, and more
nile.withContext
.
Additionally, the context for the database is enforced for calls via nile.query(...)
, but not for nile.db.query(...)
.
The SDK will try its hardest to use context whenever possible. Under the hood, it uses AsyncLocalStorage
, which relies on execution within the same request lifecycle. If you await or dispatch across async boundaries without using the SDK, the context may be lost, in which will default to the last used.
The easiest way to deal with this and ensure proper context 100% of the time is to use a cookie saved by your application when a user determines their tenant, and the SDK will automatically look for nile.tenant-id
cookie (TENANT_COOKIE
exported by @niledatabase/server
).
signIn
method with the chosen provider. Your server handles all requests, which in most cases is simply forwarding them on to the Nile auth service with some additional information to help identify the client.nile.auth.getSession
or a full user profile via nile.users.getSelf