@latticecms/core 0.0.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.
- package/README.md +1297 -2
- package/dist/access.d.ts +209 -0
- package/dist/access.d.ts.map +1 -0
- package/dist/access.js +241 -0
- package/dist/access.js.map +1 -0
- package/dist/build.d.ts +555 -0
- package/dist/build.d.ts.map +1 -0
- package/dist/build.js +655 -0
- package/dist/build.js.map +1 -0
- package/dist/collection.d.ts +480 -0
- package/dist/collection.d.ts.map +1 -0
- package/dist/collection.js +351 -0
- package/dist/collection.js.map +1 -0
- package/dist/crud.d.ts +46 -0
- package/dist/crud.d.ts.map +1 -0
- package/dist/crud.js +527 -0
- package/dist/crud.js.map +1 -0
- package/dist/describe.d.ts +89 -0
- package/dist/describe.d.ts.map +1 -0
- package/dist/describe.js +119 -0
- package/dist/describe.js.map +1 -0
- package/dist/errors.d.ts +183 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +150 -0
- package/dist/errors.js.map +1 -0
- package/dist/family.d.ts +80 -0
- package/dist/family.d.ts.map +1 -0
- package/dist/family.js +227 -0
- package/dist/family.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/marker.d.ts +15 -0
- package/dist/marker.d.ts.map +1 -0
- package/dist/marker.js +28 -0
- package/dist/marker.js.map +1 -0
- package/dist/mask.d.ts +69 -0
- package/dist/mask.d.ts.map +1 -0
- package/dist/mask.js +277 -0
- package/dist/mask.js.map +1 -0
- package/dist/populate.d.ts +39 -0
- package/dist/populate.d.ts.map +1 -0
- package/dist/populate.js +72 -0
- package/dist/populate.js.map +1 -0
- package/dist/relations.d.ts +144 -0
- package/dist/relations.d.ts.map +1 -0
- package/dist/relations.js +144 -0
- package/dist/relations.js.map +1 -0
- package/dist/repo.d.ts +99 -0
- package/dist/repo.d.ts.map +1 -0
- package/dist/repo.js +92 -0
- package/dist/repo.js.map +1 -0
- package/dist/sort.d.ts +86 -0
- package/dist/sort.d.ts.map +1 -0
- package/dist/sort.js +187 -0
- package/dist/sort.js.map +1 -0
- package/dist/store/conformance.d.ts +15 -0
- package/dist/store/conformance.d.ts.map +1 -0
- package/dist/store/conformance.js +811 -0
- package/dist/store/conformance.js.map +1 -0
- package/dist/store/index.d.ts +9 -0
- package/dist/store/index.d.ts.map +1 -0
- package/dist/store/index.js +8 -0
- package/dist/store/index.js.map +1 -0
- package/dist/store.d.ts +119 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/store.js +176 -0
- package/dist/store.js.map +1 -0
- package/dist/tenancy.d.ts +66 -0
- package/dist/tenancy.d.ts.map +1 -0
- package/dist/tenancy.js +69 -0
- package/dist/tenancy.js.map +1 -0
- package/dist/tracing.d.ts +12 -0
- package/dist/tracing.d.ts.map +1 -0
- package/dist/tracing.js +27 -0
- package/dist/tracing.js.map +1 -0
- package/dist/where.d.ts +95 -0
- package/dist/where.d.ts.map +1 -0
- package/dist/where.js +308 -0
- package/dist/where.js.map +1 -0
- package/package.json +80 -2
- package/skills/lattice/SKILL.md +106 -0
- package/skills/lattice/references/access.md +90 -0
- package/skills/lattice/references/cli.md +87 -0
- package/skills/lattice/references/collection.md +94 -0
- package/skills/lattice/references/event-handler.md +88 -0
- package/skills/lattice/references/masking.md +72 -0
- package/skills/lattice/references/method.md +88 -0
- package/skills/lattice/references/override.md +160 -0
- package/skills/lattice/references/relations.md +91 -0
- package/skills/lattice/references/tenancy.md +82 -0
- package/skills/lattice/references/testing.md +75 -0
- package/src/access.ts +449 -0
- package/src/build.ts +1888 -0
- package/src/collection.ts +1025 -0
- package/src/crud.ts +797 -0
- package/src/describe.ts +232 -0
- package/src/errors.ts +174 -0
- package/src/family.ts +334 -0
- package/src/index.ts +44 -0
- package/src/marker.ts +54 -0
- package/src/mask.ts +336 -0
- package/src/populate.ts +103 -0
- package/src/relations.ts +370 -0
- package/src/repo.ts +218 -0
- package/src/sort.ts +271 -0
- package/src/store/conformance.ts +1179 -0
- package/src/store/index.ts +22 -0
- package/src/store.ts +315 -0
- package/src/tenancy.ts +124 -0
- package/src/tracing.ts +29 -0
- package/src/where.ts +463 -0
|
@@ -0,0 +1,811 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@latticecms/core/store/conformance`: the cases every `CollectionStore`
|
|
3
|
+
* adapter passes. They are data, asserted with `node:assert`, so an adapter
|
|
4
|
+
* runs them in any test runner with a fresh store per case:
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* it.each(storeCases)('$name', ({ run }) => run(store));
|
|
8
|
+
* ```
|
|
9
|
+
*
|
|
10
|
+
* A store without transactions runs the cases that do not need them:
|
|
11
|
+
* `storeCases.filter((c) => !c.needs?.includes('transactions'))`.
|
|
12
|
+
*
|
|
13
|
+
* Each case uses its own collection names. A store's semantics follow
|
|
14
|
+
* MongoDB's query rules; `MemoryStore` passes every case.
|
|
15
|
+
*/
|
|
16
|
+
import assert from 'node:assert/strict';
|
|
17
|
+
import { inspect, isDeepStrictEqual } from 'node:util';
|
|
18
|
+
import { LatticeError } from '../errors.js';
|
|
19
|
+
function show(value) {
|
|
20
|
+
return inspect(value, { depth: 8, breakLength: Number.POSITIVE_INFINITY });
|
|
21
|
+
}
|
|
22
|
+
/** Fails, naming `what` and the expected value, unless they are deeply equal. */
|
|
23
|
+
function same(actual, expected, what) {
|
|
24
|
+
if (!isDeepStrictEqual(actual, expected)) {
|
|
25
|
+
assert.fail(`${what}: expected ${show(expected)}, got ${show(actual)}`);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/** What `promise` rejected with, or fails when it resolves. */
|
|
29
|
+
async function rejection(promise, what) {
|
|
30
|
+
const outcome = await promise.then((value) => ({ resolved: true, value }), (error) => ({ resolved: false, value: error }));
|
|
31
|
+
if (outcome.resolved) {
|
|
32
|
+
assert.fail(`${what}: expected a rejection, got ${show(outcome.value)}`);
|
|
33
|
+
}
|
|
34
|
+
return outcome.value;
|
|
35
|
+
}
|
|
36
|
+
/** Fails unless `promise` rejects with Lattice's CONFLICT for `id`. */
|
|
37
|
+
async function conflict(promise, collection, id, what) {
|
|
38
|
+
const error = await rejection(promise, what);
|
|
39
|
+
same(LatticeError.is(error) ? { code: error.code, data: error.data } : error, { code: 'CONFLICT', data: { collection, id } }, what);
|
|
40
|
+
}
|
|
41
|
+
async function seed(store, collection, docs) {
|
|
42
|
+
for (const doc of docs)
|
|
43
|
+
await store.insert(collection, doc);
|
|
44
|
+
}
|
|
45
|
+
function idsOf(docs) {
|
|
46
|
+
return docs.map((doc) => doc.id);
|
|
47
|
+
}
|
|
48
|
+
/** The ids `where` finds in `collection`, by id. */
|
|
49
|
+
async function found(store, collection, where) {
|
|
50
|
+
const page = await store.find({
|
|
51
|
+
collection,
|
|
52
|
+
where,
|
|
53
|
+
sort: ['id'],
|
|
54
|
+
optional: [],
|
|
55
|
+
limit: 100,
|
|
56
|
+
});
|
|
57
|
+
return idsOf(page.docs);
|
|
58
|
+
}
|
|
59
|
+
/** Every page of a find, as ids. */
|
|
60
|
+
async function pages(store, collection, sort, limit, where, optional = []) {
|
|
61
|
+
const seen = [];
|
|
62
|
+
let after;
|
|
63
|
+
for (;;) {
|
|
64
|
+
const page = await store.find({
|
|
65
|
+
collection,
|
|
66
|
+
sort,
|
|
67
|
+
optional,
|
|
68
|
+
limit,
|
|
69
|
+
...(where === undefined ? {} : { where }),
|
|
70
|
+
...(after === undefined ? {} : { after }),
|
|
71
|
+
});
|
|
72
|
+
seen.push(idsOf(page.docs));
|
|
73
|
+
if (page.next === null)
|
|
74
|
+
return seen;
|
|
75
|
+
after = page.next;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const post = (id, fields = {}) => ({
|
|
79
|
+
id,
|
|
80
|
+
title: `Post ${id}`,
|
|
81
|
+
status: 'draft',
|
|
82
|
+
...fields,
|
|
83
|
+
});
|
|
84
|
+
const drafts = { status: { eq: 'draft' } };
|
|
85
|
+
const published = { status: { eq: 'published' } };
|
|
86
|
+
/**
|
|
87
|
+
* Documents for the `Where` cases. `note` is a string, null or missing;
|
|
88
|
+
* `flag` is a boolean or missing; `tags` and `meta` are an array and an
|
|
89
|
+
* object; `at` is an ISO timestamp or missing.
|
|
90
|
+
*/
|
|
91
|
+
const things = [
|
|
92
|
+
{
|
|
93
|
+
id: 't1',
|
|
94
|
+
name: 'Alpha',
|
|
95
|
+
n: 1,
|
|
96
|
+
flag: true,
|
|
97
|
+
note: 'x',
|
|
98
|
+
tags: ['a'],
|
|
99
|
+
meta: { k: 1 },
|
|
100
|
+
at: '2026-01-02T00:00:00.000Z',
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
id: 't2',
|
|
104
|
+
name: 'beta',
|
|
105
|
+
n: 2,
|
|
106
|
+
flag: false,
|
|
107
|
+
note: null,
|
|
108
|
+
tags: [],
|
|
109
|
+
at: '2026-01-10T00:00:00.000Z',
|
|
110
|
+
},
|
|
111
|
+
{ id: 't3', name: 'Gamma (a.b*)', n: 3, at: '2025-12-31T00:00:00.000Z' },
|
|
112
|
+
{ id: 't4', name: 'delta\\', n: 10, flag: true, note: 'y' },
|
|
113
|
+
];
|
|
114
|
+
/** A case that seeds `things` and checks what each filter finds. */
|
|
115
|
+
function whereCase(name, expectations) {
|
|
116
|
+
return {
|
|
117
|
+
name: `where: ${name}`,
|
|
118
|
+
async run(store) {
|
|
119
|
+
await seed(store, 'where_things', things);
|
|
120
|
+
for (const [where, ids] of expectations) {
|
|
121
|
+
same(await found(store, 'where_things', where), ids, `find with ${show(where)}`);
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
// Twelve documents with ties: `a` has three values and `b` two.
|
|
127
|
+
const tied = Array.from({ length: 12 }, (_, i) => ({
|
|
128
|
+
id: `d${String(i).padStart(2, '0')}`,
|
|
129
|
+
a: i % 3,
|
|
130
|
+
b: ['x', 'y'][i % 2],
|
|
131
|
+
keep: i % 4 !== 0,
|
|
132
|
+
}));
|
|
133
|
+
/** The order of `docs` under `sort`, written without any store's code. */
|
|
134
|
+
function expectedOrder(docs, sort) {
|
|
135
|
+
const keys = sort.map((key) => key.startsWith('-')
|
|
136
|
+
? { field: key.slice(1), dir: -1 }
|
|
137
|
+
: { field: key, dir: 1 });
|
|
138
|
+
if (!keys.some((key) => key.field === 'id'))
|
|
139
|
+
keys.push({ field: 'id', dir: 1 });
|
|
140
|
+
return [...docs]
|
|
141
|
+
.sort((x, y) => {
|
|
142
|
+
for (const { field, dir } of keys) {
|
|
143
|
+
const [p, q] = [String(x[field]), String(y[field])];
|
|
144
|
+
if (p !== q)
|
|
145
|
+
return p < q ? -dir : dir;
|
|
146
|
+
}
|
|
147
|
+
return 0;
|
|
148
|
+
})
|
|
149
|
+
.map((doc) => doc.id);
|
|
150
|
+
}
|
|
151
|
+
const sorts = [
|
|
152
|
+
[],
|
|
153
|
+
['a'],
|
|
154
|
+
['-a'],
|
|
155
|
+
['a', 'b'],
|
|
156
|
+
['a', '-b'],
|
|
157
|
+
['-a', 'b'],
|
|
158
|
+
['-a', '-b'],
|
|
159
|
+
['b', '-a'],
|
|
160
|
+
['-id'],
|
|
161
|
+
['-b', 'id', 'a'],
|
|
162
|
+
];
|
|
163
|
+
/** Fails unless the paged ids are `expected`, in full pages but the last. */
|
|
164
|
+
function checkPages(seen, expected, limit, what) {
|
|
165
|
+
same(seen.flat(), expected, `${what}: every doc once, in order`);
|
|
166
|
+
same(seen.slice(0, -1).every((page) => page.length === limit) &&
|
|
167
|
+
seen.every((page) => page.length <= limit), true, `${what}: pages of ${limit}, a shorter one only last (got ${show(seen.map((page) => page.length))})`);
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Eleven documents for sorts on optional fields: `due` is a date, null,
|
|
171
|
+
* missing or explicitly undefined, and `note` a string, null or missing,
|
|
172
|
+
* with ties on each; `rank` is a required number that ties.
|
|
173
|
+
*/
|
|
174
|
+
const dated = [
|
|
175
|
+
{ id: 'e00', due: '2026-01-03', rank: 1, note: 'b' },
|
|
176
|
+
{ id: 'e01', rank: 2, note: 'a' },
|
|
177
|
+
{ id: 'e02', due: '2026-01-01', rank: 1 },
|
|
178
|
+
{ id: 'e03', due: null, rank: 1, note: 'a' },
|
|
179
|
+
{ id: 'e04', due: '2026-01-03', rank: 2, note: null },
|
|
180
|
+
{ id: 'e05', rank: 1, note: 'b' },
|
|
181
|
+
{ id: 'e06', due: '2026-01-02', rank: 2 },
|
|
182
|
+
{ id: 'e07', due: '2026-01-01', rank: 2, note: 'a' },
|
|
183
|
+
{ id: 'e08', due: null, rank: 2, note: 'b' },
|
|
184
|
+
{ id: 'e09', rank: 1, note: null },
|
|
185
|
+
{ id: 'e10', due: undefined, rank: 2, note: 'a' },
|
|
186
|
+
];
|
|
187
|
+
/** The optional fields of `dated`. */
|
|
188
|
+
const datedOptional = ['due', 'note'];
|
|
189
|
+
/**
|
|
190
|
+
* The order of `docs` under `sort`, with a missing or null value after
|
|
191
|
+
* every value in either direction, written without any store's code.
|
|
192
|
+
*/
|
|
193
|
+
function expectedMissingLast(docs, sort) {
|
|
194
|
+
const keys = sort.map((key) => key.startsWith('-')
|
|
195
|
+
? { field: key.slice(1), dir: -1 }
|
|
196
|
+
: { field: key, dir: 1 });
|
|
197
|
+
keys.push({ field: 'id', dir: 1 });
|
|
198
|
+
return [...docs]
|
|
199
|
+
.sort((x, y) => {
|
|
200
|
+
for (const { field, dir } of keys) {
|
|
201
|
+
const [p, q] = [x[field] ?? null, y[field] ?? null];
|
|
202
|
+
if (p === q)
|
|
203
|
+
continue;
|
|
204
|
+
if (p === null)
|
|
205
|
+
return 1;
|
|
206
|
+
if (q === null)
|
|
207
|
+
return -1;
|
|
208
|
+
return String(p) < String(q) ? -dir : dir;
|
|
209
|
+
}
|
|
210
|
+
return 0;
|
|
211
|
+
})
|
|
212
|
+
.map((doc) => doc.id);
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Sorts on `dated`, each with the plan MongoStore takes for it: a plain
|
|
216
|
+
* sort when no ascending key is optional, two phases when only the first
|
|
217
|
+
* key is, and a computed key otherwise.
|
|
218
|
+
*/
|
|
219
|
+
const optionalSorts = [
|
|
220
|
+
['due'], // two phases
|
|
221
|
+
['-due'], // plain
|
|
222
|
+
['due', '-rank'], // two phases
|
|
223
|
+
['-due', 'rank'], // plain
|
|
224
|
+
['rank', 'due'], // computed
|
|
225
|
+
['-rank', '-due'], // plain
|
|
226
|
+
['due', 'note'], // computed, two flags
|
|
227
|
+
['-due', '-note'], // plain: a nested or after an exists: false prefix
|
|
228
|
+
['due', '-note'], // two phases, then a descending optional key
|
|
229
|
+
['-due', 'note'], // computed
|
|
230
|
+
['note', '-due'], // two phases on note
|
|
231
|
+
];
|
|
232
|
+
export const storeCases = [
|
|
233
|
+
// ---------------------------------------------------------------- methods
|
|
234
|
+
{
|
|
235
|
+
name: 'insert and get; a missing id is null',
|
|
236
|
+
async run(store) {
|
|
237
|
+
await seed(store, 'm_get', [post('p1')]);
|
|
238
|
+
same(await store.get('m_get', 'p1'), post('p1'), 'get p1');
|
|
239
|
+
same(await store.get('m_get', 'p2'), null, 'get a missing id');
|
|
240
|
+
same(await store.get('m_get_other', 'p1'), null, 'get from another collection');
|
|
241
|
+
},
|
|
242
|
+
},
|
|
243
|
+
{
|
|
244
|
+
name: 'insert of an existing id is CONFLICT',
|
|
245
|
+
async run(store) {
|
|
246
|
+
await seed(store, 'm_conflict', [post('p1')]);
|
|
247
|
+
await conflict(store.insert('m_conflict', post('p1', { title: 'again' })), 'm_conflict', 'p1', 'insert p1 twice');
|
|
248
|
+
same(await store.get('m_conflict', 'p1'), post('p1'), 'p1 after the conflict');
|
|
249
|
+
},
|
|
250
|
+
},
|
|
251
|
+
{
|
|
252
|
+
name: 'get with where: a row outside it is null',
|
|
253
|
+
async run(store) {
|
|
254
|
+
await seed(store, 'm_get_where', [post('p1')]);
|
|
255
|
+
same(await store.get('m_get_where', 'p1', drafts), post('p1'), 'get inside where');
|
|
256
|
+
same(await store.get('m_get_where', 'p1', published), null, 'get outside where');
|
|
257
|
+
},
|
|
258
|
+
},
|
|
259
|
+
{
|
|
260
|
+
name: 'a where on id never widens a call to another row',
|
|
261
|
+
async run(store) {
|
|
262
|
+
await seed(store, 'm_where_id', [post('p1'), post('p2')]);
|
|
263
|
+
const notP1 = { id: { ne: 'p1' } };
|
|
264
|
+
same(await store.get('m_where_id', 'p1', notP1), null, 'get p1 where id is not p1');
|
|
265
|
+
same(await store.getMany('m_where_id', ['p1'], notP1), [], 'getMany [p1] where id is not p1');
|
|
266
|
+
same(await store.update('m_where_id', 'p1', { title: 'x' }, notP1), null, 'update p1 where id is not p1');
|
|
267
|
+
same(await store.remove('m_where_id', 'p1', notP1), null, 'remove p1 where id is not p1');
|
|
268
|
+
same(await store.get('m_where_id', 'p1', { id: { eq: 'p2' } }), null, 'get p1 where id is p2');
|
|
269
|
+
same(await store.get('m_where_id', 'p2'), post('p2'), 'p2 untouched');
|
|
270
|
+
same(await store.get('m_where_id', 'p1'), post('p1'), 'p1 untouched');
|
|
271
|
+
},
|
|
272
|
+
},
|
|
273
|
+
{
|
|
274
|
+
name: 'getMany returns docs in the order of ids, skipping missing ones and rows outside where',
|
|
275
|
+
async run(store) {
|
|
276
|
+
await seed(store, 'm_many', [
|
|
277
|
+
post('p1'),
|
|
278
|
+
post('p2', { status: 'published' }),
|
|
279
|
+
post('p3'),
|
|
280
|
+
]);
|
|
281
|
+
same(idsOf(await store.getMany('m_many', ['p3', 'nope', 'p1', 'p2'])), ['p3', 'p1', 'p2'], 'getMany without where');
|
|
282
|
+
same(idsOf(await store.getMany('m_many', ['p3', 'p2', 'p1'], drafts)), ['p3', 'p1'], 'getMany with where');
|
|
283
|
+
},
|
|
284
|
+
},
|
|
285
|
+
{
|
|
286
|
+
name: 'update merges shallowly, removes undefined keys and returns the doc',
|
|
287
|
+
async run(store) {
|
|
288
|
+
await seed(store, 'm_update', [
|
|
289
|
+
post('p1', { body: 'b', meta: { a: 1 } }),
|
|
290
|
+
]);
|
|
291
|
+
const expected = {
|
|
292
|
+
id: 'p1',
|
|
293
|
+
title: 'New',
|
|
294
|
+
status: 'draft',
|
|
295
|
+
meta: { b: 2 },
|
|
296
|
+
};
|
|
297
|
+
same(await store.update('m_update', 'p1', {
|
|
298
|
+
title: 'New',
|
|
299
|
+
body: undefined,
|
|
300
|
+
meta: { b: 2 },
|
|
301
|
+
}), expected, 'the updated doc');
|
|
302
|
+
same(await store.get('m_update', 'p1'), expected, 'the stored doc');
|
|
303
|
+
},
|
|
304
|
+
},
|
|
305
|
+
{
|
|
306
|
+
name: 'update of a missing id or a row outside where is null, and changes nothing',
|
|
307
|
+
async run(store) {
|
|
308
|
+
await seed(store, 'm_update_where', [post('p1')]);
|
|
309
|
+
same(await store.update('m_update_where', 'p2', { title: 'x' }), null, 'update a missing id');
|
|
310
|
+
same(await store.update('m_update_where', 'p1', { title: 'x' }, published), null, 'update outside where');
|
|
311
|
+
same(await store.get('m_update_where', 'p1'), post('p1'), 'p1 after both');
|
|
312
|
+
same(await store.update('m_update_where', 'p1', { title: 'x' }, drafts), post('p1', { title: 'x' }), 'update inside where');
|
|
313
|
+
},
|
|
314
|
+
},
|
|
315
|
+
{
|
|
316
|
+
name: 'update never changes id',
|
|
317
|
+
async run(store) {
|
|
318
|
+
await seed(store, 'm_update_id', [post('p1')]);
|
|
319
|
+
// A JavaScript caller can pass an id the Patch type forbids.
|
|
320
|
+
const patch = JSON.parse('{"id":"p9","title":"x"}');
|
|
321
|
+
same(await store.update('m_update_id', 'p1', patch), post('p1', { title: 'x' }), 'update with an id in the patch');
|
|
322
|
+
same(await store.get('m_update_id', 'p9'), null, 'no doc p9');
|
|
323
|
+
},
|
|
324
|
+
},
|
|
325
|
+
{
|
|
326
|
+
name: 'remove returns the doc; a missing id or a row outside where is null',
|
|
327
|
+
async run(store) {
|
|
328
|
+
await seed(store, 'm_remove', [post('p1'), post('p2')]);
|
|
329
|
+
same(await store.remove('m_remove', 'p1', published), null, 'remove outside where');
|
|
330
|
+
same(await store.get('m_remove', 'p1'), post('p1'), 'p1 after it');
|
|
331
|
+
same(await store.remove('m_remove', 'p1', drafts), post('p1'), 'remove inside where');
|
|
332
|
+
same(await store.get('m_remove', 'p1'), null, 'p1 after removal');
|
|
333
|
+
same(await store.remove('m_remove', 'p1'), null, 'remove p1 again');
|
|
334
|
+
same(await store.remove('m_remove', 'p2'), post('p2'), 'remove p2');
|
|
335
|
+
},
|
|
336
|
+
},
|
|
337
|
+
{
|
|
338
|
+
name: 'find on an empty or unknown collection is an empty last page',
|
|
339
|
+
async run(store) {
|
|
340
|
+
same(await store.find({
|
|
341
|
+
collection: 'm_none',
|
|
342
|
+
sort: [],
|
|
343
|
+
optional: [],
|
|
344
|
+
limit: 10,
|
|
345
|
+
}), { docs: [], next: null }, 'find on an unknown collection');
|
|
346
|
+
},
|
|
347
|
+
},
|
|
348
|
+
// ---------------------------------------------------------------- where
|
|
349
|
+
whereCase('eq, ne, in and nin on strings, numbers and booleans', [
|
|
350
|
+
[{ name: { eq: 'beta' } }, ['t2']],
|
|
351
|
+
[{ name: { ne: 'beta' } }, ['t1', 't3', 't4']],
|
|
352
|
+
[{ name: { in: ['Alpha', 'delta\\'] } }, ['t1', 't4']],
|
|
353
|
+
[{ name: { nin: ['Alpha'] } }, ['t2', 't3', 't4']],
|
|
354
|
+
[{ n: { eq: 2 } }, ['t2']],
|
|
355
|
+
[{ n: { ne: 2 } }, ['t1', 't3', 't4']],
|
|
356
|
+
[{ n: { in: [1, 3] } }, ['t1', 't3']],
|
|
357
|
+
[{ n: { nin: [1, 3] } }, ['t2', 't4']],
|
|
358
|
+
[{ flag: { eq: true } }, ['t1', 't4']],
|
|
359
|
+
[{ flag: { eq: false } }, ['t2']],
|
|
360
|
+
[{ flag: { in: [false] } }, ['t2']],
|
|
361
|
+
]),
|
|
362
|
+
whereCase('gt, gte, lt and lte; numbers by value, strings by code unit', [
|
|
363
|
+
[{ n: { gt: 2 } }, ['t3', 't4']],
|
|
364
|
+
[{ n: { gte: 2 } }, ['t2', 't3', 't4']],
|
|
365
|
+
[{ n: { lt: 3 } }, ['t1', 't2']],
|
|
366
|
+
[{ n: { lte: 3 } }, ['t1', 't2', 't3']],
|
|
367
|
+
// Upper case sorts before lower case by code unit.
|
|
368
|
+
[{ name: { gt: 'Z' } }, ['t2', 't4']],
|
|
369
|
+
[{ name: { lt: 'a' } }, ['t1', 't3']],
|
|
370
|
+
[{ at: { gt: '2026-01-01T00:00:00.000Z' } }, ['t1', 't2']],
|
|
371
|
+
[{ at: { lte: '2026-01-02T00:00:00.000Z' } }, ['t1', 't3']],
|
|
372
|
+
]),
|
|
373
|
+
whereCase('null and missing: eq null, ne, nin and in', [
|
|
374
|
+
[{ note: { eq: null } }, ['t2', 't3']],
|
|
375
|
+
[{ note: { in: [null] } }, ['t2', 't3']],
|
|
376
|
+
[{ note: { ne: null } }, ['t1', 't4']],
|
|
377
|
+
[{ note: { nin: [null] } }, ['t1', 't4']],
|
|
378
|
+
[{ note: { ne: 'x' } }, ['t2', 't3', 't4']],
|
|
379
|
+
[{ note: { nin: ['x'] } }, ['t2', 't3', 't4']],
|
|
380
|
+
[{ flag: { ne: true } }, ['t2', 't3']],
|
|
381
|
+
[{ flag: { nin: [true] } }, ['t2', 't3']],
|
|
382
|
+
]),
|
|
383
|
+
whereCase('a comparison with null or missing is false', [
|
|
384
|
+
[{ note: { gt: 'a' } }, ['t1', 't4']],
|
|
385
|
+
[{ note: { lt: 'z' } }, ['t1', 't4']],
|
|
386
|
+
[{ at: { lt: '2027-01-01T00:00:00.000Z' } }, ['t1', 't2', 't3']],
|
|
387
|
+
]),
|
|
388
|
+
{
|
|
389
|
+
name: 'where: has matches an array with an equal element; a missing or null array has none',
|
|
390
|
+
async run(store) {
|
|
391
|
+
await seed(store, 'where_has', [
|
|
392
|
+
{ id: 'h1', tags: ['a', 'b'], nums: [1, 2] },
|
|
393
|
+
{ id: 'h2', tags: ['b'], nums: [] },
|
|
394
|
+
{ id: 'h3', tags: [], nums: null },
|
|
395
|
+
{ id: 'h4', tags: null },
|
|
396
|
+
{ id: 'h5' },
|
|
397
|
+
]);
|
|
398
|
+
const expectations = [
|
|
399
|
+
[{ tags: { has: 'a' } }, ['h1']],
|
|
400
|
+
[{ tags: { has: 'b' } }, ['h1', 'h2']],
|
|
401
|
+
[{ tags: { has: 'z' } }, []],
|
|
402
|
+
[{ nums: { has: 2 } }, ['h1']],
|
|
403
|
+
[{ nums: { has: 3 } }, []],
|
|
404
|
+
[{ or: [{ tags: { has: 'a' } }, { nums: { has: 9 } }] }, ['h1']],
|
|
405
|
+
];
|
|
406
|
+
for (const [where, ids] of expectations) {
|
|
407
|
+
same(await found(store, 'where_has', where), ids, `find with ${show(where)}`);
|
|
408
|
+
}
|
|
409
|
+
same(await store.count('where_has', { tags: { has: 'b' } }), 2, 'count with has');
|
|
410
|
+
},
|
|
411
|
+
},
|
|
412
|
+
whereCase('exists: null does not exist; arrays and objects', [
|
|
413
|
+
[{ note: { exists: true } }, ['t1', 't4']],
|
|
414
|
+
[{ note: { exists: false } }, ['t2', 't3']],
|
|
415
|
+
[{ tags: { exists: true } }, ['t1', 't2']],
|
|
416
|
+
[{ meta: { exists: true } }, ['t1']],
|
|
417
|
+
[{ meta: { exists: false } }, ['t2', 't3', 't4']],
|
|
418
|
+
]),
|
|
419
|
+
whereCase('contains is case-insensitive and literal, never a regex', [
|
|
420
|
+
[{ name: { contains: 'ALPHA' } }, ['t1']],
|
|
421
|
+
[{ name: { contains: '.' } }, ['t3']],
|
|
422
|
+
[{ name: { contains: '*' } }, ['t3']],
|
|
423
|
+
[{ name: { contains: '(a.b*)' } }, ['t3']],
|
|
424
|
+
[{ name: { contains: '\\' } }, ['t4']],
|
|
425
|
+
[{ note: { contains: 'x' } }, ['t1']],
|
|
426
|
+
]),
|
|
427
|
+
whereCase('several operators, or several fields, all have to match', [
|
|
428
|
+
[{ n: { gt: 1, lt: 10 } }, ['t2', 't3']],
|
|
429
|
+
[{ flag: { eq: true }, n: { gt: 1 } }, ['t4']],
|
|
430
|
+
]),
|
|
431
|
+
whereCase('and, or and nesting', [
|
|
432
|
+
[{ or: [{ n: { eq: 1 } }, { name: { eq: 'beta' } }] }, ['t1', 't2']],
|
|
433
|
+
[
|
|
434
|
+
{
|
|
435
|
+
and: [
|
|
436
|
+
{ or: [{ n: { eq: 1 } }, { n: { eq: 10 } }] },
|
|
437
|
+
{ flag: { eq: true } },
|
|
438
|
+
],
|
|
439
|
+
},
|
|
440
|
+
['t1', 't4'],
|
|
441
|
+
],
|
|
442
|
+
[
|
|
443
|
+
{
|
|
444
|
+
or: [
|
|
445
|
+
{ and: [{ n: { gte: 3 } }, { flag: { exists: false } }] },
|
|
446
|
+
{ note: { eq: 'x' } },
|
|
447
|
+
],
|
|
448
|
+
},
|
|
449
|
+
['t1', 't3'],
|
|
450
|
+
],
|
|
451
|
+
[
|
|
452
|
+
{ n: { gt: 1 }, or: [{ note: { eq: null } }, { note: { eq: 'y' } }] },
|
|
453
|
+
['t2', 't3', 't4'],
|
|
454
|
+
],
|
|
455
|
+
]),
|
|
456
|
+
// ---------------------------------------------------------------- copies
|
|
457
|
+
{
|
|
458
|
+
name: 'stores copies: mutating an argument or a returned doc changes nothing',
|
|
459
|
+
async run(store) {
|
|
460
|
+
const original = post('p1', { meta: { tags: ['a'] } });
|
|
461
|
+
const arg = structuredClone(original);
|
|
462
|
+
await store.insert('c_copies', arg);
|
|
463
|
+
arg.meta = 'changed';
|
|
464
|
+
const got = await store.get('c_copies', 'p1');
|
|
465
|
+
Object.assign(got ?? {}, { title: 'changed' });
|
|
466
|
+
const [many] = await store.getMany('c_copies', ['p1']);
|
|
467
|
+
Object.assign(many ?? {}, { title: 'changed' });
|
|
468
|
+
const [first] = (await store.find({
|
|
469
|
+
collection: 'c_copies',
|
|
470
|
+
sort: [],
|
|
471
|
+
optional: [],
|
|
472
|
+
limit: 1,
|
|
473
|
+
})).docs;
|
|
474
|
+
Object.assign(first ?? {}, { title: 'changed' });
|
|
475
|
+
const patch = { meta: { tags: ['b'] } };
|
|
476
|
+
const updated = await store.update('c_copies', 'p1', patch);
|
|
477
|
+
patch.meta.tags.push('mutated');
|
|
478
|
+
Object.assign(updated ?? {}, { title: 'changed' });
|
|
479
|
+
same(await store.get('c_copies', 'p1'), post('p1', { meta: { tags: ['b'] } }), 'the stored doc after every mutation');
|
|
480
|
+
},
|
|
481
|
+
},
|
|
482
|
+
{
|
|
483
|
+
name: 'remove returns the stored doc, not the inserted argument',
|
|
484
|
+
async run(store) {
|
|
485
|
+
const arg = post('p1');
|
|
486
|
+
await store.insert('c_remove', arg);
|
|
487
|
+
arg.title = 'changed';
|
|
488
|
+
same(await store.remove('c_remove', 'p1'), post('p1'), 'the removed doc');
|
|
489
|
+
},
|
|
490
|
+
},
|
|
491
|
+
// ---------------------------------------------------------------- cursors
|
|
492
|
+
{
|
|
493
|
+
name: 'pages every sort with ties: each doc once, in order',
|
|
494
|
+
async run(store) {
|
|
495
|
+
await seed(store, 'k_sorts', tied);
|
|
496
|
+
for (const sort of sorts) {
|
|
497
|
+
for (const limit of [1, 2, 5, 12, 20]) {
|
|
498
|
+
checkPages(await pages(store, 'k_sorts', sort, limit), expectedOrder(tied, sort), limit, `sort ${show(sort)}, limit ${limit}`);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
},
|
|
502
|
+
},
|
|
503
|
+
{
|
|
504
|
+
name: 'next is null on the last page, even when it is full',
|
|
505
|
+
async run(store) {
|
|
506
|
+
await seed(store, 'k_full', tied);
|
|
507
|
+
same((await pages(store, 'k_full', ['a'], 4)).map((page) => page.length), [4, 4, 4], 'page sizes');
|
|
508
|
+
},
|
|
509
|
+
},
|
|
510
|
+
{
|
|
511
|
+
name: 'pages under a where with ties',
|
|
512
|
+
async run(store) {
|
|
513
|
+
await seed(store, 'k_where', tied);
|
|
514
|
+
const keep = { keep: { eq: true } };
|
|
515
|
+
const kept = tied.filter((doc) => doc.keep === true);
|
|
516
|
+
for (const sort of [['a'], ['-a', 'b'], ['b', '-a']]) {
|
|
517
|
+
checkPages(await pages(store, 'k_where', sort, 2, keep), expectedOrder(kept, sort), 2, `sort ${show(sort)} under keep`);
|
|
518
|
+
}
|
|
519
|
+
},
|
|
520
|
+
},
|
|
521
|
+
{
|
|
522
|
+
name: 'pages under an or: the keyset ANDs with the caller’s or',
|
|
523
|
+
async run(store) {
|
|
524
|
+
await seed(store, 'k_or', tied);
|
|
525
|
+
const where = { or: [{ a: { eq: 0 } }, { b: { eq: 'y' } }] };
|
|
526
|
+
const matching = tied.filter((doc) => doc.a === 0 || doc.b === 'y');
|
|
527
|
+
checkPages(await pages(store, 'k_or', ['-a', 'b'], 3, where), expectedOrder(matching, ['-a', 'b']), 3, 'sort -a, b under the or');
|
|
528
|
+
},
|
|
529
|
+
},
|
|
530
|
+
{
|
|
531
|
+
name: 'a doc inserted after page one appears by its sort position',
|
|
532
|
+
async run(store) {
|
|
533
|
+
await seed(store, 'k_insert', tied);
|
|
534
|
+
const first = await store.find({
|
|
535
|
+
collection: 'k_insert',
|
|
536
|
+
sort: ['a'],
|
|
537
|
+
optional: [],
|
|
538
|
+
limit: 4,
|
|
539
|
+
});
|
|
540
|
+
same(idsOf(first.docs), ['d00', 'd03', 'd06', 'd09'], 'page one');
|
|
541
|
+
// Before the cursor (a 0, id d09): never seen. After it: seen in place.
|
|
542
|
+
await store.insert('k_insert', { id: 'd01x', a: 0, b: 'x', keep: true });
|
|
543
|
+
await store.insert('k_insert', { id: 'd99', a: 2, b: 'x', keep: true });
|
|
544
|
+
const rest = [];
|
|
545
|
+
let after = first.next;
|
|
546
|
+
while (after !== null) {
|
|
547
|
+
const page = await store.find({
|
|
548
|
+
collection: 'k_insert',
|
|
549
|
+
sort: ['a'],
|
|
550
|
+
optional: [],
|
|
551
|
+
limit: 4,
|
|
552
|
+
after,
|
|
553
|
+
});
|
|
554
|
+
rest.push(...idsOf(page.docs));
|
|
555
|
+
after = page.next;
|
|
556
|
+
}
|
|
557
|
+
same(rest, ['d01', 'd04', 'd07', 'd10', 'd02', 'd05', 'd08', 'd11', 'd99'], 'the later pages');
|
|
558
|
+
},
|
|
559
|
+
},
|
|
560
|
+
{
|
|
561
|
+
name: 'a cursor from another sort, or a malformed one, is VALIDATION on after',
|
|
562
|
+
async run(store) {
|
|
563
|
+
await seed(store, 'k_bad', tied);
|
|
564
|
+
const first = await store.find({
|
|
565
|
+
collection: 'k_bad',
|
|
566
|
+
sort: ['a'],
|
|
567
|
+
optional: [],
|
|
568
|
+
limit: 2,
|
|
569
|
+
});
|
|
570
|
+
const next = first.next ?? '';
|
|
571
|
+
const encode = (value) => Buffer.from(JSON.stringify(value)).toString('base64url');
|
|
572
|
+
const bad = [
|
|
573
|
+
[['-a'], next],
|
|
574
|
+
[['a', 'b'], next],
|
|
575
|
+
[['a'], 'not a cursor'],
|
|
576
|
+
[['a'], encode({ sort: ['a', 'id'] })],
|
|
577
|
+
[['a'], encode({ sort: ['a', 'id'], values: [1] })],
|
|
578
|
+
[['a'], encode({ sort: ['a', 'id'], values: [1, {}] })],
|
|
579
|
+
];
|
|
580
|
+
for (const [sort, after] of bad) {
|
|
581
|
+
const error = await rejection(store.find({
|
|
582
|
+
collection: 'k_bad',
|
|
583
|
+
sort,
|
|
584
|
+
optional: [],
|
|
585
|
+
limit: 2,
|
|
586
|
+
after,
|
|
587
|
+
}), `find with sort ${show(sort)} and cursor ${after}`);
|
|
588
|
+
same(LatticeError.is(error) && error.code === 'VALIDATION'
|
|
589
|
+
? error.data.issues.map((issue) => issue.path)
|
|
590
|
+
: error, [['after']], `the error for sort ${show(sort)} and cursor ${after}`);
|
|
591
|
+
}
|
|
592
|
+
same((await store.find({
|
|
593
|
+
collection: 'k_bad',
|
|
594
|
+
sort: ['a'],
|
|
595
|
+
optional: [],
|
|
596
|
+
limit: 2,
|
|
597
|
+
after: next,
|
|
598
|
+
})).docs.length, 2, 'the good cursor still pages');
|
|
599
|
+
},
|
|
600
|
+
},
|
|
601
|
+
{
|
|
602
|
+
name: 'an optional field sorts with missing and null values last, in either direction, paging across the boundary',
|
|
603
|
+
async run(store) {
|
|
604
|
+
await seed(store, 'o_dated', dated);
|
|
605
|
+
for (const sort of optionalSorts) {
|
|
606
|
+
const expected = expectedMissingLast(dated, sort);
|
|
607
|
+
for (const limit of [1, 2, 3, 4, 10]) {
|
|
608
|
+
checkPages(await pages(store, 'o_dated', sort, limit, undefined, datedOptional), expected, limit, `sort ${show(sort)} by ${limit}`);
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
},
|
|
612
|
+
},
|
|
613
|
+
{
|
|
614
|
+
name: 'an optional sort pages under a where, and the where on the field is unchanged',
|
|
615
|
+
async run(store) {
|
|
616
|
+
await seed(store, 'o_where', dated);
|
|
617
|
+
const ranked = dated.filter((doc) => doc.rank === 2);
|
|
618
|
+
// One sort per plan, under a where.
|
|
619
|
+
for (const sort of [['-due'], ['due', '-note'], ['due', 'note']]) {
|
|
620
|
+
checkPages(await pages(store, 'o_where', sort, 2, { rank: { eq: 2 } }, datedOptional), expectedMissingLast(ranked, sort), 2, `sort ${show(sort)} under rank 2`);
|
|
621
|
+
}
|
|
622
|
+
same(await found(store, 'o_where', { due: { exists: false } }), ['e01', 'e03', 'e05', 'e08', 'e09', 'e10'], 'the rows without a due date');
|
|
623
|
+
},
|
|
624
|
+
},
|
|
625
|
+
{
|
|
626
|
+
name: 'a cursor with no value for a required key is VALIDATION on after',
|
|
627
|
+
async run(store) {
|
|
628
|
+
await seed(store, 'o_bad', dated);
|
|
629
|
+
const encode = (value) => Buffer.from(JSON.stringify(value)).toString('base64url');
|
|
630
|
+
const error = await rejection(store.find({
|
|
631
|
+
collection: 'o_bad',
|
|
632
|
+
sort: ['rank'],
|
|
633
|
+
optional: [],
|
|
634
|
+
limit: 2,
|
|
635
|
+
after: encode({ sort: ['rank', 'id'], values: [null, 'e00'] }),
|
|
636
|
+
}), 'find after a null on a required key');
|
|
637
|
+
same(LatticeError.is(error) && error.code === 'VALIDATION'
|
|
638
|
+
? error.data.issues.map((issue) => issue.path)
|
|
639
|
+
: error, [['after']], 'the error');
|
|
640
|
+
},
|
|
641
|
+
},
|
|
642
|
+
// ---------------------------------------------------------------- transaction
|
|
643
|
+
{
|
|
644
|
+
name: 'transaction commits its writes, returns its value, and reads them inside',
|
|
645
|
+
async run(store) {
|
|
646
|
+
await seed(store, 'x_commit', [post('p1'), post('p2')]);
|
|
647
|
+
const result = await store.transaction(async (tx) => {
|
|
648
|
+
await tx.insert('x_commit', post('p3'));
|
|
649
|
+
await tx.update('x_commit', 'p1', { title: 'changed' });
|
|
650
|
+
await tx.remove('x_commit', 'p2');
|
|
651
|
+
same(await tx.get('x_commit', 'p3'), post('p3'), 'p3 read inside the work');
|
|
652
|
+
same(await tx.count('x_commit'), 2, 'count inside the work');
|
|
653
|
+
return 'done';
|
|
654
|
+
});
|
|
655
|
+
same(result, 'done', 'the value of the work');
|
|
656
|
+
same(idsOf((await store.find({
|
|
657
|
+
collection: 'x_commit',
|
|
658
|
+
sort: ['id'],
|
|
659
|
+
optional: [],
|
|
660
|
+
limit: 10,
|
|
661
|
+
})).docs), ['p1', 'p3'], 'ids after the commit');
|
|
662
|
+
same(await store.get('x_commit', 'p1'), post('p1', { title: 'changed' }), 'p1 after the commit');
|
|
663
|
+
},
|
|
664
|
+
},
|
|
665
|
+
{
|
|
666
|
+
name: 'transaction rolls back every write when the work throws, and the error passes through',
|
|
667
|
+
needs: ['transactions'],
|
|
668
|
+
async run(store) {
|
|
669
|
+
await seed(store, 'x_rollback', [post('p1'), post('p2')]);
|
|
670
|
+
const failure = new Error('rolled back');
|
|
671
|
+
const error = await rejection(store.transaction(async (tx) => {
|
|
672
|
+
await tx.insert('x_rollback', post('p3'));
|
|
673
|
+
await tx.update('x_rollback', 'p1', { title: 'changed' });
|
|
674
|
+
await tx.remove('x_rollback', 'p2');
|
|
675
|
+
same(await tx.get('x_rollback', 'p3'), post('p3'), 'p3 read inside the work');
|
|
676
|
+
throw failure;
|
|
677
|
+
}), 'a throwing transaction');
|
|
678
|
+
same(error === failure, true, 'the work’s own error');
|
|
679
|
+
same((await store.find({
|
|
680
|
+
collection: 'x_rollback',
|
|
681
|
+
sort: ['id'],
|
|
682
|
+
optional: [],
|
|
683
|
+
limit: 10,
|
|
684
|
+
})).docs, [post('p1'), post('p2')], 'the documents after the rollback');
|
|
685
|
+
},
|
|
686
|
+
},
|
|
687
|
+
{
|
|
688
|
+
name: 'a nested transaction joins the outer one, and a throw rolls back both',
|
|
689
|
+
needs: ['transactions'],
|
|
690
|
+
async run(store) {
|
|
691
|
+
await seed(store, 'x_nested', [post('p1')]);
|
|
692
|
+
await rejection(store.transaction(async (tx) => {
|
|
693
|
+
await tx.transaction(async (inner) => {
|
|
694
|
+
await inner.insert('x_nested', post('p2'));
|
|
695
|
+
});
|
|
696
|
+
same(await tx.get('x_nested', 'p2'), post('p2'), 'the inner write, inside');
|
|
697
|
+
throw new Error('outer failed');
|
|
698
|
+
}), 'an outer throw after an inner transaction');
|
|
699
|
+
same(await store.get('x_nested', 'p2'), null, 'p2 after the rollback');
|
|
700
|
+
},
|
|
701
|
+
},
|
|
702
|
+
// ---------------------------------------------------------------- count
|
|
703
|
+
{
|
|
704
|
+
name: 'count on an empty or unknown collection is 0',
|
|
705
|
+
async run(store) {
|
|
706
|
+
same(await store.count('n_none'), 0, 'count of an unknown collection');
|
|
707
|
+
same(await store.count('n_none', drafts), 0, 'count with where');
|
|
708
|
+
},
|
|
709
|
+
},
|
|
710
|
+
{
|
|
711
|
+
name: 'count counts every doc, or those in where, as find does',
|
|
712
|
+
async run(store) {
|
|
713
|
+
await seed(store, 'n_count', things);
|
|
714
|
+
same(await store.count('n_count'), 4, 'count of every doc');
|
|
715
|
+
const filters = [
|
|
716
|
+
{ note: { eq: null } },
|
|
717
|
+
{ n: { gt: 1, lt: 10 } },
|
|
718
|
+
{ or: [{ flag: { eq: true } }, { tags: { exists: true } }] },
|
|
719
|
+
{ name: { eq: 'none' } },
|
|
720
|
+
];
|
|
721
|
+
for (const where of filters) {
|
|
722
|
+
same(await store.count('n_count', where), (await found(store, 'n_count', where)).length, `count with ${show(where)}`);
|
|
723
|
+
}
|
|
724
|
+
},
|
|
725
|
+
},
|
|
726
|
+
// ---------------------------------------------------------------- ensure
|
|
727
|
+
{
|
|
728
|
+
name: 'ensure is idempotent and never drops an index',
|
|
729
|
+
async run(store) {
|
|
730
|
+
const unique = { fields: { email: 1 }, unique: true };
|
|
731
|
+
await store.ensure('e_keep', [unique]);
|
|
732
|
+
await store.ensure('e_keep', [unique]);
|
|
733
|
+
await store.ensure('e_keep', []);
|
|
734
|
+
await store.insert('e_keep', { id: 'u1', email: 'a@x' });
|
|
735
|
+
await conflict(store.insert('e_keep', { id: 'u2', email: 'a@x' }), 'e_keep', 'u2', 'a duplicate after ensure([])');
|
|
736
|
+
},
|
|
737
|
+
},
|
|
738
|
+
{
|
|
739
|
+
name: 'a unique index: a duplicate insert or update is CONFLICT, and nothing is stored',
|
|
740
|
+
async run(store) {
|
|
741
|
+
await store.ensure('e_unique', [{ fields: { email: 1 }, unique: true }]);
|
|
742
|
+
await seed(store, 'e_unique', [
|
|
743
|
+
{ id: 'u1', email: 'a@x' },
|
|
744
|
+
{ id: 'u2', email: 'b@x' },
|
|
745
|
+
]);
|
|
746
|
+
await conflict(store.insert('e_unique', { id: 'u3', email: 'a@x' }), 'e_unique', 'u3', 'insert a duplicate email');
|
|
747
|
+
same(await store.get('e_unique', 'u3'), null, 'u3 after the conflict');
|
|
748
|
+
await conflict(store.update('e_unique', 'u2', { email: 'a@x' }), 'e_unique', 'u2', 'update to a duplicate email');
|
|
749
|
+
same(await store.get('e_unique', 'u2'), { id: 'u2', email: 'b@x' }, 'u2 after the conflict');
|
|
750
|
+
same(await store.update('e_unique', 'u1', { email: 'a@x', name: 'kept' }), { id: 'u1', email: 'a@x', name: 'kept' }, 'update that keeps its own value');
|
|
751
|
+
},
|
|
752
|
+
},
|
|
753
|
+
{
|
|
754
|
+
name: 'a compound unique index conflicts on the whole tuple only',
|
|
755
|
+
async run(store) {
|
|
756
|
+
await store.ensure('e_compound', [
|
|
757
|
+
{ fields: { org: 1, email: 1 }, unique: true },
|
|
758
|
+
]);
|
|
759
|
+
await seed(store, 'e_compound', [
|
|
760
|
+
{ id: 'u1', org: 'o1', email: 'a@x' },
|
|
761
|
+
{ id: 'u2', org: 'o2', email: 'a@x' },
|
|
762
|
+
]);
|
|
763
|
+
await conflict(store.insert('e_compound', { id: 'u3', org: 'o1', email: 'a@x' }), 'e_compound', 'u3', 'insert the same org and email');
|
|
764
|
+
},
|
|
765
|
+
},
|
|
766
|
+
{
|
|
767
|
+
name: 'a unique index treats a missing field as null',
|
|
768
|
+
async run(store) {
|
|
769
|
+
await store.ensure('e_missing', [{ fields: { email: 1 }, unique: true }]);
|
|
770
|
+
await store.insert('e_missing', { id: 'u1' });
|
|
771
|
+
await conflict(store.insert('e_missing', { id: 'u2' }), 'e_missing', 'u2', 'a second doc missing email');
|
|
772
|
+
await conflict(store.insert('e_missing', { id: 'u3', email: null }), 'e_missing', 'u3', 'a null email beside a missing one');
|
|
773
|
+
},
|
|
774
|
+
},
|
|
775
|
+
{
|
|
776
|
+
name: 'ensuring a unique index over duplicates rejects',
|
|
777
|
+
async run(store) {
|
|
778
|
+
await seed(store, 'e_dupes', [
|
|
779
|
+
{ id: 'u1', email: 'a@x' },
|
|
780
|
+
{ id: 'u2', email: 'a@x' },
|
|
781
|
+
]);
|
|
782
|
+
await rejection(store.ensure('e_dupes', [{ fields: { email: 1 }, unique: true }]), 'ensure a unique index over two a@x');
|
|
783
|
+
},
|
|
784
|
+
},
|
|
785
|
+
{
|
|
786
|
+
name: 'ensuring the same keys with other options rejects, and keeps the first',
|
|
787
|
+
async run(store) {
|
|
788
|
+
await store.ensure('e_options', [{ fields: { email: 1 } }]);
|
|
789
|
+
await rejection(store.ensure('e_options', [{ fields: { email: 1 }, unique: true }]), 'ensure a unique index on keys a non-unique index has');
|
|
790
|
+
await seed(store, 'e_options', [
|
|
791
|
+
{ id: 'u1', email: 'a@x' },
|
|
792
|
+
{ id: 'u2', email: 'a@x' },
|
|
793
|
+
]);
|
|
794
|
+
same(await store.count('e_options', { email: { eq: 'a@x' } }), 2, 'duplicates under the first, non-unique index');
|
|
795
|
+
await store.ensure('e_options_unique', [
|
|
796
|
+
{ fields: { email: 1 }, unique: true },
|
|
797
|
+
]);
|
|
798
|
+
await rejection(store.ensure('e_options_unique', [{ fields: { email: 1 } }]), 'ensure a non-unique index on keys a unique index has');
|
|
799
|
+
},
|
|
800
|
+
},
|
|
801
|
+
{
|
|
802
|
+
name: 'a non-unique index changes no results',
|
|
803
|
+
async run(store) {
|
|
804
|
+
await store.ensure('e_plain', [{ fields: { a: 1, b: -1 } }]);
|
|
805
|
+
await seed(store, 'e_plain', tied);
|
|
806
|
+
same((await pages(store, 'e_plain', ['a', 'b'], 5)).flat(), expectedOrder(tied, ['a', 'b']), 'find under the index');
|
|
807
|
+
same(await store.count('e_plain', { a: { eq: 1 } }), 4, 'count under the index');
|
|
808
|
+
},
|
|
809
|
+
},
|
|
810
|
+
];
|
|
811
|
+
//# sourceMappingURL=conformance.js.map
|