gatsby-source-mongodb
Advanced tools
Comparing version 1.6.0-alpha.22c8a6f1 to 1.6.0-alpha.3f307d61
@@ -9,17 +9,19 @@ "use strict"; | ||
const Db = require(`mongodb`).Db, | ||
MongoClient = require(`mongodb`).MongoClient, | ||
ObjectID = require(`mongodb`).ObjectID, | ||
crypto = require(`crypto`), | ||
createMappingChildNodes = require(`./mapping`), | ||
_ = require(`lodash`); | ||
var MongoClient = require(`mongodb`).MongoClient; | ||
var crypto = require(`crypto`); | ||
var createMappingChildNodes = require(`./mapping`); | ||
var _ = require(`lodash`); | ||
exports.sourceNodes = ({ boundActionCreators, getNode, hasNodeChanged }, pluginOptions, done) => { | ||
const { createNode, deleteNode } = boundActionCreators; | ||
exports.sourceNodes = function (_ref, pluginOptions, done) { | ||
var actions = _ref.actions, | ||
getNode = _ref.getNode, | ||
hasNodeChanged = _ref.hasNodeChanged; | ||
var createNode = actions.createNode; | ||
let serverOptions = pluginOptions.server || { | ||
var serverOptions = pluginOptions.server || { | ||
address: `localhost`, | ||
port: 27017 | ||
}; | ||
let dbName = pluginOptions.dbName || `local`, | ||
var dbName = pluginOptions.dbName || `local`, | ||
authUrlPart = ``; | ||
@@ -34,11 +36,29 @@ if (pluginOptions.auth) authUrlPart = `${pluginOptions.auth.user}:${pluginOptions.auth.password}@`; | ||
} | ||
var collection = pluginOptions.collection || `documents`; | ||
if (_.isArray(collection)) { | ||
for (var _iterator = collection, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { | ||
var _ref2; | ||
createNodes(db, pluginOptions, dbName, createNode, done); | ||
if (_isArray) { | ||
if (_i >= _iterator.length) break; | ||
_ref2 = _iterator[_i++]; | ||
} else { | ||
_i = _iterator.next(); | ||
if (_i.done) break; | ||
_ref2 = _i.value; | ||
} | ||
var col = _ref2; | ||
createNodes(db, pluginOptions, dbName, createNode, col, done); | ||
} | ||
} else { | ||
createNodes(db, pluginOptions, dbName, createNode, collection, done); | ||
} | ||
}); | ||
}; | ||
function createNodes(db, pluginOptions, dbName, createNode, done) { | ||
let collectionName = pluginOptions.collection || `documents`; | ||
let collection = db.collection(collectionName); | ||
let cursor = collection.find(); | ||
function createNodes(db, pluginOptions, dbName, createNode, collectionName, done) { | ||
var collection = db.collection(collectionName); | ||
var cursor = collection.find(); | ||
@@ -67,7 +87,13 @@ // Execute the each command, triggers for each document | ||
if (pluginOptions.map) { | ||
var mapObj = pluginOptions.map; | ||
if (pluginOptions.map[collectionName]) { | ||
mapObj = pluginOptions.map[collectionName]; | ||
} | ||
// We need to map certain fields to a contenttype. | ||
var keys = Object.keys(pluginOptions.map).forEach(mediaItemFieldKey => { | ||
node[`${mediaItemFieldKey}___NODE`] = createMappingChildNodes(node, mediaItemFieldKey, node[mediaItemFieldKey], pluginOptions.map[mediaItemFieldKey], createNode); | ||
Object.keys(mapObj).forEach(function (mediaItemFieldKey) { | ||
if (node[mediaItemFieldKey] && (typeof mapObj[mediaItemFieldKey] === `string` || mapObj[mediaItemFieldKey] instanceof String)) { | ||
node[`${mediaItemFieldKey}___NODE`] = createMappingChildNodes(node, mediaItemFieldKey, node[mediaItemFieldKey], mapObj[mediaItemFieldKey], createNode); | ||
delete node[mediaItemFieldKey]; | ||
delete node[mediaItemFieldKey]; | ||
} | ||
}); | ||
@@ -81,3 +107,5 @@ } | ||
function caps(s) { | ||
return s.replace(/\b\w/g, l => l.toUpperCase()); | ||
return s.replace(/\b\w/g, function (l) { | ||
return l.toUpperCase(); | ||
}); | ||
} |
"use strict"; | ||
const _ = require(`lodash`), | ||
crypto = require(`crypto`); | ||
var _ = require(`lodash`), | ||
crypto = require(`crypto`); | ||
module.exports = function (node, key, text, mediaType, createNode) { | ||
const str = _.isString(text) ? text : ` `; | ||
const id = `${node.id}${key}MappingNode`; | ||
const mappingNode = { | ||
var str = _.isString(text) ? text : ` `; | ||
var id = `${node.id}${key}MappingNode`; | ||
var mappingNode = { | ||
id: id, | ||
@@ -11,0 +11,0 @@ parent: node.id, |
{ | ||
"name": "gatsby-source-mongodb", | ||
"version": "1.6.0-alpha.22c8a6f1", | ||
"version": "1.6.0-alpha.3f307d61", | ||
"description": "Stub description for gatsby-source-mongodb", | ||
@@ -8,3 +8,4 @@ "main": "index.js", | ||
"build": "babel src --out-dir . --ignore __tests__", | ||
"watch": "babel -w src --out-dir . --ignore __tests__" | ||
"watch": "babel -w src --out-dir . --ignore __tests__", | ||
"prepublish": "cross-env NODE_ENV=production npm run build" | ||
}, | ||
@@ -17,6 +18,7 @@ "keywords": [ | ||
"devDependencies": { | ||
"babel-cli": "^6.24.1" | ||
"babel-cli": "^6.26.0", | ||
"cross-env": "^5.0.5" | ||
}, | ||
"dependencies": { | ||
"babel-runtime": "6.26.0", | ||
"babel-runtime": "^6.26.0", | ||
"lodash": "^4.17.4", | ||
@@ -23,0 +25,0 @@ "mongodb": "^2.2.30" |
@@ -6,2 +6,3 @@ # gatsby-source-mongodb | ||
## How to use | ||
```javascript | ||
@@ -19,24 +20,46 @@ // In your gatsby-config.js | ||
options: { dbName: `local`, collection: `documents` }, | ||
} | ||
}, | ||
], | ||
} | ||
}; | ||
``` | ||
### multiple collections | ||
```javascript | ||
// In your gatsby-config.js | ||
module.exports = { | ||
plugins: [ | ||
{ | ||
resolve: `gatsby-source-mongodb`, | ||
options: { dbName: `local`, collection: [`documents`, `vehicles`] }, | ||
}, | ||
], | ||
}; | ||
``` | ||
## Plugin options | ||
* **dbName**: indicates the database name that you want to use | ||
* **collection**: the collection name within Mongodb | ||
* **server**: contains the server info, with sub properties address and port | ||
ex. server: { address: `ds143532.mlab.com`, port: 43532 }. Defaults to a server running locally on the default port. | ||
* **auth**: the authentication data to login a Mongodb collection, with sub properties user and password. | ||
ex. auth: { user: `admin`, password: `12345` } | ||
* **collection**: the collection name within Mongodb, this can also be an array | ||
for multiple collections | ||
* **server**: contains the server info, with sub properties address and port ex. | ||
server: { address: `ds143532.mlab.com`, port: 43532 }. Defaults to a server | ||
running locally on the default port. | ||
* **auth**: the authentication data to login a Mongodb collection, with sub | ||
properties user and password. ex. auth: { user: `admin`, password: `12345` } | ||
### Mapping mediatype feature | ||
Gatsby supports transformer plugins that know how to transform one data type to another e.g. markdown to html. In the plugin options you can setup | ||
"mappings" for fields in your collections. You can tell Gatsby that a certain field is a given media type and with the correct transformer plugins installed, your data will be transformed automatically. | ||
Gatsby supports transformer plugins that know how to transform one data type to | ||
another e.g. markdown to html. In the plugin options you can setup "mappings" | ||
for fields in your collections. You can tell Gatsby that a certain field is a | ||
given media type and with the correct transformer plugins installed, your data | ||
will be transformed automatically. | ||
Let's say we have a markdown field named `body` in our mongoDB collection. We want to author our content in markdown but want to transform the markdown to HTML for including in our React components. | ||
Let's say we have a markdown field named `body` in our mongoDB collection | ||
`documents`. We want to author our content in markdown but want to transform the | ||
markdown to HTML for including in our React components. | ||
To do this, we modify the plugin configuration in `gatsby-config.js` like follows: | ||
To do this, we modify the plugin configuration in `gatsby-config.js` like | ||
follows: | ||
@@ -52,3 +75,3 @@ ```javascript{8-10} | ||
map: { | ||
body: `text/x-markdown`, | ||
{documents: {body: `text/markdown`} | ||
}, | ||
@@ -61,36 +84,38 @@ }, | ||
The GraphQL query to get the transformed markdown would look something like this. | ||
The GraphQL query to get the transformed markdown would look something like | ||
this. | ||
```graphql | ||
query ItemQuery($id: String!) { | ||
mongodbCloudDocuments(id: { eq: $id }) { | ||
id | ||
name | ||
url | ||
body { | ||
childMarkdownRemark { | ||
id | ||
html | ||
} | ||
mongodbCloudDocuments(id: { eq: $id }) { | ||
id | ||
name | ||
url | ||
body { | ||
childMarkdownRemark { | ||
id | ||
html | ||
} | ||
} | ||
} | ||
``` | ||
} | ||
``` | ||
## How to query your MongoDB data using GraphQL | ||
Below is a sample query for fetching all MongoDB document nodes from a db named **'Cloud'** and a collection named **'documents'**. | ||
Below is a sample query for fetching all MongoDB document nodes from a db named | ||
**'Cloud'** and a collection named **'documents'**. | ||
```graphql | ||
query PageQuery { | ||
allMongodbCloudDocuments { | ||
edges { | ||
node { | ||
id | ||
url | ||
name | ||
} | ||
allMongodbCloudDocuments { | ||
edges { | ||
node { | ||
id | ||
url | ||
name | ||
} | ||
} | ||
} | ||
} | ||
``` |
7639
112
118
7
2
5
Updatedbabel-runtime@^6.26.0