@peerbit/indexer-tests 1.1.3 → 1.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/src/tests.js CHANGED
@@ -8,8 +8,8 @@ var __metadata = (this && this.__metadata) || function (k, v) {
8
8
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
9
  };
10
10
  import { deserialize, field, fixedArray, option, serialize, variant, vec, } from "@dao-xyz/borsh";
11
- import { randomBytes } from "@peerbit/crypto";
12
- import { And, BoolQuery, ByteMatchQuery, Compare, IntegerCompare, IsNull, Nested, Not, Or, Query, Sort, SortDirection, StringMatch, StringMatchMethod, extractFieldValue, getIdProperty, id, toId, } from "@peerbit/indexer-interface";
11
+ import { randomBytes, sha256Base64Sync } from "@peerbit/crypto";
12
+ import { And, BoolQuery, ByteMatchQuery, Compare, IntegerCompare, IsNull, Not, Or, Query, Sort, SortDirection, StringMatch, StringMatchMethod, extractFieldValue, getIdProperty, id, toId, } from "@peerbit/indexer-interface";
13
13
  import {
14
14
  /* delay, */
15
15
  delay, waitForResolved, } from "@peerbit/time";
@@ -249,7 +249,7 @@ export const tests = (createIndicies, type = "transient", properties) => {
249
249
  };
250
250
  afterEach(async () => {
251
251
  defaultDocs = [];
252
- await indices?.stop?.();
252
+ await indices?.drop?.();
253
253
  });
254
254
  describe("indexBy", () => {
255
255
  const testIndex = async (store, doc, idProperty = ["id"]) => {
@@ -435,24 +435,25 @@ export const tests = (createIndicies, type = "transient", properties) => {
435
435
  await testIndex(store, doc);
436
436
  });
437
437
  });
