If you have not done so yet, be sure you have obtained credentials from the
console.
1
Install packages
2
Configure the extension
The simplest way to use Nile with Elysia is to register the extension. This will automatically mount Nile’s default API routes (authentication, tenant management, etc.) onto your Elysia application.
app.ts
Custom Authentication Plugin
For a more idiomatic Elysia experience, you can create a plugin that derives the authenticated user and exposes helper methods. This allows you to access the user directly in your handlers using destructuring.Auth.user directly.
Context Awareness
The@niledatabase/elysia extension automatically handles context for the built-in routes it registers.
However, for custom routes, you need to manually provide the context if your logic depends on Nile (e.g., querying the DB with tenant isolation or performing Auth operations).
The extension
Under the hood, the elysia extension does the following:- Maps Nile’s
handlersandpathsto Elysia’s router (e.g.app.get,app.post). - Intercepts requests using
onHandleRequestto detect Elysia contexts. - Automatically propagates request headers and tenant IDs into the Nile context for safe, isolated execution.
Handlers and paths
The extension iterates over the configured paths in the Nile Server SDK and registers them directly with your Elysia app instance. This ensures that all standard authentication and tenant management routes are available without manual configuration.Context Management
For requests handled by the extension, it wraps the execution innile.withContext. This ensures that nile.db and nile.auth calls within those handlers automatically respect the current tenant and user session, applying necessary authorization policies.