Urgent.News

What's breaking now, across thousands of outlets.

Tech

LioranDB TypeScript Series #3: Connection Strings, Transports and Client Lifecycle

LioranDB TypeScript Series #3: Connection Strings, Transports and Client Lifecycle LioranDB TypeScript Series: Build with a developer-first document database powered by Rust and designed for TypeScript. We connected to LioranDB in Part 2 with one line: const client = await LioranDBClient . connect ( process . env . URI ! ); There's quite a bit happening underneath it. Let's unpack it. Connection…

LioranDB TypeScript Series #3 explores connection strings, transport options, and client lifecycle management. To establish a connection, one uses const client = await LioranDBClient.connect(process.env.URI!); The connection URI follows the pattern liorandb://USER:PASSWORD@HOST:PORT/DATABASE. LioranDB supports various schemes like liorandb://, liorandb+http://, liorandb+https://, http://, https://, and grpc://.

When connection strings contain reserved characters such as @, $, or :, these must be URL-encoded using encodeURIComponent. This prevents the URI parser from misinterpreting the characters as part of URI syntax. The connection driver offers a parser function, parseConnectionString, to inspect connection configuration details for tooling, debugging, or infrastructure purposes.

LioranDB allows customizing client behavior through options such as transport, autoRefreshTokens, logoutOnClose, requestTimeoutMS, slowRequestThresholdMS, onWarning, and more. The default transport setting is 'auto', which is recommended for most applications. Diagnostic headers can be added using client.setDiagnosticHeaders({ x-trace-id: 'request-001' }), while response observations can be enabled via client.setResponseObserver(event => { console.log({ transport: event.transport, operation: event.operation, durationMS: event.durationMS, requestId: event.requestId }); });

For server inspection, the client provides functions like client.live(), client.ready(), client.serverInfo(), and client.listDatabases(). Clients should be created once and reused throughout an application's lifecycle to optimize resource usage. Closing the client with await client.close() ensures proper cleanup of resources, transports, and cursors. Attempting to interact with a closed client triggers a ClientClosedError.

Developers are advised to manage clients at appropriate application lifecycle stages, creating a single instance to interact with multiple collections or databases instead of establishing new connections for each query. This approach aligns with the intended usage of a database client and promotes cleaner, more efficient code.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

More from Saturday 15 August →