Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[gatsby-source-mongodb] Use Promises to prevent calling done too early #5738

Merged
merged 3 commits into from
Jun 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 79 additions & 84 deletions packages/gatsby-source-mongodb/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ const _ = require(`lodash`)

exports.sourceNodes = (
{ boundActionCreators, getNode, hasNodeChanged },
pluginOptions,
done
pluginOptions
) => {
const { createNode } = boundActionCreators

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

MongoClient.connect(
`mongodb://${authUrlPart}${serverOptions.address}:${
serverOptions.port
}/${dbName}`,
function(err, db) {
// Establish connection to db
if (err) {
console.warn(err)
return
}
let collection = pluginOptions.collection || `documents`
if (_.isArray(collection)) {
for (const col of collection) {
createNodes(db, pluginOptions, dbName, createNode, col, done)
}
} else {
createNodes(db, pluginOptions, dbName, createNode, collection, done)
const connectionURL = `mongodb://${authUrlPart}${serverOptions.address}:${
serverOptions.port
}/${dbName}`

return MongoClient.connect(connectionURL)
.then(db => {
let collection = pluginOptions.collection || [`documents`]
if (!_.isArray(collection)) {
collection = [collection]
}
}
)

return Promise.all(
collection.map(col =>
createNodes(db, pluginOptions, dbName, createNode, col)
)
)
})
.catch(err => {
console.warn(err)
return err
})
}

function createNodes(
db,
pluginOptions,
dbName,
createNode,
collectionName,
done
) {
let collection = db.collection(collectionName)
let cursor = collection.find()
function createNodes(db, pluginOptions, dbName, createNode, collectionName) {
return new Promise((resolve, reject) => {
let collection = db.collection(collectionName)
let cursor = collection.find()

// Execute the each command, triggers for each document
cursor.each(function(err, item) {
// If the item is null then the cursor is exhausted/empty and closed
if (item == null) {
// Let's close the db
db.close()
done()
} else {
var id = item._id.toString()
delete item._id
// Execute the each command, triggers for each document
cursor.each(function(err, item) {
// If the item is null then the cursor is exhausted/empty and closed
if (item == null) {
// Let's close the db
db.close()
resolve()
} else {
var id = item._id.toString()
delete item._id

var node = {
// Data for the node.
...item,
id: `${id}`,
parent: `__${collectionName}__`,
children: [],
internal: {
type: `mongodb${caps(dbName)}${caps(collectionName)}`,
content: JSON.stringify(item),
contentDigest: crypto
.createHash(`md5`)
.update(JSON.stringify(item))
.digest(`hex`),
},
}
const childrenNodes = []
if (pluginOptions.map) {
let mapObj = pluginOptions.map
if (pluginOptions.map[collectionName]) {
mapObj = pluginOptions.map[collectionName]
var node = {
// Data for the node.
...item,
id: `${id}`,
parent: `__${collectionName}__`,
children: [],
internal: {
type: `mongodb${caps(dbName)}${caps(collectionName)}`,
content: JSON.stringify(item),
contentDigest: crypto
.createHash(`md5`)
.update(JSON.stringify(item))
.digest(`hex`),
},
}
// We need to map certain fields to a contenttype.
Object.keys(mapObj).forEach(mediaItemFieldKey => {
if (
node[mediaItemFieldKey] &&
(typeof mapObj[mediaItemFieldKey] === `string` ||
mapObj[mediaItemFieldKey] instanceof String)
) {
const mappingChildNode = prepareMappingChildNode(
node,
mediaItemFieldKey,
node[mediaItemFieldKey],
mapObj[mediaItemFieldKey],
createNode
)
const childrenNodes = []
if (pluginOptions.map) {
let mapObj = pluginOptions.map
if (pluginOptions.map[collectionName]) {
mapObj = pluginOptions.map[collectionName]
}
// We need to map certain fields to a contenttype.
Object.keys(mapObj).forEach(mediaItemFieldKey => {
if (
node[mediaItemFieldKey] &&
(typeof mapObj[mediaItemFieldKey] === `string` ||
mapObj[mediaItemFieldKey] instanceof String)
) {
const mappingChildNode = prepareMappingChildNode(
node,
mediaItemFieldKey,
node[mediaItemFieldKey],
mapObj[mediaItemFieldKey],
createNode
)

node[`${mediaItemFieldKey}___NODE`] = mappingChildNode.id
childrenNodes.push(mappingChildNode)
node[`${mediaItemFieldKey}___NODE`] = mappingChildNode.id
childrenNodes.push(mappingChildNode)

delete node[mediaItemFieldKey]
}
delete node[mediaItemFieldKey]
}
})
}
createNode(node)
childrenNodes.forEach(node => {
createNode(node)
})
}
createNode(node)
childrenNodes.forEach(node => {
createNode(node)
})
}
})
})
}

Expand Down
13 changes: 13 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,13 @@ axios@^0.17.1:
follow-redirects "^1.2.5"
is-buffer "^1.1.5"

axios@^0.18.0:
version "0.18.0"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.18.0.tgz#32d53e4851efdc0a11993b6cd000789d70c05102"
dependencies:
follow-redirects "^1.3.0"
is-buffer "^1.1.5"

axobject-query@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/axobject-query/-/axobject-query-0.1.0.tgz#62f59dbc59c9f9242759ca349960e7a2fe3c36c0"
Expand Down Expand Up @@ -5081,6 +5088,12 @@ follow-redirects@^1.2.3, follow-redirects@^1.2.5:
dependencies:
debug "^3.1.0"

follow-redirects@^1.3.0:
version "1.5.0"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.5.0.tgz#234f49cf770b7f35b40e790f636ceba0c3a0ab77"
dependencies:
debug "^3.1.0"

for-each@^0.3.2:
version "0.3.2"
resolved "https://registry.yarnpkg.com/for-each/-/for-each-0.3.2.tgz#2c40450b9348e97f281322593ba96704b9abd4d4"
Expand Down