1616var MongoClient = require ( 'mongodb' ) . MongoClient ;
1717var ObjectID = require ( 'mongodb' ) . ObjectID ;
1818
19-
20- module . exports = function ( config ) {
19+ module . exports = function ( config ) {
2120
2221 var url = config . mongodb . url ;
2322 var collectionName = config . mongodb . collection ;
2423 var collection ;
2524
26-
2725 // [START translate]
2826 function fromMongo ( item ) {
29- if ( item . length ) { item = item . pop ( ) ; }
27+ if ( Array . isArray ( item ) && item . length ) {
28+ item = item [ 0 ] ;
29+ }
3030 item . id = item . _id ;
3131 delete item . _id ;
3232 return item ;
3333 }
3434
35-
3635 function toMongo ( item ) {
3736 delete item . id ;
3837 return item ;
3938 }
4039 // [END translate]
4140
42-
4341 function getCollection ( cb ) {
4442 if ( collection ) {
45- setImmediate ( function ( ) { cb ( null , collection ) ; } ) ;
43+ setImmediate ( function ( ) { cb ( null , collection ) ; } ) ;
4644 return ;
4745 }
48- MongoClient . connect ( url , function ( err , db ) {
46+ MongoClient . connect ( url , function ( err , db ) {
4947 if ( err ) {
50- console . log ( err ) ;
5148 return cb ( err ) ;
5249 }
5350 collection = db . collection ( collectionName ) ;
5451 cb ( null , collection ) ;
5552 } ) ;
5653 }
5754
58-
5955 // [START list]
6056 function list ( limit , token , cb ) {
6157 token = token ? parseInt ( token , 10 ) : 0 ;
62- getCollection ( function ( err , collection ) {
58+ if ( isNaN ( token ) ) {
59+ return cb ( new Error ( 'invalid token' ) ) ;
60+ }
61+ getCollection ( function ( err , collection ) {
6362 if ( err ) { return cb ( err ) ; }
6463 collection . find ( { } )
6564 . skip ( token )
6665 . limit ( limit )
67- . toArray ( function ( err , results ) {
66+ . toArray ( function ( err , results ) {
6867 if ( err ) { return cb ( err ) ; }
6968 var hasMore =
7069 results . length === limit ? token + results . length : false ;
@@ -74,12 +73,11 @@ module.exports = function(config) {
7473 }
7574 // [END list]
7675
77-
7876 // [START create]
7977 function create ( data , cb ) {
80- getCollection ( function ( err , collection ) {
78+ getCollection ( function ( err , collection ) {
8179 if ( err ) { return cb ( err ) ; }
82- collection . insert ( data , { w : 1 } , function ( err , result ) {
80+ collection . insert ( data , { w : 1 } , function ( err , result ) {
8381 if ( err ) { return cb ( err ) ; }
8482 var item = fromMongo ( result . ops ) ;
8583 cb ( null , item ) ;
@@ -88,13 +86,12 @@ module.exports = function(config) {
8886 }
8987 // [END create]
9088
91-
9289 function read ( id , cb ) {
93- getCollection ( function ( err , collection ) {
90+ getCollection ( function ( err , collection ) {
9491 if ( err ) { return cb ( err ) ; }
9592 collection . findOne ( {
9693 _id : new ObjectID ( id )
97- } , function ( err , result ) {
94+ } , function ( err , result ) {
9895 if ( err ) { return cb ( err ) ; }
9996 if ( ! result ) {
10097 return cb ( {
@@ -107,18 +104,15 @@ module.exports = function(config) {
107104 } ) ;
108105 }
109106
110-
111107 // [START update]
112108 function update ( id , data , cb ) {
113- getCollection ( function ( err , collection ) {
109+ getCollection ( function ( err , collection ) {
114110 if ( err ) { return cb ( err ) ; }
115- collection . update ( {
116- _id : new ObjectID ( id )
117- } , {
118- '$set' : toMongo ( data )
119- } ,
120- { w : 1 } ,
121- function ( err ) {
111+ collection . update (
112+ { _id : new ObjectID ( id ) } ,
113+ { '$set' : toMongo ( data ) } ,
114+ { w : 1 } ,
115+ function ( err ) {
122116 if ( err ) { return cb ( err ) ; }
123117 return read ( id , cb ) ;
124118 }
@@ -127,9 +121,8 @@ module.exports = function(config) {
127121 }
128122 // [END update]
129123
130-
131124 function _delete ( id , cb ) {
132- getCollection ( function ( err , collection ) {
125+ getCollection ( function ( err , collection ) {
133126 if ( err ) { return cb ( err ) ; }
134127 collection . remove ( {
135128 _id : new ObjectID ( id )
@@ -144,5 +137,4 @@ module.exports = function(config) {
144137 delete : _delete ,
145138 list : list
146139 } ;
147-
148140} ;
0 commit comments