ukamf-db.js 671 B

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