Skip to content

Commit 54216e3

Browse files
caubleebyron
authored andcommitted
Update SQL.md (#139)
* Update SQL.md untested, I didn't see db.parallelize in the API https://github.com/mapbox/node-sqlite3/wiki/API * Remove reference to the removed parallelize method * Keep Error fallback * Preserve formatting
1 parent 7521777 commit 54216e3

File tree

1 file changed

+11
-28
lines changed

1 file changed

+11
-28
lines changed

examples/SQL.md

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,24 @@ While not a key-value store, SQL offers a natural batch mechanism with
55
stores, it is still suited for SQL when queries remain simple. This example
66
requests the entire row at a given `id`, however your usage may differ.
77

8-
This example uses the [sqlite3][] client which offers a `parallelize` method to
9-
further batch queries together. Another non-caching `DataLoader` utilizes this
10-
method to provide a similar API. `DataLoaders` can access other `DataLoaders`.
11-
128
```js
139
const DataLoader = require('dataloader')
1410
const sqlite3 = require('sqlite3')
1511

1612
const db = new sqlite3.Database('./to/your/db.sql')
1713

1814
// Dispatch a WHERE-IN query, ensuring response has rows in correct order.
19-
const userLoader = new DataLoader(async ids => {
20-
const params = ids.map(id => '?' ).join()
21-
const query = `SELECT * FROM users WHERE id IN (${params})`
22-
const rows = await queryLoader.load([query, ids])
23-
return ids.map(
24-
id => rows.find(row => row.id === id) || new Error(`Row not found: ${id}`)
25-
)
26-
})
27-
28-
// Parallelize all queries, but do not cache.
29-
const queryLoader = new DataLoader(queries => new Promise(resolve => {
30-
const waitingOn = queries.length
31-
const results = []
32-
db.parallelize(() => {
33-
queries.forEach((query, index) => {
34-
db.all.apply(db, query.concat((error, result) => {
35-
results[index] = error || result
36-
if (--waitingOn === 0) {
37-
resolve(results)
38-
}
39-
}))
40-
})
41-
})
42-
}), { cache: false })
15+
const userLoader = new DataLoader(ids => new Promise((resolve, reject) => {
16+
db.all('SELECT * FROM users WHERE id IN $ids', {$ids: ids}, (error, rows) => {
17+
if (error) {
18+
reject(error);
19+
} else {
20+
resolve(ids.map(
21+
id => rows.find(row => rows.id === id) || new Error(`Row not found: ${id}`)
22+
));
23+
}
24+
});
25+
}));
4326

4427
// Usage
4528

0 commit comments

Comments
 (0)