Skip to content

Commit f27dfac

Browse files
tsmith123pieh
authored andcommitted
[gatsby-source-mongodb] Use Promises to prevent calling done too early (#5738)
* Use Promises to prevent calling done too early * Remove comment * promises all the way
1 parent f7c948f commit f27dfac

File tree

2 files changed

+92
-84
lines changed

2 files changed

+92
-84
lines changed

packages/gatsby-source-mongodb/src/gatsby-node.js

Lines changed: 79 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ const _ = require(`lodash`)
55

66
exports.sourceNodes = (
77
{ boundActionCreators, getNode, hasNodeChanged },
8-
pluginOptions,
9-
done
8+
pluginOptions
109
) => {
1110
const { createNode } = boundActionCreators
1211

@@ -19,98 +18,94 @@ exports.sourceNodes = (
1918
if (pluginOptions.auth)
2019
authUrlPart = `${pluginOptions.auth.user}:${pluginOptions.auth.password}@`
2120

22-
MongoClient.connect(
23-
`mongodb://${authUrlPart}${serverOptions.address}:${
24-
serverOptions.port
25-
}/${dbName}`,
26-
function(err, db) {
27-
// Establish connection to db
28-
if (err) {
29-
console.warn(err)
30-
return
31-
}
32-
let collection = pluginOptions.collection || `documents`
33-
if (_.isArray(collection)) {
34-
for (const col of collection) {
35-
createNodes(db, pluginOptions, dbName, createNode, col, done)
36-
}
37-
} else {
38-
createNodes(db, pluginOptions, dbName, createNode, collection, done)
21+
const connectionURL = `mongodb://${authUrlPart}${serverOptions.address}:${
22+
serverOptions.port
23+
}/${dbName}`
24+
25+
return MongoClient.connect(connectionURL)
26+
.then(db => {
27+
let collection = pluginOptions.collection || [`documents`]
28+
if (!_.isArray(collection)) {
29+
collection = [collection]
3930
}
40-
}
41-
)
31+
32+
return Promise.all(
33+
collection.map(col =>
34+
createNodes(db, pluginOptions, dbName, createNode, col)
35+
)
36+
)
37+
})
38+
.catch(err => {
39+
console.warn(err)
40+
return err
41+
})
4242
}
4343

44-
function createNodes(
45-
db,
46-
pluginOptions,
47-
dbName,
48-
createNode,
49-
collectionName,
50-
done
51-
) {
52-
let collection = db.collection(collectionName)
53-
let cursor = collection.find()
44+
function createNodes(db, pluginOptions, dbName, createNode, collectionName) {
45+
return new Promise((resolve, reject) => {
46+
let collection = db.collection(collectionName)
47+
let cursor = collection.find()
5448

55-
// Execute the each command, triggers for each document
56-
cursor.each(function(err, item) {
57-
// If the item is null then the cursor is exhausted/empty and closed
58-
if (item == null) {
59-
// Let's close the db
60-
db.close()
61-
done()
62-
} else {
63-
var id = item._id.toString()
64-
delete item._id
49+
// Execute the each command, triggers for each document
50+
cursor.each(function(err, item) {
51+
// If the item is null then the cursor is exhausted/empty and closed
52+
if (item == null) {
53+
// Let's close the db
54+
db.close()
55+
resolve()
56+
} else {
57+
var id = item._id.toString()
58+
delete item._id
6559

66-
var node = {
67-
// Data for the node.
68-
...item,
69-
id: `${id}`,
70-
parent: `__${collectionName}__`,
71-
children: [],
72-
internal: {
73-
type: `mongodb${caps(dbName)}${caps(collectionName)}`,
74-
content: JSON.stringify(item),
75-
contentDigest: crypto
76-
.createHash(`md5`)
77-
.update(JSON.stringify(item))
78-
.digest(`hex`),
79-
},
80-
}
81-
const childrenNodes = []
82-
if (pluginOptions.map) {
83-
let mapObj = pluginOptions.map
84-
if (pluginOptions.map[collectionName]) {
85-
mapObj = pluginOptions.map[collectionName]
60+
var node = {
61+
// Data for the node.
62+
...item,
63+
id: `${id}`,
64+
parent: `__${collectionName}__`,
65+
children: [],
66+
internal: {
67+
type: `mongodb${caps(dbName)}${caps(collectionName)}`,
68+
content: JSON.stringify(item),
69+
contentDigest: crypto
70+
.createHash(`md5`)
71+
.update(JSON.stringify(item))
72+
.digest(`hex`),
73+
},
8674
}
87-
// We need to map certain fields to a contenttype.
88-
Object.keys(mapObj).forEach(mediaItemFieldKey => {
89-
if (
90-
node[mediaItemFieldKey] &&
91-
(typeof mapObj[mediaItemFieldKey] === `string` ||
92-
mapObj[mediaItemFieldKey] instanceof String)
93-
) {
94-
const mappingChildNode = prepareMappingChildNode(
95-
node,
96-
mediaItemFieldKey,
97-
node[mediaItemFieldKey],
98-
mapObj[mediaItemFieldKey],
99-
createNode
100-
)
75+
const childrenNodes = []
76+
if (pluginOptions.map) {
77+
let mapObj = pluginOptions.map
78+
if (pluginOptions.map[collectionName]) {
79+
mapObj = pluginOptions.map[collectionName]
80+
}
81+
// We need to map certain fields to a contenttype.
82+
Object.keys(mapObj).forEach(mediaItemFieldKey => {
83+
if (
84+
node[mediaItemFieldKey] &&
85+
(typeof mapObj[mediaItemFieldKey] === `string` ||
86+
mapObj[mediaItemFieldKey] instanceof String)
87+
) {
88+
const mappingChildNode = prepareMappingChildNode(
89+
node,
90+
mediaItemFieldKey,
91+
node[mediaItemFieldKey],
92+
mapObj[mediaItemFieldKey],
93+
createNode
94+
)
10195

102-
node[`${mediaItemFieldKey}___NODE`] = mappingChildNode.id
103-
childrenNodes.push(mappingChildNode)
96+
node[`${mediaItemFieldKey}___NODE`] = mappingChildNode.id
97+
childrenNodes.push(mappingChildNode)
10498

105-
delete node[mediaItemFieldKey]
106-
}
99+
delete node[mediaItemFieldKey]
100+
}
101+
})
102+
}
103+
createNode(node)
104+
childrenNodes.forEach(node => {
105+
createNode(node)
107106
})
108107
}
109-
createNode(node)
110-
childrenNodes.forEach(node => {
111-
createNode(node)
112-
})
113-
}
108+
})
114109
})
115110
}
116111

yarn.lock

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,13 @@ axios@^0.17.1:
658658
follow-redirects "^1.2.5"
659659
is-buffer "^1.1.5"
660660

661+
axios@^0.18.0:
662+
version "0.18.0"
663+
resolved "https://registry.yarnpkg.com/axios/-/axios-0.18.0.tgz#32d53e4851efdc0a11993b6cd000789d70c05102"
664+
dependencies:
665+
follow-redirects "^1.3.0"
666+
is-buffer "^1.1.5"
667+
661668
axobject-query@^0.1.0:
662669
version "0.1.0"
663670
resolved "https://registry.yarnpkg.com/axobject-query/-/axobject-query-0.1.0.tgz#62f59dbc59c9f9242759ca349960e7a2fe3c36c0"
@@ -5081,6 +5088,12 @@ follow-redirects@^1.2.3, follow-redirects@^1.2.5:
50815088
dependencies:
50825089
debug "^3.1.0"
50835090

5091+
follow-redirects@^1.3.0:
5092+
version "1.5.0"
5093+
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.5.0.tgz#234f49cf770b7f35b40e790f636ceba0c3a0ab77"
5094+
dependencies:
5095+
debug "^3.1.0"
5096+
50845097
for-each@^0.3.2:
50855098
version "0.3.2"
50865099
resolved "https://registry.yarnpkg.com/for-each/-/for-each-0.3.2.tgz#2c40450b9348e97f281322593ba96704b9abd4d4"

0 commit comments

Comments
 (0)