UNPKG

weaviate-client

Version:
121 lines (120 loc) 4.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const errors_js_1 = require("../errors.js"); const commandBase_js_1 = require("../validation/commandBase.js"); const validation_js_1 = require("./validation.js"); const WAIT_INTERVAL = 1000; class BackupCreator extends commandBase_js_1.CommandBase { constructor(client, statusGetter) { super(client); this.validate = () => { this.addErrors([ ...(0, validation_js_1.validateIncludeClassNames)(this.includeClassNames), ...(0, validation_js_1.validateExcludeClassNames)(this.excludeClassNames), ...(0, validation_js_1.validateBackend)(this.backend), ...(0, validation_js_1.validateBackupId)(this.backupId), ]); }; this.do = () => { this.validate(); if (this.errors.length > 0) { return Promise.reject(new errors_js_1.WeaviateInvalidInputError('invalid usage: ' + this.errors.join(', '))); } const payload = { id: this.backupId, config: this.config, include: this.includeClassNames, exclude: this.excludeClassNames, }; if (this.waitForCompletion) { return this._createAndWaitForCompletion(payload); } return this._create(payload); }; this._create = (payload) => { return this.client.postReturn(this._path(), payload); }; this._createAndWaitForCompletion = (payload) => { return new Promise((resolve, reject) => { this._create(payload) .then((createResponse) => { this.statusGetter.withBackend(this.backend).withBackupId(this.backupId); const loop = () => { this.statusGetter .do() .then((createStatusResponse) => { if (createStatusResponse.status == 'SUCCESS' || createStatusResponse.status == 'FAILED') { resolve(this._merge(createStatusResponse, createResponse)); } else { setTimeout(loop, WAIT_INTERVAL); } }) .catch(reject); }; loop(); }) .catch(reject); }); }; this._path = () => { return `/backups/${this.backend}`; }; this._merge = (createStatusResponse, createResponse) => { const merged = {}; if ('id' in createStatusResponse) { merged.id = createStatusResponse.id; } if ('path' in createStatusResponse) { merged.path = createStatusResponse.path; } if ('backend' in createStatusResponse) { merged.backend = createStatusResponse.backend; } if ('status' in createStatusResponse) { merged.status = createStatusResponse.status; } if ('error' in createStatusResponse) { merged.error = createStatusResponse.error; } if ('classes' in createResponse) { merged.classes = createResponse.classes; } return merged; }; this.statusGetter = statusGetter; } withIncludeClassNames(...classNames) { let cls = classNames; if (classNames.length && Array.isArray(classNames[0])) { cls = classNames[0]; } this.includeClassNames = cls; return this; } withExcludeClassNames(...classNames) { let cls = classNames; if (classNames.length && Array.isArray(classNames[0])) { cls = classNames[0]; } this.excludeClassNames = cls; return this; } withBackend(backend) { this.backend = backend; return this; } withBackupId(backupId) { this.backupId = backupId; return this; } withWaitForCompletion(waitForCompletion) { this.waitForCompletion = waitForCompletion; return this; } withConfig(cfg) { this.config = cfg; return this; } } exports.default = BackupCreator;