ParseObjectIDTimestamp.mjs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /**
  2. * @author dmfj [dominic@dmfj.io]
  3. * @copyright Crown Copyright 2020
  4. * @license Apache-2.0
  5. */
  6. import Operation from "../Operation.mjs";
  7. import OperationError from "../errors/OperationError.mjs";
  8. import BSON from "bson";
  9. /**
  10. * Parse ObjectID timestamp operation
  11. */
  12. class ParseObjectIDTimestamp extends Operation {
  13. /**
  14. * ParseObjectIDTimestamp constructor
  15. */
  16. constructor() {
  17. super();
  18. this.name = "Parse ObjectID timestamp";
  19. this.module = "Serialise";
  20. this.description = "Parse timestamp from MongoDB/BSON ObjectID hex string.";
  21. this.infoURL = "https://docs.mongodb.com/manual/reference/method/ObjectId.getTimestamp/";
  22. this.inputType = "string";
  23. this.outputType = "string";
  24. this.args = [];
  25. }
  26. /**
  27. * @param {string} input
  28. * @param {Object[]} args
  29. * @returns {string}
  30. */
  31. run(input, args) {
  32. try {
  33. const objectId = new BSON.ObjectID(input);
  34. return objectId.getTimestamp().toISOString();
  35. } catch (err) {
  36. throw new OperationError(err);
  37. }
  38. }
  39. }
  40. export default ParseObjectIDTimestamp;