Skip to content

Commit ba70798

Browse files
Copilotcubapthehabes
authored
Use indexed _id field instead of @id for database queries (#234)
* Initial plan * Optimize queries to use _id instead of @id for root object lookups Co-authored-by: cubap <1119165+cubap@users.noreply.github.com> * Add null check validation for primeID before parsing Co-authored-by: cubap <1119165+cubap@users.noreply.github.com> * Add null check for rootObj after database query Co-authored-by: cubap <1119165+cubap@users.noreply.github.com> * Remove duplicate getAllVersions function and improve error handling Co-authored-by: cubap <1119165+cubap@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: cubap <1119165+cubap@users.noreply.github.com> Co-authored-by: Bryan Haberberger <bryan.j.haberberger@slu.edu>
1 parent 9db5012 commit ba70798

File tree

2 files changed

+22
-42
lines changed

2 files changed

+22
-42
lines changed

controllers/delete.js

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
import { newID, isValidID, db } from '../database/index.js'
88
import utils from '../utils.js'
9-
import { createExpressError, getAgentClaim, parseDocumentID } from './utils.js'
9+
import { createExpressError, getAgentClaim, parseDocumentID, getAllVersions, getAllDescendants } from './utils.js'
1010

1111
/**
1212
* Mark an object as deleted in the database.
@@ -218,37 +218,6 @@ async function newTreePrime(obj) {
218218
return true
219219
}
220220

221-
async function getAllVersions(obj) {
222-
let ls_versions
223-
let primeID = obj?.__rerum.history.prime
224-
let rootObj = ( primeID === "root")
225-
? JSON.parse(JSON.stringify(obj))
226-
: await db.findOne({ "@id": primeID })
227-
ls_versions = await db.find({ "__rerum.history.prime": rootObj['@id'] }).toArray()
228-
ls_versions.unshift(rootObj)
229-
return ls_versions
230-
}
231-
232-
function getAllDescendants(ls_versions, keyObj, discoveredDescendants) {
233-
let nextIDarr = []
234-
if (keyObj.__rerum.history.next.length === 0) {
235-
//essentially, do nothing. This branch is done.
236-
}
237-
else {
238-
nextIDarr = keyObj.__rerum.history.next
239-
}
240-
for (let nextID of nextIDarr) {
241-
for (let v of ls_versions) {
242-
if (v["@id"] === nextID) {
243-
discoveredDescendants.push(v)
244-
getAllDescendants(ls_versions, v, discoveredDescendants)
245-
break
246-
}
247-
}
248-
}
249-
return discoveredDescendants
250-
}
251-
252221
export {
253222
deleteObj
254223
}

controllers/utils.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -198,16 +198,27 @@ async function alterHistoryNext(objToUpdate, newNextID) {
198198
async function getAllVersions(obj) {
199199
let ls_versions
200200
let primeID = obj?.__rerum.history.prime
201-
let rootObj = ( primeID === "root")
202-
? //The obj passed in is root. So it is the rootObj we need.
203-
JSON.parse(JSON.stringify(obj))
204-
: //The obj passed in knows the ID of root, grab it from Mongo
205-
await db.findOne({ "@id": primeID })
206-
/**
207-
* Note that if you attempt the following code, it will cause Cannot convert undefined or null to object in getAllVersions.
208-
* rootObj = await db.findOne({"$or":[{"_id": primeID}, {"__rerum.slug": primeID}]})
209-
* This is the because some of the @ids have different RERUM URL patterns on them.
210-
**/
201+
let rootObj
202+
if (primeID === "root") {
203+
//The obj passed in is root. So it is the rootObj we need.
204+
rootObj = JSON.parse(JSON.stringify(obj))
205+
} else if (primeID) {
206+
//The obj passed in knows the ID of root, grab it from Mongo
207+
//Use _id for indexed query performance instead of @id
208+
let primeHexId
209+
try {
210+
primeHexId = parseDocumentID(primeID)
211+
} catch (error) {
212+
throw new Error(`Invalid history.prime value '${primeID}': ${error.message}`)
213+
}
214+
rootObj = await db.findOne({"$or":[{"_id": primeHexId}, {"__rerum.slug": primeHexId}]})
215+
if (!rootObj) {
216+
throw new Error(`Root object with id '${primeID}' not found in database`)
217+
}
218+
} else {
219+
//primeID is undefined or null, cannot proceed
220+
throw new Error("Object has no valid history.prime value")
221+
}
211222
//All the children of this object will have its @id in __rerum.history.prime
212223
ls_versions = await db.find({ "__rerum.history.prime": rootObj['@id'] }).toArray()
213224
//The root object is a version, prepend it in

0 commit comments

Comments
 (0)