Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/backup/backupCreateStatusGetter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Connection from '../connection';
import { validateBackupId, validateBackend } from './validation';
import { CommandBase } from '../validation/commandBase';
import { BackupCreateStatusResponse } from '../types';

export default class BackupCreateStatusGetter extends CommandBase {
private backend?: string;
Expand All @@ -20,19 +21,19 @@ export default class BackupCreateStatusGetter extends CommandBase {
return this;
}

validate() {
validate = (): void => {
this.addErrors([...validateBackend(this.backend), ...validateBackupId(this.backupId)]);
}
};

do() {
do = (): Promise<BackupCreateStatusResponse | Error> => {
this.validate();
if (this.errors.length > 0) {
return Promise.reject(new Error('invalid usage: ' + this.errors.join(', ')));
}
return this.client.get(this._path());
}
return this.client.get(this._path()) as Promise<BackupCreateStatusResponse>;
};

_path() {
private _path = (): string => {
return `/backups/${this.backend}/${this.backupId}`;
}
};
}
36 changes: 20 additions & 16 deletions src/backup/backupCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import BackupCreateStatusGetter from './backupCreateStatusGetter';
import Connection from '../connection';
import { CommandBase } from '../validation/commandBase';
import { BackupCreateRequest, BackupCreateResponse, BackupCreateStatusResponse } from '../types';

const WAIT_INTERVAL = 1000;

Expand Down Expand Up @@ -57,16 +58,16 @@ export default class BackupCreator extends CommandBase {
return this;
}

validate() {
validate = (): void => {
this.addErrors([
...validateIncludeClassNames(this.includeClassNames),
...validateExcludeClassNames(this.excludeClassNames),
...validateBackend(this.backend),
...validateBackupId(this.backupId),
]);
}
};

do() {
do = (): Promise<BackupCreateResponse> => {
this.validate();
if (this.errors.length > 0) {
return Promise.reject(new Error('invalid usage: ' + this.errors.join(', ')));
Expand All @@ -77,20 +78,20 @@ export default class BackupCreator extends CommandBase {
config: {},
include: this.includeClassNames,
exclude: this.excludeClassNames,
};
} as BackupCreateRequest;

if (this.waitForCompletion) {
return this._createAndWaitForCompletion(payload);
}
return this._create(payload);
}
};

_create(payload: any) {
return this.client.post(this._path(), payload);
}
_create = (payload: BackupCreateRequest): Promise<BackupCreateResponse> => {
return this.client.post(this._path(), payload) as Promise<BackupCreateResponse>;
};

