Skip to content

Commit d4e1e00

Browse files
authored
Merge pull request #71 from semi-technologies/feature/WEAVIATE-139
Update /objects endpoints to include class name in path
2 parents bd7008e + 0a15f48 commit d4e1e00

File tree

10 files changed

+176
-17
lines changed

10 files changed

+176
-17
lines changed

data/checker.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { buildObjectsPath } from "./path";
2+
13
export default class Checker {
24
constructor(client) {
35
this.client = client;
@@ -9,6 +11,11 @@ export default class Checker {
911
return this;
1012
};
1113

14+
withClassName = (className) => {
15+
this.className = className;
16+
return this;
17+
};
18+
1219
validateIsSet = (prop, name, setter) => {
1320
if (prop == undefined || prop == null || prop.length == 0) {
1421
this.errors = [
@@ -34,7 +41,7 @@ export default class Checker {
3441
}
3542
this.validate();
3643

37-
const path = `/objects/${this.id}`;
44+
const path = buildObjectsPath(this.id, this.className);
3845
return this.client.head(path);
3946
};
4047
}

data/deleter.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { buildObjectsPath } from "./path";
2+
13
export default class Deleter {
24
constructor(client) {
35
this.client = client;
@@ -9,6 +11,11 @@ export default class Deleter {
911
return this;
1012
};
1113

14+
withClassName = (className) => {
15+
this.className = className;
16+
return this;
17+
}
18+
1219
validateIsSet = (prop, name, setter) => {
1320
if (prop == undefined || prop == null || prop.length == 0) {
1421
this.errors = [
@@ -34,7 +41,7 @@ export default class Deleter {
3441
}
3542
this.validate();
3643

37-
const path = `/objects/${this.id}`;
44+
const path = buildObjectsPath(this.id, this.className);
3845
return this.client.delete(path);
3946
};
4047
}

data/getterById.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { buildObjectsPath } from "./path";
2+
13
export default class GetterById {
24
constructor(client) {
35
this.client = client;
@@ -10,6 +12,11 @@ export default class GetterById {
1012
return this;
1113
};
1214

15+
withClassName = (className) => {
16+
this.className = className;
17+
return this;
18+
};
19+
1320
extendAdditionals = (prop) => {
1421
this.additionals = [...this.additionals, prop];
1522
return this;
@@ -45,7 +52,8 @@ export default class GetterById {
4552
);
4653
}
4754

48-
let path = `/objects/${this.id}`;
55+
let path = buildObjectsPath(this.id, this.className);
56+
4957
if (this.additionals.length > 0) {
5058
path += `?include=${this.additionals.join(",")}`;
5159
}

data/journey.test.js

Lines changed: 107 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ describe("data", () => {
162162
.catch((e) => fail("it should not have errord: " + e));
163163
});
164164

165-
it("gets one thing by id", () => {
165+
it("gets one thing by id only", () => {
166166
return client.data
167167
.getterById()
168168
.withId("1565c06c-463f-466c-9092-5930dbac3887")
@@ -178,6 +178,34 @@ describe("data", () => {
178178
.catch((e) => fail("it should not have errord: " + e));
179179
});
180180

181+
it("gets one thing by id and class name", () => {
182+
return client.data
183+
.getterById()
184+
.withClassName(thingClassName)
185+
.withId("1565c06c-463f-466c-9092-5930dbac3887")
186+
.do()
187+
.then((res) => {
188+
expect(res).toEqual(
189+
expect.objectContaining({
190+
id: "1565c06c-463f-466c-9092-5930dbac3887",
191+
properties: { stringProp: "with-id" },
192+
})
193+
);
194+
})
195+
.catch((e) => fail("it should not have errord: " + e));
196+
});
197+
198+
it("fails to get one thing by id with invalid class name", () => {
199+
return client.data
200+
.getterById()
201+
.withClassName("DoesNotExist")
202+
.withId("1565c06c-463f-466c-9092-5930dbac3887")
203+
.do()
204+
.catch(err =>
205+
expect(err).toEqual("usage error (500): {\"error\":[{\"message\":\"repo: object by id: index not found for class DoesNotExist\"}]}")
206+
);
207+
});
208+
181209
it("gets one thing by id with all optional additional props", () => {
182210
return client.data
183211
.getterById()
@@ -211,7 +239,7 @@ describe("data", () => {
211239
});
212240
});
213241

214-
it("updates a thing", () => {
242+
it("updates a thing by id only", () => {
215243
const id = "1565c06c-463f-466c-9092-5930dbac3887";
216244
return client.data
217245
.getterById()
@@ -236,6 +264,31 @@ describe("data", () => {
236264
.catch((e) => fail("it should not have errord: " + e));
237265
});
238266

267+
it("updates a thing by id and class name", () => {
268+
const id = "1565c06c-463f-466c-9092-5930dbac3887";
269+
return client.data
270+
.getterById()
271+
.withId(id)
272+
.withClassName(thingClassName)
273+
.do()
274+
.then((res) => {
275+
const properties = res.properties;
276+
properties.stringProp = "thing-updated-with-class-name";
277+
return client.data
278+
.updater()
279+
.withId(id)
280+
.withClassName(thingClassName)
281+
.withProperties(properties)
282+
.do();
283+
})
284+
.then((res) => {
285+
expect(res.properties).toEqual({
286+
stringProp: "thing-updated-with-class-name",
287+
});
288+
})
289+
.catch((e) => fail("it should not have errord: " + e));
290+
});
291+
239292
it("merges a thing", () => {
240293
const id = "1565c06c-463f-466c-9092-5930dbac3887";
241294
return client.data
@@ -301,7 +354,7 @@ describe("data", () => {
301354
.catch((e) => fail("it should not have errord: " + e));
302355
});
303356

304-
it("checks that object exists", () => {
357+
it("checks that object exists by id only", () => {
305358
return client.data
306359
.checker()
307360
.withId("1565c06c-463f-466c-9092-5930dbac3887")
@@ -314,15 +367,29 @@ describe("data", () => {
314367
.catch((e) => fail("it should not have errord: " + e));
315368
});
316369

317-
it("deletes a thing", () => {
370+
it("checks that object exists by id and class name", () => {
371+
return client.data
372+
.checker()
373+
.withId("1565c06c-463f-466c-9092-5930dbac3887")
374+
.withClassName(thingClassName)
375+
.do()
376+
.then((exists) => {
377+
if (!exists) {
378+
fail("it should exist in DB")
379+
}
380+
})
381+
.catch((e) => fail("it should not have errord: " + e));
382+
});
383+
384+
it("deletes a thing by id only", () => {
318385
return client.data
319386
.deleter()
320387
.withId("1565c06c-463f-466c-9092-5930dbac3887")
321388
.do()
322389
.catch((e) => fail("it should not have errord: " + e));
323390
});
324391

325-
it("checks that object doesn't exist anymore", () => {
392+
it("checks that object doesn't exist anymore with delete by id only", () => {
326393
return client.data
327394
.checker()
328395
.withId("1565c06c-463f-466c-9092-5930dbac3887")
@@ -335,11 +402,41 @@ describe("data", () => {
335402
.catch((e) => fail("it should not have errord: " + e));
336403
});
337404

338-
it("waits for es index updates", () => {
339-
return new Promise((resolve, reject) => {
340-
// TODO: remove in 1.0.0
341-
setTimeout(resolve, 1000);
342-
});
405+
it("deletes a thing with id and class name", async () => {
406+
const properties = { stringProp: "with-id" };
407+
const id = "6781a974-cfbf-455d-ace8-f1dba4564230";
408+
409+
await client.data
410+
.creator()
411+
.withClassName(thingClassName)
412+
.withProperties(properties)
413+
.withId(id)
414+
.do()
415+
.then((res) => {
416+
expect(res.properties).toEqual(properties);
417+
expect(res.id).toEqual(id);
418+
})
419+
.catch((e) => fail("it should not have errord: " + e));
420+
421+
return client.data
422+
.deleter()
423+
.withId(id)
424+
.withClassName(thingClassName)
425+
.do()
426+
.catch((e) => fail("it should not have errord: " + e));
427+
})
428+
429+
it("checks that object doesn't exist anymore with delete by id and class name", () => {
430+
return client.data
431+
.checker()
432+
.withId("6781a974-cfbf-455d-ace8-f1dba4564230")
433+
.do()
434+
.then((exists) => {
435+
if (exists) {
436+
fail("it should not exist in DB")
437+
}
438+
})
439+
.catch((e) => fail("it should not have errord: " + e));
343440
});
344441

345442
it("verifies there are now fewer things (after delete)", () => {

data/merger.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { isValidStringProperty } from "../validation/string";
2+
import { buildObjectsPath } from "./path";
23

34
export default class Merger {
45
constructor(client) {
@@ -55,7 +56,8 @@ export default class Merger {
5556
new Error("invalid usage: " + this.errors.join(", "))
5657
);
5758
}
58-
const path = `/objects/${this.id}`;
59+
60+
const path = buildObjectsPath(this.id, this.className);
5961
return this.client.patch(path, this.payload());
6062
};
6163
}

data/path.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { isValidStringProperty } from "../validation/string";
2+
3+
export function buildObjectsPath(id, className) {
4+
let path = `objects`;
5+
if (isValidStringProperty(className)) {
6+
path = `${path}/${className}`;
7+
}
8+
return `/${path}/${id}`;
9+
}

data/updater.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { isValidStringProperty } from "../validation/string";
2+
import { buildObjectsPath } from "./path";
23

34
export default class Updater {
45
constructor(client) {
@@ -25,7 +26,7 @@ export default class Updater {
2526
if (!isValidStringProperty(this.className)) {
2627
this.errors = [
2728
...this.errors,
28-
"className must be set - set with withId(id)",
29+
"className must be set - use withClassName(className)",
2930
];
3031
}
3132
};
@@ -58,7 +59,8 @@ export default class Updater {
5859
new Error("invalid usage: " + this.errors.join(", "))
5960
);
6061
}
61-
const path = `/objects/${this.id}`;
62+
63+
const path = buildObjectsPath(this.id, this.className);
6264
return this.client.put(path, this.payload());
6365
};
6466
}

docker-compose.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
version: '3.4'
22
services:
33
weaviate:
4-
image: semitechnologies/weaviate:1.13.2
4+
# TODO: temporarily use this version until next release
5+
image: semitechnologies/weaviate:latest@sha256:248a1e1f6f0ba817c00ead4edeecf9d19961d482a3c432165d95762bd44e3a0c
56
restart: on-failure:0
67
ports:
78
- "8080:8080"

misc/journey.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,20 @@ describe("misc endpoints", () => {
7070
})
7171
.catch((e) => fail("it should not have errord: " + e));
7272
});
73+
74+
it("fetches the server version", async () => {
75+
let version = await client.misc.metaGetter()
76+
.do()
77+
.then(res => {
78+
return res.version;
79+
})
80+
.catch((e) => fail("it should not have errord: " + e));
81+
82+
return client.misc.metaGetter()
83+
.fetchVersion()
84+
.then(res => {
85+
expect(res).toEqual(version);
86+
})
87+
.catch((e) => fail("it should not have errord: " + e));
88+
});
7389
});

misc/metaGetter.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,14 @@ export default class MetaGetter {
66
do = () => {
77
return this.client.get("/meta", true);
88
};
9+
10+
fetchVersion = () => {
11+
return this.client.get("/meta", true)
12+
.then(res => {
13+
return res.version;
14+
})
15+
.catch(err => {
16+
return Promise.reject(err);
17+
});
18+
}
919
}

0 commit comments

Comments
 (0)