diff --git a/packages/bun-types/bun.d.ts b/packages/bun-types/bun.d.ts index 9e38df54ee..6eea46e236 100644 --- a/packages/bun-types/bun.d.ts +++ b/packages/bun-types/bun.d.ts @@ -1383,33 +1383,6 @@ declare module "bun" { prepare?: boolean; } - /** - * Represents a SQL query that can be executed, with additional control methods - * Extends Promise to allow for async/await usage - */ - interface SQLQuery extends Promise { - /** Indicates if the query is currently executing */ - active: boolean; - - /** Indicates if the query has been cancelled */ - cancelled: boolean; - - /** Cancels the executing query */ - cancel(): SQLQuery; - - /** Execute as a simple query, no parameters are allowed but can execute multiple commands separated by semicolons */ - simple(): SQLQuery; - - /** Executes the query */ - execute(): SQLQuery; - - /** Returns the raw query result */ - raw(): SQLQuery; - - /** Returns only the values from the query result */ - values(): SQLQuery; - } - namespace SQL { type AwaitPromisesArray>> = { [K in keyof T]: Awaited; @@ -1417,24 +1390,60 @@ declare module "bun" { type ContextCallbackResult = T extends Array> ? AwaitPromisesArray : Awaited; type ContextCallback = (sql: SQL) => Promise; + + /** + * Represents a SQL query that can be executed, with additional control methods + * Extends Promise to allow for async/await usage + */ + interface Query extends Promise { + /** Indicates if the query is currently executing */ + active: boolean; + + /** Indicates if the query has been cancelled */ + cancelled: boolean; + + /** Cancels the executing query */ + cancel(): SQLQuery; + + /** Execute as a simple query, no parameters are allowed but can execute multiple commands separated by semicolons */ + simple(): SQLQuery; + + /** Executes the query */ + execute(): SQLQuery; + + /** Returns the raw query result */ + raw(): SQLQuery; + + /** Returns only the values from the query result */ + values(): SQLQuery; + } + + /** + * Callback function type for transaction contexts + * @param sql Function to execute SQL queries within the transaction + */ + type TransactionContextCallback = ContextCallback; + + /** + * Callback function type for savepoint contexts + * @param sql Function to execute SQL queries within the savepoint + */ + type SavepointContextCallback = SQL.ContextCallback; + + interface Helper { + readonly value: T[]; + readonly columns: (keyof T)[]; + } } - /** - * Callback function type for transaction contexts - * @param sql Function to execute SQL queries within the transaction - */ - type SQLTransactionContextCallback = SQL.ContextCallback; + /** @deprecated Use {@link SQL.Query} */ + type SQLQuery = SQL.Query; - /** - * Callback function type for savepoint contexts - * @param sql Function to execute SQL queries within the savepoint - */ - type SQLSavepointContextCallback = SQL.ContextCallback; + /** @deprecated Use {@link SQL.TransactionContextCallback} */ + type SQLTransactionContextCallback = SQL.TransactionContextCallback; - interface SQLHelper { - readonly value: T[]; - readonly columns: (keyof T)[]; - } + /** @deprecated Use {@link SQL.SavepointContextCallback} */ + type SQLSavepointContextCallback = SQL.SavepointContextCallback; /** * Main SQL client interface providing connection and transaction management @@ -1447,12 +1456,12 @@ declare module "bun" { * const [user] = await sql`select * from users where id = ${1}`; * ``` */ - (strings: TemplateStringsArray, ...values: unknown[]): SQLQuery; + (strings: TemplateStringsArray, ...values: unknown[]): SQL.Query; /** * Execute a SQL query using a string */ - (string: string): SQLQuery; + (string: string): SQL.Query; /** * Helper function for inserting an object into a query @@ -1472,7 +1481,7 @@ declare module "bun" { ( obj: T | T[] | readonly T[], ...columns: readonly Keys[] - ): SQLHelper>; + ): SQL.Helper>; /** * Helper function for inserting any serializable value into a query @@ -1482,7 +1491,7 @@ declare module "bun" { * const result = await sql`SELECT * FROM users WHERE id IN ${sql([1, 2, 3])}`; * ``` */ - (value: T): SQLHelper; + (value: T): SQL.Helper; /** * Commits a distributed transaction also know as prepared transaction in postgres or XA transaction in MySQL @@ -1607,7 +1616,7 @@ declare module "bun" { * return [user, account] * }) */ - begin(fn: SQLTransactionContextCallback): Promise>; + begin(fn: SQL.TransactionContextCallback): Promise>; /** * Begins a new transaction with options. @@ -1635,7 +1644,7 @@ declare module "bun" { * return [user, account] * }) */ - begin(options: string, fn: SQLTransactionContextCallback): Promise>; + begin(options: string, fn: SQL.TransactionContextCallback): Promise>; /** * Alternative method to begin a transaction. @@ -1664,7 +1673,7 @@ declare module "bun" { * return [user, account] * }) */ - transaction(fn: SQLTransactionContextCallback): Promise>; + transaction(fn: SQL.TransactionContextCallback): Promise>; /** * Alternative method to begin a transaction with options @@ -1694,7 +1703,7 @@ declare module "bun" { * return [user, account] * }); */ - transaction(options: string, fn: SQLTransactionContextCallback): Promise>; + transaction(options: string, fn: SQL.TransactionContextCallback): Promise>; /** * Begins a distributed transaction @@ -1715,13 +1724,13 @@ declare module "bun" { */ beginDistributed( name: string, - fn: SQLTransactionContextCallback, + fn: SQL.TransactionContextCallback, ): Promise>; /** Alternative method to begin a distributed transaction * @alias {@link beginDistributed} */ - distributed(name: string, fn: SQLTransactionContextCallback): Promise>; + distributed(name: string, fn: SQL.TransactionContextCallback): Promise>; /**If you know what you're doing, you can use unsafe to pass any string you'd like. * Please note that this can lead to SQL injection if you're not careful. diff --git a/test/integration/bun-types/fixture/sql.ts b/test/integration/bun-types/fixture/sql.ts index beac54b857..0dc4221660 100644 --- a/test/integration/bun-types/fixture/sql.ts +++ b/test/integration/bun-types/fixture/sql.ts @@ -111,14 +111,14 @@ expectType( expectType(tx).is(); } -expectType(sql1.unsafe("SELECT * FROM users")).is(); -expectType(sql1.unsafe<{ id: string }[]>("SELECT * FROM users")).is>(); -expectType(sql1.file("query.sql", [1, 2, 3])).is(); +expectType(sql1.unsafe("SELECT * FROM users")).is(); +expectType(sql1.unsafe<{ id: string }[]>("SELECT * FROM users")).is>(); +expectType(sql1.file("query.sql", [1, 2, 3])).is(); sql1.reserve().then(reserved => { reserved.release(); - expectType(reserved<[8]>`SELECT 8`).is>(); + expectType(reserved<[8]>`SELECT 8`).is>(); }); sql1.begin(async txn => { @@ -151,23 +151,23 @@ sql1.begin("read write", 123); // @ts-expect-error sql1.transaction("read write", 123); -const sqlQueryAny: Bun.SQLQuery = {} as any; -const sqlQueryNumber: Bun.SQLQuery = {} as any; -const sqlQueryString: Bun.SQLQuery = {} as any; +const sqlQueryAny: Bun.SQL.Query = {} as any; +const sqlQueryNumber: Bun.SQL.Query = {} as any; +const sqlQueryString: Bun.SQL.Query = {} as any; expectAssignable>(sqlQueryAny); expectAssignable>(sqlQueryNumber); expectAssignable>(sqlQueryString); -expectType(sqlQueryNumber).is>(); -expectType(sqlQueryString).is>(); -expectType(sqlQueryNumber).is>(); +expectType(sqlQueryNumber).is>(); +expectType(sqlQueryString).is>(); +expectType(sqlQueryNumber).is>(); const queryA = sql`SELECT 1`; -expectType(queryA).is(); +expectType(queryA).is(); const queryB = sql({ foo: "bar" }); -expectType(queryB).is>(); +expectType(queryB).is>(); expectType(sql).is(); @@ -180,18 +180,18 @@ const spCb = (async sql => [sql<[2]>`SELECT 2`]) satisfies Bun.SQLSavepointConte expectType(await sql.begin(txCb)).is<[1][]>(); expectType(await sql.begin(spCb)).is<[2][]>(); -expectType(queryA.cancel()).is(); -expectType(queryA.simple()).is(); -expectType(queryA.execute()).is(); -expectType(queryA.raw()).is(); -expectType(queryA.values()).is(); +expectType(queryA.cancel()).is(); +expectType(queryA.simple()).is(); +expectType(queryA.execute()).is(); +expectType(queryA.raw()).is(); +expectType(queryA.values()).is(); -declare const queryNum: Bun.SQLQuery; -expectType(queryNum.cancel()).is>(); -expectType(queryNum.simple()).is>(); -expectType(queryNum.execute()).is>(); -expectType(queryNum.raw()).is>(); -expectType(queryNum.values()).is>(); +declare const queryNum: Bun.SQL.Query; +expectType(queryNum.cancel()).is>(); +expectType(queryNum.simple()).is>(); +expectType(queryNum.execute()).is>(); +expectType(queryNum.raw()).is>(); +expectType(queryNum.values()).is>(); expectType(await queryNum.cancel()).is(); expectType(await queryNum.simple()).is(); @@ -200,7 +200,7 @@ expectType(await queryNum.raw()).is(); expectType(await queryNum.values()).is(); expectType(sql({ name: "Alice", email: "alice@example.com" })).is< - Bun.SQLHelper<{ + Bun.SQL.Helper<{ name: string; email: string; }> @@ -212,7 +212,7 @@ expectType( { name: "Bob", email: "bob@example.com" }, ]), ).is< - Bun.SQLHelper<{ + Bun.SQL.Helper<{ name: string; email: string; }> @@ -221,7 +221,7 @@ expectType( const userWithAge = { name: "Alice", email: "alice@example.com", age: 25 }; expectType(sql(userWithAge, "name", "email")).is< - Bun.SQLHelper<{ + Bun.SQL.Helper<{ name: string; email: string; }> @@ -231,13 +231,13 @@ const users = [ { id: 1, name: "Alice" }, { id: 2, name: "Bob" }, ]; -expectType(sql(users, "id")).is>(); +expectType(sql(users, "id")).is>(); -expectType(sql([1, 2, 3])).is>(); -expectType(sql([1, 2, 3] as const)).is>(); +expectType(sql([1, 2, 3])).is>(); +expectType(sql([1, 2, 3] as const)).is>(); -expectType(sql("users")).is>(); -expectType(sql<1>("users")).is>(); +expectType(sql("users")).is>(); +expectType(sql<1>("users")).is>(); // @ts-expect-error - missing key in object sql(user, "notAKey");