Skip to content

Commit

Permalink
docs: Simplify initial example and use async-await
Browse files Browse the repository at this point in the history
Also add an example route to show how to use the example `User` model.

[ci skip]
  • Loading branch information
nwoltman committed Jan 29, 2018
1 parent e4c74e1 commit 9987b43
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 32 deletions.
31 changes: 15 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ const pool = mysql.createPool({
database: 'my_db',
});
// Both `mysql` and `pool` are 100% compatible with the mysql module

module.exports = pool;
```

Expand All @@ -62,24 +61,14 @@ const userTable = db.defineTable('user', {
email: db.ColTypes.varchar(255).notNull().unique(),
name: db.ColTypes.varchar(63).notNull(),
},
autoIncrement: 5000000000,
});

const User = {
insertAndSelectExample() {
userTable.insert({email: 'newuser@email.com', name: 'newuser'})
.then(result => userTable.select('*', 'WHERE `id` = ?', [result.insertId]))
.then(rows => {
console.log(rows);
/*
[{
id: 5000000001,
email: 'newuser@email.com',
name: 'newuser'
}]
*/
})
.catch(err => console.error(err));
async insertAndSelectExample() {
const result = await userTable.insert({email: 'newuser@email.com', name: 'newuser'})
const rows = await userTable.select('*', 'WHERE `id` = ?', [result.insertId]))
console.log(rows); // [ { id: 1, email: 'newuser@email.com', name: 'newuser' } ]
return rows[0];
}
};

Expand All @@ -90,9 +79,19 @@ module.exports = User;

```js
const db = require('./db');
const User = require('./User');
const express = require('express');
const app = express();

app.get('/user', async (req, res, next) => {
try {
const user = await User.insertAndSelectExample();
res.send(user);
} catch (err) {
next(err)
}
})

// Sync the table schemas to the database
db.sync((err) => {
if (err) throw err;
Expand Down
31 changes: 15 additions & 16 deletions jsdoc2md/README.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ const pool = mysql.createPool({
database: 'my_db',
});
// Both `mysql` and `pool` are 100% compatible with the mysql module

module.exports = pool;
```

Expand All @@ -62,24 +61,14 @@ const userTable = db.defineTable('user', {
email: db.ColTypes.varchar(255).notNull().unique(),
name: db.ColTypes.varchar(63).notNull(),
},
autoIncrement: 5000000000,
});

const User = {
insertAndSelectExample() {
userTable.insert({email: 'newuser@email.com', name: 'newuser'})
.then(result => userTable.select('*', 'WHERE `id` = ?', [result.insertId]))
.then(rows => {
console.log(rows);
/*
[{
id: 5000000001,
email: 'newuser@email.com',
name: 'newuser'
}]
*/
})
.catch(err => console.error(err));
async insertAndSelectExample() {
const result = await userTable.insert({email: 'newuser@email.com', name: 'newuser'})
const rows = await userTable.select('*', 'WHERE `id` = ?', [result.insertId]))
console.log(rows); // [ { id: 1, email: 'newuser@email.com', name: 'newuser' } ]
return rows[0];
}
};

Expand All @@ -90,9 +79,19 @@ module.exports = User;

```js
const db = require('./db');
const User = require('./User');
const express = require('express');
const app = express();

app.get('/user', async (req, res, next) => {
try {
const user = await User.insertAndSelectExample();
res.send(user);
} catch (err) {
next(err)
}
})

// Sync the table schemas to the database
db.sync((err) => {
if (err) throw err;
Expand Down

0 comments on commit 9987b43

Please sign in to comment.