| 
 | 1 | +'use strict';  | 
 | 2 | +Object.defineProperty(exports, '__esModule', { value: true });  | 
 | 3 | +const errors_js_1 = require('../errors.js');  | 
 | 4 | +const commandBase_js_1 = require('../validation/commandBase.js');  | 
 | 5 | +const validation_js_1 = require('./validation.js');  | 
 | 6 | +const WAIT_INTERVAL = 1000;  | 
 | 7 | +class BackupCreator extends commandBase_js_1.CommandBase {  | 
 | 8 | +  constructor(client, statusGetter) {  | 
 | 9 | +    super(client);  | 
 | 10 | +    this.validate = () => {  | 
 | 11 | +      this.addErrors([  | 
 | 12 | +        ...(0, validation_js_1.validateIncludeClassNames)(this.includeClassNames),  | 
 | 13 | +        ...(0, validation_js_1.validateExcludeClassNames)(this.excludeClassNames),  | 
 | 14 | +        ...(0, validation_js_1.validateBackend)(this.backend),  | 
 | 15 | +        ...(0, validation_js_1.validateBackupId)(this.backupId),  | 
 | 16 | +      ]);  | 
 | 17 | +    };  | 
 | 18 | +    this.do = () => {  | 
 | 19 | +      this.validate();  | 
 | 20 | +      if (this.errors.length > 0) {  | 
 | 21 | +        return Promise.reject(  | 
 | 22 | +          new errors_js_1.WeaviateInvalidInputError('invalid usage: ' + this.errors.join(', '))  | 
 | 23 | +        );  | 
 | 24 | +      }  | 
 | 25 | +      const payload = {  | 
 | 26 | +        id: this.backupId,  | 
 | 27 | +        config: this.config,  | 
 | 28 | +        include: this.includeClassNames,  | 
 | 29 | +        exclude: this.excludeClassNames,  | 
 | 30 | +      };  | 
 | 31 | +      if (this.waitForCompletion) {  | 
 | 32 | +        return this._createAndWaitForCompletion(payload);  | 
 | 33 | +      }  | 
 | 34 | +      return this._create(payload);  | 
 | 35 | +    };  | 
 | 36 | +    this._create = (payload) => {  | 
 | 37 | +      return this.client.postReturn(this._path(), payload);  | 
 | 38 | +    };  | 
 | 39 | +    this._createAndWaitForCompletion = (payload) => {  | 
 | 40 | +      return new Promise((resolve, reject) => {  | 
 | 41 | +        this._create(payload)  | 
 | 42 | +          .then((createResponse) => {  | 
 | 43 | +            this.statusGetter.withBackend(this.backend).withBackupId(this.backupId);  | 
 | 44 | +            const loop = () => {  | 
 | 45 | +              this.statusGetter  | 
 | 46 | +                .do()  | 
 | 47 | +                .then((createStatusResponse) => {  | 
 | 48 | +                  if (createStatusResponse.status == 'SUCCESS' || createStatusResponse.status == 'FAILED') {  | 
 | 49 | +                    resolve(this._merge(createStatusResponse, createResponse));  | 
 | 50 | +                  } else {  | 
 | 51 | +                    setTimeout(loop, WAIT_INTERVAL);  | 
 | 52 | +                  }  | 
 | 53 | +                })  | 
 | 54 | +                .catch(reject);  | 
 | 55 | +            };  | 
 | 56 | +            loop();  | 
 | 57 | +          })  | 
 | 58 | +          .catch(reject);  | 
 | 59 | +      });  | 
 | 60 | +    };  | 
 | 61 | +    this._path = () => {  | 
 | 62 | +      return `/backups/${this.backend}`;  | 
 | 63 | +    };  | 
 | 64 | +    this._merge = (createStatusResponse, createResponse) => {  | 
 | 65 | +      const merged = {};  | 
 | 66 | +      if ('id' in createStatusResponse) {  | 
 | 67 | +        merged.id = createStatusResponse.id;  | 
 | 68 | +      }  | 
 | 69 | +      if ('path' in createStatusResponse) {  | 
 | 70 | +        merged.path = createStatusResponse.path;  | 
 | 71 | +      }  | 
 | 72 | +      if ('backend' in createStatusResponse) {  | 
 | 73 | +        merged.backend = createStatusResponse.backend;  | 
 | 74 | +      }  | 
 | 75 | +      if ('status' in createStatusResponse) {  | 
 | 76 | +        merged.status = createStatusResponse.status;  | 
 | 77 | +      }  | 
 | 78 | +      if ('error' in createStatusResponse) {  | 
 | 79 | +        merged.error = createStatusResponse.error;  | 
 | 80 | +      }  | 
 | 81 | +      if ('classes' in createResponse) {  | 
 | 82 | +        merged.classes = createResponse.classes;  | 
 | 83 | +      }  | 
 | 84 | +      return merged;  | 
 | 85 | +    };  | 
 | 86 | +    this.statusGetter = statusGetter;  | 
 | 87 | +  }  | 
 | 88 | +  withIncludeClassNames(...classNames) {  | 
 | 89 | +    let cls = classNames;  | 
 | 90 | +    if (classNames.length && Array.isArray(classNames[0])) {  | 
 | 91 | +      cls = classNames[0];  | 
 | 92 | +    }  | 
 | 93 | +    this.includeClassNames = cls;  | 
 | 94 | +    return this;  | 
 | 95 | +  }  | 
 | 96 | +  withExcludeClassNames(...classNames) {  | 
 | 97 | +    let cls = classNames;  | 
 | 98 | +    if (classNames.length && Array.isArray(classNames[0])) {  | 
 | 99 | +      cls = classNames[0];  | 
 | 100 | +    }  | 
 | 101 | +    this.excludeClassNames = cls;  | 
 | 102 | +    return this;  | 
 | 103 | +  }  | 
 | 104 | +  withBackend(backend) {  | 
 | 105 | +    this.backend = backend;  | 
 | 106 | +    return this;  | 
 | 107 | +  }  | 
 | 108 | +  withBackupId(backupId) {  | 
 | 109 | +    this.backupId = backupId;  | 
 | 110 | +    return this;  | 
 | 111 | +  }  | 
 | 112 | +  withWaitForCompletion(waitForCompletion) {  | 
 | 113 | +    this.waitForCompletion = waitForCompletion;  | 
 | 114 | +    return this;  | 
 | 115 | +  }  | 
 | 116 | +  withConfig(cfg) {  | 
 | 117 | +    this.config = cfg;  | 
 | 118 | +    return this;  | 
 | 119 | +  }  | 
 | 120 | +}  | 
 | 121 | +exports.default = BackupCreator;  | 
0 commit comments