@@ -14,11 +14,8 @@ function storage(env, ctx) {
1414
1515 // TODO: Code is a little redundant.
1616
17- var with_collection = ctx . store . with_collection ( env . mongo_collection ) ;
18-
1917 // query for entries from storage
2018 function list ( opts , fn ) {
21- with_collection ( function ( err , collection ) {
2219 // these functions, find, sort, and limit, are used to
2320 // dynamically configure the request, based on the options we've
2421 // been given
@@ -42,17 +39,14 @@ function storage(env, ctx) {
4239 }
4340
4441 // now just stitch them all together
45- limit . call ( collection
42+ limit . call ( api ( )
4643 . find ( query_for ( opts ) )
4744 . sort ( sort ( ) )
4845 ) . toArray ( toArray ) ;
49- } ) ;
5046 }
5147
5248 function remove ( opts , fn ) {
53- with_collection ( function ( err , collection ) {
54- collection . remove ( query_for ( opts ) , fn ) ;
55- } ) ;
49+ api ( ) . remove ( query_for ( opts ) , fn ) ;
5650 }
5751
5852 // return writable stream to lint each sgv record passing through it
@@ -86,39 +80,30 @@ function storage(env, ctx) {
8680
8781 // store new documents using the storage mechanism
8882 function create ( docs , fn ) {
89- with_collection ( function ( err , collection ) {
90- if ( err ) { fn ( err ) ; return ; }
91- // potentially a batch insert
92- var firstErr = null ,
93- numDocs = docs . length ,
94- totalCreated = 0 ;
95-
96- docs . forEach ( function ( doc ) {
97- var query = ( doc . sysTime && doc . type ) ? { sysTime : doc . sysTime , type : doc . type } : doc ;
98- collection . update ( query , doc , { upsert : true } , function ( err ) {
99- firstErr = firstErr || err ;
100- if ( ++ totalCreated === numDocs ) {
101- //TODO: this is triggering a read from Mongo, we can do better
102- ctx . bus . emit ( 'data-received' ) ;
103- fn ( firstErr , docs ) ;
104- }
105- } ) ;
83+ // potentially a batch insert
84+ var firstErr = null ,
85+ numDocs = docs . length ,
86+ totalCreated = 0 ;
87+
88+ docs . forEach ( function ( doc ) {
89+ var query = ( doc . sysTime && doc . type ) ? { sysTime : doc . sysTime , type : doc . type } : doc ;
90+ api ( ) . update ( query , doc , { upsert : true } , function ( err ) {
91+ firstErr = firstErr || err ;
92+ if ( ++ totalCreated === numDocs ) {
93+ //TODO: this is triggering a read from Mongo, we can do better
94+ ctx . bus . emit ( 'data-received' ) ;
95+ fn ( firstErr , docs ) ;
96+ }
10697 } ) ;
10798 } ) ;
10899 }
109100
110101 function getEntry ( id , fn ) {
111- with_collection ( function ( err , collection ) {
102+ api ( ) . findOne ( { _id : ObjectID ( id ) } , function ( err , entry ) {
112103 if ( err ) {
113104 fn ( err ) ;
114105 } else {
115- collection . findOne ( { _id : ObjectID ( id ) } , function ( err , entry ) {
116- if ( err ) {
117- fn ( err ) ;
118- } else {
119- fn ( null , entry ) ;
120- }
121- } ) ;
106+ fn ( null , entry ) ;
122107 }
123108 } ) ;
124109 }
@@ -131,7 +116,7 @@ function storage(env, ctx) {
131116 function api ( ) {
132117 // obtain handle usable for querying the collection associated
133118 // with these records
134- return ctx . store . db . collection ( env . mongo_collection ) ;
119+ return ctx . store . collection ( env . entries_collection ) ;
135120 }
136121
137122 // Expose all the useful functions
0 commit comments