Skip to main content

SqbClient

SqbClient is the top-level entry point of @sqb/connect: it resolves a database Adapter, owns a connection pool built on top of it, and exposes acquire()/execute() to run queries. See the Creating a Client and Connection Pooling guides for usage and configuration details.

SqbClient extends an AsyncEventEmitter (via TypedEventEmitterClass) and emits execute, error, closing, close, acquire, terminate and connection-return events.

For running queries against entities, see Repository and the ORM guides (getRepository() below only constructs one — it doesn't implement any ORM behavior itself).

Constructor

new SqbClient(config: ClientConfiguration)

Throws TypeError if config isn't an object, and Error if config doesn't resolve to a registered Adapter via config.driver or config.dialect. See ClientConfiguration for the full list of fields and Creating a Client for the adapter-resolution rules.

import '@sqb/postgres';
import { SqbClient } from '@sqb/connect';

const client = new SqbClient({
dialect: 'postgres',
host: 'localhost',
database: 'mydb',
});

Properties

PropertyTypeDescription
configClientConfigurationThe configuration object passed to the constructor.
defaultsClientDefaultsThe effective query-execution defaults (config.defaults, or {}).
dialectstringThe resolved adapter's dialect name.
driverstringThe resolved adapter's driver name.
isClosedbooleantrue once the pool has been closed.
poolLightningPoolThe underlying lightning-pool pool instance.

Methods

acquire()

acquire(fn: TransactionFunction, options?: ConnectionOptions): Promise<any>;
acquire(options?: ConnectionOptions): Promise<SqbConnection>;

Checks a connection out of the pool. See Acquiring Connections for the two call forms.

await client.acquire(async connection => {
await connection.execute('select 1');
});

close()

close(terminateWait?: number): Promise<void>

Shuts the pool down and destroys all its connections.

await client.close(0); // terminate immediately, don't wait for in-flight acquisitions

execute()

execute(query: string | Query, options?: QueryExecuteOptions): Promise<QueryResult>

Acquires a connection, executes the query on it, and releases the connection (unless the result carries an open Cursor, in which case the connection is released once the cursor closes). See Executing Queries for the full option/defaults reference.

const result = await client.execute('select * from customers where id = $1', {
params: [1],
});

getEntity()

getEntity<T>(name: string): Maybe<Type<T>>

Looks up a previously registered entity constructor by name. See the ORM guides.

getRepository()

getRepository<T>(entity: Type<T> | string, opts?: { schema?: string }): Repository<T>

Constructs a Repository for the given @Entity-annotated class (or a name previously registered via getEntity()). See the ORM guides for what you can do with the returned repository.

test()

test(): Promise<void>

Acquires a connection, calls test() on it, and releases it — a simple pool health check.