If you have not done so yet, be sure you have obtained credentials from the console.
1
Install packages
2
Configure the extension
Out of the box, the express extension will add all routes and middleware to your express application to ensure everything works properly.
app/api/[...nile]/route.ts
The extension
Under the hood, the express extension does the following:- Reconfigures
handlers[method]
andpaths[method]
to be compatible with express. - Modifies the request handling to match express
- Uses a combination of
AsyncLocalStorage
and middleware to ensure each request has the correct context.
Handlers and paths
The express extension registers routes though the specific methods, not as middleware, because nile-auth can span a lot of routes (/api/auth/signin
, /api/tenants/:tenantId/users
, etc). This allows you to override specific routes, or add new ones since nile only matches specific routes (either the default, or ones that have been configured)
Additionally, since the base @niledatabase/server
assumes standard Request
and Response
objects, this extension turns express objects into those and responds accordingly.
Lastly, because of the express lifecycle, its important to be sure that each requests is executed within a specific context. We automatically add a :tenantId
param listener and set the context of the nile instance to that tenantId for convenience. That way, no matter the request or DB query, if you have the tenantId in your param (this also works automatically with the nile.tenantId
cookie), there’s no additional work that needs done.