Skip to content

Commit 2ff075c

Browse files
authored
Add method to check if class exists (#70)
1 parent 3e0840e commit 2ff075c

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

src/schema/classExists.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { isValidStringProperty } from '../validation/string';
2+
import Connection from '../connection';
3+
import { CommandBase } from '../validation/commandBase';
4+
import { WeaviateSchema } from '../openapi/types';
5+
6+
export default class ClassExists extends CommandBase {
7+
private className?: string;
8+
9+
constructor(client: Connection) {
10+
super(client);
11+
}
12+
13+
withClassName = (className: string) => {
14+
this.className = className;
15+
return this;
16+
};
17+
18+
validateClassName = () => {
19+
if (!isValidStringProperty(this.className)) {
20+
this.addError('className must be set - set with .withClassName(className)');
21+
}
22+
};
23+
24+
validate = () => {
25+
this.validateClassName();
26+
};
27+
28+
do = (): Promise<boolean> => {
29+
this.validate();
30+
if (this.errors.length > 0) {
31+
return Promise.reject(new Error('invalid usage: ' + this.errors.join(', ')));
32+
}
33+
const path = `/schema`;
34+
return this.client
35+
.get(path)
36+
.then((res: WeaviateSchema) => res.classes?.some((c) => c.class === this.className));
37+
};
38+
}

src/schema/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import ClassCreator from './classCreator';
22
import ClassDeleter from './classDeleter';
3+
import ClassExists from './classExists';
34
import ClassGetter from './classGetter';
45
import PropertyCreator from './propertyCreator';
56
import SchemaGetter from './getter';
@@ -12,6 +13,7 @@ export interface Schema {
1213
classCreator: () => ClassCreator;
1314
classDeleter: () => ClassDeleter;
1415
classGetter: () => ClassGetter;
16+
exists: (className: string) => Promise<boolean>;
1517
getter: () => SchemaGetter;
1618
propertyCreator: () => PropertyCreator;
1719
shardsGetter: () => ShardsGetter;
@@ -24,6 +26,7 @@ const schema = (client: Connection): Schema => {
2426
classCreator: () => new ClassCreator(client),
2527
classDeleter: () => new ClassDeleter(client),
2628
classGetter: () => new ClassGetter(client),
29+
exists: (className: string) => new ClassExists(client).withClassName(className).do(),
2730
getter: () => new SchemaGetter(client),
2831
propertyCreator: () => new PropertyCreator(client),
2932
shardsGetter: () => new ShardsGetter(client),

src/schema/journey.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ describe('schema', () => {
3030
});
3131
});
3232

33+
it('checks class existence', () => {
34+
return client.schema.exists(classObj.class).then((res) => expect(res).toEqual(true));
35+
});
36+
37+
it('checks class non-existence', () => {
38+
return client.schema.exists('NonExistingClass').then((res) => expect(res).toEqual(false));
39+
});
40+
3341
it('extends the thing class with a new property', () => {
3442
const className = 'MyThingClass';
3543
const prop: Property = {

0 commit comments

Comments
 (0)