@latticecms/core 0.1.0 → 0.2.0

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.
Files changed (78) hide show
  1. package/README.md +180 -57
  2. package/dist/access.d.ts +2 -3
  3. package/dist/access.d.ts.map +1 -1
  4. package/dist/access.js +21 -5
  5. package/dist/access.js.map +1 -1
  6. package/dist/build.d.ts +52 -4
  7. package/dist/build.d.ts.map +1 -1
  8. package/dist/build.js +78 -21
  9. package/dist/build.js.map +1 -1
  10. package/dist/collection.d.ts.map +1 -1
  11. package/dist/collection.js +12 -5
  12. package/dist/collection.js.map +1 -1
  13. package/dist/crud.d.ts +24 -8
  14. package/dist/crud.d.ts.map +1 -1
  15. package/dist/crud.js +47 -59
  16. package/dist/crud.js.map +1 -1
  17. package/dist/errors.d.ts +5 -1
  18. package/dist/errors.d.ts.map +1 -1
  19. package/dist/errors.js +5 -1
  20. package/dist/errors.js.map +1 -1
  21. package/dist/family.js +15 -19
  22. package/dist/family.js.map +1 -1
  23. package/dist/index.d.ts +1 -1
  24. package/dist/index.d.ts.map +1 -1
  25. package/dist/index.js.map +1 -1
  26. package/dist/mask.d.ts +11 -11
  27. package/dist/mask.d.ts.map +1 -1
  28. package/dist/mask.js +24 -2
  29. package/dist/mask.js.map +1 -1
  30. package/dist/populate.d.ts +9 -2
  31. package/dist/populate.d.ts.map +1 -1
  32. package/dist/populate.js +3 -4
  33. package/dist/populate.js.map +1 -1
  34. package/dist/relations.d.ts +2 -1
  35. package/dist/relations.d.ts.map +1 -1
  36. package/dist/relations.js +18 -2
  37. package/dist/relations.js.map +1 -1
  38. package/dist/repo.d.ts +1 -1
  39. package/dist/repo.d.ts.map +1 -1
  40. package/dist/repo.js +15 -6
  41. package/dist/repo.js.map +1 -1
  42. package/dist/sort.d.ts +32 -7
  43. package/dist/sort.d.ts.map +1 -1
  44. package/dist/sort.js +66 -24
  45. package/dist/sort.js.map +1 -1
  46. package/dist/store/conformance.d.ts.map +1 -1
  47. package/dist/store/conformance.js +132 -4
  48. package/dist/store/conformance.js.map +1 -1
  49. package/dist/store/index.d.ts +1 -1
  50. package/dist/store/index.d.ts.map +1 -1
  51. package/dist/store/index.js +1 -1
  52. package/dist/store/index.js.map +1 -1
  53. package/dist/store.d.ts +6 -0
  54. package/dist/store.d.ts.map +1 -1
  55. package/dist/store.js +1 -1
  56. package/dist/store.js.map +1 -1
  57. package/package.json +12 -11
  58. package/skills/lattice/SKILL.md +2 -2
  59. package/skills/lattice/references/access.md +19 -5
  60. package/skills/lattice/references/collection.md +4 -1
  61. package/skills/lattice/references/method.md +3 -0
  62. package/skills/lattice/references/override.md +89 -6
  63. package/skills/lattice/references/relations.md +3 -2
  64. package/src/access.ts +24 -4
  65. package/src/build.ts +214 -48
  66. package/src/collection.ts +12 -5
  67. package/src/crud.ts +74 -72
  68. package/src/errors.ts +5 -1
  69. package/src/family.ts +21 -21
  70. package/src/index.ts +2 -0
  71. package/src/mask.ts +31 -2
  72. package/src/populate.ts +11 -4
  73. package/src/relations.ts +19 -2
  74. package/src/repo.ts +16 -7
  75. package/src/sort.ts +92 -35
  76. package/src/store/conformance.ts +175 -3
  77. package/src/store/index.ts +7 -1
  78. package/src/store.ts +7 -1
@@ -96,6 +96,7 @@ async function found(
96
96
  collection,
97
97
  where,
98
98
  sort: ['id'],
99
+ optional: [],
99
100
  limit: 100,
100
101
  });
