Creates a new SQL client instance
constructor
SQL.constructor
The connection string for the SQL client
const sql = new SQL("postgres://localhost:5432/mydb");
const sql = new SQL(new URL("postgres://localhost:5432/mydb"));
Creates a new SQL client instance with options
The connection string for the SQL client
The options for the SQL client
const sql = new SQL("postgres://localhost:5432/mydb", { idleTimeout: 1000 });
Referenced types
class URL
The URL interface represents an object providing static methods used for creating object URLs.
The
toString()
method on theURL
object returns the serialized URL. The value returned is equivalent to that of href and toJSON.
namespace SQL
class MySQLError
- targetObject: object,constructorOpt?: Function): void;
Creates a
.stack
property ontargetObject
, which when accessed returns a string representing the location in the code at whichError.captureStackTrace()
was called.const myObject = {}; Error.captureStackTrace(myObject); myObject.stack; // Similar to `new Error().stack`
The first line of the trace will be prefixed with
${myObject.name}: ${myObject.message}
.The optional
constructorOpt
argument accepts a function. If given, all frames aboveconstructorOpt
, includingconstructorOpt
, will be omitted from the generated stack trace.The
constructorOpt
argument is useful for hiding implementation details of error generation from the user. For instance:function a() { b(); } function b() { c(); } function c() { // Create an error without stack trace to avoid calculating the stack trace twice. const { stackTraceLimit } = Error; Error.stackTraceLimit = 0; const error = new Error(); Error.stackTraceLimit = stackTraceLimit; // Capture the stack trace above function b Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace throw error; } a();
class PostgresError
- targetObject: object,constructorOpt?: Function): void;
Creates a
.stack
property ontargetObject
, which when accessed returns a string representing the location in the code at whichError.captureStackTrace()
was called.const myObject = {}; Error.captureStackTrace(myObject); myObject.stack; // Similar to `new Error().stack`
The first line of the trace will be prefixed with
${myObject.name}: ${myObject.message}
.The optional
constructorOpt
argument accepts a function. If given, all frames aboveconstructorOpt
, includingconstructorOpt
, will be omitted from the generated stack trace.The
constructorOpt
argument is useful for hiding implementation details of error generation from the user. For instance:function a() { b(); } function b() { c(); } function c() { // Create an error without stack trace to avoid calculating the stack trace twice. const { stackTraceLimit } = Error; Error.stackTraceLimit = 0; const error = new Error(); Error.stackTraceLimit = stackTraceLimit; // Capture the stack trace above function b Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace throw error; } a();
class SQLError
- targetObject: object,constructorOpt?: Function): void;
Creates a
.stack
property ontargetObject
, which when accessed returns a string representing the location in the code at whichError.captureStackTrace()
was called.const myObject = {}; Error.captureStackTrace(myObject); myObject.stack; // Similar to `new Error().stack`
The first line of the trace will be prefixed with
${myObject.name}: ${myObject.message}
.The optional
constructorOpt
argument accepts a function. If given, all frames aboveconstructorOpt
, includingconstructorOpt
, will be omitted from the generated stack trace.The
constructorOpt
argument is useful for hiding implementation details of error generation from the user. For instance:function a() { b(); } function b() { c(); } function c() { // Create an error without stack trace to avoid calculating the stack trace twice. const { stackTraceLimit } = Error; Error.stackTraceLimit = 0; const error = new Error(); Error.stackTraceLimit = stackTraceLimit; // Capture the stack trace above function b Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace throw error; } a();
class SQLiteError
- targetObject: object,constructorOpt?: Function): void;
Creates a
.stack
property ontargetObject
, which when accessed returns a string representing the location in the code at whichError.captureStackTrace()
was called.const myObject = {}; Error.captureStackTrace(myObject); myObject.stack; // Similar to `new Error().stack`
The first line of the trace will be prefixed with
${myObject.name}: ${myObject.message}
.The optional
constructorOpt
argument accepts a function. If given, all frames aboveconstructorOpt
, includingconstructorOpt
, will be omitted from the generated stack trace.The
constructorOpt
argument is useful for hiding implementation details of error generation from the user. For instance:function a() { b(); } function b() { c(); } function c() { // Create an error without stack trace to avoid calculating the stack trace twice. const { stackTraceLimit } = Error; Error.stackTraceLimit = 0; const error = new Error(); Error.stackTraceLimit = stackTraceLimit; // Capture the stack trace above function b Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace throw error; } a();
interface PostgresOrMySQLOptions
- bigint?: boolean
By default values outside i32 range are returned as strings. If this is true, values outside i32 range are returned as BigInts.
interface Query<T>
Represents a SQL query that can be executed, with additional control methods Extends Promise to allow for async/await usage
- onrejected?: null | (reason: any) => TResult | PromiseLike<TResult>): Promise<T | TResult>;
Attaches a callback for only the rejection of the Promise.
@param onrejectedThe callback to execute when the Promise is rejected.
@returnsA Promise for the completion of the callback.
- 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 onfinallyThe callback to execute when the Promise is settled (fulfilled or rejected).
@returnsA Promise for the completion of the callback.
- 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 onfulfilledThe callback to execute when the Promise is resolved.
@param onrejectedThe callback to execute when the Promise is rejected.
@returnsA Promise for the completion of which ever callback is executed.
interface SQLiteOptions
Options for Database
- readonly?: boolean
Open the database as read-only (no write operations, no create).
Equivalent to constants.SQLITE_OPEN_READONLY
- safeIntegers?: boolean
When set to
true
, integers are returned asbigint
types.When set to
false
, integers are returned asnumber
types and truncated to 52 bits. - strict?: boolean
When set to
false
orundefined
:- Queries missing bound parameters will NOT throw an error
- Bound named parameters in JavaScript need to exactly match the SQL query.
const db = new Database(":memory:", { strict: false }); db.run("INSERT INTO foo (name) VALUES ($name)", { $name: "foo" });
When set to
true
:- Queries missing bound parameters will throw an error
- Bound named parameters in JavaScript no longer need to be
$
,:
, or@
. The SQL query will remain prefixed.
- type AwaitPromisesArray<T extends PromiseLike<any>[]> = { [K in keyof T]: Awaited<T[K]> }
- type ContextCallback<T, SQL> = (sql: SQL) => Bun.MaybePromise<T>
- type ContextCallbackResult<T> = T extends PromiseLike<any>[] ? AwaitPromisesArray<T> : Awaited<T>
- type Options = SQLiteOptions | PostgresOrMySQLOptions
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'); } };
- type SavepointContextCallback<T> = ContextCallback<T, SavepointSQL>
Callback function type for savepoint contexts
- type TransactionContextCallback<T> = ContextCallback<T, TransactionSQL>
Callback function type for transaction contexts
interface SQLiteOptions
Options for Database
- readonly?: boolean
Open the database as read-only (no write operations, no create).
Equivalent to constants.SQLITE_OPEN_READONLY
- safeIntegers?: boolean
When set to
true
, integers are returned asbigint
types.When set to
false
, integers are returned asnumber
types and truncated to 52 bits. - strict?: boolean
When set to
false
orundefined
:- Queries missing bound parameters will NOT throw an error
- Bound named parameters in JavaScript need to exactly match the SQL query.
const db = new Database(":memory:", { strict: false }); db.run("INSERT INTO foo (name) VALUES ($name)", { $name: "foo" });
When set to
true
:- Queries missing bound parameters will throw an error
- Bound named parameters in JavaScript no longer need to be
$
,:
, or@
. The SQL query will remain prefixed.
interface PostgresOrMySQLOptions
- bigint?: boolean
By default values outside i32 range are returned as strings. If this is true, values outside i32 range are returned as BigInts.
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');
}
};