@niledatabase/server 1.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -4,25 +4,57 @@ Consolidates the API and DB for working with Nile.
4
4
 
5
5
  ## Usage
6
6
 
7
+ ### With configuration object
8
+
7
9
  ```ts
8
10
  import Server from '@niledatabase/server';
9
11
 
10
- const connection = {
11
- user: process.env.NILE_USER,
12
- password: process.env.NILE_PASSWORD,
13
- };
14
12
  const nile = Server({
15
- workspace: String(process.env.NILE_WORKSPACE),
16
- database: String(process.env.NILE_DATABASE),
17
- api: {
18
- basePath: String(process.env.BASE_PATH),
19
- },
20
- db: {
21
- connection,
22
- },
13
+ user: 'username',
14
+ password: 'password',
23
15
  });
24
16
 
25
- await nile.api.createTenant({name: 'name'})
17
+ await nile.api.createTenant({ name: 'name' });
18
+
19
+ await nile.db.query('select * from todo');
20
+ ```
21
+
22
+ ### With env vars
23
+
24
+ ```bash
25
+ NILEDB_USER=username
26
+ NILEDB_PASSWORD=password
27
+ ```
28
+
29
+ ```ts
30
+ import Server from '@niledatabase/server';
31
+
32
+ const nile = Server();
26
33
 
27
- await nile.db.("todo").withTenant('tenant-id');
34
+ await nile.api.createTenant({ name: 'name' });
35
+
36
+ await nile.db.query('select * from todo');
28
37
  ```
