model/index.js

  1. /**
  2. * @module model
  3. *
  4. * @description
  5. *
  6. * This page will give you an introduction on one of the largest and more complex parts of d2, the metadata models.
  7. *
  8. * Models are an abstraction on top of the dhis2 metadata api. We can use these models to interact with the dhis2 api.
  9. * The models are accessible on the `.models` property that is attached to the d2 instance (that is retrieved by calling
  10. * `getInstance()`).
  11. *
  12. * A schema is a description of a type of metadata object. To find out which schemas are available for you to use with
  13. * d2 take a look at https://play.dhis2.org/demo/api/schemas.json?fields=name,metadata. Any schema that is marked as
  14. * `metadata: true` can be used with d2.
  15. *
  16. * There are a few concepts that you should familiarise yourself with before you start. (More documentation on the following
  17. * concepts can be found on the classes that represent them.)
  18. *
  19. * ### ModelDefinitions
  20. *
  21. * A {@link module:model.ModelDefinition} is where it all starts. In d2 the schemas you have loaded are represented by a `ModelDefinition`.
  22. * A `ModelDefinition` is essentially a wrapper around the schemas that allows you to create `Model` instances for that
  23. * specific schema (e.g. the dataElementModelDefinition allows you to create dataElement Models).
  24. * ```js
  25. * d2.models.dataElement // This is the dataElement ModelDefinition
  26. * .create() // Creates a Model object based of the dataElement ModelDefinition
  27. * ```
  28. *
  29. * When working with the d2 object the `d2.models` object contains modelDefinitions for the schemas that you have loaded.
  30. * (by default that is all schemas). To get a modelDefinition for a specific schema you ask for it by the name of the schema.
  31. * A few examples other than the dataElement one from above would be:
  32. *
  33. * ```js
  34. * d2.models.user
  35. * d2.models.legendSet
  36. * d2.models.organisationUnit
  37. * ```
  38. *
  39. * The ModelDefinition allows you to load objects from the api. You can either load individual items or items in bulk.
  40. *
  41. * We can use it to get specific objects
  42. * ```js
  43. * d2.models.organisationUnit.get('ImspTQPwCqd')
  44. * .then(organisationUnitModel => console.log(organisationUnitModel));
  45. * ```
  46. *
  47. * or to load a list of objects (by default paging is enabled and it will only load the first 50 entries).
  48. * ```js
  49. * d2.models.organisationUnit.list()
  50. * .then(organisationUnitCollection => console.log(organisationUnitCollection));
  51. * ```
  52. *
  53. * The ModelDefinition also allows you to filter on properties using a programatic syntax. To find out which
  54. * filter methods are available you can a look at the {@link module:model.Filter|Filter} class.
  55. * ```js
  56. * d2.models.organisationUnit
  57. * .filter().on('level').equals(2)
  58. * .list()
  59. * .then(organisationUnitCollection => console.log(organisationUnitCollection));
  60. * ```
  61. * Applying a filter like this will create a clone of the modelDefinition with the filter applied, so you can
  62. * store the reference to the cloned modelDefinition and use it later, without globally applying the filter everywhere.
  63. *
  64. * ```js
  65. * const organisationUnitsOnLevel3 = d2.models.organisationUnit
  66. * .filter().on('level').equals(3);
  67. *
  68. * const organisationUnitsOnLevel3WithParent = organisationUnitsOnLevel3
  69. * .filter().on('parent.id').equals('O6uvpzGd5pu);
  70. *
  71. * organisationUnitsOnLevel3
  72. * .list({ paging: false }) // Loads all organisation units on level 3
  73. * .then(organisationUnitCollection => console.log(organisationUnitCollection));
  74. *
  75. * organisationUnitsOnLevel3WithParent
  76. * .list({ paging: false }) // Loads all organisation units on level 3 with O6uvpzGd5pu as their parent
  77. * .then(organisationUnitCollection => console.log(organisationUnitCollection));
  78. * ```
  79. *
  80. * As you might have noticed we passed `{ paging: false }` to the `.list` method. Any options passed to the list method
  81. * (with the exception of filter) will be passed through as regular query parameters.
  82. *
  83. * If you ran the above examples you would notice that the `.list` method returns a ModelCollection.
  84. *
  85. * ## ModelCollection
  86. * A {@link module:model.ModelCollection|ModelCollection} is simple a collection of Model objects. The ModelCollection is
  87. * sometimes somewhat cumbersome to work with and often you would like to transform the ModelCollection to an array using
  88. * the `.toArray()` method or spreading the values of the ModelCollection.
  89. *
  90. * ```js
  91. * d2.models.organisationUnit
  92. * .filter().on('level').equals(2)
  93. * .list()
  94. * .then(organisationUnitCollection => {
  95. * let organisationUnits = organisationUnitCollection.toArray();
  96. * // or
  97. * organisationUnits = [...organisationUnitCollection.values()];
  98. * });
  99. * ```
  100. *
  101. * The ModelCollection does not only contain the Models, it also gives you access to a {@link module:model.Pager|Pager}.
  102. * The pager objects keeps track of which page was loaded and how many pages there are.
  103. *
  104. * The following example will load the first page of organisation units and then keep loading a new page every second
  105. * until we run out of pages.
  106. * ```js
  107. * function loadNextPage(pager, callback) {
  108. * if (pager.hasNextPage()) {
  109. * pager.getNextPage()
  110. * .then(organisationUnitCollection => {
  111. * setTimeout(() => {
  112. * callback(organisationUnitCollection);
  113. * loadNextPage(organisationUnitCollection.pager, callback);
  114. * }, 1000);
  115. * });
  116. * }
  117. * }
  118. *
  119. * const printCollection = collection => console.log(collection.toArray());
  120. *
  121. * d2.models.organisationUnit
  122. * .list()
  123. * .then(organisationUnitCollection => {
  124. * printCollection(organisationUnitCollection);
  125. * loadNextPage(organisationUnitCollection.pager, printCollection);
  126. * });
  127. * ```
  128. *
  129. * ## Model
  130. * TODO: :(
  131. */
  132. import ModelBase from './ModelBase'
  133. import Model from './Model'
  134. import ModelDefinition from './ModelDefinition'
  135. import ModelDefinitions from './ModelDefinitions'
  136. import ModelValidation from './ModelValidation'
  137. export default {
  138. ModelBase,
  139. Model,
  140. ModelDefinition,
  141. ModelDefinitions,
  142. ModelValidation,
  143. }