Skip to content

Commit

Permalink
fix(server): use env vars, add fake phone home, integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jrea committed Apr 1, 2024
1 parent ad0d56b commit 1cddfd6
Show file tree
Hide file tree
Showing 21 changed files with 504 additions and 1,659 deletions.
1,397 changes: 0 additions & 1,397 deletions lib/nile/CHANGELOG.md

This file was deleted.

50 changes: 0 additions & 50 deletions lib/nile/package.json

This file was deleted.

3 changes: 2 additions & 1 deletion packages/server/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
dist
dist
*.env*
55 changes: 47 additions & 8 deletions packages/server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,59 @@ Consolidates the API and DB for working with Nile.

## Usage

### With configuration object

```ts
import Server from '@niledatabase/server';

const nile = Server({
databaseId: String(process.env.NILE_DATABASE),
db: {
connection: {
user: process.env.NILE_USER,
password: process.env.NILE_PASSWORD,
},
},
user: 'username',
password: 'password',
});

await nile.api.createTenant({name: 'name'})
await nile.api.createTenant({ name: 'name' })

await nile.db.("todo").withTenant('tenant-id');
```

### With env vars

```bash
NILEDB_USER=username
NILEDB_PASSWORD=password
```

```ts
import Server from '@niledatabase/server';

const nile = Server();

await nile.api.createTenant({ name: 'name' })

await nile.db.("todo").withTenant('tenant-id');
```

## Phone home

In addition to `user` and `password`, a fully configured SDK must also have values for `db.connection.host`, `databaseName`, and `databaseId`. If the values are not provided in either the `.env` file or the instance configuration, the SDK will automatically phone home to configure itself. For production, it is recommended to set those values.

## Configuration

Configuration passed to `Server` takes precedence over `.env` vars.

| Property | Type | .env var | Description |
| ------------------ | ------------- | --------------- | ------------------------------------------------------------------------------------ |
| databaseId | `string` | NILEDB_ID | ID of the database. |
| username | `string` | NILEDB_USER | Username for database authentication. |
| password | `string` | NILEDB_PASSWORD | Password for database authentication. |
| 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 | `Knex.Config` | | Configuration object for [knex.js](https://knexjs.org/guide/#configuration-options). |
| db.connection.host | `string` | NILEDB_HOST | The host of the database. Default is `db.thenile.dev`. |
| db.connection.port | `number` | NILEDB_PORT | The port of the database. Default is `5432`. |
| api | `object` | | Configuration object for API settings. |
| api.basePath | `string` | NILEDB_API | Base host for API for a specific region. Default is `api.thenile.dev`. |
| 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 mode. |
8 changes: 3 additions & 5 deletions packages/server/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('dotenv').config();

module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};

process.env.USER = '';
process.env.PASSWORD = '';
process.env.WORKSPACE = '';
process.env.DATABASE = '';
process.env.USER_ID = '';
process.env.NODE_ENV = 'TEST';
6 changes: 5 additions & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"@apidevtools/swagger-cli": "^4.0.4",
"@babel/core": "^7.23.3",
"@types/jest": "^29.5.9",
"@types/sync-fetch": "^0",
"@typescript-eslint/parser": "^5.62.0",
"babel-loader": "^9.1.3",
"dts-cli": "^2.0.3",
Expand All @@ -63,8 +64,11 @@
},
"dependencies": {
"@niledatabase/js": "^2.0.0",
"dotenv": "^16.4.5",
"install": "^0.13.0",
"jose": "^4.15.4",
"knex": "^2.5.1",
"pg": "^8.11.3"
"pg": "^8.11.3",
"sync-fetch": "^0.5.2"
}
}
5 changes: 3 additions & 2 deletions packages/server/src/Server.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import Nile from './Server';

