Mconnect
Bun

method

SavepointSQL.connect

connect(): Promise<SQL>;

Waits for the database connection to be established

await sql.connect();

Referenced types

namespace SQL

  • interface Helper<T>

    SQL.Helper represents a parameter or serializable value inside of a query.

    const helper = sql(users, 'id');
    await sql`insert into users ${helper}`;
    
  • interface Options

    Configuration options for SQL client connection and behavior

    const config: Bun.SQL.Options = {
      host: 'localhost',
      port: 5432,
      user: 'dbuser',
      password: 'secretpass',
      database: 'myapp',
      idleTimeout: 30,
      max: 20,
      onconnect: (client) => {
        console.log('Connected to database');
      }
    };
    
    • adapter?: string & {} | 'postgres'

      Database adapter/driver to use

    • bigint?: boolean

      By default values outside i32 range are returned as strings. If this is true, values outside i32 range are returned as BigInts.

    • connection?: Record<string, string | number | boolean>

      Postgres client runtime configuration options

    • connectionTimeout?: number

      Maximum time in seconds to wait when establishing a connection

    • database?: string

      Name of the database to connect to

    • host?: string

      Database server hostname

    • idleTimeout?: number

      Maximum time in seconds to wait for connection to become available

    • max?: number

      Maximum number of connections in the pool

    • maxLifetime?: number

      Maximum lifetime in seconds of a connection

    • onclose?: (client: SQL) => void

      Callback function executed when a connection is closed

    • onconnect?: (client: SQL) => void

      Callback function executed when a connection is established

    • password?: string | () => MaybePromise<string>

      Database password for authentication

    • port?: string | number

      Database server port number

    • prepare?: boolean

      Automatic creation of prepared statements

    • ssl?: boolean | TLSOptions

      Whether to use TLS/SSL for the connection (alias for tls)

    • tls?: boolean | TLSOptions

      Whether to use TLS/SSL for the connection

    • url?: string | URL

      Connection URL (can be string or URL object)

    • username?: string

      Database user for authentication

  • interface Query<T>

    Represents a SQL query that can be executed, with additional control methods Extends Promise to allow for async/await usage

    • readonly [Symbol.toStringTag]: string
    • active: boolean

      Indicates if the query is currently executing

    • cancelled: boolean

      Indicates if the query has been cancelled

    • cancel(): Query<T>;

      Cancels the executing query

    • catch<TResult = never>(
      onrejected?: null | (reason: any) => TResult | PromiseLike<TResult>
      ): Promise<T | TResult>;

      Attaches a callback for only the rejection of the Promise.

      @param onrejected

      The callback to execute when the Promise is rejected.

      @returns

      A Promise for the completion of the callback.

    • execute(): Query<T>;

      Executes the query

    • onfinally?: null | () => void
      ): Promise<T>;

      Attaches a callback that is invoked when the Promise is settled (fulfilled or rejected). The resolved value cannot be modified from the callback.

      @param onfinally

      The callback to execute when the Promise is settled (fulfilled or rejected).

      @returns

      A Promise for the completion of the callback.

    • raw(): Query<T>;

      Returns the raw query result

    • simple(): Query<T>;

      Executes the query as a simple query, no parameters are allowed but can execute multiple commands separated by semicolons

    • then<TResult1 = T, TResult2 = never>(
      onfulfilled?: null | (value: T) => TResult1 | PromiseLike<TResult1>,
      onrejected?: null | (reason: any) => TResult2 | PromiseLike<TResult2>
      ): Promise<TResult1 | TResult2>;

      Attaches callbacks for the resolution and/or rejection of the Promise.

      @param onfulfilled

      The callback to execute when the Promise is resolved.

      @param onrejected

      The callback to execute when the Promise is rejected.

      @returns

      A Promise for the completion of which ever callback is executed.

    • values(): Query<T>;

      Returns only the values from the query result

  • type AwaitPromisesArray<T extends PromiseLike<any>[]> = { [K in keyof T]: Awaited<T[K]> }
  • type ContextCallback<T, SQL> = (sql: SQL) => Promise<T>
  • type ContextCallbackResult<T> = T extends PromiseLike<any>[] ? AwaitPromisesArray<T> : Awaited<T>
  • type SavepointContextCallback<T> = ContextCallback<T, SavepointSQL>

    Callback function type for savepoint contexts

  • type TransactionContextCallback<T> = ContextCallback<T, TransactionSQL>

    Callback function type for transaction contexts