438
- /* TMP renable with sqlite support u64
439
- describe("bigint", () => {
438
+ describe("bigint", () => {
440
439
  class DocumentBigintId {
441
- @field({ type: "u64" })
442
- id: bigint;
443
-
444
- @field({ type: "u64" })
445
- value: bigint;
446
-
447
- constructor(properties: { id: bigint; value: bigint }) {
440
+ id;
441
+ value;
442
+ constructor(properties) {
448
443
  this.id = properties.id;
449
444
  this.value = properties.value;
450
445
  }
451
446
  }
452
-
447
+ __decorate([
448
+ field({ type: "u64" }),
449
+ __metadata("design:type", BigInt)
450
+ ], DocumentBigintId.prototype, "id", void 0);
451
+ __decorate([
452
+ field({ type: "u64" }),
453
+ __metadata("design:type", BigInt)
454
+ ], DocumentBigintId.prototype, "value", void 0);
453
455
  it("index as bigint", async () => {
454
456
  const { store } = await setup({ schema: DocumentBigintId });
455
-
456
457
  // make the id less than 2^53, but greater than u32 max
457
458
  const id = BigInt(2 ** 63 - 1);
458
459
  const doc = new DocumentBigintId({
@@ -461,7 +462,7 @@ export const tests = (createIndicies, type = "transient", properties) => {
461
462
  });
462
463
  await testIndex(store, doc);
463
464
  });
464
- }); */
465
+ });
465
466
  describe("by decorator", () => {
466
467
  class DocumentWithDecoratedId {
467
468
  xyz;
@@ -838,28 +839,171 @@ export const tests = (createIndicies, type = "transient", properties) => {
838
839
  field({ type: vec(Document) }),
839
840
  __metadata("design:type", Array)
840
841
  ], DocumentsVec.prototype, "documents", void 0);
841
- it("can search", async () => {
842
- const out = await setup({ schema: DocumentsVec });
843
- store = out.store;
844
- const d1 = new DocumentsVec({
845
- documents: [
846
- new Document({ id: uuid(), number: 123n, tags: [] }),
847
- ],
842
+ describe("search", () => {
843
+ let d1;
844
+ let d2;
845
+ let d3;
846
+ beforeEach(async () => {
847
+ const out = await setup({ schema: DocumentsVec });
848
+ store = out.store;
849
+ d1 = new DocumentsVec({
850
+ id: new Uint8Array([0]),
851
+ documents: [
852
+ new Document({ id: uuid(), number: 123n, tags: [] }),
853
+ ],
854
+ });
855
+ await store.put(d1);
856
+ d2 = new DocumentsVec({
857
+ id: new Uint8Array([1]),
858
+ documents: [
859
+ new Document({ id: uuid(), number: 124n, tags: [] }),
860
+ ],
861
+ });
862
+ await store.put(d2);
863
+ d3 = new DocumentsVec({
864
+ id: new Uint8Array([2]),
865
+ documents: [
866
+ new Document({ id: uuid(), number: 122n, tags: [] }),
867
+ new Document({ id: uuid(), number: 125n, tags: [] }),
868
+ ],
869
+ });
870
+ await store.put(d3);
848
871
  });
849
- await store.put(d1);
850
- await store.put(new DocumentsVec({
851
- documents: [
852
- new Document({ id: uuid(), number: 124n, tags: [] }),
853
- ],
854
- }));
855
- const results = await search(store, {
856
- query: new IntegerCompare({
857
- key: ["documents", "number"],
858
- compare: Compare.Equal,
859
- value: d1.documents[0].number,
860
- }),
872
+ it("match", async () => {
873
+ // equality
874
+ const results = await search(store, {
875
+ query: new IntegerCompare({
876
+ key: ["documents", "number"],
877
+ compare: Compare.Equal,
878
+ value: d1.documents[0].number,
879
+ }),
880
+ });
881
+ expect(results.map((x) => x.value.id)).to.deep.equal([d1.id]);
882
+ });
883
+ describe("logical", () => {
884
+ it("and", async () => {
885
+ // can not be equal to two different things at once
886
+ let results = await search(store, {
887
+ query: [
888
+ new And([
889
+ new IntegerCompare({
890
+ key: ["documents", "number"],
891
+ compare: Compare.Equal,
892
+ value: 123n,
893
+ }),
894
+ new IntegerCompare({
895
+ key: ["documents", "number"],
896
+ compare: Compare.Equal,
897
+ value: 124n,
898
+ }),
899
+ ]),
900
+ ],
901
+ });
902
+ expect(results).to.have.length(0);
903
+ // can not be between two different things at once
904
+ results = await search(store, {
905
+ query: [
906
+ new And([
907
+ new IntegerCompare({
908
+ key: ["documents", "number"],
909
+ compare: Compare.Less,
910
+ value: 1000n,
911
+ }),
912
+ new IntegerCompare({
913
+ key: ["documents", "number"],
914
+ compare: Compare.Greater,
915
+ value: 1000n,
916
+ }),
917
+ ]),
918
+ ],
919
+ });
920
+ expect(results).to.have.length(0);
921
+ // between one value matches
922
+ results = await search(store, {
923
+ query: [
924
+ new Or([
925
+ new IntegerCompare({
926
+ key: ["documents", "number"],
927
+ compare: Compare.Less,
928
+ value: 124n,
929
+ }),
930
+ new IntegerCompare({
931
+ key: ["documents", "number"],
932
+ compare: Compare.GreaterOrEqual,
933
+ value: 125n,
934
+ }),
935
+ ]),
936
+ ],
937
+ });
938
+ // because each query is applied separately
939
+ expect(results.map((x) => sha256Base64Sync(x.value.id))).to.have.members([d1.id, d3.id].map(sha256Base64Sync));
940
+ results = await search(store, {
941
+ query: [
942
+ new And([
943
+ new IntegerCompare({
944
+ key: ["documents", "number"],
945
+ compare: Compare.Less,
946
+ value: 124n,
947
+ }),
948
+ new IntegerCompare({
949
+ key: ["documents", "number"],
950
+ compare: Compare.GreaterOrEqual,
951
+ value: 123n,
952
+ }),
953
+ ]),
954
+ ],
955
+ });
956
+ expect(results.map((x) => x.value.id)).to.deep.eq([d1.id]);
957
+ });
958
+ it("or", async () => {
959
+ const results3 = await search(store, {
960
+ query: [
961
+ new Or([
962
+ new IntegerCompare({
963
+ key: ["documents", "number"],
964
+ compare: Compare.Equal,
965
+ value: 123n,
966
+ }),
967
+ new IntegerCompare({
968
+ key: ["documents", "number"],
969
+ compare: Compare.Equal,
970
+ value: 124n,
971
+ }),
972
+ ]),
973
+ ],
974
+ });
975
+ expect(results3.map((x) => sha256Base64Sync(x.value.id))).to.have.members([
976
+ sha256Base64Sync(d1.id),
977
+ sha256Base64Sync(d2.id),
978
+ ]);
979
+ });
980
+ it("or arr, or field", async () => {
981
+ const results3 = await search(store, {
982
+ query: [
983
+ new Or([
984
+ new IntegerCompare({
985
+ key: ["documents", "number"],
986
+ compare: Compare.Equal,
987
+ value: 123n,
988
+ }),
989
+ new IntegerCompare({
990
+ key: ["documents", "number"],
991
+ compare: Compare.Equal,
992
+ value: 124n,
993
+ }),
994
+ new ByteMatchQuery({
995
+ key: "id",
996
+ value: new Uint8Array([1]),
997
+ }),
998
+ ]),
999
+ ],
1000
+ });
1001
+ expect(results3.map((x) => sha256Base64Sync(x.value.id))).to.have.members([
1002
+ sha256Base64Sync(d1.id),
1003
+ sha256Base64Sync(d2.id),
1004
+ ]);
1005
+ });
861
1006
  });
862
- expect(results.map((x) => x.value.id)).to.deep.equal([d1.id]);
863
1007
  });
864
1008
  it("update array", async () => {
865
1009
  const out = await setup({ schema: DocumentsVec });
@@ -931,6 +1075,281 @@ export const tests = (createIndicies, type = "transient", properties) => {
931
1075
  })).length).to.equal(0);
932
1076
  });
933
1077
  });
1078
+ describe("simple value", () => {
1079
+ class ArrayDocument {
1080
+ id;
1081
+ array;
1082
+ constructor(properties) {
1083
+ this.id = properties.id || randomBytes(32);
1084
+ this.array = properties?.array || [];
1085
+ }
1086
+ }
1087
+ __decorate([
1088
+ field({ type: Uint8Array }),
1089
+ __metadata("design:type", Uint8Array)
1090
+ ], ArrayDocument.prototype, "id", void 0);
1091
+ __decorate([
1092
+ field({ type: vec("u32") }),
1093
+ __metadata("design:type", Array)
1094
+ ], ArrayDocument.prototype, "array", void 0);
1095
+ describe("search", () => {
1096
+ let d1;
1097
+ let d2;
1098
+ let d3;
1099
+ beforeEach(async () => {
1100
+ const out = await setup({ schema: ArrayDocument });
1101
+ store = out.store;
1102
+ d1 = new ArrayDocument({
1103
+ id: new Uint8Array([0]),
1104
+ array: [1],
1105
+ });
1106
+ await store.put(d1);
1107
+ d2 = new ArrayDocument({
1108
+ id: new Uint8Array([1]),
1109
+ array: [2],
1110
+ });
1111
+ await store.put(d2);
1112
+ d3 = new ArrayDocument({
1113
+ id: new Uint8Array([2]),
1114
+ array: [0, 3],
1115
+ });
1116
+ await store.put(d3);
1117
+ });
1118
+ it("match", async () => {
1119
+ // equality
1120
+ const results = await search(store, {
1121
+ query: new IntegerCompare({
1122
+ key: ["array"],
1123
+ compare: Compare.Equal,
1124
+ value: d1.array[0],
1125
+ }),
1126
+ });
1127
+ expect(results.map((x) => x.value.id)).to.deep.equal([d1.id]);
1128
+ });
1129
+ describe("logical", () => {
1130
+ it("and", async () => {
1131
+ // can not be equal to two different things at once
1132
+ let results = await search(store, {
1133
+ query: [
1134
+ new And([
1135
+ new IntegerCompare({
1136
+ key: ["array"],
1137
+ compare: Compare.Equal,
1138
+ value: d1.array[0],
1139
+ }),
1140
+ new IntegerCompare({
1141
+ key: ["array"],
1142
+ compare: Compare.Equal,
1143
+ value: d2.array[0],
1144
+ }),
1145
+ ]),
1146
+ ],
1147
+ });
1148
+ expect(results).to.have.length(0);
1149
+ // can not be between two different things at once
1150
+ results = await search(store, {
1151
+ query: [
1152
+ new And([
1153
+ new IntegerCompare({
1154
+ key: ["array"],
1155
+ compare: Compare.Less,
1156
+ value: 1000,
1157
+ }),
1158
+ new IntegerCompare({
1159
+ key: ["array"],
1160
+ compare: Compare.Greater,
1161
+ value: 1000,
1162
+ }),
1163
+ ]),
1164
+ ],
1165
+ });
1166
+ expect(results).to.have.length(0);
1167
+ // between one value matches
1168
+ results = await search(store, {
1169
+ query: [
1170
+ new Or([
1171
+ new IntegerCompare({
1172
+ key: ["array"],
1173
+ compare: Compare.Less,
1174
+ value: 2,
1175
+ }),
1176
+ new IntegerCompare({
1177
+ key: ["array"],
1178
+ compare: Compare.GreaterOrEqual,
1179
+ value: 3,
1180
+ }),
1181
+ ]),
1182
+ ],
1183
+ });
1184
+ // because each query is applied separately
1185
+ expect(results.map((x) => sha256Base64Sync(x.value.id))).to.have.members([d1.id, d3.id].map(sha256Base64Sync));
1186
+ results = await search(store, {
1187
+ query: [
1188
+ new And([
1189
+ new IntegerCompare({
1190
+ key: ["array"],
1191
+ compare: Compare.Less,
1192
+ value: 2,
1193
+ }),
1194
+ new IntegerCompare({
1195
+ key: ["array"],
1196
+ compare: Compare.GreaterOrEqual,
1197
+ value: 1,
1198
+ }),
1199
+ ]),
1200
+ ],
1201
+ });
1202
+ // using nested path will apply the queries together
1203
+ expect(results.map((x) => sha256Base64Sync(x.value.id))).to.have.members([d1.id].map(sha256Base64Sync));
1204
+ });
1205
+ it("or", async () => {
1206
+ const results3 = await search(store, {
1207
+ query: [
1208
+ new Or([
1209
+ new IntegerCompare({
1210
+ key: ["array"],
1211
+ compare: Compare.Equal,
1212
+ value: d1.array[0],
1213
+ }),
1214
+ new IntegerCompare({
1215
+ key: ["array"],
1216
+ compare: Compare.Equal,
1217
+ value: d3.array[0],
1218
+ }),
1219
+ ]),
1220
+ ],
1221
+ });
1222
+ expect(results3.map((x) => sha256Base64Sync(x.value.id))).to.have.members([
1223
+ sha256Base64Sync(d1.id),
1224
+ sha256Base64Sync(d3.id),
1225
+ ]);
1226
+ });
1227
+ it("or array, or field", async () => {
1228
+ const results3 = await search(store, {
1229
+ query: [
1230
+ new Or([
1231
+ new And([
1232
+ new IntegerCompare({
1233
+ key: ["array"],
1234
+ compare: Compare.LessOrEqual,
1235
+ value: 0,
1236
+ }),
1237
+ new IntegerCompare({
1238
+ key: ["array"],
1239
+ compare: Compare.LessOrEqual,
1240
+ value: 1,
1241
+ }),
1242
+ ]),
1243
+ new ByteMatchQuery({
1244
+ key: "id",
1245
+ value: new Uint8Array([0]),
1246
+ }),
1247
+ ]),
1248
+ ],
1249
+ });
1250
+ expect(results3.map((x) => sha256Base64Sync(x.value.id))).to.have.members([
1251
+ sha256Base64Sync(d1.id),
1252
+ sha256Base64Sync(d3.id),
1253
+ ]);
1254
+ });
1255
+ it("or all", async () => {
1256
+ const results = await search(store, {
1257
+ query: [
1258
+ new Or([
1259
+ new Or([
1260
+ new And([
1261
+ new IntegerCompare({
1262
+ key: ["array"],
1263
+ compare: Compare.GreaterOrEqual,
1264
+ value: 0,
1265
+ }),
1266
+ ]),
1267
+ new And([
1268
+ new IntegerCompare({
1269
+ key: ["array"],
1270
+ compare: Compare.GreaterOrEqual,
1271
+ value: 0,
1272
+ }),
1273
+ ]),
1274
+ ]),
1275
+ new IntegerCompare({
1276
+ key: ["array"],
1277
+ compare: Compare.GreaterOrEqual,
1278
+ value: 0,
1279
+ }),
1280
+ ]),
1281
+ ],
1282
+ });
1283
+ expect(results.map((x) => sha256Base64Sync(x.value.id))).to.have.members([
1284
+ sha256Base64Sync(d1.id),
1285
+ sha256Base64Sync(d2.id),
1286
+ sha256Base64Sync(d3.id),
1287
+ ]);
1288
+ });
1289
+ });
1290
+ });
1291
+ it("update array", async () => {
1292
+ const out = await setup({ schema: ArrayDocument });
1293
+ store = out.store;
1294
+ const d1 = new ArrayDocument({
1295
+ array: [123],
1296
+ });
1297
+ await store.put(d1);
1298
+ d1.array = [124];
1299
+ await store.put(d1);
1300
+ // should have update results
1301
+ expect((await search(store, {
1302
+ query: new IntegerCompare({
1303
+ key: ["array"],
1304
+ compare: Compare.Equal,
1305
+ value: 123,
1306
+ }),
1307
+ })).length).to.equal(0);
1308
+ expect((await search(store, {
1309
+ query: new IntegerCompare({
1310
+ key: ["array"],
1311
+ compare: Compare.Equal,
1312
+ value: 124,
1313
+ }),
1314
+ })).map((x) => x.value.id)).to.deep.equal([d1.id]);
1315
+ });
1316
+ it("put delete put", async () => {
1317
+ const { store } = await setup({ schema: ArrayDocument });
1318
+ const d1 = new ArrayDocument({
1319
+ array: [123],
1320
+ });
1321
+ await store.put(d1);
1322
+ const [deleted] = await store.del({
1323
+ query: {
1324
+ id: d1.id,
1325
+ },
1326
+ });
1327
+ expect(deleted.key).to.deep.equal(d1.id);
1328
+ expect((await search(store, {
1329
+ query: new IntegerCompare({
1330
+ key: ["array"],
1331
+ compare: Compare.Equal,
1332
+ value: 123,
1333
+ }),
1334
+ })).length).to.equal(0);
1335
+ d1.array = [124];
1336
+ await store.put(d1);
1337
+ expect((await search(store, {
1338
+ query: new IntegerCompare({
1339
+ key: ["array"],
1340
+ compare: Compare.Equal,
1341
+ value: 124,
1342
+ }),
1343
+ })).map((x) => x.value.id)).to.deep.equal([d1.id]);
1344
+ expect((await search(store, {
1345
+ query: new IntegerCompare({
1346
+ key: ["array"],
1347
+ compare: Compare.Equal,
1348
+ value: 123,
1349
+ }),
1350
+ })).length).to.equal(0);
1351
+ });
1352
+ });
934
1353
  });
935
1354
  describe("logical", () => {
936
1355
  beforeEach(async () => {
@@ -1008,7 +1427,7 @@ export const tests = (createIndicies, type = "transient", properties) => {
1008
1427
  new IntegerCompare({
1009
1428
  key: "number",
1010
1429
  compare: Compare.Equal,
1011
- value: 2n,
1430
+ value: 2,
1012
1431
  }),
1013
1432
  ],
1014
1433
  });
@@ -1021,7 +1440,7 @@ export const tests = (createIndicies, type = "transient", properties) => {
1021
1440
  new IntegerCompare({
1022
1441
  key: "number",
1023
1442
  compare: Compare.Greater,
1024
- value: 2n,
1443
+ value: 2,
1025
1444
  }),
1026
1445
  ],
1027
1446
  });
@@ -1034,7 +1453,7 @@ export const tests = (createIndicies, type = "transient", properties) => {
1034
1453
  new IntegerCompare({
1035
1454
  key: "number",
1036
1455
  compare: Compare.GreaterOrEqual,
1037
- value: 2n,
1456
+ value: 2,
1038
1457
  }),
1039
1458
  ],
1040
1459
  });
