Skip to content

Commit d9387cb

Browse files
committed
feat(deno-client): support mysql
1 parent 4ef84f5 commit d9387cb

File tree

2 files changed

+81
-9
lines changed

2 files changed

+81
-9
lines changed

deno-client/mysql.ts

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { createConnection } from "https://deno.land/x/[email protected]/mod.ts";
2+
import { type Resource } from "./mod.ts";
3+
4+
/**
5+
* Establish MySQL connection using MySQL client for Deno:
6+
* https://deno.land/x/[email protected]/mod.ts
7+
*
8+
* IMPORTANT: make sure to close the connection with `.end()`
9+
*
10+
* @param db the MySQL resource to establish the connection for
11+
*
12+
* @returns MySQL database connection
13+
*
14+
* @example
15+
* ```ts
16+
* const conn = await mysqlClient(db);
17+
* await conn.execute('CREATE TABLE IF NOT EXISTS pets (name varchar(255), kind varchar(255))');
18+
* await conn.execute('INSERT INTO pets VALUES (?, ?)', ['behemot','cat']);
19+
* const [rows] = await conn.execute('SELECT * from pets');
20+
* conn.end();
21+
* console.log(rows);
22+
* ```
23+
*/
24+
export async function mysqlClient(
25+
db: Resource<"mysql">
26+
) {
27+
const conn = await createConnection(db);
28+
return conn;
29+
}
30+
31+
/**
32+
* Execute SQL query. For more info check:
33+
* https://deno.land/x/[email protected]/mod.ts
34+
*
35+
* @param db the MySQL resource to run sql query for
36+
*
37+
* @returns array with two items: rows and fields
38+
*
39+
* @example
40+
* ```ts
41+
* const kind = 'cat';
42+
* const { rows } = await mySql(db)`SELECT * from pets WHERE kind = ${kind}`;
43+
* console.log(rows);
44+
* ```
45+
*/
46+
export function mySql(
47+
db: Resource<"mysql">,
48+
asObjects = false
49+
) {
50+
return async function execute(
51+
query: TemplateStringsArray,
52+
...args: unknown[],
53+
) {
54+
const conn = await mysqlClient(db);
55+
const adapter = getQueryAdapter(query, args);
56+
const [rows, fields] = await conn.execute(...adapter);
57+
conn.end();
58+
return { rows: asObjects ? rows : getRowsAdapter(rows), fields };
59+
}
60+
}
61+
62+
function getQueryAdapter(template: TemplateStringsArray, args: unknown[]) {
63+
const text = template.reduce((curr, next) => {
64+
return `${curr}?${next}`;
65+
});
66+
return [text, args];
67+
}
68+
69+
function getRowsAdapter(rows: object[]) {
70+
return rows.map((r) => Object.values(r))
71+
}

deno-client/pg.ts

+10-9
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@ import { type Resource } from "./mod.ts"
33

44
/**
55
* deno-postgres client API is very flexible:
6-
* https://deno.land/x/postgres@v0.16.1/mod.ts?s=QueryClient
6+
* https://deno.land/x/postgres@v0.17.0/mod.ts?s=QueryClient
77
*
8-
* @param db the postgresql resource to generate the client for
8+
* @param db the PostgreSQL resource to generate the client for
99
*
1010
* @returns the client for the resource
1111
*
12-
* Usage:
13-
* Static query:
12+
* @example
13+
* // Static query
1414
* ```ts
15-
* const {rows} = await pgClient(db).queryObject(
15+
* const { rows } = await pgClient(db).queryObject(
1616
* "SELECT ID, NAME FROM CLIENTS"
1717
* );
1818
* ```
1919
*
20-
* Prepared Statements:
20+
* // Prepared Statements
2121
* ```ts
2222
* const { rows } = await pgClient(db).queryObject`SELECT ID, NAME FROM CLIENTS WHERE ID = ${id}`;
2323
* ```
@@ -32,13 +32,14 @@ export function pgClient(
3232

3333
/**
3434
* deno-postgres client API is very flexible:
35-
* https://deno.land/x/postgres@v0.16.1/mod.ts?s=QueryClient
35+
* https://deno.land/x/postgres@v0.17.0/mod.ts?s=QueryClient
3636
*
37-
* @param db the postgresql resource to generate the client for
37+
* @param db the PostgreSQL resource to run sql query for
3838
*
3939
* @returns the rows corresponding to the returned objetcs
4040
*
41-
* Prepared Statements:
41+
* @example
42+
* // Prepared Statements
4243
* ```ts
4344
* const { rows } = await pgSql(db)`SELECT ID, NAME FROM CLIENTS WHERE ID = ${id}`;
4445
* ```

0 commit comments

Comments
 (0)