MySQL examples (#501)

* Update README.md

* Update index.ts
pull/505/head
David Blythe 2 years ago committed by GitHub
parent 4fd6b58070
commit 51f20b438e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -65,6 +65,37 @@ const db = await Database.load("postgres://postgres:password@localhost/test");
await db.execute("INSERT INTO ..."); await db.execute("INSERT INTO ...");
``` ```
## Syntax
We use sqlx as our underlying library, adopting their query syntax:
- sqlite and postgres use the "$#" syntax when substituting query data
- mysql uses "?" when substituting query data
```javascript
// INSERT and UPDATE examples for sqlite and postgres
const result = await db.execute(
"INSERT into todos (id, title, status) VALUES ($1, $2, $3)",
[todos.id, todos.title, todos.status],
);
const result = await db.execute(
"UPDATE todos SET title = $1, completed = $2 WHERE id = $3",
[todos.title, todos.status, todos.id],
);
// INSERT and UPDATE examples for mysql
const result = await db.execute(
"INSERT into todos (id, title, status) VALUES (?, ?, ?)",
[todos.id, todos.title, todos.status],
);
const result = await db.execute(
"UPDATE todos SET title = ?, completed = ? WHERE id = ?",
[todos.title, todos.status, todos.id],
);
```
## Contributing ## Contributing
PRs accepted. Please make sure to read the Contributing Guide before making a pull request. PRs accepted. Please make sure to read the Contributing Guide before making a pull request.

@ -76,10 +76,29 @@ export default class Database {
* *
* @example * @example
* ```ts * ```ts
* // for sqlite & postgres
* // INSERT example
* const result = await db.execute(
* "INSERT into todos (id, title, status) VALUES ($1, $2, $3)",
* [ todos.id, todos.title, todos.status ]
* );
* // UPDATE example
* const result = await db.execute( * const result = await db.execute(
* "UPDATE todos SET title = $1, completed = $2 WHERE id = $3", * "UPDATE todos SET title = $1, completed = $2 WHERE id = $3",
* [ todos.title, todos.status, todos.id ] * [ todos.title, todos.status, todos.id ]
* ); * );
*
* // for mysql
* // INSERT example
* const result = await db.execute(
* "INSERT into todos (id, title, status) VALUES (?, ?, ?)",
* [ todos.id, todos.title, todos.status ]
* );
* // UPDATE example
* const result = await db.execute(
* "UPDATE todos SET title = ?, completed = ? WHERE id = ?",
* [ todos.title, todos.status, todos.id ]
* );
* ``` * ```
*/ */
async execute(query: string, bindValues?: unknown[]): Promise<QueryResult> { async execute(query: string, bindValues?: unknown[]): Promise<QueryResult> {
@ -91,13 +110,11 @@ export default class Database {
values: bindValues ?? [], values: bindValues ?? [],
}, },
); );
return { return {
lastInsertId, lastInsertId,
rowsAffected, rowsAffected,
}; };
} }
/** /**
* **select** * **select**
* *
@ -105,9 +122,15 @@ export default class Database {
* *
* @example * @example
* ```ts * ```ts
* // for sqlite & postgres
* const result = await db.select( * const result = await db.select(
* "SELECT * from todos WHERE id = $1", id * "SELECT * from todos WHERE id = $1", id
* ); * );
*
* // for mysql
* const result = await db.select(
* "SELECT * from todos WHERE id = ?", id
* );
* ``` * ```
*/ */
async select<T>(query: string, bindValues?: unknown[]): Promise<T> { async select<T>(query: string, bindValues?: unknown[]): Promise<T> {

Loading…
Cancel
Save