Skip to content

Commit b583315

Browse files
committed
withReferences in referencesBatcher, withObjects in objectsBatcher
1 parent 1dc733b commit b583315

File tree

3 files changed

+134
-68
lines changed

3 files changed

+134
-68
lines changed

batch/journey.test.js

Lines changed: 99 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,78 @@ const otherThingIds = [
1717
"8727fa2b-610a-4a5c-af26-e558943f71c7",
1818
];
1919

20+
const someObjects = [
21+
{
22+
class: thingClassName,
23+
id: thingIds[0],
24+
properties: { stringProp: "foo1" },
25+
},
26+
{
27+
class: thingClassName,
28+
id: thingIds[1],
29+
properties: { stringProp: "bar1" },
30+
},
31+
{
32+
class: thingClassName,
33+
id: thingIds[2],
34+
properties: { stringProp: "foo2" },
35+
},
36+
{
37+
class: thingClassName,
38+
id: thingIds[3],
39+
properties: { stringProp: "bar2" },
40+
},
41+
{
42+
class: otherThingClassName,
43+
id: otherThingIds[0],
44+
properties: { stringProp: "foo3" },
45+
},
46+
{
47+
class: otherThingClassName,
48+
id: otherThingIds[1],
49+
properties: { stringProp: "bar3" },
50+
},
51+
];
52+
53+
const someReferences = [
54+
{
55+
from: `weaviate://localhost/${thingClassName}/${thingIds[0]}/refProp`,
56+
to: `weaviate://localhost/${otherThingClassName}/${otherThingIds[0]}`,
57+
},
58+
{
59+
from: `weaviate://localhost/${thingClassName}/${thingIds[1]}/refProp`,
60+
to: `weaviate://localhost/${otherThingClassName}/${otherThingIds[1]}`,
61+
},
62+
{
63+
from: `weaviate://localhost/${thingClassName}/${thingIds[2]}/refProp`,
64+
to: `weaviate://localhost/${otherThingClassName}/${otherThingIds[1]}`,
65+
},
66+
{
67+
from: `weaviate://localhost/${thingClassName}/${thingIds[3]}/refProp`,
68+
to: `weaviate://localhost/${otherThingClassName}/${otherThingIds[1]}`,
69+
}
70+
];
71+
2072
describe("batch importing", () => {
2173
const client = weaviate.client({
2274
scheme: "http",
2375
host: "localhost:8080",
2476
});
2577

78+
it("can add objects with different methods", () => {
79+
const batcher = client.batch.objectsBatcher()
80+
.withObject(someObjects[0])
81+
.withObjects(someObjects[1])
82+
.withObjects(someObjects[2], someObjects[3])
83+
.withObjects([someObjects[4], someObjects[5]]);
84+
85+
expect(batcher.objects).toHaveLength(someObjects.length);
86+
batcher.objects.forEach((obj, i) => {
87+
expect(obj.class).toBe(someObjects[i].class);
88+
expect(obj.id).toBe(someObjects[i].id);
89+
})
90+
});
91+
2692
it("sets up", () => setup(client));
2793

2894
describe("import thing objects", () => {
@@ -81,8 +147,7 @@ describe("batch importing", () => {
81147
it("imports them", () => {
82148
client.batch
83149
.objectsBatcher()
84-
.withObject(toImport[0])
85-
.withObject(toImport[1])
150+
.withObjects(toImport[0], toImport[1])
86151
.do()
87152
.then()
88153
.catch((e) => fail("it should not have error'd " + e));
@@ -119,8 +184,7 @@ describe("batch importing", () => {
119184
it("imports them", () => {
120185
client.batch
121186
.objectsBatcher()
122-
.withObject(toImport[0])
123-
.withObject(toImport[1])
187+
.withObjects([toImport[0], toImport[1]])
124188
.do()
125189
.then()
126190
.catch((e) => fail("it should not have error'd " + e));
@@ -140,6 +204,19 @@ describe("batch importing", () => {
140204
});
141205

142206
describe("batch reference between the thing and otherThing objects", () => {
207+
it("can add references with different methods", () => {
208+
const batcher = client.batch.referencesBatcher()
209+
.withReference(someReferences[0])
210+
.withReferences(someReferences[1], someReferences[2])
211+
.withReferences([someReferences[3]]);
212+
213+
expect(batcher.references).toHaveLength(someReferences.length);
214+
batcher.references.forEach((ref, i) => {
215+
expect(ref.from).toBe(someReferences[i].from);
216+
expect(ref.to).toBe(someReferences[i].to);
217+
})
218+
});
219+
143220
it("imports the refs with raw objects", () => {
144221
return client.batch
145222
.referencesBatcher()
@@ -161,28 +238,25 @@ describe("batch importing", () => {
161238
});
162239

163240
it("imports more refs with a builder pattern", () => {
241+
const reference1 = client.batch
242+
.referencePayloadBuilder()
243+
.withFromClassName(thingClassName)
244+
.withFromRefProp("refProp")
245+
.withFromId(thingIds[2])
246+
.withToId(otherThingIds[0])
247+
.withToClassName(otherThingClassName)
248+
.payload();
249+
const reference2 = client.batch
250+
.referencePayloadBuilder()
251+
.withFromClassName(thingClassName)
252+
.withFromRefProp("refProp")
253+
.withFromId(thingIds[3])
254+
.withToId(otherThingIds[1])
255+
.withToClassName(otherThingClassName)
256+
.payload();
164257
return client.batch
165258
.referencesBatcher()
166-
.withReference(
167-
client.batch
168-
.referencePayloadBuilder()
169-
.withFromClassName(thingClassName)
170-
.withFromRefProp("refProp")
171-
.withFromId(thingIds[2])
172-
.withToId(otherThingIds[0])
173-
.withToClassName(otherThingClassName)
174-
.payload()
175-
)
176-
.withReference(
177-
client.batch
178-
.referencePayloadBuilder()
179-
.withFromClassName(thingClassName)
180-
.withFromRefProp("refProp")
181-
.withFromId(thingIds[3])
182-
.withToId(otherThingIds[1])
183-
.withToClassName(otherThingClassName)
184-
.payload()
185-
)
259+
.withReferences(reference1, reference2)
186260
.do()
187261
.then((res) => {
188262
res.forEach((elem) => {
@@ -453,47 +527,9 @@ const setup = async (client) => {
453527
};
454528

455529
const setupData = (client) => {
456-
const toImport = [
457-
{
458-
class: thingClassName,
459-
id: thingIds[0],
460-
properties: { stringProp: "foo1" },
461-
},
462-
{
463-
class: thingClassName,
464-
id: thingIds[1],
465-
properties: { stringProp: "bar1" },
466-
},
467-
{
468-
class: thingClassName,
469-
id: thingIds[2],
470-
properties: { stringProp: "foo2" },
471-
},
472-
{
473-
class: thingClassName,
474-
id: thingIds[3],
475-
properties: { stringProp: "bar2" },
476-
},
477-
{
478-
class: otherThingClassName,
479-
id: otherThingIds[0],
480-
properties: { stringProp: "foo3" },
481-
},
482-
{
483-
class: otherThingClassName,
484-
id: otherThingIds[1],
485-
properties: { stringProp: "bar3" },
486-
},
487-
];
488-
489530
return client.batch
490531
.objectsBatcher()
491-
.withObject(toImport[0])
492-
.withObject(toImport[1])
493-
.withObject(toImport[2])
494-
.withObject(toImport[3])
495-
.withObject(toImport[4])
496-
.withObject(toImport[5])
532+
.withObjects(someObjects)
497533
.do();
498534
}
499535

batch/objectsBatcher.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,24 @@ export default class ObjectsBatcher {
55
this.errors = [];
66
}
77

8-
withObject = (obj) => {
9-
this.objects = [...this.objects, obj];
8+
/**
9+
* can be called as:
10+
* - withObjects([obj1, obj2, obj3])
11+
* - withObjects(obj1, obj2, obj3)
12+
* - withObjects(obj1)
13+
* @param {...any} objects
14+
*/
15+
withObjects(...objects) {
16+
let objs = objects;
17+
if (objects.length && Array.isArray(objects[0])) {
18+
objs = objects[0];
19+
}
20+
this.objects = [...this.objects, ...objs];
1021
return this;
22+
}
23+
24+
withObject(object) {
25+
return this.withObjects(object);
1126
};
1227

1328
payload = () => ({

batch/referencesBatcher.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,25 @@ export default class ReferencesBatcher {
66
this.errors = [];
77
}
88

9-
withReference = (obj) => {
10-
this.references = [...this.references, obj];
9+
/**
10+
* can be called as:
11+
* - withReferences([ref1, ref2, ref3])
12+
* - withReferences(ref1, ref2, ref3)
13+
* - withReferences(ref1)
14+
* @param {...any} references
15+
*/
16+
withReferences(...references) {
17+
let refs = references;
18+
if (references.length && Array.isArray(references[0])) {
19+
refs = references[0];
20+
}
21+
this.references = [...this.references, ...refs];
1122
return this;
12-
};
23+
}
24+
25+
withReference(reference) {
26+
return this.withReferences(reference);
27+
}
1328

1429
payload = () => this.references;
1530

0 commit comments

Comments
 (0)