Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add driver for native Bun SQL
  • Loading branch information
davisriedel committed Feb 21, 2025
commit f16ec5dbfc944d4d8a0dc27f37460993dbebdd52
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ sql:

## Supported engines and drivers

- PostgreSQL via [pg](https://www.npmjs.com/package/pg) or [postgres](https://www.npmjs.com/package/postgres).
- PostgreSQL via [pg](https://www.npmjs.com/package/pg), [postgres](https://www.npmjs.com/package/postgres) or [Bun SQL](https://bun.sh/docs/api/sql).
- MySQL via [mysql2](https://www.npmjs.com/package/mysql2).
- SQLite via [sqlite3](https://www.npmjs.com/package/better-sqlite3).

Expand Down Expand Up @@ -291,6 +291,26 @@ sql:
driver: postgres # npm package name
```

### PostgreSQL and Bun SQL

```yaml
version: '2'
plugins:
- name: ts
wasm:
url: https://downloads.sqlc.dev/plugin/sqlc-gen-typescript_0.1.4.wasm
sha256: e8628d800e9c48197e8cbbe289c74839f869cb282b2717ffc85eb622e81a036c
sql:
- schema: "schema.sql"
queries: "query.sql"
engine: postgresql
codegen:
- out: db
plugin: ts
options:
runtime: bun
driver: bun-sql # to use native SQL library of Bun 1.2
```

### MySQL and mysql2

Expand Down Expand Up @@ -390,4 +410,4 @@ Check the `Makefile` for details.
```

For more details on sqlc development, refer to the sqlc core development guide. This guide provides additional information on setting up and working with sqlc in general, which may be useful for contributors to this project.
https://docs.sqlc.dev/en/latest/guides/development.html
https://docs.sqlc.dev/en/latest/guides/development.html
176 changes: 176 additions & 0 deletions examples/bun-sql/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore

# Logs

logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)

report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json

# Runtime data

pids
_.pid
_.seed
\*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover

lib-cov

# Coverage directory used by tools like istanbul

coverage
\*.lcov

# nyc test coverage

.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)

.grunt

# Bower dependency directory (https://bower.io/)

bower_components

# node-waf configuration

.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)

build/Release

# Dependency directories

node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)

web_modules/

# TypeScript cache

\*.tsbuildinfo

# Optional npm cache directory

.npm

# Optional eslint cache

.eslintcache

# Optional stylelint cache

.stylelintcache

# Microbundle cache

.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history

.node_repl_history

# Output of 'npm pack'

\*.tgz

# Yarn Integrity file

.yarn-integrity

# dotenv environment variable files

.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)

.cache
.parcel-cache

# Next.js build output

.next
out

# Nuxt.js build / generate output

.nuxt
dist

# Gatsby files

.cache/

# Comment in the public line in if your project uses Gatsby and not Next.js

# https://nextjs.org/blog/next-9-1#public-directory-support

# public

# vuepress build output

.vuepress/dist

# vuepress v2.x temp and cache directory

.temp
.cache

# Docusaurus cache and generated files

.docusaurus

# Serverless directories

.serverless/

# FuseBox cache

.fusebox/

# DynamoDB Local files

.dynamodb/

# TernJS port file

.tern-port

# Stores VSCode versions used for testing VSCode extensions

.vscode-test

# yarn v2

.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.\*

# IntelliJ based IDEs
.idea

# Finder (MacOS) folder config
.DS_Store

15 changes: 15 additions & 0 deletions examples/bun-sql/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# bun-postgres

To install dependencies:

```bash
bun install
```

To run:

```bash
bun run src/main.ts
```

This project was created using `bun init` in bun v1.0.11. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
25 changes: 25 additions & 0 deletions examples/bun-sql/bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions examples/bun-sql/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "bun-postgres",
"module": "src/main.ts",
"type": "module",
"devDependencies": {
"bun-types": "latest"
},
"peerDependencies": {
"typescript": "^5.0.0"
}
}
99 changes: 99 additions & 0 deletions examples/bun-sql/src/db/query_sql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Code generated by sqlc. DO NOT EDIT.

import { SQL } from "bun";

export const getAuthorQuery = `-- name: GetAuthor :one
SELECT id, name, bio FROM authors
WHERE id = $1 LIMIT 1`;

export interface GetAuthorArgs {
id: string;
}

export interface GetAuthorRow {
id: string;
name: string;
bio: string | null;
}

export async function getAuthor(sql: SQL, args: GetAuthorArgs): Promise<GetAuthorRow | null> {
const rows = await sql.unsafe(getAuthorQuery, [args.id]).values();
if (rows.length !== 1) {
return null;
}
const row = rows[0];
if (!row) {
return null;
}
return {
id: row[0],
name: row[1],
bio: row[2]
};
}

export const listAuthorsQuery = `-- name: ListAuthors :many
SELECT id, name, bio FROM authors
ORDER BY name`;

export interface ListAuthorsRow {
id: string;
name: string;
bio: string | null;
}

export async function listAuthors(sql: SQL): Promise<ListAuthorsRow[]> {
return (await sql.unsafe(listAuthorsQuery, []).values()).map(row => ({
id: row[0],
name: row[1],
bio: row[2]
}));
}

export const createAuthorQuery = `-- name: CreateAuthor :one
INSERT INTO authors (
name, bio
) VALUES (
$1, $2
)
RETURNING id, name, bio`;

export interface CreateAuthorArgs {
name: string;
bio: string | null;
}

export interface CreateAuthorRow {
id: string;
name: string;
bio: string | null;
}

export async function createAuthor(sql: SQL, args: CreateAuthorArgs): Promise<CreateAuthorRow | null> {
const rows = await sql.unsafe(createAuthorQuery, [args.name, args.bio]).values();
if (rows.length !== 1) {
return null;
}
const row = rows[0];
if (!row) {
return null;
}
return {
id: row[0],
name: row[1],
bio: row[2]
};
}

export const deleteAuthorQuery = `-- name: DeleteAuthor :exec
DELETE FROM authors
WHERE id = $1`;

export interface DeleteAuthorArgs {
id: string;
}

export async function deleteAuthor(sql: SQL, args: DeleteAuthorArgs): Promise<void> {
await sql.unsafe(deleteAuthorQuery, [args.id]);
}

Loading