feat(deno-client): support mysql (#971)

This commit is contained in:
Jakub Kołodziejczak
2022-12-01 23:44:52 +01:00
committed by GitHub
parent 08d489b674
commit 0e402f6a9d
2 changed files with 81 additions and 9 deletions
+71
View File
@@ -0,0 +1,71 @@
import { createConnection } from "https://deno.land/x/mysql2@v1.0.6/mod.ts";
import { type Resource } from "./mod.ts";
/**
* Establish MySQL connection using MySQL client for Deno:
* https://deno.land/x/mysql2@v1.0.6/mod.ts
*
* IMPORTANT: make sure to close the connection with `.end()`
*
* @param db the MySQL resource to establish the connection for
*
* @returns MySQL database connection
*
* @example
* ```ts
* const conn = await mysqlClient(db);
* await conn.execute('CREATE TABLE IF NOT EXISTS pets (name varchar(255), kind varchar(255))');
* await conn.execute('INSERT INTO pets VALUES (?, ?)', ['behemot','cat']);
* const [rows] = await conn.execute('SELECT * from pets');
* conn.end();
* console.log(rows);
* ```
*/
export async function mysqlClient(
db: Resource<"mysql">
) {
const conn = await createConnection(db);
return conn;
}
/**
* Execute SQL query. For more info check:
* https://deno.land/x/mysql2@v1.0.6/mod.ts
*
* @param db the MySQL resource to run sql query for
*
* @returns array with two items: rows and fields
*
* @example
* ```ts
* const kind = 'cat';
* const { rows } = await mySql(db)`SELECT * from pets WHERE kind = ${kind}`;
* console.log(rows);
* ```
*/
export function mySql(
db: Resource<"mysql">,
asObjects = false
) {
return async function execute(
query: TemplateStringsArray,
...args: unknown[],
) {
const conn = await mysqlClient(db);
const adapter = getQueryAdapter(query, args);
const [rows, fields] = await conn.execute(...adapter);
conn.end();
return { rows: asObjects ? rows : getRowsAdapter(rows), fields };
}
}
function getQueryAdapter(template: TemplateStringsArray, args: unknown[]) {
const text = template.reduce((curr, next) => {
return `${curr}?${next}`;
});
return [text, args];
}
function getRowsAdapter(rows: object[]) {
return rows.map((r) => Object.values(r))
}
+10 -9
View File
@@ -3,21 +3,21 @@ import { type Resource } from "./mod.ts"
/**
* deno-postgres client API is very flexible:
* https://deno.land/x/postgres@v0.16.1/mod.ts?s=QueryClient
* https://deno.land/x/postgres@v0.17.0/mod.ts?s=QueryClient
*
* @param db the postgresql resource to generate the client for
* @param db the PostgreSQL resource to generate the client for
*
* @returns the client for the resource
*
* Usage:
* Static query:
* @example
* // Static query
* ```ts
* const {rows} = await pgClient(db).queryObject(
* const { rows } = await pgClient(db).queryObject(
* "SELECT ID, NAME FROM CLIENTS"
* );
* ```
*
* Prepared Statements:
* // Prepared Statements
* ```ts
* const { rows } = await pgClient(db).queryObject`SELECT ID, NAME FROM CLIENTS WHERE ID = ${id}`;
* ```
@@ -32,13 +32,14 @@ export function pgClient(
/**
* deno-postgres client API is very flexible:
* https://deno.land/x/postgres@v0.16.1/mod.ts?s=QueryClient
* https://deno.land/x/postgres@v0.17.0/mod.ts?s=QueryClient
*
* @param db the postgresql resource to generate the client for
* @param db the PostgreSQL resource to run sql query for
*
* @returns the rows corresponding to the returned objetcs
*
* Prepared Statements:
* @example
* // Prepared Statements
* ```ts
* const { rows } = await pgSql(db)`SELECT ID, NAME FROM CLIENTS WHERE ID = ${id}`;
* ```