From fc41174f247716200135cb70405c4c30fdfc3031 Mon Sep 17 00:00:00 2001 From: Marcin Antas Date: Tue, 28 Mar 2023 08:32:22 +0200 Subject: [PATCH 1/4] Update Backup types --- src/backup/backupCreateStatusGetter.ts | 2 +- src/backup/backupGetter.ts | 2 +- src/backup/backupRestoreStatusGetter.ts | 2 +- src/backup/journey.test.ts | 57 ++++++++++++++----------- src/types.ts | 2 +- 5 files changed, 35 insertions(+), 30 deletions(-) diff --git a/src/backup/backupCreateStatusGetter.ts b/src/backup/backupCreateStatusGetter.ts index 95d8692e..6a6e343d 100644 --- a/src/backup/backupCreateStatusGetter.ts +++ b/src/backup/backupCreateStatusGetter.ts @@ -25,7 +25,7 @@ export default class BackupCreateStatusGetter extends CommandBase { this.addErrors([...validateBackend(this.backend), ...validateBackupId(this.backupId)]); }; - do = (): Promise => { + do = (): Promise => { this.validate(); if (this.errors.length > 0) { return Promise.reject(new Error('invalid usage: ' + this.errors.join(', '))); diff --git a/src/backup/backupGetter.ts b/src/backup/backupGetter.ts index 71745acd..9b9fa6dd 100644 --- a/src/backup/backupGetter.ts +++ b/src/backup/backupGetter.ts @@ -19,7 +19,7 @@ export default class BackupGetter extends CommandBase { this.addErrors(validateBackend(this.backend)); }; - do = (): Promise => { + do = (): Promise => { this.validate(); if (this.errors.length > 0) { return Promise.reject(new Error('invalid usage: ' + this.errors.join(', '))); diff --git a/src/backup/backupRestoreStatusGetter.ts b/src/backup/backupRestoreStatusGetter.ts index 57a163c9..11cb46e2 100644 --- a/src/backup/backupRestoreStatusGetter.ts +++ b/src/backup/backupRestoreStatusGetter.ts @@ -25,7 +25,7 @@ export default class BackupRestoreStatusGetter extends CommandBase { this.addErrors([...validateBackend(this.backend), ...validateBackupId(this.backupId)]); }; - do = (): Promise => { + do = (): Promise => { this.validate(); if (this.errors.length > 0) { return Promise.reject(new Error('invalid usage: ' + this.errors.join(', '))); diff --git a/src/backup/journey.test.ts b/src/backup/journey.test.ts index 1f2dab8b..f25f9571 100644 --- a/src/backup/journey.test.ts +++ b/src/backup/journey.test.ts @@ -1,5 +1,10 @@ import weaviate, { WeaviateClient } from '../index'; -import { BackupCreateResponse } from '../types'; +import { + BackupCreateResponse, + BackupCreateStatusResponse, + BackupRestoreResponse, + BackupRestoreStatusResponse, +} from '../types'; const { createTestFoodSchemaAndData, @@ -53,14 +58,14 @@ describe('create and restore backup with waiting', () => { .withBackend(BACKEND) .withBackupId(BACKUP_ID) .do() - .then((createStatusResponse: any) => { + .then((createStatusResponse: BackupCreateStatusResponse) => { expect(createStatusResponse.id).toBe(BACKUP_ID); expect(createStatusResponse.path).toBe(`${DOCKER_COMPOSE_BACKUPS_DIR}/${BACKUP_ID}`); expect(createStatusResponse.backend).toBe(BACKEND); expect(createStatusResponse.status).toBe(weaviate.backup.CreateStatus.SUCCESS); expect(createStatusResponse.error).toBeUndefined(); }) - .catch((err: any) => { + .catch((err: Error) => { throw new Error('should not fail on create status: ' + err); }); }); @@ -83,7 +88,7 @@ describe('create and restore backup with waiting', () => { .withBackupId(BACKUP_ID) .withWaitForCompletion(true) .do() - .then((restoreResponse: any) => { + .then((restoreResponse: BackupRestoreResponse) => { expect(restoreResponse.id).toBe(BACKUP_ID); expect(restoreResponse.classes).toHaveLength(1); expect(restoreResponse.classes).toContain(PIZZA_CLASS_NAME); @@ -92,7 +97,7 @@ describe('create and restore backup with waiting', () => { expect(restoreResponse.status).toBe(weaviate.backup.RestoreStatus.SUCCESS); expect(restoreResponse.error).toBeUndefined(); }) - .catch((err: any) => { + .catch((err: Error) => { throw new Error('should not fail on restore backup: ' + err); }); }); @@ -105,14 +110,14 @@ describe('create and restore backup with waiting', () => { .withBackend(BACKEND) .withBackupId(BACKUP_ID) .do() - .then((restoreStatusResponse: any) => { + .then((restoreStatusResponse: BackupRestoreStatusResponse) => { expect(restoreStatusResponse.id).toBe(BACKUP_ID); expect(restoreStatusResponse.path).toBe(`${DOCKER_COMPOSE_BACKUPS_DIR}/${BACKUP_ID}`); expect(restoreStatusResponse.backend).toBe(BACKEND); expect(restoreStatusResponse.status).toBe(weaviate.backup.RestoreStatus.SUCCESS); expect(restoreStatusResponse.error).toBeUndefined(); }) - .catch((err: any) => { + .catch((err: Error) => { throw new Error('should not fail on restore status: ' + err); }); }); @@ -140,7 +145,7 @@ describe('create and restore backup without waiting', () => { .withBackend(BACKEND) .withBackupId(BACKUP_ID) .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); @@ -149,7 +154,7 @@ describe('create and restore backup without waiting', () => { expect(createResponse.status).toBe(weaviate.backup.CreateStatus.STARTED); expect(createResponse.error).toBeUndefined(); }) - .catch((err: any) => { + .catch((err: Error) => { throw new Error('should not fail on create backup: ' + err); }); }); @@ -160,7 +165,7 @@ describe('create and restore backup without waiting', () => { const loop = () => { statusGetter .do() - .then((createStatusResponse: any) => { + .then((createStatusResponse: BackupCreateStatusResponse) => { if ( createStatusResponse.status == weaviate.backup.CreateStatus.SUCCESS || createStatusResponse.status == weaviate.backup.CreateStatus.FAILED @@ -205,7 +210,7 @@ describe('create and restore backup without waiting', () => { .withBackend(BACKEND) .withBackupId(BACKUP_ID) .do() - .then((restoreResponse: any) => { + .then((restoreResponse: BackupRestoreResponse) => { expect(restoreResponse.id).toBe(BACKUP_ID); expect(restoreResponse.classes).toHaveLength(1); expect(restoreResponse.classes).toContain(PIZZA_CLASS_NAME); @@ -214,7 +219,7 @@ describe('create and restore backup without waiting', () => { expect(restoreResponse.status).toBe(weaviate.backup.RestoreStatus.STARTED); expect(restoreResponse.error).toBeUndefined(); }) - .catch((err: any) => { + .catch((err: Error) => { throw new Error('should not fail on restore backup: ' + err); }); }); @@ -225,7 +230,7 @@ describe('create and restore backup without waiting', () => { const loop = () => { statusGetter .do() - .then((restoreStatusResponse: any) => { + .then((restoreStatusResponse: BackupRestoreStatusResponse) => { if ( restoreStatusResponse.status == weaviate.backup.RestoreStatus.SUCCESS || restoreStatusResponse.status == weaviate.backup.RestoreStatus.FAILED @@ -277,7 +282,7 @@ describe('create and restore 1 of 2 classes', () => { .withBackupId(BACKUP_ID) .withWaitForCompletion(true) .do() - .then((createResponse: any) => { + .then((createResponse: BackupCreateResponse) => { expect(createResponse.id).toBe(BACKUP_ID); expect(createResponse.classes).toHaveLength(2); expect(createResponse.classes).toContain(PIZZA_CLASS_NAME); @@ -287,7 +292,7 @@ describe('create and restore 1 of 2 classes', () => { expect(createResponse.status).toBe(weaviate.backup.CreateStatus.SUCCESS); expect(createResponse.error).toBeUndefined(); }) - .catch((err: any) => { + .catch((err: Error) => { throw new Error('should not fail on create backup: ' + err); }); }); @@ -301,14 +306,14 @@ describe('create and restore 1 of 2 classes', () => { .withBackend(BACKEND) .withBackupId(BACKUP_ID) .do() - .then((createStatusResponse: any) => { + .then((createStatusResponse: BackupCreateStatusResponse) => { expect(createStatusResponse.id).toBe(BACKUP_ID); expect(createStatusResponse.path).toBe(`${DOCKER_COMPOSE_BACKUPS_DIR}/${BACKUP_ID}`); expect(createStatusResponse.backend).toBe(BACKEND); expect(createStatusResponse.status).toBe(weaviate.backup.CreateStatus.SUCCESS); expect(createStatusResponse.error).toBeUndefined(); }) - .catch((err: any) => { + .catch((err: Error) => { throw new Error('should not fail on create status: ' + err); }); }); @@ -331,7 +336,7 @@ describe('create and restore 1 of 2 classes', () => { .withBackupId(BACKUP_ID) .withWaitForCompletion(true) .do() - .then((restoreResponse: any) => { + .then((restoreResponse: BackupRestoreResponse) => { expect(restoreResponse.id).toBe(BACKUP_ID); expect(restoreResponse.classes).toHaveLength(1); expect(restoreResponse.classes).toContain(PIZZA_CLASS_NAME); @@ -354,14 +359,14 @@ describe('create and restore 1 of 2 classes', () => { .withBackend(BACKEND) .withBackupId(BACKUP_ID) .do() - .then((restoreStatusResponse: any) => { + .then((restoreStatusResponse: BackupRestoreStatusResponse) => { expect(restoreStatusResponse.id).toBe(BACKUP_ID); expect(restoreStatusResponse.path).toBe(`${DOCKER_COMPOSE_BACKUPS_DIR}/${BACKUP_ID}`); expect(restoreStatusResponse.backend).toBe(BACKEND); expect(restoreStatusResponse.status).toBe(weaviate.backup.RestoreStatus.SUCCESS); expect(restoreStatusResponse.error).toBeUndefined(); }) - .catch((err: any) => { + .catch((err: Error) => { throw new Error('should not fail on restore status: ' + err); }); }); @@ -513,7 +518,7 @@ describe('fail restoring backup for existing class', () => { .withBackupId(BACKUP_ID) .withWaitForCompletion(true) .do() - .catch((err: any) => { + .catch((err: Error) => { throw new Error('should not fail on create backup: ' + err); }); }); @@ -526,7 +531,7 @@ describe('fail restoring backup for existing class', () => { .withBackupId(BACKUP_ID) .withWaitForCompletion(true) .do() - .then((resp: any) => { + .then((resp: BackupRestoreResponse) => { expect(resp.error).toContain('already exists'); expect(resp.error).toContain(PIZZA_CLASS_NAME); expect(resp.status).toBe(weaviate.backup.RestoreStatus.FAILED); @@ -555,7 +560,7 @@ describe('fail creating existing backup', () => { .withBackupId(BACKUP_ID) .withWaitForCompletion(true) .do() - .catch((err: any) => { + .catch((err: Error) => { throw new Error('should not fail on create backup: ' + err); }); }); @@ -657,7 +662,7 @@ describe('fail checking restore status for not started restore', () => { .withBackupId(BACKUP_ID) .withWaitForCompletion(true) .do() - .catch((err: any) => { + .catch((err: Error) => { throw new Error('should not fail on create backup: ' + err); }); }); @@ -732,7 +737,7 @@ describe('fail restoring backup for both include and exclude classes', () => { .withBackupId(BACKUP_ID) .withWaitForCompletion(true) .do() - .catch((err: any) => { + .catch((err: Error) => { throw new Error('should not fail on create backup: ' + err); }); }); @@ -742,7 +747,7 @@ describe('fail restoring backup for both include and exclude classes', () => { .classDeleter() .withClassName(PIZZA_CLASS_NAME) .do() - .catch((err: any) => { + .catch((err: Error) => { throw new Error('should not fail on class delete: ' + err); }); }); diff --git a/src/types.ts b/src/types.ts index e96cc072..62e98402 100644 --- a/src/types.ts +++ b/src/types.ts @@ -6,7 +6,7 @@ export type Reference = definitions['SingleRef']; export type WeaviateError = definitions['ErrorResponse']; export type Properties = definitions['PropertySchema']; export type DataObject = definitions['Object']; - +// Backup export type BackupCreateRequest = definitions['BackupCreateRequest']; export type BackupCreateResponse = definitions['BackupCreateResponse']; export type BackupCreateStatusResponse = definitions['BackupCreateStatusResponse']; From 2a91c980a12fc11e3e7d2a0c98de1e94c9338b2a Mon Sep 17 00:00:00 2001 From: Marcin Antas Date: Tue, 28 Mar 2023 08:49:42 +0200 Subject: [PATCH 2/4] Add C11y types --- src/c11y/conceptsGetter.ts | 5 +++-- src/c11y/extensionCreator.ts | 9 +++++---- src/c11y/journey.test.ts | 7 ++++--- src/types.ts | 3 +++ 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/c11y/conceptsGetter.ts b/src/c11y/conceptsGetter.ts index 74e6b2e5..f2971575 100644 --- a/src/c11y/conceptsGetter.ts +++ b/src/c11y/conceptsGetter.ts @@ -1,5 +1,6 @@ import Connection from '../connection'; import { CommandBase } from '../validation/commandBase'; +import { C11yWordsResponse } from '../types'; export default class ConceptsGetter extends CommandBase { private concept?: string; @@ -19,11 +20,11 @@ export default class ConceptsGetter extends CommandBase { return this; }; - validate = () => { + validate = (): void => { this.validateIsSet(this.concept, 'concept', 'withConcept(concept)'); }; - do = () => { + do = (): Promise => { this.validate(); if (this.errors.length > 0) { return Promise.reject(new Error('invalid usage: ' + this.errors.join(', '))); diff --git a/src/c11y/extensionCreator.ts b/src/c11y/extensionCreator.ts index 2c058996..22855e8c 100644 --- a/src/c11y/extensionCreator.ts +++ b/src/c11y/extensionCreator.ts @@ -1,5 +1,6 @@ import Connection from '../connection'; import { CommandBase } from '../validation/commandBase'; +import { C11yExtension } from '../types'; export default class ExtensionCreator extends CommandBase { private concept?: string; @@ -25,25 +26,25 @@ export default class ExtensionCreator extends CommandBase { return this; }; - validateIsSet = (prop: string | undefined | null, name: string, setter: string) => { + validateIsSet = (prop: string | undefined | null, name: string, setter: string): void => { if (prop == undefined || prop == null || prop.length == 0) { this.addError(`${name} must be set - set with ${setter}`); } }; - validate = () => { + validate = (): void => { this.validateIsSet(this.concept, 'concept', 'withConcept(concept)'); this.validateIsSet(this.definition, 'definition', 'withDefinition(definition)'); this.validateIsSet(this.weight?.toString() || '', 'weight', 'withWeight(weight)'); }; - payload = () => ({ + payload = (): C11yExtension => ({ concept: this.concept, definition: this.definition, weight: this.weight, }); - do = () => { + do = (): Promise => { this.validate(); if (this.errors.length > 0) { return Promise.reject(new Error('invalid usage: ' + this.errors.join(', '))); diff --git a/src/c11y/journey.test.ts b/src/c11y/journey.test.ts index 46b9d0bc..37164b38 100644 --- a/src/c11y/journey.test.ts +++ b/src/c11y/journey.test.ts @@ -1,4 +1,5 @@ import weaviate from '../index'; +import { C11yWordsResponse, C11yExtension } from '../types'; describe('c11y endpoints', () => { const client = weaviate.client({ @@ -11,8 +12,8 @@ describe('c11y endpoints', () => { .conceptsGetter() .withConcept('car') .do() - .then((res: any) => { - expect(res.individualWords[0].word).toEqual('car'); + .then((res: C11yWordsResponse) => { + expect(res.individualWords![0].word!).toEqual('car'); }); }); @@ -26,7 +27,7 @@ describe('c11y endpoints', () => { ) .withWeight(1) .do() - .then((res: any) => { + .then((res: C11yExtension) => { expect(res).toEqual({ concept: 'clientalmostdonehappyness', definition: diff --git a/src/types.ts b/src/types.ts index 62e98402..6f079ec3 100644 --- a/src/types.ts +++ b/src/types.ts @@ -13,3 +13,6 @@ export type BackupCreateStatusResponse = definitions['BackupCreateStatusResponse export type BackupRestoreRequest = definitions['BackupRestoreRequest']; export type BackupRestoreResponse = definitions['BackupRestoreResponse']; export type BackupRestoreStatusResponse = definitions['BackupRestoreStatusResponse']; +// C11y +export type C11yWordsResponse = definitions['C11yWordsResponse']; +export type C11yExtension = definitions['C11yExtension']; From 90c6efae77eff6ac06bd0eec55e72f6f2a171252 Mon Sep 17 00:00:00 2001 From: Marcin Antas Date: Tue, 28 Mar 2023 08:57:32 +0200 Subject: [PATCH 3/4] Add Classifications types --- .../contextual.journey.test.ts | 5 +++-- src/classifications/getter.ts | 9 +++++---- src/classifications/knn.journey.test.ts | 15 ++++++++------- src/classifications/scheduler.ts | 19 ++++++++++--------- src/types.ts | 2 ++ 5 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/classifications/contextual.journey.test.ts b/src/classifications/contextual.journey.test.ts index eb936d2c..85b74d8b 100644 --- a/src/classifications/contextual.journey.test.ts +++ b/src/classifications/contextual.journey.test.ts @@ -1,4 +1,5 @@ import weaviate from '../index'; +import { Classification } from '../types'; const targetDessertId = '9f399d3e-45a4-44f4-b0fd-fa291abfb211'; const targetSavoryId = 'b7a64fbd-7c22-44ac-afbb-8d1432b8061b'; @@ -32,12 +33,12 @@ describe('a classification journey', () => { .withWaitForCompletion() .withWaitTimeout(60 * 1000) .do() - .then((res: any) => { + .then((res: Classification) => { expect(res.status).toEqual('completed'); expect(res.type).toEqual('text2vec-contextionary-contextual'); id = res.id; }) - .catch((e: any) => { + .catch((e: Error) => { throw new Error('it should not have errord: ' + e); }); }, diff --git a/src/classifications/getter.ts b/src/classifications/getter.ts index fc2f8805..7bb4b0c2 100644 --- a/src/classifications/getter.ts +++ b/src/classifications/getter.ts @@ -1,5 +1,6 @@ import Connection from '../connection'; import { CommandBase } from '../validation/commandBase'; +import { Classification } from '../types'; export default class Getter extends CommandBase { private id?: string; @@ -13,21 +14,21 @@ export default class Getter extends CommandBase { return this; }; - validateIsSet = (prop: string | undefined | null, name: string, setter: string) => { + validateIsSet = (prop: string | undefined | null, name: string, setter: string): void => { if (prop == undefined || prop == null || prop.length == 0) { this.addError(`${name} must be set - set with ${setter}`); } }; - validateId = () => { + validateId = (): void => { this.validateIsSet(this.id, 'id', '.withId(id)'); }; - validate = () => { + validate = (): void => { this.validateId(); }; - do = () => { + do = (): Promise => { this.validate(); if (this.errors.length > 0) { return Promise.reject(new Error('invalid usage: ' + this.errors.join(', '))); diff --git a/src/classifications/knn.journey.test.ts b/src/classifications/knn.journey.test.ts index 2b923298..7aa76ee5 100644 --- a/src/classifications/knn.journey.test.ts +++ b/src/classifications/knn.journey.test.ts @@ -1,5 +1,6 @@ import weaviate, { WeaviateClient } from '../index'; import Connection from '../connection'; +import { Classification } from '../types'; const targetDessertId = 'cd54852a-209d-423b-bf1c-884468215237'; const targetSavoryId = 'e5da0127-327e-4184-85b8-7b9d1af4a850'; @@ -26,7 +27,7 @@ describe('a classification journey', () => { .withClassifyProperties(['toTarget']) .withBasedOnProperties(['description']) .do() - .then((res: any) => { + .then((res: Classification) => { expect(res.id).toBeDefined(); id = res.id; }) @@ -40,7 +41,7 @@ describe('a classification journey', () => { .getter() .withId(id) .do() - .then((res: any) => { + .then((res: Classification) => { expect(res.status).toEqual('completed'); }); }); @@ -55,7 +56,7 @@ describe('a classification journey', () => { .getter() .withId(id) .do() - .then((res: any) => { + .then((res: Classification) => { clearInterval(backgroundWork); clearTimeout(timeout); if (res.status === 'completed') resolve(undefined); @@ -122,11 +123,11 @@ describe('a classification journey', () => { .withWaitForCompletion() .withWaitTimeout(60 * 1000) .do() - .then((res: any) => { + .then((res: Classification) => { expect(res.status).toEqual('completed'); id = res.id; }) - .catch((e: any) => { + .catch((e: Error) => { throw new Error('it should not have errord: ' + e); }); }); @@ -184,10 +185,10 @@ describe('a classification journey', () => { .withWaitForCompletion() .withWaitTimeout(1) // that's going to be difficult ;-) .do() - .then((res: any) => { + .then((res: Classification) => { fail('it should have errord'); }) - .catch((e: any) => { + .catch((e: Error) => { expect(e).toEqual( new Error( "classification didn't finish within configured timeout, " + diff --git a/src/classifications/scheduler.ts b/src/classifications/scheduler.ts index dd328bcd..f47456ef 100644 --- a/src/classifications/scheduler.ts +++ b/src/classifications/scheduler.ts @@ -1,6 +1,7 @@ import Getter from './getter'; import Connection from '../connection'; import { CommandBase } from '../validation/commandBase'; +import { Classification } from '../types'; export default class Scheduler extends CommandBase { private basedOnProperties?: string[]; @@ -52,17 +53,17 @@ export default class Scheduler extends CommandBase { return this; }; - validateIsSet = (prop: string | undefined | null | any[], name: string, setter: string) => { + validateIsSet = (prop: string | undefined | null | any[], name: string, setter: string): void => { if (prop == undefined || prop == null || prop.length == 0) { this.addError(`${name} must be set - set with ${setter}`); } }; - validateClassName = () => { + validateClassName = (): void => { this.validateIsSet(this.className, 'className', '.withClassName(className)'); }; - validateBasedOnProperties = () => { + validateBasedOnProperties = (): void => { this.validateIsSet( this.basedOnProperties, 'basedOnProperties', @@ -70,7 +71,7 @@ export default class Scheduler extends CommandBase { ); }; - validateClassifyProperties = () => { + validateClassifyProperties = (): void => { this.validateIsSet( this.classifyProperties, 'classifyProperties', @@ -78,13 +79,13 @@ export default class Scheduler extends CommandBase { ); }; - validate = () => { + validate = (): void => { this.validateClassName(); this.validateClassifyProperties(); this.validateBasedOnProperties(); }; - payload = () => ({ + payload = (): Classification => ({ type: this.type, settings: this.settings, class: this.className, @@ -92,7 +93,7 @@ export default class Scheduler extends CommandBase { basedOnProperties: this.basedOnProperties, }); - pollForCompletion = (id: any) => { + pollForCompletion = (id: any): Promise => { return new Promise((resolve, reject) => { setTimeout( () => @@ -109,14 +110,14 @@ export default class Scheduler extends CommandBase { new Getter(this.client) .withId(id) .do() - .then((res: any) => { + .then((res: Classification) => { if (res.status === 'completed') resolve(res); }); }, 500); }); }; - do = () => { + do = (): Promise => { if (this.errors.length > 0) { return Promise.reject(new Error('invalid usage: ' + this.errors.join(', '))); } diff --git a/src/types.ts b/src/types.ts index 6f079ec3..11ca2b73 100644 --- a/src/types.ts +++ b/src/types.ts @@ -16,3 +16,5 @@ export type BackupRestoreStatusResponse = definitions['BackupRestoreStatusRespon // C11y export type C11yWordsResponse = definitions['C11yWordsResponse']; export type C11yExtension = definitions['C11yExtension']; +// Classifications +export type Classification = definitions['Classification']; From 40d7f5a30b791cebefcf2b5328cdc2b1b075af36 Mon Sep 17 00:00:00 2001 From: Marcin Antas Date: Tue, 28 Mar 2023 09:49:31 +0200 Subject: [PATCH 4/4] Add Batch types --- src/batch/journey.test.ts | 34 ++++++++++++++++------------ src/batch/objectsBatchDeleter.ts | 21 +++++++++-------- src/batch/objectsBatcher.ts | 19 ++++++++-------- src/batch/path.ts | 6 ++--- src/batch/referencePayloadBuilder.ts | 11 +++++---- src/batch/referencesBatcher.ts | 23 ++++++++++--------- src/types.ts | 10 ++++++++ src/utils/testData.ts | 11 ++++++--- 8 files changed, 79 insertions(+), 56 deletions(-) diff --git a/src/batch/journey.test.ts b/src/batch/journey.test.ts index 7ef77054..728c17d9 100644 --- a/src/batch/journey.test.ts +++ b/src/batch/journey.test.ts @@ -1,4 +1,5 @@ import weaviate, { WeaviateClient } from '../index'; +import { BatchReference, BatchReferenceResponse, WeaviateObject } from '../types'; const thingClassName = 'BatchJourneyTestThing'; const otherThingClassName = 'BatchJourneyTestOtherThing'; @@ -12,7 +13,7 @@ const thingIds = [ const otherThingIds = ['5b354a0f-fe66-4fe7-ad62-4db72ddab815', '8727fa2b-610a-4a5c-af26-e558943f71c7']; -const someObjects = [ +const someObjects: WeaviateObject[] = [ { class: thingClassName, id: thingIds[0], @@ -76,10 +77,10 @@ describe('batch importing', () => { .withObject(someObjects[0]) .withObjects(someObjects[1]) .withObjects(someObjects[2], someObjects[3]) - .withObjects([someObjects[4], someObjects[5]]); + .withObjects(...[someObjects[4], someObjects[5]]); expect(batcher.objects).toHaveLength(someObjects.length); - batcher.objects.forEach((obj: any, i: number) => { + batcher.objects.forEach((obj: WeaviateObject, i: number) => { expect(obj.class).toBe(someObjects[i].class); expect(obj.id).toBe(someObjects[i].id); }); @@ -109,7 +110,7 @@ describe('batch importing', () => { .withObject(toImport[1]) .do() .then() - .catch((e: any) => { + .catch((e: Error) => { throw new Error('it should not have errord ' + e); }); }); @@ -122,7 +123,7 @@ describe('batch importing', () => { return Promise.all([ client.data.getterById().withId(thingIds[0]).withClassName(thingClassName).do(), client.data.getterById().withId(thingIds[1]).withClassName(thingClassName).do(), - ]).catch((e: any) => { + ]).catch((e: Error) => { throw new Error('it should not have errord ' + e); }); }); @@ -189,7 +190,7 @@ describe('batch importing', () => { client.batch .objectsBatcher() .withConsistencyLevel(weaviate.replication.ConsistencyLevel.ONE) - .withObjects([toImport[0], toImport[1]]) + .withObjects(...[toImport[0], toImport[1]]) .do() .then() .catch((e: any) => { @@ -218,10 +219,10 @@ describe('batch importing', () => { .referencesBatcher() .withReference(someReferences[0]) .withReferences(someReferences[1], someReferences[2]) - .withReferences([someReferences[3]]); + .withReferences(...[someReferences[3]]); expect(batcher.references).toHaveLength(someReferences.length); - batcher.references.forEach((ref: any, i: number) => { + batcher.references.forEach((ref: BatchReference, i: number) => { expect(ref.from).toBe(someReferences[i].from); expect(ref.to).toBe(someReferences[i].to); }); @@ -240,9 +241,9 @@ describe('batch importing', () => { }) .withConsistencyLevel(weaviate.replication.ConsistencyLevel.ALL) .do() - .then((res: any) => { - res.forEach((elem: any) => { - expect(elem.result.errors).toBeUndefined(); + .then((res: BatchReferenceResponse[]) => { + res.forEach((elem: BatchReferenceResponse) => { + expect(elem.result!.errors).toBeUndefined(); }); }) .catch((e: any) => { @@ -271,9 +272,9 @@ describe('batch importing', () => { .referencesBatcher() .withReferences(reference1, reference2) .do() - .then((res: any[]) => { - res.forEach((elem: any) => { - expect(elem.result.errors).toBeUndefined(); + .then((res: BatchReferenceResponse[]) => { + res.forEach((elem: BatchReferenceResponse) => { + expect(elem.result!.errors).toBeUndefined(); }); }) .catch((e: any) => { @@ -546,7 +547,10 @@ const setup = async (client: WeaviateClient) => { }; const setupData = (client: WeaviateClient) => { - return client.batch.objectsBatcher().withObjects(someObjects).do(); + return client.batch + .objectsBatcher() + .withObjects(...someObjects) + .do(); }; const cleanup = (client: WeaviateClient) => diff --git a/src/batch/objectsBatchDeleter.ts b/src/batch/objectsBatchDeleter.ts index 24a4ac4c..8b474da4 100644 --- a/src/batch/objectsBatchDeleter.ts +++ b/src/batch/objectsBatchDeleter.ts @@ -2,6 +2,7 @@ import { isValidStringProperty } from '../validation/string'; import { buildObjectsPath } from './path'; import Connection from '../connection'; import { CommandBase } from '../validation/commandBase'; +import { BatchDelete, BatchDeleteResponse } from '../types'; export default class ObjectsBatchDeleter extends CommandBase { private className?: string; @@ -39,7 +40,7 @@ export default class ObjectsBatchDeleter extends CommandBase { return this; }; - payload() { + payload = (): BatchDelete => { return { match: { class: this.className, @@ -48,26 +49,26 @@ export default class ObjectsBatchDeleter extends CommandBase { output: this.output, dryRun: this.dryRun, }; - } + }; - validateClassName() { + validateClassName = (): void => { if (!isValidStringProperty(this.className)) { this.addError('string className must be set - set with .withClassName(className)'); } - } + }; - validateWhereFilter() { + validateWhereFilter = (): void => { if (typeof this.whereFilter != 'object') { this.addError('object where must be set - set with .withWhere(whereFilter)'); } - } + }; - validate() { + validate = (): void => { this.validateClassName(); this.validateWhereFilter(); - } + }; - do() { + do = (): Promise => { this.validate(); if (this.errors.length > 0) { return Promise.reject(new Error('invalid usage: ' + this.errors.join(', '))); @@ -78,5 +79,5 @@ export default class ObjectsBatchDeleter extends CommandBase { } const path = buildObjectsPath(params); return this.client.delete(path, this.payload(), true); - } + }; } diff --git a/src/batch/objectsBatcher.ts b/src/batch/objectsBatcher.ts index 751b6cec..c8dea149 100644 --- a/src/batch/objectsBatcher.ts +++ b/src/batch/objectsBatcher.ts @@ -1,10 +1,11 @@ import { buildObjectsPath } from './path'; import Connection from '../connection'; import { CommandBase } from '../validation/commandBase'; +import { BatchRequest, WeaviateObject, WeaviateObjectsGet } from '../types'; export default class ObjectsBatcher extends CommandBase { private consistencyLevel?: string; - public objects: any[]; + public objects: WeaviateObject[]; constructor(client: Connection) { super(client); @@ -13,12 +14,12 @@ export default class ObjectsBatcher extends CommandBase { /** * can be called as: - * - withObjects([obj1, obj2, obj3]) + * - withObjects(...[obj1, obj2, obj3]) * - withObjects(obj1, obj2, obj3) * - withObjects(obj1) - * @param {...any} objects + * @param {...WeaviateObject[]} objects */ - withObjects(...objects: any) { + withObjects(...objects: WeaviateObject[]) { let objs = objects; if (objects.length && Array.isArray(objects[0])) { objs = objects[0]; @@ -27,7 +28,7 @@ export default class ObjectsBatcher extends CommandBase { return this; } - withObject(object: any) { + withObject(object: WeaviateObject) { return this.withObjects(object); } @@ -36,21 +37,21 @@ export default class ObjectsBatcher extends CommandBase { return this; }; - payload = () => ({ + payload = (): BatchRequest => ({ objects: this.objects, }); - validateObjectCount = () => { + validateObjectCount = (): void => { if (this.objects.length == 0) { this.addError('need at least one object to send a request, add one with .withObject(obj)'); } }; - validate = () => { + validate = (): void => { this.validateObjectCount(); }; - do = () => { + do = (): Promise => { this.validate(); if (this.errors.length > 0) { return Promise.reject(new Error('invalid usage: ' + this.errors.join(', '))); diff --git a/src/batch/path.ts b/src/batch/path.ts index 2555896f..ac2809f2 100644 --- a/src/batch/path.ts +++ b/src/batch/path.ts @@ -1,14 +1,14 @@ -export function buildObjectsPath(queryParams: any) { +export function buildObjectsPath(queryParams: any): string { const path = '/batch/objects'; return buildPath(path, queryParams); } -export function buildRefsPath(queryParams: any) { +export function buildRefsPath(queryParams: any): string { const path = '/batch/references'; return buildPath(path, queryParams); } -function buildPath(path: string, queryParams: any) { +function buildPath(path: string, queryParams: any): string { if (queryParams && queryParams.toString() != '') { path = `${path}?${queryParams.toString()}`; } diff --git a/src/batch/referencePayloadBuilder.ts b/src/batch/referencePayloadBuilder.ts index adc9b015..2897b29b 100644 --- a/src/batch/referencePayloadBuilder.ts +++ b/src/batch/referencePayloadBuilder.ts @@ -1,6 +1,7 @@ import { isValidStringProperty } from '../validation/string'; import Connection from '../connection'; import { CommandBase } from '../validation/commandBase'; +import { BatchReference } from '../types'; export default class ReferencesBatcher extends CommandBase { private fromClassName?: string; @@ -38,20 +39,20 @@ export default class ReferencesBatcher extends CommandBase { return this; } - validateIsSet = (prop: string | undefined | null, name: string, setter: string) => { + validateIsSet = (prop: string | undefined | null, name: string, setter: string): void => { if (prop == undefined || prop == null || prop.length == 0) { this.addError(`${name} must be set - set with ${setter}`); } }; - validate = () => { + validate = (): void => { this.validateIsSet(this.fromId, 'fromId', '.withFromId(id)'); this.validateIsSet(this.toId, 'toId', '.withToId(id)'); this.validateIsSet(this.fromClassName, 'fromClassName', '.withFromClassName(className)'); this.validateIsSet(this.fromRefProp, 'fromRefProp', '.withFromRefProp(refProp)'); }; - payload = () => { + payload = (): BatchReference => { this.validate(); if (this.errors.length > 0) { throw new Error(this.errors.join(', ')); @@ -68,7 +69,7 @@ export default class ReferencesBatcher extends CommandBase { }; }; - do(): Promise { + do = (): Promise => { return Promise.reject(new Error('Should never be called')); - } + }; } diff --git a/src/batch/referencesBatcher.ts b/src/batch/referencesBatcher.ts index 78ed5d72..b16d92ec 100644 --- a/src/batch/referencesBatcher.ts +++ b/src/batch/referencesBatcher.ts @@ -2,11 +2,12 @@ import { buildRefsPath } from './path'; import { BeaconPath } from '../utils/beaconPath'; import Connection from '../connection'; import { CommandBase } from '../validation/commandBase'; +import { BatchReference, BatchReferenceResponse } from '../types'; export default class ReferencesBatcher extends CommandBase { private beaconPath: BeaconPath; private consistencyLevel?: string; - public references: any[]; + public references: BatchReference[]; constructor(client: Connection, beaconPath: BeaconPath) { super(client); @@ -16,12 +17,12 @@ export default class ReferencesBatcher extends CommandBase { /** * can be called as: - * - withReferences([ref1, ref2, ref3]) + * - withReferences(...[ref1, ref2, ref3]) * - withReferences(ref1, ref2, ref3) * - withReferences(ref1) - * @param {...any} references + * @param {...BatchReference[]} references */ - withReferences(...references: any) { + withReferences(...references: BatchReference[]) { let refs = references; if (references.length && Array.isArray(references[0])) { refs = references[0]; @@ -30,7 +31,7 @@ export default class ReferencesBatcher extends CommandBase { return this; } - withReference(reference: any) { + withReference(reference: BatchReference) { return this.withReferences(reference); } @@ -39,9 +40,9 @@ export default class ReferencesBatcher extends CommandBase { return this; }; - payload = () => this.references; + payload = (): BatchReference[] => this.references; - validateReferenceCount = () => { + validateReferenceCount = (): void => { if (this.references.length == 0) { this.addError('need at least one reference to send a request, add one with .withReference(obj)'); } @@ -51,7 +52,7 @@ export default class ReferencesBatcher extends CommandBase { this.validateReferenceCount(); }; - do = () => { + do = (): Promise => { this.validate(); if (this.errors.length > 0) { return Promise.reject(new Error('invalid usage: ' + this.errors.join(', '))); @@ -66,10 +67,10 @@ export default class ReferencesBatcher extends CommandBase { return payloadPromise.then((payload) => this.client.post(path, payload)); }; - rebuildReferencePromise(reference: any) { - return this.beaconPath.rebuild(reference.to).then((beaconTo: any) => ({ + rebuildReferencePromise = (reference: BatchReference): Promise => { + return this.beaconPath.rebuild(reference.to!).then((beaconTo: any) => ({ from: reference.from, to: beaconTo, })); - } + }; } diff --git a/src/types.ts b/src/types.ts index 11ca2b73..3bd6514c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -2,6 +2,7 @@ import { definitions } from './schema'; export type WeaviateObject = definitions['Object']; export type WeaviateObjectList = definitions['ObjectsListResponse']; +export type WeaviateObjectsGet = definitions['ObjectsGetResponse']; export type Reference = definitions['SingleRef']; export type WeaviateError = definitions['ErrorResponse']; export type Properties = definitions['PropertySchema']; @@ -13,6 +14,15 @@ export type BackupCreateStatusResponse = definitions['BackupCreateStatusResponse export type BackupRestoreRequest = definitions['BackupRestoreRequest']; export type BackupRestoreResponse = definitions['BackupRestoreResponse']; export type BackupRestoreStatusResponse = definitions['BackupRestoreStatusResponse']; +// Batch +export type BatchDelete = definitions['BatchDelete']; +export type BatchDeleteResponse = definitions['BatchDeleteResponse']; +export type BatchRequest = { + fields?: ('ALL' | 'class' | 'schema' | 'id' | 'creationTimeUnix')[]; + objects?: WeaviateObject[]; +}; +export type BatchReference = definitions['BatchReference']; +export type BatchReferenceResponse = definitions['BatchReferenceResponse']; // C11y export type C11yWordsResponse = definitions['C11yWordsResponse']; export type C11yExtension = definitions['C11yExtension']; diff --git a/src/utils/testData.ts b/src/utils/testData.ts index d2d06f13..d40e86cc 100644 --- a/src/utils/testData.ts +++ b/src/utils/testData.ts @@ -1,4 +1,5 @@ import { WeaviateClient } from '../index'; +import { WeaviateObject } from '../types'; export const PIZZA_CLASS_NAME = 'Pizza'; export const SOUP_CLASS_NAME = 'Soup'; @@ -38,7 +39,7 @@ const soupClass = { properties: foodProperties, }; -const pizzaObjects = [ +const pizzaObjects: WeaviateObject[] = [ { class: PIZZA_CLASS_NAME, id: '10523cdd-15a2-42f4-81fa-267fe92f7cd6', @@ -79,7 +80,7 @@ const pizzaObjects = [ }, ]; -const soupObjects = [ +const soupObjects: WeaviateObject[] = [ { class: SOUP_CLASS_NAME, id: '8c156d37-81aa-4ce9-a811-621e2702b825', @@ -108,7 +109,11 @@ export function createTestFoodSchema(client: WeaviateClient) { } export function createTestFoodData(client: WeaviateClient) { - return client.batch.objectsBatcher().withObjects(pizzaObjects).withObjects(soupObjects).do(); + return client.batch + .objectsBatcher() + .withObjects(...pizzaObjects) + .withObjects(...soupObjects) + .do(); } export function createTestFoodSchemaAndData(client: WeaviateClient) {