Skip to content

Commit 920b3dc

Browse files
aliicirospaciari
andauthored
@types/bun: Improvements to Bun.SQL (#19488)
Co-authored-by: Ciro Spaciari <[email protected]>
1 parent b478171 commit 920b3dc

File tree

5 files changed

+336
-66
lines changed

5 files changed

+336
-66
lines changed

packages/bun-types/bun.d.ts

Lines changed: 144 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -991,11 +991,9 @@ declare module "bun" {
991991
function fileURLToPath(url: URL | string): string;
992992

993993
/**
994-
* Fast incremental writer that becomes an `ArrayBuffer` on end().
994+
* Fast incremental writer that becomes an {@link ArrayBuffer} on end().
995995
*/
996996
class ArrayBufferSink {
997-
constructor();
998-
999997
start(options?: {
1000998
asUint8Array?: boolean;
1001999
/**
@@ -1320,7 +1318,8 @@ declare module "bun" {
13201318
* }
13211319
* };
13221320
*/
1323-
type SQLOptions = {
1321+
1322+
interface SQLOptions {
13241323
/** Connection URL (can be string or URL object) */
13251324
url?: URL | string;
13261325
/** Database server hostname */
@@ -1369,27 +1368,33 @@ declare module "bun" {
13691368
bigint?: boolean;
13701369
/** Automatic creation of prepared statements, defaults to true */
13711370
prepare?: boolean;
1372-
};
1371+
}
13731372

13741373
/**
13751374
* Represents a SQL query that can be executed, with additional control methods
13761375
* Extends Promise to allow for async/await usage
13771376
*/
1378-
interface SQLQuery extends Promise<any> {
1377+
interface SQLQuery<T = any> extends Promise<T> {
13791378
/** Indicates if the query is currently executing */
13801379
active: boolean;
1380+
13811381
/** Indicates if the query has been cancelled */
13821382
cancelled: boolean;
1383+
13831384
/** Cancels the executing query */
1384-
cancel(): SQLQuery;
1385+
cancel(): SQLQuery<T>;
1386+
13851387
/** Execute as a simple query, no parameters are allowed but can execute multiple commands separated by semicolons */
1386-
simple(): SQLQuery;
1388+
simple(): SQLQuery<T>;
1389+
13871390
/** Executes the query */
1388-
execute(): SQLQuery;
1391+
execute(): SQLQuery<T>;
1392+
13891393
/** Returns the raw query result */
1390-
raw(): SQLQuery;
1394+
raw(): SQLQuery<T>;
1395+
13911396
/** Returns only the values from the query result */
1392-
values(): SQLQuery;
1397+
values(): SQLQuery<T>;
13931398
}
13941399

13951400
/**
@@ -1407,65 +1412,117 @@ declare module "bun" {
14071412
* Main SQL client interface providing connection and transaction management
14081413
*/
14091414
interface SQL {
1410-
/** Creates a new SQL client instance
1411-
* @example
1412-
* const sql = new SQL("postgres://localhost:5432/mydb");
1413-
* const sql = new SQL(new URL("postgres://localhost:5432/mydb"));
1414-
*/
1415-
new (connectionString: string | URL): SQL;
1416-
/** Creates a new SQL client instance with options
1417-
* @example
1418-
* const sql = new SQL("postgres://localhost:5432/mydb", { idleTimeout: 1000 });
1419-
*/
1420-
new (connectionString: string | URL, options: SQLOptions): SQL;
1421-
/** Creates a new SQL client instance with options
1422-
* @example
1423-
* const sql = new SQL({ url: "postgres://localhost:5432/mydb", idleTimeout: 1000 });
1424-
*/
1425-
new (options?: SQLOptions): SQL;
1426-
/** Executes a SQL query using template literals
1415+
/**
1416+
* Executes a SQL query using template literals
14271417
* @example
1418+
* ```ts
14281419
* const [user] = await sql`select * from users where id = ${1}`;
1420+
* ```
14291421
*/
1430-
(strings: string | TemplateStringsArray, ...values: any[]): SQLQuery;
1422+
(strings: string[] | TemplateStringsArray, ...values: any[]): SQLQuery;
1423+
14311424
/**
1432-
* Helper function to allow easy use to insert values into a query
1425+
* Helper function for inserting an object into a query
1426+
*
14331427
* @example
1428+
* ```ts
1429+
* // Insert an object
14341430
* const result = await sql`insert into users ${sql(users)} RETURNING *`;
1431+
*
1432+
* // Or pick specific columns
1433+
* const result = await sql`insert into users ${sql(users, "id", "name")} RETURNING *`;
1434+
*
1435+
* // Or a single object
1436+
* const result = await sql`insert into users ${sql(user)} RETURNING *`;
1437+
* ```
14351438
*/
1436-
(obj: any): SQLQuery;
1437-
/** Commits a distributed transaction also know as prepared transaction in postgres or XA transaction in MySQL
1439+
<T extends { [Key in PropertyKey]: unknown }>(obj: T | T[] | readonly T[], ...columns: (keyof T)[]): SQLQuery;
1440+
1441+
/**
1442+
* Helper function for inserting any serializable value into a query
1443+
*
14381444
* @example
1445+
* ```ts
1446+
* const result = await sql`SELECT * FROM users WHERE id IN ${sql([1, 2, 3])}`;
1447+
* ```
1448+
*/
1449+
(obj: unknown): SQLQuery;
1450+
1451+
/**
1452+
* Commits a distributed transaction also know as prepared transaction in postgres or XA transaction in MySQL
1453+
*
1454+
* @param name - The name of the distributed transaction
1455+
*
1456+
* @example
1457+
* ```ts
14391458
* await sql.commitDistributed("my_distributed_transaction");
1459+
* ```
14401460
*/
14411461
commitDistributed(name: string): Promise<void>;
1442-
/** Rolls back a distributed transaction also know as prepared transaction in postgres or XA transaction in MySQL
1462+
1463+
/**
1464+
* Rolls back a distributed transaction also know as prepared transaction in postgres or XA transaction in MySQL
1465+
*
1466+
* @param name - The name of the distributed transaction
1467+
*
14431468
* @example
1469+
* ```ts
14441470
* await sql.rollbackDistributed("my_distributed_transaction");
1471+
* ```
14451472
*/
14461473
rollbackDistributed(name: string): Promise<void>;
1474+
14471475
/** Waits for the database connection to be established
1476+
*
14481477
* @example
1478+
* ```ts
14491479
* await sql.connect();
1480+
* ```
14501481
*/
14511482
connect(): Promise<SQL>;
1452-
/** Closes the database connection with optional timeout in seconds. If timeout is 0, it will close immediately, if is not provided it will wait for all queries to finish before closing.
1483+
1484+
/**
1485+
* Closes the database connection with optional timeout in seconds. If timeout is 0, it will close immediately, if is not provided it will wait for all queries to finish before closing.
1486+
*
1487+
* @param options - The options for the close
1488+
*
14531489
* @example
1490+
* ```ts
14541491
* await sql.close({ timeout: 1 });
1492+
* ```
14551493
*/
14561494
close(options?: { timeout?: number }): Promise<void>;
1457-
/** Closes the database connection with optional timeout in seconds. If timeout is 0, it will close immediately, if is not provided it will wait for all queries to finish before closing.
1458-
* @alias close
1495+
1496+
/**
1497+
* Closes the database connection with optional timeout in seconds. If timeout is 0, it will close immediately, if is not provided it will wait for all queries to finish before closing.
1498+
* This is an alias of {@link SQL.close}
1499+
*
1500+
* @param options - The options for the close
1501+
*
14591502
* @example
1503+
* ```ts
14601504
* await sql.end({ timeout: 1 });
1505+
* ```
14611506
*/
14621507
end(options?: { timeout?: number }): Promise<void>;
1463-
/** Flushes any pending operations */
1508+
1509+
/**
1510+
* Flushes any pending operations
1511+
*
1512+
* @example
1513+
* ```ts
1514+
* sql.flush();
1515+
* ```
1516+
*/
14641517
flush(): void;
1465-
/** The reserve method pulls out a connection from the pool, and returns a client that wraps the single connection.
1466-
* This can be used for running queries on an isolated connection.
1467-
* Calling reserve in a reserved Sql will return a new reserved connection, not the same connection (behavior matches postgres package).
1518+
1519+
/**
1520+
* The reserve method pulls out a connection from the pool, and returns a client that wraps the single connection.
1521+
* This can be used for running queries on an isolated connection.
1522+
* Calling reserve in a reserved Sql will return a new reserved connection, not the same connection (behavior matches postgres package).
1523+
*
14681524
* @example
1525+
* ```ts
14691526
* const reserved = await sql.reserve();
14701527
* await reserved`select * from users`;
14711528
* await reserved.release();
@@ -1476,12 +1533,14 @@ declare module "bun" {
14761533
* } finally {
14771534
* await reserved.release();
14781535
* }
1479-
* //To make it simpler bun supportsSymbol.dispose and Symbol.asyncDispose
1536+
*
1537+
* // Bun supports Symbol.dispose and Symbol.asyncDispose
14801538
* {
1481-
* // always release after context (safer)
1482-
* using reserved = await sql.reserve()
1483-
* await reserved`select * from users`
1539+
* // always release after context (safer)
1540+
* using reserved = await sql.reserve()
1541+
* await reserved`select * from users`
14841542
* }
1543+
* ```
14851544
*/
14861545
reserve(): Promise<ReservedSQL>;
14871546
/** Begins a new transaction
@@ -1626,6 +1685,45 @@ declare module "bun" {
16261685

16271686
[Symbol.asyncDispose](): Promise<any>;
16281687
}
1688+
const SQL: {
1689+
/**
1690+
* Creates a new SQL client instance
1691+
*
1692+
* @param connectionString - The connection string for the SQL client
1693+
*
1694+
* @example
1695+
* ```ts
1696+
* const sql = new SQL("postgres://localhost:5432/mydb");
1697+
* const sql = new SQL(new URL("postgres://localhost:5432/mydb"));
1698+
* ```
1699+
*/
1700+
new (connectionString: string | URL): SQL;
1701+
1702+
/**
1703+
* Creates a new SQL client instance with options
1704+
*
1705+
* @param connectionString - The connection string for the SQL client
1706+
* @param options - The options for the SQL client
1707+
*
1708+
* @example
1709+
* ```ts
1710+
* const sql = new SQL("postgres://localhost:5432/mydb", { idleTimeout: 1000 });
1711+
* ```
1712+
*/
1713+
new (connectionString: string | URL, options: Omit<SQLOptions, "url">): SQL;
1714+
1715+
/**
1716+
* Creates a new SQL client instance with options
1717+
*
1718+
* @param options - The options for the SQL client
1719+
*
1720+
* @example
1721+
* ```ts
1722+
* const sql = new SQL({ url: "postgres://localhost:5432/mydb", idleTimeout: 1000 });
1723+
* ```
1724+
*/
1725+
new (options?: SQLOptions): SQL;
1726+
};
16291727

16301728
/**
16311729
* Represents a reserved connection from the connection pool
@@ -1697,21 +1795,14 @@ declare module "bun" {
16971795
*
16981796
* @category Database
16991797
*/
1700-
var sql: SQL;
1798+
const sql: SQL;
17011799

17021800
/**
17031801
* SQL client for PostgreSQL
17041802
*
17051803
* @category Database
17061804
*/
1707-
var postgres: SQL;
1708-
1709-
/**
1710-
* The SQL constructor
1711-
*
1712-
* @category Database
1713-
*/
1714-
var SQL: SQL;
1805+
const postgres: SQL;
17151806

17161807
/**
17171808
* Generate and verify CSRF tokens

packages/bun-types/globals.d.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -954,13 +954,6 @@ interface ErrorConstructor {
954954
*/
955955
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
956956

957-
/**
958-
* Optional override for formatting stack traces
959-
*
960-
* @see https://v8.dev/docs/stack-trace-api#customizing-stack-traces
961-
*/
962-
prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined;
963-
964957
/**
965958
* The maximum number of stack frames to capture.
966959
*/

test/integration/bun-types/fixture/spawn.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,11 @@ function depromise<T>(_promise: Promise<T>): T {
179179
tsd.expectType<number>(proc.stdin);
180180
}
181181
tsd.expectAssignable<PipedSubprocess>(Bun.spawn([], { stdio: ["pipe", "pipe", "pipe"] }));
182-
tsd.expectNotAssignable<PipedSubprocess>(Bun.spawn([], { stdio: ["inherit", "inherit", "inherit"] }));
183182
tsd.expectAssignable<ReadableSubprocess>(Bun.spawn([], { stdio: ["ignore", "pipe", "pipe"] }));
184183
tsd.expectAssignable<ReadableSubprocess>(Bun.spawn([], { stdio: ["pipe", "pipe", "pipe"] }));
185-
tsd.expectNotAssignable<ReadableSubprocess>(Bun.spawn([], { stdio: ["pipe", "ignore", "pipe"] }));
186184
tsd.expectAssignable<WritableSubprocess>(Bun.spawn([], { stdio: ["pipe", "pipe", "pipe"] }));
187185
tsd.expectAssignable<WritableSubprocess>(Bun.spawn([], { stdio: ["pipe", "ignore", "inherit"] }));
188-
tsd.expectNotAssignable<WritableSubprocess>(Bun.spawn([], { stdio: ["ignore", "pipe", "pipe"] }));
189186
tsd.expectAssignable<NullSubprocess>(Bun.spawn([], { stdio: ["ignore", "inherit", "ignore"] }));
190187
tsd.expectAssignable<NullSubprocess>(Bun.spawn([], { stdio: [null, null, null] }));
191-
tsd.expectNotAssignable<ReadableSubprocess>(Bun.spawn([], {}));
192-
tsd.expectNotAssignable<PipedSubprocess>(Bun.spawn([], {}));
193188

194189
tsd.expectAssignable<SyncSubprocess<Bun.SpawnOptions.Readable, Bun.SpawnOptions.Readable>>(Bun.spawnSync([], {}));

0 commit comments

Comments
 (0)