@@ -5,8 +5,7 @@ const _ = require(`lodash`)
55
66exports . 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
0 commit comments