101
102
  return idsOf(page.docs);
@@ -108,6 +109,7 @@ async function pages(
108
109
  sort: readonly string[],
109
110
  limit: number,
110
111
  where?: AnyWhere,
112
+ optional: readonly string[] = [],
111
113
  ): Promise<string[][]> {
112
114
  const seen: string[][] = [];
113
115
  let after: string | undefined;
@@ -115,6 +117,7 @@ async function pages(
115
117
  const page = await store.find({
116
118
  collection,
117
119
  sort,
120
+ optional,
118
121
  limit,
119
122
  ...(where === undefined ? {} : { where }),
120
123
  ...(after === undefined ? {} : { after }),
@@ -244,6 +247,75 @@ function checkPages(
244
247
  );
245
248
  }
246
249
 
250
+ /**
251
+ * Eleven documents for sorts on optional fields: `due` is a date, null,
252
+ * missing or explicitly undefined, and `note` a string, null or missing,
253
+ * with ties on each; `rank` is a required number that ties.
254
+ */
255
+ const dated: readonly StoredDoc[] = [
256
+ { id: 'e00', due: '2026-01-03', rank: 1, note: 'b' },
257
+ { id: 'e01', rank: 2, note: 'a' },
258
+ { id: 'e02', due: '2026-01-01', rank: 1 },
259
+ { id: 'e03', due: null, rank: 1, note: 'a' },
260
+ { id: 'e04', due: '2026-01-03', rank: 2, note: null },
261
+ { id: 'e05', rank: 1, note: 'b' },
262
+ { id: 'e06', due: '2026-01-02', rank: 2 },
263
+ { id: 'e07', due: '2026-01-01', rank: 2, note: 'a' },
264
+ { id: 'e08', due: null, rank: 2, note: 'b' },
265
+ { id: 'e09', rank: 1, note: null },
266
+ { id: 'e10', due: undefined, rank: 2, note: 'a' },
267
+ ];
268
+
269
+ /** The optional fields of `dated`. */
270
+ const datedOptional = ['due', 'note'];
271
+
272
+ /**
273
+ * The order of `docs` under `sort`, with a missing or null value after
274
+ * every value in either direction, written without any store's code.
275
+ */
276
+ function expectedMissingLast(
277
+ docs: readonly StoredDoc[],
278
+ sort: readonly string[],
279
+ ): string[] {
280
+ const keys = sort.map((key) =>
281
+ key.startsWith('-')
282
+ ? { field: key.slice(1), dir: -1 }
283
+ : { field: key, dir: 1 },
284
+ );
285
+ keys.push({ field: 'id', dir: 1 });
286
+ return [...docs]
287
+ .sort((x, y) => {
288
+ for (const { field, dir } of keys) {
289
+ const [p, q] = [x[field] ?? null, y[field] ?? null];
290
+ if (p === q) continue;
291
+ if (p === null) return 1;
292
+ if (q === null) return -1;
293
+ return String(p) < String(q) ? -dir : dir;
294
+ }
295
+ return 0;
296
+ })
297
+ .map((doc) => doc.id);
298
+ }
299
+
300
+ /**
301
+ * Sorts on `dated`, each with the plan MongoStore takes for it: a plain
302
+ * sort when no ascending key is optional, two phases when only the first
303
+ * key is, and a computed key otherwise.
304
+ */
305
+ const optionalSorts: readonly (readonly string[])[] = [
306
+ ['due'], // two phases
307
+ ['-due'], // plain
308
+ ['due', '-rank'], // two phases
309
+ ['-due', 'rank'], // plain
310
+ ['rank', 'due'], // computed
311
+ ['-rank', '-due'], // plain
312
+ ['due', 'note'], // computed, two flags
313
+ ['-due', '-note'], // plain: a nested or after an exists: false prefix
314
+ ['due', '-note'], // two phases, then a descending optional key
315
+ ['-due', 'note'], // computed
316
+ ['note', '-due'], // two phases on note
317
+ ];
318
+
247
319
  export const storeCases: readonly StoreCase[] = [
248
320
  // ---------------------------------------------------------------- methods
249
321
  {
@@ -434,7 +506,12 @@ export const storeCases: readonly StoreCase[] = [
434
506
  name: 'find on an empty or unknown collection is an empty last page',
435
507
  async run(store) {
436
508
  same(
437
- await store.find({ collection: 'm_none', sort: [], limit: 10 }),
509
+ await store.find({
510
+ collection: 'm_none',
511
+ sort: [],
512
+ optional: [],
513
+ limit: 10,
514
+ }),
438
515
  { docs: [], next: null },
439
516
  'find on an unknown collection',
440
517
  );
@@ -574,7 +651,12 @@ export const storeCases: readonly StoreCase[] = [
574
651
  const [many] = await store.getMany('c_copies', ['p1']);
575
652
  Object.assign(many ?? {}, { title: 'changed' });
576
653
  const [first] = (
577
- await store.find({ collection: 'c_copies', sort: [], limit: 1 })
654
+ await store.find({
655
+ collection: 'c_copies',
656
+ sort: [],
657
+ optional: [],
658
+ limit: 1,
659
+ })
578
660
  ).docs;
579
661
  Object.assign(first ?? {}, { title: 'changed' });
580
662
  const patch = { meta: { tags: ['b'] } };
@@ -664,6 +746,7 @@ export const storeCases: readonly StoreCase[] = [
664
746
  const first = await store.find({
665
747
  collection: 'k_insert',
666
748
  sort: ['a'],
749
+ optional: [],
667
750
  limit: 4,
668
751
  });
669
752
  same(idsOf(first.docs), ['d00', 'd03', 'd06', 'd09'], 'page one');
@@ -676,6 +759,7 @@ export const storeCases: readonly StoreCase[] = [
676
759
  const page = await store.find({
677
760
  collection: 'k_insert',
678
761
  sort: ['a'],
762
+ optional: [],
679
763
  limit: 4,
680
764
  after,
681
765
  });
@@ -696,6 +780,7 @@ export const storeCases: readonly StoreCase[] = [
696
780
  const first = await store.find({
697
781
  collection: 'k_bad',
698
782
  sort: ['a'],
783
+ optional: [],
699
784
  limit: 2,
700
785
  });
701
786
  const next = first.next ?? '';
@@ -711,7 +796,13 @@ export const storeCases: readonly StoreCase[] = [
711
796
  ];
712
797
  for (const [sort, after] of bad) {
713
798
  const error = await rejection(
714
- store.find({ collection: 'k_bad', sort, limit: 2, after }),
799
+ store.find({
800
+ collection: 'k_bad',
801
+ sort,
802
+ optional: [],
803
+ limit: 2,
804
+ after,
805
+ }),
715
806
  `find with sort ${show(sort)} and cursor ${after}`,
716
807
  );
717
808
  same(
@@ -727,6 +818,7 @@ export const storeCases: readonly StoreCase[] = [
727
818
  await store.find({
728
819
  collection: 'k_bad',
729
820
  sort: ['a'],
821
+ optional: [],
730
822
  limit: 2,
731
823
  after: next,
732
824
  })
@@ -737,6 +829,84 @@ export const storeCases: readonly StoreCase[] = [
737
829
  },
738
830
  },
739
831
 
832
+ {
833
+ name: 'an optional field sorts with missing and null values last, in either direction, paging across the boundary',
834
+ async run(store) {
835
+ await seed(store, 'o_dated', dated);
836
+ for (const sort of optionalSorts) {
837
+ const expected = expectedMissingLast(dated, sort);
838
+ for (const limit of [1, 2, 3, 4, 10]) {
839
+ checkPages(
840
+ await pages(
841
+ store,
842
+ 'o_dated',
843
+ sort,
844
+ limit,
845
+ undefined,
846
+ datedOptional,
847
+ ),
848
+ expected,
849
+ limit,
850
+ `sort ${show(sort)} by ${limit}`,
851
+ );
852
+ }
853
+ }
854
+ },
855
+ },
856
+ {
857
+ name: 'an optional sort pages under a where, and the where on the field is unchanged',
858
+ async run(store) {
859
+ await seed(store, 'o_where', dated);
860
+ const ranked = dated.filter((doc) => doc.rank === 2);
861
+ // One sort per plan, under a where.
862
+ for (const sort of [['-due'], ['due', '-note'], ['due', 'note']]) {
863
+ checkPages(
864
+ await pages(
865
+ store,
866
+ 'o_where',
867
+ sort,
868
+ 2,
869
+ { rank: { eq: 2 } },
870
+ datedOptional,
871
+ ),
872
+ expectedMissingLast(ranked, sort),
873
+ 2,
874
+ `sort ${show(sort)} under rank 2`,
875
+ );
876
+ }
877
+ same(
878
+ await found(store, 'o_where', { due: { exists: false } }),
879
+ ['e01', 'e03', 'e05', 'e08', 'e09', 'e10'],
880
+ 'the rows without a due date',
881
+ );
882
+ },
883
+ },
884
+ {
885
+ name: 'a cursor with no value for a required key is VALIDATION on after',
886
+ async run(store) {
887
+ await seed(store, 'o_bad', dated);
888
+ const encode = (value: unknown) =>
889
+ Buffer.from(JSON.stringify(value)).toString('base64url');
890
+ const error = await rejection(
891
+ store.find({
892
+ collection: 'o_bad',
893
+ sort: ['rank'],
894
+ optional: [],
895
+ limit: 2,
896
+ after: encode({ sort: ['rank', 'id'], values: [null, 'e00'] }),
897
+ }),
898
+ 'find after a null on a required key',
899
+ );
900
+ same(
901
+ LatticeError.is(error) && error.code === 'VALIDATION'
902
+ ? error.data.issues.map((issue) => issue.path)
903
+ : error,
904
+ [['after']],
905
+ 'the error',
906
+ );
907
+ },
908
+ },
909
+
740
910
  // ---------------------------------------------------------------- transaction
741
911
  {
742
912
  name: 'transaction commits its writes, returns its value, and reads them inside',
@@ -761,6 +931,7 @@ export const storeCases: readonly StoreCase[] = [
761
931
  await store.find({
762
932
  collection: 'x_commit',
763
933
  sort: ['id'],
934
+ optional: [],
764
935
  limit: 10,
765
936
  })
766
937
  ).docs,
@@ -801,6 +972,7 @@ export const storeCases: readonly StoreCase[] = [
801
972
  await store.find({
802
973
  collection: 'x_rollback',
803
974
  sort: ['id'],
975
+ optional: [],
804
976
  limit: 10,
805
977
  })
806
978
  ).docs,
@@ -4,7 +4,13 @@
4
4
  * The conformance suite is `@latticecms/core/store/conformance`.
5
5
  */
6
6
 
7
- export { after, encodeCursor, type Order, orderOf } from '../sort.js';
7
+ export {
8
+ after,
9
+ cursorValues,
10
+ encodeCursor,
11
+ type Order,
12
+ orderOf,
13
+ } from '../sort.js';
8
14
  export {
9
15
  CollectionStore,
10
16
  type FindQuery,
package/src/store.ts CHANGED
@@ -18,6 +18,12 @@ export type FindQuery = {
18
18
  readonly where?: AnyWhere;
19
19
  /** Keys as `field` or `-field`. Stores add `id` as the last tiebreak. */
20
20
  readonly sort: readonly string[];
21
+ /**
22
+ * The sort fields a document may have no value for. Such a document comes
23
+ * after every one with a value, in either direction (0037). `orderOf`
24
+ * takes it, so a store plans a plain sort when nothing here is ascending.
25
+ */
26
+ readonly optional: readonly string[];
21
27
  readonly limit: number;
22
28
  /** The `next` cursor of the previous page. */
23
29
  readonly after?: string;
@@ -214,7 +220,7 @@ export class MemoryStore extends Adapter(CollectionStore) {
214
220
  }
215
221
 
216
222
  async find(query: FindQuery): Promise<Page> {
217
- const order = orderOf(query.sort);
223
+ const order = orderOf(query.sort, query.optional);
218
224
  const start =
219
225
  query.after === undefined ? undefined : after(order, query.after);
220
226
  const matching = [...this.rows(query.collection).values()]