Skip to content

Commit 96764e2

Browse files
author
Theo Gravity
authored
Merge pull request #45 from LinusU/readme
Move away from global db object in readme
2 parents 9d6f3c1 + 9a87112 commit 96764e2

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

README.md

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,15 @@ Below is an example of how to use it with [Node.js](https://nodejs.org), [Expres
3535
```js
3636
import express from 'express';
3737
import Promise from 'bluebird';
38-
import db from 'sqlite';
38+
import sqlite from 'sqlite';
3939

4040
const app = express();
4141
const port = process.env.PORT || 3000;
42+
const dbPromise = sqlite.open('./database.sqlite', { Promise }));
4243

4344
app.get('/post/:id', async (req, res, next) => {
4445
try {
46+
const db = await dbPromise;
4547
const [post, categories] = await Promise.all([
4648
db.get('SELECT * FROM Post WHERE id = ?', req.params.id),
4749
db.all('SELECT * FROM Category');
@@ -52,20 +54,15 @@ app.get('/post/:id', async (req, res, next) => {
5254
}
5355
});
5456

55-
Promise.resolve()
56-
// First, try connect to the database
57-
.then(() => db.open('./database.sqlite', { Promise }))
58-
.catch(err => console.error(err.stack))
59-
// Finally, launch Node.js app
60-
.finally(() => app.listen(port));
57+
app.listen(port);
6158
```
6259

6360
### Cached DB Driver
6461

6562
If you want to enable the [database object cache](https://github.com/mapbox/node-sqlite3/wiki/Caching)
6663

67-
```
68-
db.open('./database.sqlite', { cached: true }))
64+
```js
65+
sqlite.open('./database.sqlite', { cached: true }))
6966
```
7067

7168
### Migrations
@@ -104,25 +101,23 @@ DROP INDEX Post_ix_categoryId;
104101
```js
105102
import express from 'express';
106103
import Promise from 'bluebird';
107-
import db from 'sqlite';
104+
import sqlite from 'sqlite';
108105

109106
const app = express();
110107
const port = process.env.PORT || 3000;
111108

109+
const dbPromise = Promise.resolve()
110+
.then(() => sqlite.open('./database.sqlite', { Promise }))
111+
.then(db => db.migrate({ force: 'last' }));
112+
112113
app.use(/* app routes */);
113114

114-
Promise.resolve()
115-
// First, try connect to the database and update its schema to the latest version
116-
.then(() => db.open('./database.sqlite', { Promise }))
117-
.then(() => db.migrate({ force: 'last' }))
118-
.catch(err => console.error(err.stack))
119-
// Finally, launch Node.js app
120-
.finally(() => app.listen(port));
115+
app.listen(port);
121116
```
122117

123118
**NOTE**: For the development environment, while working on the database schema, you may want to set
124119
`force: 'last'` (default `false`) that will force the migration API to rollback and re-apply the
125-
latest migration over again each time when Node.js app launches.
120+
latest migration over again each time when Node.js app launches.
126121

127122

128123
### Multiple Connections

0 commit comments

Comments
 (0)