@@ -1049,7 +1468,7 @@ export const tests = (createIndicies, type = "transient", properties) => {
1049
1468
  new IntegerCompare({
1050
1469
  key: "number",
1051
1470
  compare: Compare.Less,
1052
- value: 2n,
1471
+ value: 2,
1053
1472
  }),
1054
1473
  ],
1055
1474
  });
@@ -1071,6 +1490,19 @@ export const tests = (createIndicies, type = "transient", properties) => {
1071
1490
  expect(response[0].value.number).to.be.oneOf([1n, 1]);
1072
1491
  expect(response[1].value.number).to.be.oneOf([2n, 2]);
1073
1492
  });
1493
+ it("bigint as compare value", async () => {
1494
+ const response = await search(store, {
1495
+ query: [
1496
+ new IntegerCompare({
1497
+ key: "number",
1498
+ compare: Compare.Less,
1499
+ value: 2n,
1500
+ }),
1501
+ ],
1502
+ });
1503
+ expect(response).to.have.length(1);
1504
+ expect(response[0].value.number).to.be.oneOf([1n, 1]);
1505
+ });
1074
1506
  });
1075
1507
  describe("bigint", () => {
1076
1508
  let BigInt = class BigInt {
@@ -1173,6 +1605,18 @@ export const tests = (createIndicies, type = "transient", properties) => {
1173
1605
  expect(response[0].value.bigint).to.equal(first);
1174
1606
  expect(response[1].value.bigint).to.equal(second);
1175
1607
  });
1608
+ it("number as compare value", async () => {
1609
+ const response = await search(store, {
1610
+ query: [
1611
+ new IntegerCompare({
1612
+ key: "bigint",
1613
+ compare: Compare.Greater,
1614
+ value: 1,
1615
+ }),
1616
+ ],
1617
+ });
1618
+ expect(response).to.have.length(3);
1619
+ });
1176
1620
  });
