-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e62e3b8
commit 5a3bac7
Showing
9 changed files
with
860 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
}); | ||
} |
Oops, something went wrong.