Skip to content

Commit

Permalink
Run MySQL test
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleconroy committed Nov 30, 2023
1 parent e62e3b8 commit 5a3bac7
Show file tree
Hide file tree
Showing 9 changed files with 860 additions and 3 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
bun-version: latest
- run: bun install
working-directory: ${{ matrix.dir }}
- run: echo "DATABASE_URL=$(sqlc createdb)" >> $GITHUB_ENV
- run: echo "DATABASE_URL=$(sqlc createdb --queryset postgresql)" >> $GITHUB_ENV
working-directory: examples/authors
env:
SQLC_AUTH_TOKEN: ${{ secrets.SQLC_AUTH_TOKEN }}
Expand All @@ -29,8 +29,12 @@ jobs:
strategy:
matrix:
include:
- dir: "examples/node-mysql2"
qs: "mysql"
- dir: "examples/node-postgres"
qs: "postgresql"
- dir: "examples/node-pg"
qs: "postgresql"
steps:
- uses: actions/checkout@v4
- uses: sqlc-dev/setup-sqlc@v4
Expand All @@ -41,7 +45,7 @@ jobs:
working-directory: ${{ matrix.dir }}
- run: npx tsc
working-directory: ${{ matrix.dir }}
- run: echo "DATABASE_URL=$(sqlc createdb)" >> $GITHUB_ENV
- run: echo "DATABASE_URL=$(sqlc createdb --queryset ${{ matrix.qs }})" >> $GITHUB_ENV
working-directory: examples/authors
env:
SQLC_AUTH_TOKEN: ${{ secrets.SQLC_AUTH_TOKEN }}
Expand Down
9 changes: 8 additions & 1 deletion examples/authors/sqlc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@ version: "2"
cloud:
project: "01HAQMMECEYQYKFJN8MP16QC41"
sql:
- schema: "postgresql/schema.sql"
- name: postgresql
schema: "postgresql/schema.sql"
queries: "postgresql/query.sql"
engine: "postgresql"
database:
managed: true
- name: mysql
schema: "mysql/schema.sql"
queries: "mysql/query.sql"
engine: "mysql"
database:
managed: true
148 changes: 148 additions & 0 deletions examples/node-mysql2/package-lock.json

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

18 changes: 18 additions & 0 deletions examples/node-mysql2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "mysql2",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"mysql2": "^3.6.3"
},
"devDependencies": {
"@types/node": "^20.9.0",
"typescript": "^5.3.2"
}
}
119 changes: 119 additions & 0 deletions examples/node-mysql2/src/db/handwritten_query_sql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import mysql, { RowDataPacket } from "mysql2/promise";

type Client = mysql.Connection | mysql.Pool;

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(
client: Client,
args: GetAuthorArgs
): Promise<GetAuthorRow | null> {
const [rows] = await client.query<RowDataPacket[]>({
sql: getAuthorQuery,
values: [args.id],
rowsAsArray: true,
});
if (rows.length !== 1) {
return null;
}
const row = rows[0];
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(client: Client): Promise<ListAuthorsRow[]> {
const [rows] = await client.query<RowDataPacket[]>({
sql: listAuthorsQuery,
values: [],
rowsAsArray: true,
});
return rows.map((row) => {
return {
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(
client: Client,
args: CreateAuthorArgs
): Promise<CreateAuthorRow | null> {
const [rows] = await client.query<RowDataPacket[]>({
sql: createAuthorQuery,
values: [args.name, args.bio],
rowsAsArray: true,
});
if (rows.length !== 1) {
return null;
}
const row = rows[0];
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(
client: Client,
args: DeleteAuthorArgs
): Promise<void> {
await client.query({
sql: deleteAuthorQuery,
values: [args.id],
});
}
Loading

0 comments on commit 5a3bac7

Please sign in to comment.