BaseES.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (C) 2024 Puter Technologies Inc.
  3. *
  4. * This file is part of Puter.
  5. *
  6. * Puter is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published
  8. * by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. const { AdvancedBase } = require("@heyputer/puter-js-common");
  20. const { WeakConstructorFeature } = require("../../traits/WeakConstructorFeature");
  21. const { Context } = require("../../util/context");
  22. /**
  23. * BaseES is a base class for Entity Store classes.
  24. */
  25. class BaseES extends AdvancedBase {
  26. static FEATURES = [
  27. new WeakConstructorFeature(),
  28. ]
  29. // Default implementations
  30. static METHODS = {
  31. async upsert (entity, extra) {
  32. if ( ! this.upstream ) {
  33. throw Error('Missing terminal operation');
  34. }
  35. return await this.upstream.upsert(entity, extra);
  36. },
  37. async read (uid) {
  38. if ( ! this.upstream ) {
  39. throw Error('Missing terminal operation');
  40. }
  41. return await this.upstream.read(uid);
  42. },
  43. async delete (uid, extra) {
  44. if ( ! this.upstream ) {
  45. throw Error('Missing terminal operation');
  46. }
  47. return await this.upstream.delete(uid, extra);
  48. },
  49. async select (options) {
  50. if ( ! this.upstream ) {
  51. throw Error('Missing terminal operation');
  52. }
  53. return await this.upstream.select(options);
  54. },
  55. async create_predicate (id, ...args) {
  56. if ( ! this.upstream ) {
  57. throw Error('Missing terminal operation');
  58. }
  59. return await this.upstream.create_predicate(id, ...args);
  60. }
  61. };
  62. constructor (...a) {
  63. super(...a);
  64. const public_wrappers = [
  65. 'upsert', 'read', 'delete', 'select',
  66. 'read_transform',
  67. ];
  68. this.impl_methods = this._get_merged_static_object('METHODS');
  69. for ( const k in this.impl_methods ) {
  70. // Some methods are part of the implicit EntityStorage interface.
  71. // We won't let the implementor override these; instead we
  72. // provide a delegating implementation where they override a
  73. // lower-level method of the same name.
  74. if ( public_wrappers.includes(k) ) continue;
  75. this[k] = this.impl_methods[k];
  76. }
  77. this.log = Context.get('services').get('log-service')
  78. .create(`ES:${this.entity_name}:${this.constructor.name}`);
  79. }
  80. async provide_context ( args ) {
  81. for ( const k in args ) this[k] = args[k];
  82. if ( this.upstream ) {
  83. await this.upstream.provide_context(args);
  84. }
  85. if ( this._on_context_provided ) {
  86. await this._on_context_provided(args);
  87. }
  88. this.log = Context.get('services').get('log-service')
  89. .create(`ES:${this.entity_name}:${this.constructor.name}`);
  90. }
  91. async read (uid) {
  92. const entity = await this.call_on_impl_('read', uid);
  93. if ( ! this.impl_methods.read_transform ) return entity;
  94. return await this.read_transform(entity);
  95. }
  96. async upsert (entity, extra) {
  97. return await this.call_on_impl_('upsert', entity, extra ?? {});
  98. }
  99. async delete (uid, extra) {
  100. return await this.call_on_impl_('delete', uid, extra ?? {});
  101. }
  102. async select (options) {
  103. const results = await this.call_on_impl_('select', options);
  104. if ( ! this.impl_methods.read_transform ) return results;
  105. // Promises "solved callback hell" but like...
  106. return await Promise.all(results.map(async entity => {
  107. return await this.read_transform(entity);
  108. }));
  109. }
  110. async read_transform (entity) {
  111. if ( ! entity ) return entity;
  112. if ( ! this.impl_methods.read_transform ) return entity;
  113. const maybe_entity = await this.call_on_impl_('read_transform', entity);
  114. if ( ! maybe_entity ) return entity;
  115. return maybe_entity;
  116. }
  117. call_on_impl_ (method_name, ...args) {
  118. // const pseudo_this = { ...this };
  119. // pseudo_this.next = this.upstream?.call_on_impl?.bind(this.upstream, method_name);
  120. return this.impl_methods[method_name].call(this, ...args);
  121. }
  122. }
  123. module.exports = {
  124. BaseES,
  125. };