This package (@niledatabase/server
) is part of Nile's Javascript SDK.
Nile's server-side Javascript package provides:
- 🔐 Methods for working with Nile's user and tenant APIs
- 🔑 Generated routes for authentication
- 🔍 SQL query execution with tenant and user context management
Nile is a Postgres platform that decouples storage from compute, virtualizes tenants, and supports vertical and horizontal scaling globally to ship B2B applications fast while being safe with limitless scale. All B2B applications are multi-tenant. A tenant/customer is primarily a company, an organization, or a workspace in your product that contains a group of users. A B2B application provides services to multiple tenants. Tenant is the basic building block of all B2B applications.
npm install @niledatabase/server
The recommended way to initialize Nile SDK is with .env
file. You can copy the environment variables from Nile console. It should look similar to this:
NILEDB_USER=username
NILEDB_PASSWORD=password
NILEDB_API_URL=https://us-west-2.api.thenile.dev/v2/databases/DBID
NILEDB_POSTGRES_URL=postgres://us-west-2.db.thenile.dev:5432/dbname
import Nile from '@niledatabase/server';
const nile = await Nile();
import Nile from '@niledatabase/server';
const nile = await Nile({
user: 'username',
password: 'password',
debug: true
});
const tenant = await nile.api.tenants.createTenant(tenantName);
if (tenant instanceof Response) {
const err = await tenant.text()
console.log("ERROR creating tenant: ", tenant, err);
return { message: "Error creating tenant" };
}
nile.tenant_id = tenant.id // set context
// Get tenant name doesn't need any input parameters
// because it uses the tenant ID and user token from the context
const checkTenant = await tenantNile.api.tenants.getTenant();
nile.tenant_id = tenant.id // set context
const todos = await nile.db
.query("select * from todos order by title")
.catch((e: Error) => {
console.error(e);
}); // no need for where clause if you previously set Nile context
In addition to user
and password
, a fully configured SDK must also have values for db.host
, databaseName
, and databaseId
. If the values are not provided in either the .env
file or the instance configuration, the init()
function should be called, which will allow the SDK to automatically configure itself. For production, it is recommended to set those values.
Configuration passed to Server
takes precedence over .env
vars.
Property | Type | .env var | Description |
---|---|---|---|
user | string |
NILEDB_USER | Required. Username for database authentication. |
password | string |
NILEDB_PASSWORD | Required. Password for database authentication. |
databaseId | string |
NILEDB_ID | ID of the database. |
databaseName | string |
NILEDB_NAME | Name of the database. |
tenantId | string |
NILEDB_TENANT | ID of the tenant associated. |
userId | string |
ID of the user associated. | |
db | PoolConfig |
Configuration object for pg.Pool. | |
db.host | string |
NILEDB_HOST | Base host for DB. Defaut is db.thenile.dev
|
api | object |
Configuration object for API settings. | |
api.basePath | string |
NILEDB_API_URL | Base host for API for a specific region. |
api.cookieKey | string |
Key for API cookie. Default is token . |
|
api.token | string |
NILEDB_TOKEN | Token for API authentication. Mostly for debugging. |
debug | boolean |
Flag for enabling debug logging. |
- You can learn more about Nile and the SDK in [https://thenile.dev/docs]
- You can find detailed code examples in our main repo
- Nile SDK interacts with APIs in Nile Auth service. You can learn more about it in the repository and the docs