projection-schema-registry.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import {Service} from '@e22m4u/js-service';
  2. import {InvalidArgumentError} from '@e22m4u/js-format';
  3. import {validateProjectionSchemaDefinition} from './validate-projection-schema-definition.js';
  4. /**
  5. * Projection schema registry.
  6. */
  7. export class ProjectionSchemaRegistry extends Service {
  8. /**
  9. * Definitions.
  10. */
  11. _definitions = new Map();
  12. /**
  13. * Define schema.
  14. *
  15. * @param {object} schemaDef
  16. * @returns {this}
  17. */
  18. defineSchema(schemaDef) {
  19. validateProjectionSchemaDefinition(schemaDef);
  20. if (this._definitions.has(schemaDef.name)) {
  21. throw new InvalidArgumentError(
  22. 'Projection schema %v is already registered.',
  23. schemaDef.name,
  24. );
  25. }
  26. this._definitions.set(schemaDef.name, schemaDef);
  27. return this;
  28. }
  29. /**
  30. * Has schema.
  31. *
  32. * @param {string} schemaName
  33. * @returns {boolean}
  34. */
  35. hasSchema(schemaName) {
  36. return this._definitions.has(schemaName);
  37. }
  38. /**
  39. * Get schema.
  40. *
  41. * @param {string} schemaName
  42. * @returns {object}
  43. */
  44. getSchema(schemaName) {
  45. const schemaDef = this._definitions.get(schemaName);
  46. if (!schemaDef) {
  47. throw new InvalidArgumentError(
  48. 'Projection schema %v is not found.',
  49. schemaName,
  50. );
  51. }
  52. return schemaDef.schema;
  53. }
  54. /**
  55. * Get definition.
  56. *
  57. * @param {string} schemaName
  58. * @returns {object}
  59. */
  60. getDefinition(schemaName) {
  61. const schemaDef = this._definitions.get(schemaName);
  62. if (!schemaDef) {
  63. throw new InvalidArgumentError(
  64. 'Schema definition %v is not found.',
  65. schemaName,
  66. );
  67. }
  68. return schemaDef;
  69. }
  70. }