Skip to content

Commit b3c4645

Browse files
committed
single ref data moved to database
1 parent 66abac6 commit b3c4645

2 files changed

Lines changed: 26 additions & 20 deletions

File tree

‎src/database/index.js‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,30 @@ class Database {
236236
return referenceIds;
237237
}
238238

239+
// Get all ids available in a given reference
240+
getReferenceData = async (referenceName, referenceId) => {
241+
// Get the requested reference configuration
242+
const reference = REFERENCES[referenceName];
243+
if (!reference) return {
244+
headerError: NOT_FOUND,
245+
error: `Unknown reference "${referenceName}". Available references: ${AVAILABLE_REFERENCES}`
246+
};
247+
// Set the target mongo collection
248+
const collection = this[referenceName];
249+
// Set the target query
250+
const query = { [reference.idField]: referenceId };
251+
// Set a projection to get rid of the internal id
252+
const projection = { projection: { _id: false } };
253+
// Get the target reference
254+
const referenceData = await collection.findOne(query, projection);
255+
// If we did not found the reference then stop here
256+
if (!referenceData) return {
257+
headerError: NOT_FOUND,
258+
error: `Not found "${referenceName}" reference with id "${referenceId}"`
259+
};
260+
return referenceData;
261+
}
262+
239263
// Process a projects query which may include reference and topology fields
240264
// Returns an error response if something is wrong
241265
processProjectsQuery = async query => {

‎src/routes/references/index.js‎

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -108,28 +108,10 @@ const specificReferenceResponse = handler({
108108
const database = await getDatabase(request);
109109
// Get the requested reference configuration
110110
const referenceName = request.params.reference;
111-
const reference = REFERENCES[referenceName];
112-
if (!reference) return {
113-
headerError: NOT_FOUND,
114-
error: `Unknown reference "${referenceName}". Available references: ${availableReferences}`
115-
};
116-
// Set the target mongo collection
117-
const referenceCollection = database[referenceName];
118111
// Get the requested id
119112
const referenceId = request.params.id;
120-
// Return the reference which matches the request id
121-
const result = await referenceCollection.findOne(
122-
// Set the query
123-
{ [reference.idField]: referenceId },
124-
// Set the projection
125-
{ projection: { _id: false } }
126-
);
127-
// If there was no result then raise a not found error
128-
if (!result) return {
129-
headerError: NOT_FOUND,
130-
error: `Not found "${referenceName}" reference with id "${referenceId}"`
131-
};
132-
return result;
113+
// Use the handler to get the specified reference data
114+
return await database.getReferenceData(referenceName, referenceId);
133115
}
134116
});
135117

0 commit comments

Comments
 (0)