adapter.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* eslint no-unused-vars: 0 */
  2. import {Service} from '../service/index.js';
  3. import {NotImplementedError} from '../errors/index.js';
  4. import {InclusionDecorator} from './decorator/index.js';
  5. import {DefaultValuesDecorator} from './decorator/index.js';
  6. import {DataValidationDecorator} from './decorator/index.js';
  7. import {DataSanitizingDecorator} from './decorator/index.js';
  8. import {FieldsFilteringDecorator} from './decorator/index.js';
  9. /**
  10. * Adapter.
  11. */
  12. export class Adapter extends Service {
  13. /**
  14. * Settings.
  15. */
  16. _settings;
  17. /**
  18. * Settings.
  19. *
  20. * @return {*}
  21. */
  22. get settings() {
  23. return this._settings;
  24. }
  25. /**
  26. * Constructor.
  27. *
  28. * @param services
  29. * @param settings
  30. */
  31. constructor(services = undefined, settings = undefined) {
  32. super(services);
  33. this._settings = settings;
  34. // decorate only extended classes
  35. if (this.constructor !== Adapter) {
  36. this.get(DataValidationDecorator).decorate(this);
  37. this.get(DataSanitizingDecorator).decorate(this);
  38. this.get(DefaultValuesDecorator).decorate(this);
  39. this.get(FieldsFilteringDecorator).decorate(this);
  40. this.get(InclusionDecorator).decorate(this);
  41. }
  42. }
  43. /**
  44. * Create.
  45. *
  46. * @param {string} modelName
  47. * @param {Record<string, unknown>} modelData
  48. * @param {Record<string, unknown>|undefined} filter
  49. * @return {Promise<object>}
  50. */
  51. create(modelName, modelData, filter = undefined) {
  52. throw new NotImplementedError(
  53. '%s.create is not implemented.',
  54. new String(this.constructor.name),
  55. );
  56. }
  57. /**
  58. * Replace by id.
  59. *
  60. * @param {string} modelName
  61. * @param {string|number} id
  62. * @param {Record<string, unknown>} modelData
  63. * @param {Record<string, unknown>|undefined} filter
  64. * @return {Promise<object>}
  65. */
  66. replaceById(modelName, id, modelData, filter = undefined) {
  67. throw new NotImplementedError(
  68. '%s.replaceById is not implemented.',
  69. new String(this.constructor.name),
  70. );
  71. }
  72. /**
  73. * Patch by id.
  74. *
  75. * @param {string} modelName
  76. * @param {string|number} id
  77. * @param {Record<string, unknown>} modelData
  78. * @param {Record<string, unknown>|undefined} filter
  79. * @return {Promise<object>}
  80. */
  81. patchById(modelName, id, modelData, filter = undefined) {
  82. throw new NotImplementedError(
  83. '%s.patchById is not implemented.',
  84. new String(this.constructor.name),
  85. );
  86. }
  87. /**
  88. * Find.
  89. *
  90. * @param {string} modelName
  91. * @param {Record<string, unknown>|undefined} filter
  92. * @return {Promise<object[]>}
  93. */
  94. find(modelName, filter = undefined) {
  95. throw new NotImplementedError(
  96. '%s.find is not implemented.',
  97. new String(this.constructor.name),
  98. );
  99. }
  100. /**
  101. * Find by id.
  102. *
  103. * @param {string} modelName
  104. * @param {string|number} id
  105. * @param {Record<string, unknown>|undefined} filter
  106. * @return {Promise<object>}
  107. */
  108. findById(modelName, id, filter = undefined) {
  109. throw new NotImplementedError(
  110. '%s.findById is not implemented.',
  111. new String(this.constructor.name),
  112. );
  113. }
  114. /**
  115. * Delete.
  116. *
  117. * @param {string} modelName
  118. * @param {Record<string, unknown>|undefined} where
  119. * @return {Promise<number>}
  120. */
  121. delete(modelName, where = undefined) {
  122. throw new NotImplementedError(
  123. '%s.delete is not implemented.',
  124. new String(this.constructor.name),
  125. );
  126. }
  127. /**
  128. * Delete by id.
  129. *
  130. * @param {string} modelName
  131. * @param {string|number} id
  132. * @return {Promise<boolean>}
  133. */
  134. deleteById(modelName, id) {
  135. throw new NotImplementedError(
  136. '%s.deleteById is not implemented.',
  137. new String(this.constructor.name),
  138. );
  139. }
  140. /**
  141. * Exists.
  142. *
  143. * @param {string} modelName
  144. * @param {string|number} id
  145. * @return {Promise<boolean>}
  146. */
  147. exists(modelName, id) {
  148. throw new NotImplementedError(
  149. '%s.exists is not implemented.',
  150. new String(this.constructor.name),
  151. );
  152. }
  153. /**
  154. * Count.
  155. *
  156. * @param {string} modelName
  157. * @param {Record<string, unknown>|undefined} where
  158. * @return {Promise<number>}
  159. */
  160. count(modelName, where = undefined) {
  161. throw new NotImplementedError(
  162. '%s.count is not implemented.',
  163. new String(this.constructor.name),
  164. );
  165. }
  166. }