Repository navigation
Expand file tree
/
Copy pathgatsby-node.js
More file actions
142 lines (129 loc) · 3.8 KB
/
Copy pathgatsby-node.js
File metadata and controls
142 lines (129 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const MongoClient = require(`mongodb`).MongoClient
const crypto = require(`crypto`)
const prepareMappingChildNode = require(`./mapping`)
const sanitizeName = require(`./sanitize-name`)
const _ = require(`lodash`)
const queryString = require(`query-string`)
exports.sourceNodes = (
{ actions, getNode, createNodeId, hasNodeChanged },
pluginOptions
) => {
const { createNode } = actions
let serverOptions = pluginOptions.server || {
address: `localhost`,
port: 27017,
}
let dbName = pluginOptions.dbName || `local`,
authUrlPart = ``
if (pluginOptions.auth)
authUrlPart = `${pluginOptions.auth.user}:${pluginOptions.auth.password}@`
let connectionExtraParams = getConnectionExtraParams(
pluginOptions.extraParams
)
const connectionURL = `mongodb://${authUrlPart}${serverOptions.address}:${
serverOptions.port
}/${dbName}${connectionExtraParams}`
return MongoClient.connect(connectionURL)
.then(db => {
let collection = pluginOptions.collection || [`documents`]
if (!Array.isArray(collection)) {
collection = [collection]
}
return Promise.all(
collection.map(col =>
createNodes(db, pluginOptions, dbName, createNode, createNodeId, col)
)
)
.then(() => {
db.close()
})
.catch(err => {
console.warn(err)
db.close()
return err
})
})
.catch(err => {
console.warn(err)
return err
})
}
function createNodes(
db,
pluginOptions,
dbName,
createNode,
createNodeId,
collectionName
) {
return new Promise((resolve, reject) => {
let collection = db.collection(collectionName)
let cursor = collection.find()
// Execute the each command, triggers for each document
cursor.toArray((err, documents) => {
if (err) {
reject(err)
}
documents.forEach(item => {
var id = item._id.toString()
delete item._id
var node = {
// Data for the node.
...item,
id: createNodeId(`${id}`),
mongodb_id: id,
parent: `__${collectionName}__`,
children: [],
internal: {
type: `mongodb${sanitizeName(dbName)}${sanitizeName(
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]
}
// 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)
delete node[mediaItemFieldKey]
}
})
}
createNode(node)
childrenNodes.forEach(node => {
createNode(node)
})
})
resolve()
})
})
}
function getConnectionExtraParams(extraParams) {
let connectionSuffix
if (extraParams) {
connectionSuffix = queryString.stringify(extraParams, { sort: false })
}
return connectionSuffix ? `?` + connectionSuffix : ``
}