Skip to content

Commit 35b99ad

Browse files
Copilotcubap
andcommitted
Optimize bulk operations and gog controllers, standardize string quotes
Co-authored-by: cubap <1119165+cubap@users.noreply.github.com>
1 parent e225989 commit 35b99ad

3 files changed

Lines changed: 53 additions & 46 deletions

File tree

controllers/bulk.js

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,19 @@ import { _contextid, ObjectID, createExpressError, getAgentClaim, parseDocumentI
1818
const 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) {
106110
const 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

controllers/gog.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,21 @@ const _gog_fragments_from_manuscript = async function (req, res, next) {
2828
const manID = req.body["ManuscriptWitness"]
2929
const limit = parseInt(req.query.limit ?? 50)
3030
const skip = parseInt(req.query.skip ?? 0)
31-
let err = { message: `` }
3231
// This request can only be made my Gallery of Glosses production apps.
3332
if (agentID !== "61043ad4ffce846a83e700dd") {
34-
err = Object.assign(err, {
33+
const err = {
3534
message: `Only the Gallery of Glosses can make this request.`,
3635
status: 403
37-
})
36+
}
37+
next(createExpressError(err))
38+
return
3839
}
3940
// Must have a properly formed body with a usable value
40-
else if(!manID || !manID.startsWith("http")){
41-
err = Object.assign(err, {
41+
if(!manID || !manID.startsWith("http")){
42+
const err = {
4243
message: `The body must be JSON like {"ManuscriptWitness":"witness_uri_here"}.`,
4344
status: 400
44-
})
45-
}
46-
if (err.status) {
45+
}
4746
next(createExpressError(err))
4847
return
4948
}

utils.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,17 +153,17 @@ const configureLDHeadersFor = function(obj){
153153
const headers = {}
154154
/**
155155
if(isLD(obj)){
156-
headers["Content-Type"] = 'application/ld+json;charset=utf-8;profile="http://www.w3.org/ns/anno.jsonld"'
156+
headers["Content-Type"] = "application/ld+json;charset=utf-8;profile=\"http://www.w3.org/ns/anno.jsonld\""
157157
}
158158
else {
159159
// This breaks Web Annotation compliance, but allows us to return requested
160160
// objects without misrepresenting the content.
161-
headers["Content-Type"] = "application/json;charset=utf-8;"
161+
headers["Content-Type"] = "application/json;charset=utf-8"
162162
}
163163
*/
164164
headers.Allow = "GET,OPTIONS,HEAD,PUT,PATCH,DELETE,POST"
165-
headers["Content-Type"] = 'application/ld+json;charset=utf-8;profile="http://www.w3.org/ns/anno.jsonld"'
166-
headers.Link = '<http://store.rerum.io/v1/context.json>; rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json"'
165+
headers["Content-Type"] = "application/ld+json;charset=utf-8;profile=\"http://www.w3.org/ns/anno.jsonld\""
166+
headers.Link = "<http://store.rerum.io/v1/context.json>; rel=\"http://www.w3.org/ns/json-ld#context\"; type=\"application/ld+json\""
167167
return headers
168168
}
169169

0 commit comments

Comments
 (0)