_createAndWaitForCompletion(payload: any) {
return new Promise((resolve, reject) => {
_createAndWaitForCompletion = (payload: BackupCreateRequest): Promise<BackupCreateResponse> => {
return new Promise<BackupCreateResponse>((resolve, reject) => {
this._create(payload)
.then((createResponse: any) => {
this.statusGetter.withBackend(this.backend!).withBackupId(this.backupId!);
Expand All @@ -115,14 +116,17 @@ export default class BackupCreator extends CommandBase {
})
.catch(reject);
});
}
};

_path() {
private _path = (): string => {
return `/backups/${this.backend}`;
}
};

_merge(createStatusResponse: any, createResponse: any) {
const merged: any = {};
_merge = (
createStatusResponse: BackupCreateStatusResponse,
createResponse: BackupCreateResponse
): BackupCreateResponse => {
const merged: BackupCreateResponse = {};
if ('id' in createStatusResponse) {
merged.id = createStatusResponse.id;
}
Expand All @@ -142,5 +146,5 @@ export default class BackupCreator extends CommandBase {
merged.classes = createResponse.classes;
}
return merged;
}
};
}
13 changes: 7 additions & 6 deletions src/backup/backupGetter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { validateBackend } from './validation';
import Connection from '../connection';
import { CommandBase } from '../validation/commandBase';
import { BackupCreateResponse } from '../types';

export default class BackupGetter extends CommandBase {
private backend?: string;
Expand All @@ -14,20 +15,20 @@ export default class BackupGetter extends CommandBase {
return this;
}

validate() {
validate = (): void => {
this.addErrors(validateBackend(this.backend));
}
};

do() {
do = (): Promise<BackupCreateResponse[] | Error> => {
this.validate();
if (this.errors.length > 0) {
return Promise.reject(new Error('invalid usage: ' + this.errors.join(', ')));
}

return this.client.get(this._path());
}
};

_path() {
private _path = (): string => {
return `/backups/${this.backend}`;
}
};
}
13 changes: 7 additions & 6 deletions src/backup/backupRestoreStatusGetter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { validateBackupId, validateBackend } from './validation';
import Connection from '../connection';
import { CommandBase } from '../validation/commandBase';
import { BackupRestoreStatusResponse } from '../types';

export default class BackupRestoreStatusGetter extends CommandBase {
private backend?: string;
Expand All @@ -20,20 +21,20 @@ export default class BackupRestoreStatusGetter extends CommandBase {
return this;
}

validate() {
validate = (): void => {
this.addErrors([...validateBackend(this.backend), ...validateBackupId(this.backupId)]);
}
};

do() {
do = (): Promise<BackupRestoreStatusResponse | Error> => {
this.validate();
if (this.errors.length > 0) {
return Promise.reject(new Error('invalid usage: ' + this.errors.join(', ')));
}

return this.client.get(this._path());
}
};

_path() {
private _path = (): string => {
return `/backups/${this.backend}/${this.backupId}/restore`;
}
};
}
34 changes: 19 additions & 15 deletions src/backup/backupRestorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import Connection from '../connection';
import BackupRestoreStatusGetter from './backupRestoreStatusGetter';
import { CommandBase } from '../validation/commandBase';
import { BackupRestoreRequest, BackupRestoreResponse, BackupRestoreStatusResponse } from '../types';

const WAIT_INTERVAL = 1000;

Expand Down Expand Up @@ -57,16 +58,16 @@ export default class BackupRestorer extends CommandBase {
return this;
}

validate() {
validate = (): void => {
this.addErrors([
...validateIncludeClassNames(this.includeClassNames || []),
...validateExcludeClassNames(this.excludeClassNames || []),
...validateBackend(this.backend),
...validateBackupId(this.backupId),
]);
}
};

do() {
do = (): Promise<BackupRestoreResponse> => {
this.validate();
if (this.errors.length > 0) {
return Promise.reject(new Error('invalid usage: ' + this.errors.join(', ')));
Expand All @@ -76,20 +77,20 @@ export default class BackupRestorer extends CommandBase {
config: {},
include: this.includeClassNames,
exclude: this.excludeClassNames,
};
} as BackupRestoreRequest;

if (this.waitForCompletion) {
return this._restoreAndWaitForCompletion(payload);
}
return this._restore(payload);
}
};

_restore(payload: any) {
_restore = (payload: BackupRestoreRequest): Promise<BackupRestoreResponse> => {
return this.client.post(this._path(), payload);
}
};

_restoreAndWaitForCompletion(payload: any) {
return new Promise((resolve, reject) => {
_restoreAndWaitForCompletion = (payload: BackupRestoreRequest): Promise<BackupRestoreResponse> => {
return new Promise<BackupRestoreResponse>((resolve, reject) => {
this._restore(payload)
.then((restoreResponse: any) => {
this.statusGetter.withBackend(this.backend!).withBackupId(this.backupId!);
Expand All @@ -114,14 +115,17 @@ export default class BackupRestorer extends CommandBase {
})
.catch(reject);
});
}
};

_path() {
private _path = (): string => {
return `/backups/${this.backend}/${this.backupId}/restore`;
}
};

_merge(restoreStatusResponse: any, restoreResponse: any) {
const merged: any = {};
_merge = (
restoreStatusResponse: BackupRestoreStatusResponse,
restoreResponse: BackupRestoreResponse
): BackupRestoreResponse => {
const merged: BackupRestoreResponse = {};
if ('id' in restoreStatusResponse) {
merged.id = restoreStatusResponse.id;
}
Expand All @@ -141,5 +145,5 @@ export default class BackupRestorer extends CommandBase {
merged.classes = restoreResponse.classes;
}
return merged;
}
};
}
3 changes: 2 additions & 1 deletion src/backup/journey.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import weaviate, { WeaviateClient } from '../index';
import { BackupCreateResponse } from '../types';

const {
createTestFoodSchemaAndData,
Expand Down Expand Up @@ -30,7 +31,7 @@ describe('create and restore backup with waiting', () => {
.withBackupId(BACKUP_ID)
.withWaitForCompletion(true)
.do()
.then((createResponse: any) => {
.then((createResponse: BackupCreateResponse) => {
expect(createResponse.id).toBe(BACKUP_ID);
expect(createResponse.classes).toHaveLength(1);
expect(createResponse.classes).toContain(PIZZA_CLASS_NAME);
Expand Down
8 changes: 8 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@ export type WeaviateObjectList = definitions['ObjectsListResponse'];
export type Reference = definitions['SingleRef'];
export type WeaviateError = definitions['ErrorResponse'];
export type Properties = definitions['PropertySchema'];
export type DataObject = definitions['Object'];

export type BackupCreateRequest = definitions['BackupCreateRequest'];
export type BackupCreateResponse = definitions['BackupCreateResponse'];
export type BackupCreateStatusResponse = definitions['BackupCreateStatusResponse'];
export type BackupRestoreRequest = definitions['BackupRestoreRequest'];
export type BackupRestoreResponse = definitions['BackupRestoreResponse'];
export type BackupRestoreStatusResponse = definitions['BackupRestoreStatusResponse'];