data-projector.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import {Service} from '@e22m4u/js-service';
  2. import {projectData} from './project-data.js';
  3. import {ProjectionSchemaRegistry} from './definitions/index.js';
  4. /**
  5. * Data projector.
  6. */
  7. export class DataProjector extends Service {
  8. /**
  9. * Define schema.
  10. *
  11. * @param {object} schemaDef
  12. * @returns {this}
  13. */
  14. defineSchema(schemaDef) {
  15. this.getService(ProjectionSchemaRegistry).defineSchema(schemaDef);
  16. return this;
  17. }
  18. /**
  19. * Has schema.
  20. *
  21. * @param {string} schemaName
  22. * @returns {boolean}
  23. */
  24. hasSchema(schemaName) {
  25. return this.getService(ProjectionSchemaRegistry).hasSchema(schemaName);
  26. }
  27. /**
  28. * Project.
  29. *
  30. * @param {object|Function|string} schemaOrFactory
  31. * @param {*} data
  32. * @param {object} [options]
  33. * @returns {*}
  34. */
  35. project(schemaOrFactory, data, options) {
  36. const registry = this.getService(ProjectionSchemaRegistry);
  37. const resolver = schemaName => {
  38. return registry.getSchema(schemaName);
  39. };
  40. return projectData(schemaOrFactory, data, {...options, resolver});
  41. }
  42. }