Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 28 additions & 19 deletions docs/runtime/sql.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -660,16 +660,13 @@ const sql = new SQL({
// cert: "path/to/cert.pem",
// },

// Callbacks
onconnect: client => {
console.log("Connected to MySQL");
// Callbacks (see "Connection callbacks" below)
onconnect: err => {
if (!err) console.log("Connected to MySQL");
},
onclose: (client, err) => {
if (err) {
console.error("MySQL connection error:", err);
} else {
console.log("MySQL connection closed");
}
onclose: err => {
// err.code is e.g. ERR_MYSQL_CONNECTION_CLOSED or ERR_MYSQL_IDLE_TIMEOUT
console.log("MySQL connection closed:", err);
},
});
```
Expand Down Expand Up @@ -709,12 +706,13 @@ const sql = new SQL({
// },
// },

// Callbacks
onconnect: client => {
console.log("Connected to PostgreSQL");
// Callbacks (see "Connection callbacks" below)
onconnect: err => {
if (!err) console.log("Connected to PostgreSQL");
},
onclose: client => {
console.log("PostgreSQL connection closed");
onclose: err => {
// err.code is e.g. ERR_POSTGRES_CONNECTION_CLOSED or ERR_POSTGRES_IDLE_TIMEOUT
console.log("PostgreSQL connection closed:", err);
},
});
```
Expand All @@ -738,12 +736,16 @@ const sql = new SQL({
strict: true, // Enable strict mode for better type safety
safeIntegers: false, // Use BigInt for integers exceeding JS number range

// Callbacks
onconnect: client => {
console.log("SQLite database opened");
// Callbacks (see "Connection callbacks" below)
onconnect: err => {
if (err) {
console.error("Failed to open SQLite database:", err);
} else {
console.log("SQLite database opened");
}
},
onclose: client => {
console.log("SQLite database closed");
onclose: err => {
console.log("SQLite database closed:", err);
},
});
```
Expand All @@ -757,6 +759,13 @@ const sql = new SQL({

</Accordion>

### Connection callbacks

`onconnect` and `onclose` each receive a single argument, `err`, which is an `Error` or `null`. Neither callback receives the connection.

- `onconnect` is called once for each connection that is opened, with `null`. SQLite passes the error instead when the database could not be opened. PostgreSQL and MySQL report a failed connection attempt through `onclose`, not `onconnect`.
- `onclose` is called once for each connection that closes, with the error describing why it closed. `err` is set even when you closed the connection yourself: `sql.close()` passes `ERR_POSTGRES_CONNECTION_CLOSED` / `ERR_MYSQL_CONNECTION_CLOSED` (for SQLite, an `Error` with the message `"Connection closed"`), `idleTimeout` passes `ERR_POSTGRES_IDLE_TIMEOUT` / `ERR_MYSQL_IDLE_TIMEOUT`, and a failed connection attempt passes the connection error, such as `ERR_POSTGRES_CONNECTION_REFUSED`.

---

## Dynamic passwords
Expand Down
Loading