@niledatabase/server
package to integrate into developer applications easier.
It allows developers to hook into the life cycle of the SDK, from REST calls to db queries and set the context according to whatever method is being used to store user session information beyond cookies.
Understanding context
@niledatabase/server
contains three basic items that are controlled via context:
headers
tenantId
userId
Each of these can be set and will cause the SDK to use them either as a REST call or in a database query.
Headers
Headers contain the auth cookies used to allows users access tonile-auth
.
More often than not, a framework-specific extension will handle setting headers
automatically when it comes to serving REST request to the client.
nextjs
express
tenantId and userId
The tenantId and userId are used to further authorize users in the database. There are many ways that a tenantId can be stored, but the most common ways would be stored in the URL or in a cookie. Lets have a look at a cookie in express for illustrative purposes.naive approach
This relies on synchronous execution within the same request lifecycle. If you await or dispatch across async boundaries without using the SDK’s internal context propagation (like AsyncLocalStorage), the context may be lost, which defaults to the last used.
Extension methods
withContext
Called when everwithContext
is called, including internally by the SDK. Use this function set the tenantId
, userId
or headers
onRequest
Called before the request is sent viafetch
to nile-auth
. the internal Request
object and ctx
methods are provided to the function.
onResponse
Called immediately after thefetch
completes. The raw Response
object and ctx
are provided to the function.
onHandleRequest
Replaces the calls in the sdk that go tonile-auth
. This function is provided the params
from the calling framework. Useful for turning whatever framework’s REST handlers into a standard Request
object to be sent to nile-auth
and then calling nile-auth
directly from the extension.
Must return ExtensionState.onHandleRequest
from the function to stop the standard handlers from being called.
onConfigure
Allows for runtime configuration changes by extensions.withUserId
Called withwithContext
, allows overriding of a user id within the context.
withTenantId
Called withwithContext
, allows overriding of a tenant id within the context.
replace.handlers
Allows a full replacement of handlers that do not match any expected signature from the SDK. This is specifically for server side frameworks like Nitro, which expect a specific signature fromdefineEventHandler
that the sdk does not have, as the standard signature is const { GET, POST, PUT, DELETE } = nile.handlers