Skip to content

Commit 3172afd

Browse files
authored
Merge pull request #267 from gbv/feature/261-Add-mapping-sameness-identifier
Feature/261 add mapping sameness identifier
2 parents 19f7cba + 17694eb commit 3172afd

File tree

8 files changed

+40
-24
lines changed

8 files changed

+40
-24
lines changed

bin/import.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ async function doImport({ input, format, type, concordance }) {
350350
}
351351
// Add mapping identifier
352352
try {
353-
object.identifier = jskos.addMappingIdentifiers(object).identifier
353+
object.identifier = (await jskos.addMappingIdentifiers(object)).identifier
354354
} catch (error) {
355355
log("Could not add identifier to mapping.", error)
356356
}

models/mappings.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ const mappingSchema = new Schema({
1111
autoIndex: false,
1212
})
1313

14-
mappingSchema.pre("save", function(next) {
14+
mappingSchema.pre("save", async function() {
1515
// Add mapping identifier
16-
this.set("identifier", jskos.addMappingIdentifiers(this).identifier)
17-
next()
16+
this.set("identifier", (await jskos.addMappingIdentifiers(this)).identifier)
1817
})
1918

2019
export const Mapping = mongoose.model("Mapping", mappingSchema)

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"express-basic-auth": "^1.2.1",
7171
"express-ws": "^5.0.2",
7272
"ipaddr.js": "^2.2.0",
73-
"jskos-tools": "^1.0.43",
73+
"jskos-tools": "^1.2.0",
7474
"jskos-validate": "1.2.1",
7575
"json-anystream": "^2.0.1",
7676
"JSONStream": "^1.3.5",

services/mappings.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ export class MappingService extends AbstractService {
534534
for (const uri of ancestors.map(a => a && a.uri).filter(Boolean)) {
535535
mappings = await this.queryItems(Object.assign({}, query, { from: uri, type: types.join("|") }))
536536
if (mappings.length) {
537-
return mappings.map(m => {
537+
return Promise.all(mappings.map(async m => {
538538
const mapping = {
539539
from: {},
540540
fromScheme: m.fromScheme,
@@ -566,7 +566,7 @@ export class MappingService extends AbstractService {
566566
mapping.type = [type]
567567
}
568568
return jskos.addMappingIdentifiers(mapping)
569-
})
569+
}))
570570
}
571571
}
572572
} catch (error) {
@@ -631,7 +631,7 @@ export class MappingService extends AbstractService {
631631
mapping.uri = mapping.uri.replace("http:", "https:")
632632
}
633633
// Set mapping identifier
634-
mapping.identifier = jskos.addMappingIdentifiers(mapping).identifier
634+
mapping.identifier = (await jskos.addMappingIdentifiers(mapping)).identifier
635635
// Set mapping type to mappingRelation if not set
636636
if (!mapping.type || !mapping.type.length) {
637637
mapping.type = ["http://www.w3.org/2004/02/skos/core#mappingRelation"]
@@ -667,7 +667,7 @@ export class MappingService extends AbstractService {
667667
mapping.uri = existing.uri
668668
mapping.created = existing.created
669669
// Set mapping identifier
670-
mapping.identifier = jskos.addMappingIdentifiers(mapping).identifier
670+
mapping.identifier = (await jskos.addMappingIdentifiers(mapping)).identifier
671671
// Set mapping type to mappingRelation if not set
672672
if (!mapping.type || !mapping.type.length) {
673673
mapping.type = ["http://www.w3.org/2004/02/skos/core#mappingRelation"]
@@ -721,7 +721,7 @@ export class MappingService extends AbstractService {
721721
// Merge mappings
722722
const newMapping = Object.assign({}, existing, mapping)
723723
// Set mapping identifier
724-
newMapping.identifier = jskos.addMappingIdentifiers(newMapping).identifier
724+
newMapping.identifier = (await jskos.addMappingIdentifiers(newMapping)).identifier
725725
// Set mapping type to mappingRelation if not set
726726
if (!mapping.type || !mapping.type.length) {
727727
mapping.type = ["http://www.w3.org/2004/02/skos/core#mappingRelation"]

test/api.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,7 @@ describe("Express Server", () => {
822822
for (let mapping of res.body) {
823823
mapping.identifier.should.be.a("array")
824824
mapping.identifier.filter(id => id.startsWith("urn:jskos:mapping:")).length.should.be.eql(2)
825+
mapping.identifier.filter(id => id.startsWith("mapping:")).length.should.be.eql(1)
825826
}
826827
done()
827828
})

test/services.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,20 @@ describe("Services Features", () => {
8484
describe("filter mappings by annotations", () => {
8585
const mappings = [
8686
{
87-
uri: "mapping:1",
87+
uri: "urn:test:mapping:1",
8888
},
8989
{
90-
uri: "mapping:2",
90+
uri: "urn:test:mapping:2",
9191
},
9292
{
93-
uri: "mapping:3",
93+
uri: "urn:test:mapping:3",
9494
},
9595
{
9696
to: { memberSet: [{ uri: "urn:test:concept" }] },
97-
uri: "mapping:4",
97+
uri: "urn:test:mapping:4",
9898
},
9999
{
100-
uri: "mapping:5",
100+
uri: "urn:test:mapping:5",
101101
},
102102
].map(mapping => {
103103
// Add fromScheme and toScheme
@@ -115,28 +115,28 @@ describe("Services Features", () => {
115115
})
116116
const annotations = [
117117
{
118-
target: "mapping:1",
118+
target: "urn:test:mapping:1",
119119
motivation: "assessing",
120120
bodyValue: "+1",
121121
},
122122
{
123-
target: "mapping:1",
123+
target: "urn:test:mapping:1",
124124
motivation: "assessing",
125125
bodyValue: "-1",
126126
},
127127
{
128-
target: "mapping:2",
128+
target: "urn:test:mapping:2",
129129
motivation: "moderating",
130130
creator: {
131131
id: "urn:test:creator",
132132
},
133133
},
134134
{
135-
target: "mapping:3",
135+
target: "urn:test:mapping:3",
136136
motivation: "moderating",
137137
},
138138
{
139-
target: "mapping:4",
139+
target: "urn:test:mapping:4",
140140
motivation: "assessing",
141141
bodyValue: "+1",
142142
},

utils/version.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { addMappingSchemes } from "./utils.js"
22
import { uuid } from "./uuid.js"
33
import _ from "lodash"
44
import yesno from "yesno"
5+
import jskos from "jskos-tools"
56

67
import { Scheme, Concordance, Mapping, Annotation } from "../models/index.js"
78
import { SchemeService } from "../services/schemes.js"
@@ -321,4 +322,19 @@ export class Upgrader {
321322
await this.registryService.createIndexes()
322323
console.log("... done.")
323324
}
325+
async "2.5.0"() {
326+
console.log("Adding mapping sameness identifier to all existing mappings...")
327+
let updatedCount = 0
328+
const cursor = Mapping.find().lean().cursor()
329+
for await (const mapping of cursor) {
330+
// Skip if sameness identifier already present
331+
if ((mapping.identifier || []).some(id => id && id.startsWith("mapping:"))) {
332+
continue
333+
}
334+
const updated = await jskos.addMappingIdentifiers(mapping)
335+
await Mapping.updateOne({ _id: mapping._id }, { $set: { identifier: updated.identifier } })
336+
updatedCount += 1
337+
}
338+
console.log(`... done (${updatedCount} mappings updated).`)
339+
}
324340
}

0 commit comments

Comments
 (0)