From b9af518b98cf5bd4645cd67d6e9f9ae2e03ea20d Mon Sep 17 00:00:00 2001 From: jrea Date: Fri, 5 Apr 2024 11:25:55 -0400 Subject: [PATCH] chore(react): clean out unused deps --- packages/server/README.md | 8 ++------ packages/server/src/Server.test.ts | 10 +++++----- packages/server/src/Server.ts | 10 ++++++++-- packages/server/src/db/PoolProxy.ts | 19 +++++++++++-------- packages/server/src/index.ts | 8 +++++--- packages/server/src/utils/Config/index.ts | 5 ++--- .../test/integration/integration.test.ts | 5 ++--- 7 files changed, 35 insertions(+), 30 deletions(-) diff --git a/packages/server/README.md b/packages/server/README.md index a33a9a2f..45d84010 100644 --- a/packages/server/README.md +++ b/packages/server/README.md @@ -9,13 +9,11 @@ Consolidates the API and DB for working with Nile. ```ts import Nile from '@niledatabase/server'; -const nile = new Nile({ +const nile = await Nile({ user: 'username', password: 'password', }); -await nile.init(); - await nile.api.createTenant({ name: 'name' }); await nile.db.query('select * from todo'); @@ -31,9 +29,7 @@ NILEDB_PASSWORD=password ```ts import Nile from '@niledatabase/server'; -const nile = new Nile(); - -await nile.init(); +const nile = await Nile(); await nile.api.createTenant({ name: 'name' }); diff --git a/packages/server/src/Server.test.ts b/packages/server/src/Server.test.ts index 69ef1305..3b70688e 100644 --- a/packages/server/src/Server.test.ts +++ b/packages/server/src/Server.test.ts @@ -1,4 +1,4 @@ -import Nile from './Server'; +import { Server } from './Server'; describe('server', () => { fit('has reasonable defaults', () => { @@ -8,7 +8,7 @@ describe('server', () => { user: 'username', password: 'password', }; - const server = new Nile(config); + const server = new Server(config); expect(server.config.db).toEqual({ host: 'db.thenile.dev', port: 5432, @@ -24,7 +24,7 @@ describe('server', () => { user: 'username', password: 'password', }; - const nile = new Nile(config); + const nile = new Server(config); nile.tenantId = 'tenantId'; nile.userId = 'userId'; for (const api in nile.api) { @@ -39,7 +39,7 @@ describe('server', () => { user: 'username', password: 'password', }; - const nile = new Nile(config); + const nile = new Server(config); const another = nile.getInstance({ databaseId: 'somethingelse?!', @@ -68,7 +68,7 @@ describe('server', () => { user: 'username', password: 'password', }; - const nile = new Nile(config); + const nile = new Server(config); const another = nile.getInstance({ databaseId: 'somethingelse?!', diff --git a/packages/server/src/Server.ts b/packages/server/src/Server.ts index 2ae09eee..7983ee15 100644 --- a/packages/server/src/Server.ts +++ b/packages/server/src/Server.ts @@ -26,7 +26,7 @@ const init = (config: Config): Api => { }; }; -class Server { +export class Server { config: Config; api: Api; private manager: DbManager; @@ -63,6 +63,7 @@ class Server { this.setConfig(updatedConfig); this.manager = new DbManager(this.config); this.api = init(updatedConfig); + return this; } set databaseId(val: string | void) { @@ -156,4 +157,9 @@ class Server { } } -export default Server; +export async function create(config?: ServerConfig): Promise { + const server = new Server(config); + return await server.init(); +} + +export default create; diff --git a/packages/server/src/db/PoolProxy.ts b/packages/server/src/db/PoolProxy.ts index 300076b5..8e4ee9a6 100644 --- a/packages/server/src/db/PoolProxy.ts +++ b/packages/server/src/db/PoolProxy.ts @@ -11,14 +11,17 @@ export function createProxyForPool(pool: Pool, config: Config): Pool { return new Proxy(pool, { get(target: AllowAny, property) { if (property === 'query') { - if (!config.user || !config.password) { - error( - 'Cannot connect to the database. User and/or password are missing. Generate them at https://console.thenile.dev' - ); - } else if (!config.db.database) { - error( - 'Database id is missing from the config. Either call `nile.init()` when you create the NileDB server or set NILEDB_ID in your .env' - ); + // give connection string a pass for these problems + if (!config.db.connectionString) { + if (!config.user || !config.password) { + error( + 'Cannot connect to the database. User and/or password are missing. Generate them at https://console.thenile.dev' + ); + } else if (!config.db.database) { + error( + 'Database name is missing from the config. Call `nile.init()` or set NILEDB_ID in your .env' + ); + } } const caller = target[property]; return function query(...args: AllowAny) { diff --git a/packages/server/src/index.ts b/packages/server/src/index.ts index 922e362d..03109fa5 100644 --- a/packages/server/src/index.ts +++ b/packages/server/src/index.ts @@ -1,6 +1,8 @@ export * from './types'; -import { default as Server } from './Server'; +import create from './Server'; -export default Server; +export { Server } from './Server'; -module.exports = Server; +export default create; + +module.exports = create; diff --git a/packages/server/src/utils/Config/index.ts b/packages/server/src/utils/Config/index.ts index cfd4d362..75e33c2a 100644 --- a/packages/server/src/utils/Config/index.ts +++ b/packages/server/src/utils/Config/index.ts @@ -82,9 +82,8 @@ export class Config { } constructor(config: ServerConfig) { - const envVarConfig: EnvConfig = { - config, - }; + const envVarConfig: EnvConfig = { config }; + this.databaseId = getDatbaseId(envVarConfig) as string; this.user = getUsername(envVarConfig) as string; this.password = getPassword(envVarConfig) as string; diff --git a/packages/server/test/integration/integration.test.ts b/packages/server/test/integration/integration.test.ts index b455de94..99e655b8 100644 --- a/packages/server/test/integration/integration.test.ts +++ b/packages/server/test/integration/integration.test.ts @@ -1,4 +1,4 @@ -import Server from '../../src/Server'; +import Nile, { Server } from '../../src/Server'; import { ServerConfig } from '../../src/types'; async function toJSON(body: BodyInit) { @@ -58,8 +58,7 @@ describe.skip('api integration', () => { describe.skip('db integration', () => { it('queries', async () => { - const nile = new Server(config); - await nile.init(); + const nile = await Nile(config); const res = await nile.db.query('select * from tenants'); expect(res.rowCount).toBeGreaterThan(0); });