ukamf-db.js 697 B

12345678910111213141516171819202122232425262728293031
  1. 'use strict'
  2. const fs = require('fs-extra')
  3. const xml2js = require('xml2js')
  4. const UKAMFEntity = require('./ukamf-entity')
  5. class UKAMFDB {
  6. constructor(file) {
  7. this.file = file
  8. }
  9. async init() {
  10. const data = await fs.readFile(this.file, 'utf8')
  11. const parser = new xml2js.Parser()
  12. const xml = await parser.parseStringPromise(data)
  13. this.entities = xml.EntitiesDescriptor.EntityDescriptor
  14. }
  15. findByEntityID(matcher) {
  16. const entity = this.entities.find(
  17. matcher instanceof RegExp
  18. ? e => e.$.entityID.match(matcher)
  19. : e => e.$.entityID.includes(matcher)
  20. )
  21. return entity ? new UKAMFEntity(entity) : null
  22. }
  23. }
  24. module.exports = UKAMFDB