Thanks to visit codestin.com
Credit goes to github.com

Skip to content

sample: add support for PostgreSql enum types #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 6 additions & 3 deletions examples/authors/postgresql/schema.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
CREATE TYPE authors_role AS ENUM ('admin', 'guest');

CREATE TABLE authors (
id BIGSERIAL PRIMARY KEY,
name text NOT NULL,
bio text
id BIGSERIAL PRIMARY KEY,
name text NOT NULL,
bio text,
role authors_role
);
18 changes: 12 additions & 6 deletions examples/bun-pg/src/db/query_sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface Client {
}

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

export interface GetAuthorArgs {
Expand All @@ -18,6 +18,7 @@ export interface GetAuthorRow {
id: string;
name: string;
bio: string | null;
role: "admin" | "guest" | null;
}

export async function getAuthor(client: Client, args: GetAuthorArgs): Promise<GetAuthorRow | null> {
Expand All @@ -33,18 +34,20 @@ export async function getAuthor(client: Client, args: GetAuthorArgs): Promise<Ge
return {
id: row[0],
name: row[1],
bio: row[2]
bio: row[2],
role: row[3]
};
}

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

export interface ListAuthorsRow {
id: string;
name: string;
bio: string | null;
role: "admin" | "guest" | null;
}

export async function listAuthors(client: Client): Promise<ListAuthorsRow[]> {
Expand All @@ -57,7 +60,8 @@ export async function listAuthors(client: Client): Promise<ListAuthorsRow[]> {
return {
id: row[0],
name: row[1],
bio: row[2]
bio: row[2],
role: row[3]
};
});
}
Expand All @@ -68,7 +72,7 @@ INSERT INTO authors (
) VALUES (
$1, $2
)
RETURNING id, name, bio`;
RETURNING id, name, bio, role`;

export interface CreateAuthorArgs {
name: string;
Expand All @@ -79,6 +83,7 @@ export interface CreateAuthorRow {
id: string;
name: string;
bio: string | null;
role: "admin" | "guest" | null;
}

export async function createAuthor(client: Client, args: CreateAuthorArgs): Promise<CreateAuthorRow | null> {
Expand All @@ -94,7 +99,8 @@ export async function createAuthor(client: Client, args: CreateAuthorArgs): Prom
return {
id: row[0],
name: row[1],
bio: row[2]
bio: row[2],
role: row[3]
};
}

Expand Down
18 changes: 12 additions & 6 deletions examples/bun-postgres/src/db/query_sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { Sql } from "postgres";

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

export interface GetAuthorArgs {
Expand All @@ -14,6 +14,7 @@ export interface GetAuthorRow {
id: string;
name: string;
bio: string | null;
role: ("admin" | "guest") | null;
}

export async function getAuthor(sql: Sql, args: GetAuthorArgs): Promise<GetAuthorRow | null> {
Expand All @@ -28,25 +29,28 @@ export async function getAuthor(sql: Sql, args: GetAuthorArgs): Promise<GetAutho
return {
id: row[0],
name: row[1],
bio: row[2]
bio: row[2],
role: row[3]
};
}

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

export interface ListAuthorsRow {
id: string;
name: string;
bio: string | null;
role: ("admin" | "guest") | 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]
bio: row[2],
role: row[3]
}));
}

Expand All @@ -56,7 +60,7 @@ INSERT INTO authors (
) VALUES (
$1, $2
)
RETURNING id, name, bio`;
RETURNING id, name, bio, role`;