1177
1621
  describe("nested", () => {
1178
1622
  describe("one level", () => {
@@ -1215,6 +1659,24 @@ export const tests = (createIndicies, type = "transient", properties) => {
1215
1659
  beforeEach(async () => {
1216
1660
  await setup({ schema: DocumentWithNesting });
1217
1661
  });
1662
+ it("all", async () => {
1663
+ await store.put(new DocumentWithNesting({
1664
+ id: "1",
1665
+ nested: new Nested({ number: 1n, bool: false }),
1666
+ }));
1667
+ await store.put(new DocumentWithNesting({
1668
+ id: "2",
1669
+ nested: undefined,
1670
+ }));
1671
+ const all = await search(store, {});
1672
+ expect(all).to.have.length(2);
1673
+ expect(all.map((x) => x.id.primitive)).to.have.members([
1674
+ "1",
1675
+ "2",
1676
+ ]);
1677
+ expect(all[0].value.nested).to.be.instanceOf(Nested);
1678
+ expect(all[1].value.nested).to.be.undefined;
1679
+ });
1218
1680
  it("number", async () => {
1219
1681
  await store.put(new DocumentWithNesting({
1220
1682
  id: "1",
@@ -1994,21 +2456,16 @@ export const tests = (createIndicies, type = "transient", properties) => {
1994
2456
  ],
1995
2457
  }));
1996
2458
  const response = await search(store, {
1997
- query: [
1998
- new Nested({
1999
- path: "array",
2000
- query: new And([
2001
- new StringMatch({
2002
- key: "a",
2003
- value: "hello",
2004
- }),
2005
- new StringMatch({
2006
- key: "b",
2007
- value: "world",
2008
- }),
2009
- ]),
2459
+ query: new And([
2460
+ new StringMatch({
2461
+ key: ["array", "a"],
2462
+ value: "hello",
2010
2463
  }),
2011
- ],
2464
+ new StringMatch({
2465
+ key: ["array", "b"],
2466
+ value: "world",
2467
+ }),
2468
+ ]),
2012
2469
  });
2013
2470
  expect(response).to.have.length(1);
2014
2471
  expect(response[0].value.id).to.equal("1");
@@ -2025,8 +2482,7 @@ export const tests = (createIndicies, type = "transient", properties) => {
2025
2482
  }),
2026
2483
  ],
2027
2484
  });
2028
- await store.put(doc1);
2029
- await store.put(new NestedMultipleFieldsArrayDocument({
2485
+ const doc2 = new NestedMultipleFieldsArrayDocument({
2030
2486
  id: "2",
2031
2487
  array: [
2032
2488
  new NestedMultipleFieldsDocument({
@@ -2038,8 +2494,25 @@ export const tests = (createIndicies, type = "transient", properties) => {
2038
2494
  b: "world",
2039
2495
  }),
2040
2496
  ],
2041
- }));
2042
- const response = await search(store, {
2497
+ });
2498
+ const doc3 = new NestedMultipleFieldsArrayDocument({
2499
+ id: "3",
2500
+ array: [
2501
+ new NestedMultipleFieldsDocument({
2502
+ a: "_",
2503
+ b: "_",
2504
+ }),
2505
+ new NestedMultipleFieldsDocument({
2506
+ a: "_",
2507
+ b: "_",
2508
+ }),
2509
+ ],
2510
+ });
2511
+ await store.put(doc1);
2512
+ await store.put(doc2);
2513
+ await store.put(doc3);
2514
+ // AND will only yield doc 1 since only doc 1 contains the combination below
2515
+ const responseAnd = await search(store, {
2043
2516
  query: [
2044
2517
  new StringMatch({
2045
2518
  key: ["array", "a"],
@@ -2051,8 +2524,26 @@ export const tests = (createIndicies, type = "transient", properties) => {
2051
2524
  }),
2052
2525
  ],
2053
2526
  });
2527
+ expect(responseAnd).to.have.length(1);
2528
+ checkDocument(responseAnd[0].value, doc1);
2529
+ // OR will only yield doc 1 and doc 2 since both will fulfill one of two conditions
2530
+ const response = await search(store, {
2531
+ query: [
2532
+ new Or([
2533
+ new StringMatch({
2534
+ key: ["array", "a"],
2535
+ value: "hello",
2536
+ }),
2537
+ new StringMatch({
2538
+ key: ["array", "b"],
2539
+ value: "world",
2540
+ }),
2541
+ ]),
2542
+ ],
2543
+ });
2054
2544
  expect(response).to.have.length(2);
2055
2545
  checkDocument(response[0].value, doc1);
2546
+ checkDocument(response[1].value, doc2);
2056
2547
  });
2057
2548
  });
2058
2549
  });
@@ -2480,11 +2971,11 @@ export const tests = (createIndicies, type = "transient", properties) => {
2480
2971
  });
2481
2972
  });
