docs: add example using Postgres and update MySQL (#1076)

* docs: add example using Postgres and update MySQL
* docs: update to pass linter
This commit is contained in:
Forbes Lindesay 2019-12-13 20:01:51 +00:00 committed by Jason Etcovitch
parent 0f777079a2
commit 95994a0927
1 changed files with 45 additions and 19 deletions

View File

@ -85,40 +85,66 @@ module.exports = app => {
### MySQL ### MySQL
Using the [`mysql`](https://github.com/mysqljs/mysql) module, we can connect to our MySQL database and perform queries. Using the [`@databases/mysql`](https://www.atdatabases.org/docs/mysql.html) module, we can connect to our MySQL database and perform queries.
```js ```js
// connection.js // connection.js
const mysql = require('mysql') const mysql = require('@databases/mysql')
const connection = mysql.createConnection({ // DATABASE_URL = mysql://my-user:my-password@localhost/my-db
host: process.env.DB_HOST, const connection = connect(process.env.DATABASE_URL)
user: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_DATABASE
})
connection.connect()
module.exports = connection module.exports = connection
``` ```
```js ```js
// index.js // index.js
const { sql } = require('@databases/mysql')
const connection = require('./connection') const connection = require('./connection')
function performQuery (query) {
return new Promise((resolve, reject) => {
connection.query(query, function (error, results, fields) {
if (error) reject(new Error(error))
resolve(results)
})
})
}
module.exports = app => { module.exports = app => {
app.on('issues.opened', async context => { app.on('issues.opened', async context => {
// Find all the people in the database // Find all the people in the database
const people = await performQuery('SELECT * FROM `people`') const people = await connection.query(sql`SELECT * FROM people`)
// Generate a string using all the peoples' names.
// It would look like: 'Jason, Jane, James, Jennifer'
const peoplesNames = people.map(key => people[key].name).join(', ')
// `context` extracts information from the event, which can be passed to
// GitHub API calls. This will return:
// { owner: 'yourname', repo: 'yourrepo', number: 123, body: 'The following people are in the database: Jason, Jane, James, Jennifer' }
const params = context.issue({ body: `The following people are in the database: ${peoplesNames}` })
// Post a comment on the issue
return context.github.issues.createComment(params)
})
}
```
### Postgres
Using the [`@databases/pg`](https://www.atdatabases.org/docs/pg.html) module, we can connect to our Postgres database and perform queries.
```js
// connection.js
const mysql = require('@databases/pg')
// DATABASE_URL = postgresql://my-user:my-password@localhost/my-db
const connection = connect(process.env.DATABASE_URL)
module.exports = connection
```
```js
// index.js
const { sql } = require('@databases/pg')
const connection = require('./connection')
module.exports = app => {
app.on('issues.opened', async context => {
// Find all the people in the database
const people = await connection.query(sql`SELECT * FROM people`)
// Generate a string using all the peoples' names. // Generate a string using all the peoples' names.
// It would look like: 'Jason, Jane, James, Jennifer' // It would look like: 'Jason, Jane, James, Jennifer'