export interface CreateAuthorArgs {
name: string;
Expand All @@ -67,6 +71,7 @@ export interface CreateAuthorRow {
id: string;
name: string;
bio: string | null;
role: ("admin" | "guest") | null;
}

export async function createAuthor(sql: Sql, args: CreateAuthorArgs): Promise<CreateAuthorRow | null> {
Expand All @@ -81,7 +86,8 @@ export async function createAuthor(sql: Sql, args: CreateAuthorArgs): Promise<Cr
return {
id: row[0],
name: row[1],
bio: row[2]
bio: row[2],
role: row[3]
};
}

Expand Down
18 changes: 12 additions & 6 deletions examples/node-pg/src/db/query_sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface Client {
}

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

export interface GetAuthorArgs {
Expand All @@ -18,6 +18,7 @@ export interface GetAuthorRow {
id: string;
name: string;
bio: string | null;
role: "admin" | "guest" | null;
}

export async function getAuthor(client: Client, args: GetAuthorArgs): Promise<GetAuthorRow | null> {
Expand All @@ -33,18 +34,20 @@ export async function getAuthor(client: Client, args: GetAuthorArgs): Promise<Ge
return {
id: row[0],
name: row[1],
bio: row[2]
bio: row[2],
role: row[3]
};
}

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

export interface ListAuthorsRow {
id: string;
name: string;
bio: string | null;
role: "admin" | "guest" | null;
}

export async function listAuthors(client: Client): Promise<ListAuthorsRow[]> {
Expand All @@ -57,7 +60,8 @@ export async function listAuthors(client: Client): Promise<ListAuthorsRow[]> {
return {
id: row[0],
name: row[1],
bio: row[2]
bio: row[2],
role: row[3]
};
});
}
Expand All @@ -68,7 +72,7 @@ INSERT INTO authors (
) VALUES (
$1, $2
)
RETURNING id, name, bio`;
RETURNING id, name, bio, role`;

export interface CreateAuthorArgs {
name: string;
Expand All @@ -79,6 +83,7 @@ export interface CreateAuthorRow {
id: string;
name: string;
bio: string | null;
role: "admin" | "guest" | null;
}

export async function createAuthor(client: Client, args: CreateAuthorArgs): Promise<CreateAuthorRow | null> {
Expand All @@ -94,7 +99,8 @@ export async function createAuthor(client: Client, args: CreateAuthorArgs): Prom
return {
id: row[0],
name: row[1],
bio: row[2]
bio: row[2],
role: row[3]
};
}

Expand Down
18 changes: 12 additions & 6 deletions examples/node-postgres/src/db/query_sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { Sql } from "postgres";

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

export interface GetAuthorArgs {
Expand All @@ -14,6 +14,7 @@ export interface GetAuthorRow {
id: string;
name: string;
bio: string | null;
role: ("admin" | "guest") | null;
}

export async function getAuthor(sql: Sql, args: GetAuthorArgs): Promise<GetAuthorRow | null> {
Expand All @@ -28,25 +29,28 @@ export async function getAuthor(sql: Sql, args: GetAuthorArgs): Promise<GetAutho
return {
id: row[0],
name: row[1],
bio: row[2]
bio: row[2],
role: row[3]
};
}

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

export interface ListAuthorsRow {
id: string;
name: string;
bio: string | null;
role: ("admin" | "guest") | 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]
bio: row[2],
role: row[3]
}));
}

Expand All @@ -56,7 +60,7 @@ INSERT INTO authors (
) VALUES (
$1, $2
)
RETURNING id, name, bio`;
RETURNING id, name, bio, role`;

export interface CreateAuthorArgs {
name: string;
Expand All @@ -67,6 +71,7 @@ export interface CreateAuthorRow {
id: string;
name: string;
bio: string | null;
role: ("admin" | "guest") | null;
}

export async function createAuthor(sql: Sql, args: CreateAuthorArgs): Promise<CreateAuthorRow | null> {
Expand All @@ -81,7 +86,8 @@ export async function createAuthor(sql: Sql, args: CreateAuthorArgs): Promise<Cr
return {
id: row[0],
name: row[1],
bio: row[2]
bio: row[2],
role: row[3]
};
}

Expand Down
19 changes: 11 additions & 8 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
Column,
File,
Query,
Catalog,
} from "./gen/plugin/codegen_pb";

import { argName, colName } from "./drivers/utlis";
Expand All @@ -48,7 +49,7 @@ interface Options {

interface Driver {
preamble: (queries: Query[]) => Node[];
columnType: (c?: Column) => TypeNode;
columnType: (c?: Column, catalog?: Catalog) => TypeNode;
execDecl: (
name: string,
text: string,
Expand Down Expand Up @@ -151,13 +152,13 @@ ${query.text}`
let returnIface = undefined;
if (query.params.length > 0) {
argIface = `${query.name}Args`;
nodes.push(argsDecl(argIface, driver, query.params));
nodes.push(argsDecl(argIface, driver, query.params, input.catalog));
}
if (query.columns.length > 0) {
returnIface = `${query.name}Row`;
nodes.push(rowDecl(returnIface, driver, query.columns));
nodes.push(rowDecl(returnIface, driver, query.columns, input.catalog));
}

switch (query.cmd) {
case ":exec": {
nodes.push(
Expand Down Expand Up @@ -240,7 +241,8 @@ function queryDecl(name: string, sql: string) {
function argsDecl(
name: string,
driver: Driver,
params: Parameter[]
params: Parameter[],
catalog?: Catalog
) {
return factory.createInterfaceDeclaration(
[factory.createToken(SyntaxKind.ExportKeyword)],
Expand All @@ -252,7 +254,7 @@ function argsDecl(
undefined,
factory.createIdentifier(argName(i, param.column)),
undefined,
driver.columnType(param.column)
driver.columnType(param.column, catalog)
)
)
);
Expand All @@ -261,7 +263,8 @@ function argsDecl(
function rowDecl(
name: string,
driver: Driver,
columns: Column[]
columns: Column[],
catalog?: Catalog
) {
return factory.createInterfaceDeclaration(
[factory.createToken(SyntaxKind.ExportKeyword)],
Expand All @@ -273,7 +276,7 @@ function rowDecl(
undefined,
factory.createIdentifier(colName(i, column)),
undefined,
driver.columnType(column)
driver.columnType(column, catalog)
)
)
);
Expand Down
Loading