describe('server', () => {
it('has reasonable defaults', () => {
fit('has reasonable defaults', () => {
const config = {
databaseId: 'databaseId',
databaseName: 'databaseName',
username: 'username',
password: 'password',
};
const server = Nile(config);
expect(server.config.db.connection).toEqual({
host: 'db.thenile.dev',
port: 5432,
database: 'databaseId',
database: 'databaseName',
user: 'username',
password: 'password',
});
Expand Down
15 changes: 8 additions & 7 deletions packages/server/src/Server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { InstanceConfig, PgConnectionConfig, ServerConfig } from './types';
import { InstanceConfig, ServerConfig } from './types';
import { Config } from './utils/Config';
import Auth from './auth';
import Users from './users';
Expand Down Expand Up @@ -34,7 +34,7 @@ class Server {
private servers: Map<string, Server>;

constructor(config?: ServerConfig) {
this.config = new Config(config);
this.config = new Config(config as ServerConfig, true);
this.servers = new Map();
const [api] = init(this.config);
this.api = api;
Expand All @@ -43,22 +43,23 @@ class Server {
watchTenantId((tenantId) => {
this.tenantId = tenantId;
});

watchUserId((userId) => {
this.userId = userId;
});

watchToken((token) => {
this.token = token;
});
}

setConfig(cfg: Config) {
this.config = new Config(cfg);
this.config = new Config(cfg, false);
}

set databaseId(val: string | void) {
if (val) {
this.config.databaseId = val;
(this.config.db.connection as PgConnectionConfig).database = val;
this.api.auth.databaseId = val;
this.api.users.databaseId = val;
this.api.tenants.databaseId = val;
Expand Down Expand Up @@ -133,7 +134,7 @@ class Server {

if (existing) {
// be sure the config is up to date
const updatedConfig = new Config(_config);
const updatedConfig = new Config(_config, false);
existing.setConfig(updatedConfig);
// propagage special config items
existing.tenantId = updatedConfig.tenantId;
Expand All @@ -149,8 +150,8 @@ class Server {
}

// export default Server;
export default function Nile(config: ServerConfig) {
export default function Nile(config?: ServerConfig) {
const server = new Server(config);
server.setConfig(new Config(config as ServerConfig));
// server.setConfig(new Config(config as ServerConfig, false));
return server;
}
2 changes: 2 additions & 0 deletions packages/server/src/auth/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ const baseConfig = [
'api',
'createProvider',
'databaseId',
'databaseName',
'db',
'debug',
'getSSOCallbackUrl',
'listProviders',
'listTenantProviders',
Expand Down
2 changes: 2 additions & 0 deletions packages/server/src/db/db.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ describe('db', () => {
it('has expected properties', () => {
const db = new NileDB({
databaseId: 'databaseId',
databaseName: 'databaseName',
username: 'username',
password: 'password',
debug: false,
db: {
connection: { port: 4433 },
},
Expand Down
2 changes: 2 additions & 0 deletions packages/server/src/tenants/tenants.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ const baseConfig = [
'createTenant',
'getTenant',
'db',
'debug',
'databaseId',
'databaseName',
'username',
'password',
];
Expand Down
9 changes: 6 additions & 3 deletions packages/server/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ export type Opts = {
};

export type ServerConfig = {
databaseId: string;
username: string;
password: string;
databaseId?: string;
username?: string;
password?: string;
databaseName?: string;
tenantId?: string | null | undefined;
userId?: string | null | undefined;
debug?: boolean;
db?: Knex.Config;
api?: {
basePath?: string;
Expand All @@ -31,6 +33,7 @@ export type InstanceConfig = {
password: string;
tenantId?: string | null | undefined;
userId?: string | null | undefined;
debug?: boolean;
db?: Knex.Config;
api?: {
basePath?: string;
Expand Down
2 changes: 2 additions & 0 deletions packages/server/src/users/users.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const baseConfig = [
'listTenantUsers',
'listUsers',
'me',
'databaseName',
'debug',
'updateUser',
'username',
'password',
Expand Down
Loading

0 comments on commit 1cddfd6

Please sign in to comment.