Skip to content

Commit

Permalink
Reimplement GET /data for all item types (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefandesu committed Apr 17, 2023
1 parent fc5fdb6 commit beb5300
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1552,14 +1552,16 @@ Returns concept scheme suggestions.
Currently the same as `/voc/suggest` with parameter `format=jskos`.

### GET /data
Returns data for a certain URI or URIs. Can return concept schemes, concepts, concordances, mappings, and annotations.
Returns data for a certain URI or URIs. Can return concept schemes, concepts, concordances, mappings, and annotations. This endpoint does not offer pagination via `limit` and `offset`. It will always return all results. Furthermore, there is no certain order to the result set (but it should be consistent across requests). If a certain type of data requires authentication and the user is not authenticated, that type of data will not be returned.

**Note:** As of version 2.0, this endpoint was adjusted to return all types of items that are available in the database, instead of just concepts and concept schemes. The additional parameters, apart from `uri`, were also removed. For the previous behavior (only without returning concept schemes), see [GET /concepts](#get-concepts).

* **URL Params**

`uri=[uri]` URIs for concepts or concept schemes separated by `|`

`properties=[list]` with `[list]` being a comma-separated list of properties (currently supporting `ancestors`, `narrower`, and `annotations`)

* **Success Response**

JSON array of [JSKOS Items]
Expand Down
6 changes: 2 additions & 4 deletions routes/data.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import express from "express"
import { conceptService } from "../services/concepts.js"
import { dataService } from "../services/data.js"
import * as utils from "../utils/index.js"
import * as auth from "../utils/auth.js"

Expand All @@ -11,9 +11,7 @@ router.get(
auth.optional,
utils.supportDownloadFormats([]),
utils.wrappers.async(async (req) => {
return await conceptService.getDetails(req.query)
return await dataService.getData(req)
}),
utils.addPaginationHeaders,
utils.adjust,
utils.returnJSON,
)
23 changes: 23 additions & 0 deletions services/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { adjust } from "../utils/index.js"
import { byType as models } from "../models/index.js"

export class DataService {
async getData(req) {
const uris = req.query.uri?.split("|") ?? []
return [].concat(...await Promise.all(Object.keys(models).map(async type => {
// Don't return data the user is not authorized to read
if (!req.isAuthorizedFor({ type: `${type}s`, action: "read" })) {
return []
}
const model = models[type]
const results = await model.find({ $or: [
{ uri: { $in: uris } },
{ identifier: { $in: uris } },
] }).lean()
// Return adjusted data (needs to be done separately for each data type)
return adjust.data({ req, data: results, type: `${type}s` })
})))
}
}

export const dataService = new DataService()
2 changes: 2 additions & 0 deletions services/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { conceptService } from "./concepts.js"
import { concordanceService } from "./concordances.js"
import { mappingService } from "./mappings.js"
import { schemeService } from "./schemes.js"
import { dataService } from "./data.js"
import { statusService } from "./status.js"
import { validateService } from "./validate.js"

Expand All @@ -12,6 +13,7 @@ export {
concordanceService,
mappingService,
schemeService,
dataService,
statusService,
validateService,
}
Expand Down

0 comments on commit beb5300

Please sign in to comment.