Skip to content

Commit 9987b43

Browse files
committed
docs: Simplify initial example and use async-await
Also add an example route to show how to use the example `User` model. [ci skip]
1 parent e4c74e1 commit 9987b43

File tree

2 files changed

+30
-32
lines changed

2 files changed

+30
-32
lines changed

README.md

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ const pool = mysql.createPool({
4747
database: 'my_db',
4848
});
4949
// Both `mysql` and `pool` are 100% compatible with the mysql module
50-
5150
module.exports = pool;
5251
```
5352

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

6866
const User = {
69-
insertAndSelectExample() {
70-
userTable.insert({email: 'newuser@email.com', name: 'newuser'})
71-
.then(result => userTable.select('*', 'WHERE `id` = ?', [result.insertId]))
72-
.then(rows => {
73-
console.log(rows);
74-
/*
75-
[{
76-
id: 5000000001,
77-
email: 'newuser@email.com',
78-
name: 'newuser'
79-
}]
80-
*/
81-
})
82-
.catch(err => console.error(err));
67+
async insertAndSelectExample() {
68+
const result = await userTable.insert({email: 'newuser@email.com', name: 'newuser'})
69+
const rows = await userTable.select('*', 'WHERE `id` = ?', [result.insertId]))
70+
console.log(rows); // [ { id: 1, email: 'newuser@email.com', name: 'newuser' } ]
71+
return rows[0];
8372
}
8473
};
8574

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

9180
```js
9281
const db = require('./db');
82+
const User = require('./User');
9383
const express = require('express');
9484
const app = express();
9585

86+
app.get('/user', async (req, res, next) => {
87+
try {
88+
const user = await User.insertAndSelectExample();
89+
res.send(user);
90+
} catch (err) {
91+
next(err)
92+
}
93+
})
94+
9695
// Sync the table schemas to the database
9796
db.sync((err) => {
9897
if (err) throw err;

jsdoc2md/README.hbs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ const pool = mysql.createPool({
4747
database: 'my_db',
4848
});
4949
// Both `mysql` and `pool` are 100% compatible with the mysql module
50-
5150
module.exports = pool;
5251
```
5352

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

6866
const User = {
69-
insertAndSelectExample() {
70-
userTable.insert({email: 'newuser@email.com', name: 'newuser'})
71-
.then(result => userTable.select('*', 'WHERE `id` = ?', [result.insertId]))
72-
.then(rows => {
73-
console.log(rows);
74-
/*
75-
[{
76-
id: 5000000001,
77-
email: 'newuser@email.com',
78-
name: 'newuser'
79-
}]
80-
*/
81-
})
82-
.catch(err => console.error(err));
67+
async insertAndSelectExample() {
68+
const result = await userTable.insert({email: 'newuser@email.com', name: 'newuser'})
69+
const rows = await userTable.select('*', 'WHERE `id` = ?', [result.insertId]))
70+
console.log(rows); // [ { id: 1, email: 'newuser@email.com', name: 'newuser' } ]
71+
return rows[0];
8372
}
8473
};
8574

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

9180
```js
9281
const db = require('./db');
82+
const User = require('./User');
9383
const express = require('express');
9484
const app = express();
9585

86+
app.get('/user', async (req, res, next) => {
87+
try {
88+
const user = await User.insertAndSelectExample();
89+
res.send(user);
90+
} catch (err) {
91+
next(err)
92+
}
93+
})
94+
9695
// Sync the table schemas to the database
9796
db.sync((err) => {
9897
if (err) throw err;

0 commit comments

Comments
 (0)