@@ -18,16 +18,19 @@ import { _contextid, ObjectID, createExpressError, getAgentClaim, parseDocumentI
1818const bulkCreate = async function ( req , res , next ) {
1919 res . set ( "Content-Type" , "application/json; charset=utf-8" )
2020 const documents = req . body
21- let err = { }
2221 if ( ! Array . isArray ( documents ) ) {
23- err . message = "The request body must be an array of objects."
24- err . status = 400
22+ const err = {
23+ message : "The request body must be an array of objects." ,
24+ status : 400
25+ }
2526 next ( createExpressError ( err ) )
2627 return
2728 }
2829 if ( documents . length === 0 ) {
29- err . message = "No action on an empty array."
30- err . status = 400
30+ const err = {
31+ message : "No action on an empty array." ,
32+ status : 400
33+ }
3134 next ( createExpressError ( err ) )
3235 return
3336 }
@@ -44,8 +47,10 @@ const bulkCreate = async function (req, res, next) {
4447 if ( idcheck ) return d
4548 } )
4649 if ( gatekeep . length > 0 ) {
47- err . message = "All objects in the body of a `/bulkCreate` must be JSON and must not contain a declared identifier property."
48- err . status = 400
50+ const err = {
51+ message : "All objects in the body of a `/bulkCreate` must be JSON and must not contain a declared identifier property." ,
52+ status : 400
53+ }
4954 next ( createExpressError ( err ) )
5055 return
5156 }
@@ -64,28 +69,27 @@ const bulkCreate = async function (req, res, next) {
6469 // }
6570
6671 // unordered bulkWrite() operations have better performance metrics.
67- let bulkOps = [ ]
72+ const bulkOps = [ ]
6873 const generatorAgent = getAgentClaim ( req , next )
69- for ( let d of documents ) {
74+ for ( const d of documents ) {
7075 // Do not create empty {}s
7176 if ( Object . keys ( d ) . length === 0 ) continue
7277 const providedID = d ?. _id
7378 const id = isValidID ( providedID ) ? providedID : ObjectID ( )
74- d = utils . configureRerumOptions ( generatorAgent , d )
79+ const configuredDoc = utils . configureRerumOptions ( generatorAgent , d )
7580 // id is also protected in this case, so it can't be set.
76- if ( _contextid ( d [ "@context" ] ) ) delete d . id
77- d . _id = id
78- d [ '@id' ] = `${ process . env . RERUM_ID_PREFIX } ${ id } `
79- bulkOps . push ( { insertOne : { "document" : d } } )
81+ if ( _contextid ( configuredDoc [ "@context" ] ) ) delete configuredDoc . id
82+ configuredDoc . _id = id
83+ configuredDoc [ '@id' ] = `${ process . env . RERUM_ID_PREFIX } ${ id } `
84+ bulkOps . push ( { insertOne : { "document" : configuredDoc } } )
8085 }
8186 try {
82- let dbResponse = await db . bulkWrite ( bulkOps , { 'ordered' :false } )
87+ const dbResponse = await db . bulkWrite ( bulkOps , { 'ordered' :false } )
8388 res . set ( "Content-Type" , "application/json; charset=utf-8" )
8489 res . set ( "Link" , dbResponse . result . insertedIds . map ( r => `${ process . env . RERUM_ID_PREFIX } ${ r . _id } ` ) ) // https://www.rfc-editor.org/rfc/rfc5988
8590 res . status ( 201 )
8691 const estimatedResults = bulkOps . map ( f => {
87- let doc = f . insertOne . document
88- doc = idNegotiation ( doc )
92+ const doc = idNegotiation ( f . insertOne . document )
8993 return doc
9094 } )
9195 res . json ( estimatedResults ) // https://www.rfc-editor.org/rfc/rfc7231#section-6.3.2
@@ -106,17 +110,20 @@ const bulkCreate = async function (req, res, next) {
106110const bulkUpdate = async function ( req , res , next ) {
107111 res . set ( "Content-Type" , "application/json; charset=utf-8" )
108112 const documents = req . body
109- let err = { }
110- let encountered = [ ]
113+ const encountered = [ ]
111114 if ( ! Array . isArray ( documents ) ) {
112- err . message = "The request body must be an array of objects."
113- err . status = 400
115+ const err = {
116+ message : "The request body must be an array of objects." ,
117+ status : 400
118+ }
114119 next ( createExpressError ( err ) )
115120 return
116121 }
117122 if ( documents . length === 0 ) {
118- err . message = "No action on an empty array."
119- err . status = 400
123+ const err = {
124+ message : "No action on an empty array." ,
125+ status : 400
126+ }
120127 next ( createExpressError ( err ) )
121128 return
122129 }
@@ -134,13 +141,15 @@ const bulkUpdate = async function (req, res, next) {
134141 } )
135142 // The empty {}s will cause this error
136143 if ( gatekeep . length > 0 ) {
137- err . message = "All objects in the body of a `/bulkUpdate` must be JSON and must contain a declared identifier property."
138- err . status = 400
144+ const err = {
145+ message : "All objects in the body of a `/bulkUpdate` must be JSON and must contain a declared identifier property." ,
146+ status : 400
147+ }
139148 next ( createExpressError ( err ) )
140149 return
141150 }
142151 // unordered bulkWrite() operations have better performance metrics.
143- let bulkOps = [ ]
152+ const bulkOps = [ ]
144153 const generatorAgent = getAgentClaim ( req , next )
145154 for ( const objectReceived of documents ) {
146155 // We know it has an id
@@ -149,7 +158,7 @@ const bulkUpdate = async function (req, res, next) {
149158 // if(encountered.includes(idReceived)) continue
150159 encountered . push ( idReceived )
151160 if ( ! idReceived . includes ( process . env . RERUM_ID_PREFIX ) ) continue
152- let id = parseDocumentID ( idReceived )
161+ const id = parseDocumentID ( idReceived )
153162 let originalObject
154163 try {
155164 originalObject = await db . findOne ( { "$or" :[ { "_id" : id } , { "__rerum.slug" : id } ] } )
@@ -159,16 +168,16 @@ const bulkUpdate = async function (req, res, next) {
159168 }
160169 if ( null === originalObject ) continue
161170 if ( utils . isDeleted ( originalObject ) ) continue
162- id = ObjectID ( )
163- let context = objectReceived [ "@context" ] ? { "@context" : objectReceived [ "@context" ] } : { }
164- let rerumProp = { "__rerum" : utils . configureRerumOptions ( generatorAgent , originalObject , true , false ) [ "__rerum" ] }
171+ const newId = ObjectID ( )
172+ const context = objectReceived [ "@context" ] ? { "@context" : objectReceived [ "@context" ] } : { }
173+ const rerumProp = { "__rerum" : utils . configureRerumOptions ( generatorAgent , originalObject , true , false ) [ "__rerum" ] }
165174 delete objectReceived [ "__rerum" ]
166175 delete objectReceived [ "_id" ]
167176 delete objectReceived [ "@id" ]
168177 // id is also protected in this case, so it can't be set.
169178 if ( _contextid ( objectReceived [ "@context" ] ) ) delete objectReceived . id
170179 delete objectReceived [ "@context" ]
171- let newObject = Object . assign ( context , { "@id" : process . env . RERUM_ID_PREFIX + id } , objectReceived , rerumProp , { "_id" : id } )
180+ const newObject = Object . assign ( context , { "@id" : process . env . RERUM_ID_PREFIX + newId } , objectReceived , rerumProp , { "_id" : newId } )
172181 bulkOps . push ( { insertOne : { "document" : newObject } } )
173182 if ( originalObject . __rerum . history . next . indexOf ( newObject [ "@id" ] ) === - 1 ) {
174183 originalObject . __rerum . history . next . push ( newObject [ "@id" ] )
@@ -183,13 +192,12 @@ const bulkUpdate = async function (req, res, next) {
183192 }
184193 }
185194 try {
186- let dbResponse = await db . bulkWrite ( bulkOps , { 'ordered' :false } )
195+ const dbResponse = await db . bulkWrite ( bulkOps , { 'ordered' :false } )
187196 res . set ( "Content-Type" , "application/json; charset=utf-8" )
188197 res . set ( "Link" , dbResponse . result . insertedIds . map ( r => `${ process . env . RERUM_ID_PREFIX } ${ r . _id } ` ) ) // https://www.rfc-editor.org/rfc/rfc5988
189198 res . status ( 200 )
190199 const estimatedResults = bulkOps . filter ( f => f . insertOne ) . map ( f => {
191- let doc = f . insertOne . document
192- doc = idNegotiation ( doc )
200+ const doc = idNegotiation ( f . insertOne . document )
193201 return doc
194202 } )
195203 res . json ( estimatedResults ) // https://www.rfc-editor.org/rfc/rfc7231#section-6.3.2
0 commit comments