UpsertGraphAndFetchOperation.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. const DelegateOperation = require('./DelegateOperation');
  2. const RelationExpression = require('../RelationExpression');
  3. const UpsertGraphOperation = require('./UpsertGraphOperation');
  4. class UpsertGraphAndFetchOperation extends DelegateOperation {
  5. constructor(name, opt) {
  6. super(name, opt);
  7. if (!this.delegate.is(UpsertGraphOperation)) {
  8. throw new Error('Invalid delegate');
  9. }
  10. }
  11. get models() {
  12. return this.delegate.models;
  13. }
  14. get isArray() {
  15. return this.delegate.isArray;
  16. }
  17. onAfter3(builder) {
  18. const eager = RelationExpression.fromModelGraph(this.models);
  19. const modelClass = this.models[0].constructor;
  20. const ids = new Array(this.models.length);
  21. for (let i = 0, l = this.models.length; i < l; ++i) {
  22. ids[i] = this.models[i].$id();
  23. }
  24. return modelClass
  25. .query()
  26. .childQueryOf(builder)
  27. .whereIn(builder.fullIdColumnFor(modelClass), ids)
  28. .eager(eager)
  29. .then(models => {
  30. return this.isArray ? models : models[0] || null;
  31. });
  32. }
  33. }
  34. module.exports = UpsertGraphAndFetchOperation;