Skip to content

Commit 6181f85

Browse files
committed
Register the bulkUpdate route. Make the errors behave.
1 parent 46cc3e2 commit 6181f85

5 files changed

Lines changed: 111 additions & 5 deletions

File tree

db-controller.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,7 @@ const id = async function (req, res, next) {
10001000
const bulkCreate = async function (req, res, next) {
10011001
res.set("Content-Type", "application/json; charset=utf-8")
10021002
const documents = req.body
1003-
let err
1003+
let err = {}
10041004
// TODO: validate documents gatekeeper function?
10051005
if (!Array.isArray(documents)) {
10061006
err.message = "The request body must be an array of objects."
@@ -1036,10 +1036,10 @@ const bulkCreate = async function (req, res, next) {
10361036
// }
10371037
// }
10381038
let bulkOps = []
1039+
let generatorAgent = getAgentClaim(req, next)
10391040
documents.forEach(d => {
10401041
const providedID = d._id
10411042
const id = isValidID(providedID) ? providedID : ObjectID()
1042-
let generatorAgent = getAgentClaim(req, next)
10431043
d = utils.configureRerumOptions(generatorAgent, d)
10441044
if(_contextid(d["@context"])) {
10451045
// id is also protected in this case, so it can't be set.
@@ -1070,6 +1070,8 @@ const bulkCreate = async function (req, res, next) {
10701070
const bulkUpdate = async function (req, res, next) {
10711071
res.set("Content-Type", "application/json; charset=utf-8")
10721072
const documents = req.body
1073+
console.log("DOCUMENTS")
1074+
console.log(documents)
10731075
let err = {}
10741076
// TODO: validate documents gatekeeper function?
10751077
if (!Array.isArray(documents)) {
@@ -1087,13 +1089,15 @@ const bulkUpdate = async function (req, res, next) {
10871089
return
10881090
}
10891091
if (documents.filter(d=>!(d["@id"] ?? d.id)).length > 0) {
1090-
err.message = "`/bulkUpdate` will only accept objects with @id or id properties."
1092+
err.message = "All objects in the body of a `/bulkUpdate` must contain an @id or id property."
10911093
//err.status = 422
10921094
err.status = 400
10931095
next(createExpressError(err))
10941096
return
10951097
}
10961098
let bulkOps = []
1099+
let generatorAgent = getAgentClaim(req, next)
1100+
console.log("Process docs")
10971101
for(const d of documents){
10981102
const idReceived = d["@id"] ?? d.id
10991103
if (idReceived) {
@@ -1701,8 +1705,10 @@ function createExpressError(err) {
17011705
error.statusCode = 500
17021706
}
17031707
}
1704-
if(!err.statusCode) error.statusCode === "500"
1705-
if(!err.statusMessage) error.statusMessage === "Detected Error"
1708+
error.statusCode = err.statusCode ?? err.status ?? 500
1709+
error.statusMessage = err.statusMessage ?? err.message ?? "Detected Error"
1710+
console.log("Error helper created")
1711+
console.log(error)
17061712
return error
17071713
}
17081714

@@ -2336,6 +2342,7 @@ export default {
23362342
query,
23372343
id,
23382344
bulkCreate,
2345+
bulkUpdate,
23392346
idHeadRequest,
23402347
queryHeadRequest,
23412348
since,

public/API.html

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,80 @@ <h3 id="update">Update</h3>
730730
</p>
731731
</p>
732732

733+
<h3 id="bulk-create">Bulk Update</h3>
734+
735+
<table>
736+
<thead>
737+
<tr>
738+
<th>Patterns</th>
739+
<th>Payloads</th>
740+
<th>Responses</th>
741+
</tr>
742+
</thead>
743+
<tbody>
744+
<tr>
745+
<td><code class="language-plaintext highlighter-rouge">/bulkUpdate</code></td>
746+
<td><code class="language-plaintext highlighter-rouge">[{JSON}]</code></td>
747+
<td>201 <code>
748+
<code class="language-plaintext highlighter-rouge">[{JSON}]</code></td>
749+
</tr>
750+
</tbody>
751+
</table>
752+
753+
<ul>
754+
<li><strong><code class="language-plaintext highlighter-rouge">[{JSON}]</code></strong>—an array RERUM objects
755+
to be updated.</li>
756+
<li><strong>Response: <code class="language-plaintext highlighter-rouge">[{JSON}]</code></strong>—an array
757+
of the resolved records from the update process</li>
758+
</ul>
759+
760+
<p>
761+
Update multiple existing RERUM objects at once and recieve an array of the complete records as the response body.
762+
Accepts only a single array of JSON objects in the request body. The '@id' property must be present for each object.
763+
The array of JSON objects passed in will be updated in the order submitted and the response will have the URI
764+
of the new resource or an error message as an array in the same order. When errors are encountered,
765+
the batch process will attempt to continue for all submitted items.
766+
</p>
767+
768+
<p>
769+
<div class="exHeading">Javascript Example</div>
770+
<pre><code class="jsExample">
771+
<span>const saved_obj = await fetch("https://devstore.rerum.io/v1/api/bulkUpdate", {</span>
772+
<span class="ind1">method: "POST",</span>
773+
<span class="ind1">headers:{</span>
774+
<span class="ind2">"Authorization": "Bearer eyJz93a...k4laUWw" </span>
775+
<span class="ind2">"Content-Type": "application/json; charset=utf-8"</span>
776+
<span class="ind1">},</span>
777+
<span class="ind1">body: JSON.stringify([
778+
<span class="ind2">"@id": "https://devstore.rerum.io/v1/id/abcdef1234567890",</span>
779+
<span class="ind2">"hello": "new world",</span>
780+
<span class="ind2">"@id": "https://devstore.rerum.io/v1/id/1234567890abcdef",</span>
781+
<span class="ind2">"goodbye": "old planet"</span>
782+
<span class="ind1">])</span>
783+
<span>})</span>
784+
<span>.then(resp => resp.json())</span>
785+
<span>.catch(err => {throw err})
786+
</code></pre>
787+
</p>
788+
789+
<p>
790+
<div class="exHeading">Here is what the response <code>resp</code> looks like:</div>
791+
<pre><code class="respExample">
792+
<span>[</span>
793+
<span class="ind1">{</span>
794+
<span class="ind2">"@id": "https://devstore.rerum.io/v1/id/abcabc1231231230",</span>
795+
<span class="ind2">"hello": "new world",</span>
796+
<span class="ind2">"__rerum":{...}</span>
797+
<span class="ind1">},</span>
798+
<span class="ind1">{</span>
799+
<span class="ind2">"@id": "https://devstore.rerum.io/v1/id/defdef4564564567",</span>
800+
<span class="ind2">"goodbye": "old planet",</span>
801+
<span class="ind2"> "__rerum":{...}</span>
802+
<span class="ind1">}</span>
803+
<span>]</span>
804+
</code></pre>
805+
</p>
806+
733807
<h3 id="overwrite">Overwrite</h3>
734808

735809
<p>

rest.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ const checkPatchOverrideSupport = function (req, res) {
3232
* You have likely reached this with a next(createExpressError(err)) call. End here and send the error.
3333
*/
3434
const messenger = function (err, req, res, next) {
35+
console.log("REST MESSENGER")
3536
if (res.headersSent) {
3637
return
3738
}
39+
console.log("GO MESSENGER")
3840
let error = {}
3941
error.message = err.statusMessage ?? err.message ?? ``
4042
error.status = err.statusCode ?? err.status ?? 500
@@ -94,6 +96,9 @@ The requested web page or resource could not be found.`
9496
case 409:
9597
// These are all handled in db-controller.js already.
9698
break
99+
case 501:
100+
// Not implemented. Handled upstream.
101+
break
97102
case 503:
98103
//RERUM is down or readonly. Handled upstream.
99104
break

routes/api-routes.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import queryRouter from './query.js';
2222
import createRouter from './create.js';
2323
// Support POST requests with JSON Array bodies used for establishing new objects.
2424
import bulkCreateRouter from './bulkCreate.js';
25+
//Support PUT requests with JSON Array bodies used for updating a number of existing objects.
26+
import bulkUpdateRouter from './bulkUpdate.js';
2527
// Support DELETE requests like v1/delete/{object id} to mark an object as __deleted.
2628
import deleteRouter from './delete.js';
2729
// Support POST requests with JSON bodies used for replacing some existing object.
@@ -47,6 +49,7 @@ router.use('/api', compatabilityRouter)
4749
router.use('/api/query', queryRouter)
4850
router.use('/api/create', createRouter)
4951
router.use('/api/bulkCreate', bulkCreateRouter)
52+
router.use('/api/bulkUpdate', bulkUpdateRouter)
5053
router.use('/api/delete', deleteRouter)
5154
router.use('/api/overwrite', overwriteRouter)
5255
router.use('/api/update', updateRouter)

routes/bulkUpdate.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env node
2+
import express from 'express'
3+
const router = express.Router()
4+
5+
//This controller will handle all MongoDB interactions.
6+
import controller from '../db-controller.js'
7+
import auth from '../auth/index.js'
8+
9+
router.route('/')
10+
.put(auth.checkJwt, controller.bulkUpdate)
11+
.all((req, res, next) => {
12+
res.statusMessage = 'Improper request method for creating, please use PUT.'
13+
res.status(405)
14+
next(res)
15+
})
16+
17+
export default router

0 commit comments

Comments
 (0)