38
+
39
+ ## Autoconfiguration
40
+
41
+ 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 SDK will automatically phone home to configure itself. For production, it is recommended to set those values.
42
+
43
+ ## Configuration
44
+
45
+ Configuration passed to `Server` takes precedence over `.env` vars.
46
+
47
+ | Property | Type | .env var | Description |
48
+ | ------------- | ------------ | --------------- | ------------------------------------------------------------------------ |
49
+ | user | `string` | NILEDB_USER | Required. Username for database authentication. |
50
+ | password | `string` | NILEDB_PASSWORD | Required. Password for database authentication. |
51
+ | databaseId | `string` | NILEDB_ID | ID of the database. |
52
+ | databaseName | `string` | NILEDB_NAME | Name of the database. |
53
+ | tenantId | `string` | NILEDB_TENANT | ID of the tenant associated. |
54
+ | userId | `string` | | ID of the user associated. |
55
+ | db | `PoolConfig` | | Configuration object for [pg.Pool](https://node-postgres.com/apis/pool). |
56
+ | api | `object` | | Configuration object for API settings. |
57
+ | api.basePath | `string` | NILEDB_API | Base host for API for a specific region. Default is `api.thenile.dev`. |
58
+ | api.cookieKey | `string` | | Key for API cookie. Default is `token`. |
59
+ | api.token | `string` | NILEDB_TOKEN | Token for API authentication. Mostly for debugging. |
60
+ | debug | `boolean` | | Flag for enabling debug logging. |
package/dist/Server.d.ts CHANGED
@@ -1,9 +1,9 @@
1
+ import { Pool } from 'pg';
1
2
  import { InstanceConfig, ServerConfig } from './types';
2
3
  import { Config } from './utils/Config';
3
4
  import Auth from './auth';
4
5
  import Users from './users';
5
6
  import Tenants from './tenants';
6
- import { NileDatabaseI } from './db';
7
7
  type Api = {
8
8
  auth: Auth;
9
9
  users: Users;
@@ -16,15 +16,14 @@ declare class Server {
16
16
  private servers;
17
17
  constructor(config?: ServerConfig);
18
18
  setConfig(cfg: Config): void;
19
- set database(val: string | void);
20
- set workspace(val: string | void);
19
+ set databaseId(val: string | void);
21
20
  get userId(): string | undefined | null;
22
21
  set userId(userId: string | undefined | null);
23
22
  get tenantId(): string | undefined | null;
24
23
  set tenantId(tenantId: string | undefined | null);
25
24
  get token(): string | undefined | null;
26
25
  set token(token: string | undefined | null);
27
- get db(): NileDatabaseI;
26
+ get db(): Pool;
28
27
  /**
29
28
  * A utility function if you want to manage different NileDB instances yourself
30
29
  * returns the global Server object, an existing server that's already been configured,
@@ -32,5 +31,5 @@ declare class Server {
32
31
  */
33
32
  getInstance(config: InstanceConfig): Server;
34
33
  }
35
- export default function Nile(config: ServerConfig): Server;
34
+ export default function Nile(config?: ServerConfig): Server;
36
35
  export {};
@@ -1,20 +1,39 @@
1
- import { RestModels } from '@niledatabase/js';
2
1
  import { Config } from '../utils/Config';
3
2
  import { NileRequest, NileResponse } from '../utils/Requester';
3
+ import { CreateBasicUserRequest, LoginUserResponse } from '../users';
4
+ export interface TenantSSORegistration {
5
+ configUrl: string;
6
+ clientId: string;
7
+ clientSecret: string;
8
+ redirectURI: string;
9
+ emailDomains: Array<string>;
10
+ enabled?: boolean;
11
+ }
12
+ export interface SSOProvider {
13
+ readonly id?: string;
14
+ tenantId?: string;
15
+ provider?: string;
16
+ configUrl: string;
17
+ clientSecret?: string;
18
+ clientId: string;
19
+ redirectURI: string;
20
+ emailDomains?: Array<string>;
21
+ enabled?: boolean;
22
+ }
4
23
  export default class Auth extends Config {
5
24
  constructor(config: Config);
6
25
  get loginUrl(): string;
7
- login: (req: NileRequest<RestModels.CreateBasicUserRequest>, init?: RequestInit) => NileResponse<RestModels.LoginUserResponse>;
8
- loginSSO: (redirectUrl: string) => (req: NileRequest<unknown>) => NileResponse<RestModels.TenantSSORegistration[]>;
26
+ login: (req: NileRequest<CreateBasicUserRequest>, init?: RequestInit) => NileResponse<LoginUserResponse>;
27
+ loginSSO: (redirectUrl: string) => (req: NileRequest<unknown>) => NileResponse<Response>;
9
28
  loginSSOUrl: (provider: string) => string;
10
29
  get signUpUrl(): string;
11
- signUp: (req: NileRequest<RestModels.CreateBasicUserRequest>, init?: RequestInit) => NileResponse<RestModels.LoginUserResponse>;
30
+ signUp: (req: NileRequest<CreateBasicUserRequest>, init?: RequestInit) => NileResponse<LoginUserResponse>;
12
31
  updateProviderUrl(providerName: string): string;
13
32
  get listTenantProvidersUrl(): string;
14
- listTenantProviders: (req: NileRequest<void | Headers>, init?: RequestInit) => NileResponse<RestModels.TenantSSORegistration[]>;
15
- createProvider: (req: NileRequest<RestModels.RegisterTenantSSORequest>, init?: RequestInit) => NileResponse<RestModels.TenantSSORegistration>;
16
- updateProvider: (req: NileRequest<RestModels.RegisterTenantSSORequest>, init?: RequestInit) => NileResponse<RestModels.TenantSSORegistration>;
33
+ listTenantProviders: (req: NileRequest<void | Headers>, init?: RequestInit) => NileResponse<TenantSSORegistration[]>;
34
+ createProvider: (req: NileRequest<SSOProvider>, init?: RequestInit) => NileResponse<TenantSSORegistration>;
35
+ updateProvider: (req: NileRequest<SSOProvider>, init?: RequestInit) => NileResponse<TenantSSORegistration>;
17
36
  providerUrl(email?: undefined | string): string;
18
- listProviders: (req: NileRequest<void | RestModels.CreateBasicUserRequest>, init?: RequestInit) => NileResponse<RestModels.TenantSSORegistration[]>;
37
+ listProviders: (req: NileRequest<void | CreateBasicUserRequest>, init?: RequestInit) => NileResponse<TenantSSORegistration[]>;
19
38
  getSSOCallbackUrl: (param: Headers | string) => string;
20
39
  }
@@ -1,8 +1,9 @@
1
+ import { Pool } from 'pg';
1
2
  import { Config } from '../utils/Config';
2
- import NileDatabase, { NileDatabaseI } from './NileInstance';
3
+ import NileDatabase from './NileInstance';
3
4
  export default class DBManager {
4
5
  connections: Map<string, NileDatabase>;
5
6
  private makeId;
6
7
  constructor(config: Config);
7
- getConnection(config: Config): NileDatabaseI;
8
+ getConnection(config: Config): Pool;
8
9
  }
@@ -1,15 +1,14 @@
1
1
  /// <reference types="node" />
2
- import { Knex } from 'knex';
2
+ import { Pool } from 'pg';
3
3
  import { Config } from '../utils/Config';
4
4
  declare class NileDatabase {
5
- knex: Knex;
5
+ pool: Pool;
6
6
  tenantId?: undefined | null | string;
7
7
  userId?: undefined | null | string;
8
8
  id: string;
9
- config: any;
9
+ config: Config;
10
10
  timer: NodeJS.Timeout | undefined;
11
11
  constructor(config: Config, id: string);
12
12
  startTimeout(): void;
13
13
  }
14
- export type NileDatabaseI = (table?: string) => Knex;
15
14
  export default NileDatabase;
@@ -1,2 +1 @@
1
1
  export { default } from './DBManager';
2
- export { NileDatabaseI } from './NileInstance';