-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: check is referenced when delete
- The resource will be association with other resource, If we delete the resource that be referenced by other resource it will ruin the binding association. - Add `resourceRefBy` schema to store the association - Remove unused schema `FHIRValidationFiles` - Add functions as title does > In `models/mongodb/common.js`
- Loading branch information
1 parent
6c6a40f
commit ef49215
Showing
5 changed files
with
249 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
const jp = require("jsonpath"); | ||
const mongoose = require("mongoose"); | ||
|
||
/** | ||
* Store the resource reference by which resources | ||
* e.g. | ||
* { | ||
* resourceType: "Organization", | ||
* id: "123", | ||
* refBy: [ | ||
* { | ||
* resourceType: "Patient", | ||
* id: | ||
* } | ||
* ] | ||
* } | ||
* @param {Object} resource | ||
*/ | ||
async function storeResourceRefBy(resource) { | ||
let referenceInItem = jp.nodes(resource, "$..reference"); | ||
for (let refNode of referenceInItem) { | ||
let referenceSplit = refNode.value.split("/"); | ||
let id = referenceSplit[referenceSplit.length - 1]; | ||
let resourceType = referenceSplit[referenceSplit.length - 2]; | ||
|
||
await mongoose.model("resourceRefBy").findOneAndUpdate({ | ||
$and: [ | ||
{ | ||
resourceType: resourceType | ||
}, | ||
{ | ||
id: id | ||
} | ||
] | ||
}, { | ||
$set: { | ||
resourceType: resourceType, | ||
id: id | ||
}, | ||
$addToSet: { | ||
refBy: { | ||
resourceType: resource.resourceType, | ||
id: resource.id | ||
} | ||
} | ||
}, { | ||
upsert: true | ||
}); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* If resource not reference by any resource, delete this resource info in any refBy array | ||
* > Use in post delete | ||
* @param {Object} resource | ||
*/ | ||
async function updateRefBy(resource) { | ||
try { | ||
await mongoose.model("resourceRefBy").updateMany({ | ||
$and: [ | ||
{ | ||
"refBy.resourceType": resource.resourceType | ||
}, | ||
{ | ||
"refBy.id": resource.id | ||
} | ||
] | ||
}, { | ||
$pull: { | ||
refBy: { | ||
resourceType: resource.resourceType, | ||
id: resource.id | ||
} | ||
} | ||
}); | ||
} catch (e) { | ||
throw e; | ||
} | ||
} | ||
|
||
/** | ||
* After updating refBy, some array will be empty that mean the resource is not referenced by any resource anymore. | ||
* So, we need to delete document that have empty refBy array. | ||
*/ | ||
async function deleteEmptyRefBy() { | ||
try { | ||
await mongoose.model("resourceRefBy").deleteMany({ | ||
$and: [ | ||
{ | ||
refBy: { | ||
$exists: true | ||
} | ||
}, | ||
{ | ||
refBy: { | ||
$size: 0 | ||
} | ||
} | ||
] | ||
}); | ||
} catch (e) { | ||
throw e; | ||
} | ||
} | ||
|
||
/** | ||
* We must check the resource is referenced by any resources when fire deleting. | ||
* 1. If resource has referenced by any resource, throw error | ||
* 2. Do next process otherwise. | ||
* > Use in pre delete | ||
*/ | ||
async function checkResourceHaveReferenceByOthers(resource) { | ||
try { | ||
let data = await mongoose.model("resourceRefBy").countDocuments({ | ||
$and: [ | ||
{ | ||
resourceType: resource.resourceType, | ||
id: resource.id | ||
} | ||
] | ||
}).limit(1); | ||
|
||
if (data > 0) { | ||
return true; | ||
} | ||
return false; | ||
} catch (e) { | ||
console.error(e); | ||
throw e; | ||
} | ||
} | ||
|
||
module.exports.storeResourceRefBy = storeResourceRefBy; | ||
module.exports.updateRefBy = updateRefBy; | ||
module.exports.deleteEmptyRefBy = deleteEmptyRefBy; | ||
module.exports.checkResourceHaveReferenceByOthers = checkResourceHaveReferenceByOthers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
const instant = require("../FHIRDataTypesSchema/instant"); | ||
|
||
/** | ||
* The schema to storing each resource is refer by which resources | ||
* 1. Update data when update or create resource | ||
* 2. Check resource is exist in this data to prevent delete the resource refer by another resources | ||
* @Author Chin-Lin Lee <a5566qq2581@gmail.com> | ||
*/ | ||
|
||
/** | ||
* | ||
* @param {import("mongoose")} mongodb | ||
* @returns | ||
*/ | ||
module.exports = function (mongodb) { | ||
let basicInfo = new mongodb.Schema({ | ||
"resourceType": { | ||
type: String, | ||
required: true, | ||
default: void 0 | ||
}, | ||
"id": { | ||
type: String, | ||
required: true, | ||
default: void 0 | ||
} | ||
}, { | ||
_id : false, | ||
id: false | ||
}); | ||
|
||
let resourceRefBy = new mongodb.Schema({ | ||
|
||
|
||
}, { | ||
versionKey : false | ||
}); | ||
|
||
resourceRefBy.add(basicInfo); | ||
resourceRefBy.add({ | ||
lastUpdated: { | ||
...instant, | ||
default: Date.now() | ||
} | ||
}); | ||
resourceRefBy.add(new mongodb.Schema({ | ||
refBy: { | ||
type: [basicInfo], | ||
default: void 0 | ||
} | ||
}, { | ||
_id : false, | ||
id: false | ||
})); | ||
|
||
resourceRefBy.index({ | ||
"id": 1, | ||
"resourceType": 1 | ||
}, { | ||
background: true | ||
}); | ||
|
||
resourceRefBy.index({ | ||
"refBy.id": 1, | ||
"refBy.resourceType": 1 | ||
}, { | ||
background: true | ||
}); | ||
|
||
|
||
let resourceRefByModel = mongodb.model('resourceRefBy', resourceRefBy, 'resourceRefBy'); | ||
return resourceRefByModel; | ||
}; |