Skip to content
Draft
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,18 @@ jobs:
with:
sqlc-version: '1.24.0'
- uses: actions/setup-node@v6
- uses: sqlc-dev/action-setup-postgres@master
with:
postgres-version: "16"
id: postgres
- name: Build
run: |
make generate
make validate
- name: Integration tests
run: npm run test:integration
env:
DATABASE_URL: ${{ steps.postgres.outputs.connection-uri }}?sslmode=disable
- uses: actions/upload-artifact@v7
with:
name: plugin
Expand Down
24 changes: 24 additions & 0 deletions examples/authors/postgresql/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,30 @@ INSERT INTO authors (
)
RETURNING *;

-- name: CopyAuthors :copyfrom
INSERT INTO authors (
name, bio
) VALUES (
$1, $2
);

-- name: DeleteAuthor :exec
DELETE FROM authors
WHERE id = $1;

-- name: BatchCreateAuthor :batchone
INSERT INTO authors (
name, bio
) VALUES (
$1, $2
)
RETURNING *;

-- name: BatchListAuthorsByBio :batchmany
SELECT * FROM authors
WHERE bio = $1
ORDER BY name;

-- name: BatchDeleteAuthor :batchexec
DELETE FROM authors
WHERE id = $1;
121 changes: 121 additions & 0 deletions examples/bun-pg/src/db/query_sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,43 @@ interface Client {
query: (config: QueryArrayConfig) => Promise<QueryArrayResult>;
}

/**
* Accepted by pg batch stubs for source compatibility only.
* node-postgres batch annotations always throw SqlcBatchUnsupportedError.
*/
export interface SqlcBatchOptions {
batchSize?: number;
}

/**
* node-postgres does not expose pgx-style SendBatch/pipelining semantics.
* Use the postgres driver for generated batch query support.
*/
export class SqlcBatchUnsupportedError extends Error {
readonly driver = "pg";
readonly command: string;

constructor(command: string) {
super(`pg driver does not support ${command}; use the postgres driver for batch annotations.`);
this.name = "SqlcBatchUnsupportedError";
this.command = command;
}
}

/**
* node-postgres needs external COPY stream wiring that this generator does not emit.
* Use the postgres driver for generated :copyfrom support.
*/
export class SqlcCopyFromUnsupportedError extends Error {
readonly driver = "pg";
readonly command: string;

constructor(command: string) {
super(`pg driver does not support ${command}; use the postgres driver for :copyfrom annotations.`);
this.name = "SqlcCopyFromUnsupportedError";
this.command = command;
}
}
export const getAuthorQuery = `-- name: GetAuthor :one
SELECT id, name, bio FROM authors
WHERE id = $1 LIMIT 1`;
Expand Down Expand Up @@ -104,6 +141,22 @@ export async function createAuthor(client: Client, args: CreateAuthorArgs): Prom
};
}

export const copyAuthorsQuery = `-- name: CopyAuthors :copyfrom
INSERT INTO authors (
name, bio
) VALUES (
$1, $2
)`;

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

export async function copyAuthors(_client: Client, _args: CopyAuthorsArgs[]): Promise<number> {
throw new SqlcCopyFromUnsupportedError(":copyfrom");
}

export const deleteAuthorQuery = `-- name: DeleteAuthor :exec
DELETE FROM authors
WHERE id = $1`;
Expand All @@ -120,3 +173,71 @@ export async function deleteAuthor(client: Client, args: DeleteAuthorArgs): Prom
});
}

export const batchCreateAuthorQuery = `-- name: BatchCreateAuthor :batchone
INSERT INTO authors (
name, bio
) VALUES (
$1, $2
)
RETURNING id, name, bio`;

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

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

export interface BatchCreateAuthorBatchResult {
index: number;
row: BatchCreateAuthorRow | null;
}

export async function batchCreateAuthor(_client: Client, _args: BatchCreateAuthorArgs[], _options?: SqlcBatchOptions): Promise<BatchCreateAuthorBatchResult[]> {
throw new SqlcBatchUnsupportedError(":batchone");
}

export const batchListAuthorsByBioQuery = `-- name: BatchListAuthorsByBio :batchmany
SELECT id, name, bio FROM authors
WHERE bio = $1
ORDER BY name`;

export interface BatchListAuthorsByBioArgs {
bio: string | null;
}

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

export interface BatchListAuthorsByBioBatchResult {
index: number;
rows: BatchListAuthorsByBioRow[];
}

export async function batchListAuthorsByBio(_client: Client, _args: BatchListAuthorsByBioArgs[], _options?: SqlcBatchOptions): Promise<BatchListAuthorsByBioBatchResult[]> {
throw new SqlcBatchUnsupportedError(":batchmany");
}

export const batchDeleteAuthorQuery = `-- name: BatchDeleteAuthor :batchexec
DELETE FROM authors
WHERE id = $1`;

export interface BatchDeleteAuthorArgs {
id: string;
}

export interface BatchDeleteAuthorBatchResult {
index: number;
}

export async function batchDeleteAuthor(_client: Client, _args: BatchDeleteAuthorArgs[], _options?: SqlcBatchOptions): Promise<BatchDeleteAuthorBatchResult[]> {
throw new SqlcBatchUnsupportedError(":batchexec");
}

Loading