2482
2973
  describe("sort", () => {
2483
- const put = async (id, stringId) => {
2974
+ const put = async (id, options) => {
2484
2975
  const doc = new Document({
2485
- id: stringId ?? String(id),
2486
- name: String(id),
2487
- number: BigInt(id),
2976
+ id: String(id),
2977
+ name: options?.name ?? String(id),
2978
+ number: options?.number ?? BigInt(id),
2488
2979
  tags: [],
2489
2980
  });
2490
2981
  const resp = await store.put(doc);
@@ -2639,12 +3130,12 @@ export const tests = (createIndicies, type = "transient", properties) => {
2639
3130
  });
2640
3131
  /* it("no sort is stable", async () => {
2641
3132
  // TODO this test is actually not a good predictor of stability
2642
-
3133
+
2643
3134
  const insertCount = 500;
2644
3135
  for (let i = 0; i < insertCount; i++) {
2645
3136
  await put(i, uuid());
2646
3137
  }
2647
-
3138
+
2648
3139
  const resolvedValues: Set<number> = new Set()
2649
3140
  const batchSize = 123;
2650
3141
  const iterator = store.iterate();
@@ -2667,6 +3158,21 @@ export const tests = (createIndicies, type = "transient", properties) => {
2667
3158
  expect(next.map((x) => x.value.name)).to.deep.equal(["0", "1", "2"]);
2668
3159
  await assertIteratorIsDone(iterator);
2669
3160
  });
3161
+ it("sort by multiple properties", async () => {
3162
+ await put(0, { name: "a", number: 2n });
3163
+ await put(1, { name: "a", number: 1n });
3164
+ await put(2, { name: "b", number: 2n });
3165
+ await put(3, { name: "b", number: 1n });
3166
+ const iterator = store.iterate({
3167
+ query: [],
3168
+ sort: [
3169
+ new Sort({ direction: SortDirection.DESC, key: "name" }),
3170
+ new Sort({ direction: SortDirection.ASC, key: "number" }),
3171
+ ],
3172
+ });
3173
+ const out = await iterator.all();
3174
+ expect(out.map((x) => x.value.id)).to.deep.equal(["3", "2", "1", "0"]);
3175
+ });
2670
3176
  describe("nested", () => {
2671
3177
  it("variants", async () => {
2672
3178
  const doc1 = new Document({
@@ -3138,6 +3644,7 @@ export const tests = (createIndicies, type = "transient", properties) => {
3138
3644
  expect(await store.getSize()).equal(0);
3139
3645
  });
3140
3646
  it("indices", async () => {
3647
+ // TODO make this test more clear and the purpose of each call
3141
3648
  let { directory, indices } = await setupDefault();
3142
3649
  let subindex = await indices.scope("x");
3143
3650
  store = await subindex.init({ indexBy: ["id"], schema: Document });
@@ -3148,12 +3655,14 @@ export const tests = (createIndicies, type = "transient", properties) => {
3148
3655
  await store.start();
3149
3656
  expect(await store.getSize()).equal(4);
3150
3657
  await indices.drop();
3151
- await store.start();
3658
+ const out = await setup({ schema: Document }, directory);
3659
+ indices = out.indices;
3660
+ await indices.start();
3661
+ store = out.store;
3152
3662
  expect(await store.getSize()).equal(0);
3153
3663
  await store.stop(); /// TODO why do w
3154
3664
  await indices.stop();
3155
3665
  store = (await setup({ schema: Document }, directory)).store;
3156
- await store.start();
3157
3666
  expect(await store.getSize()).equal(0);
3158
3667
  });
3159
3668
  });