@ultimat3/entity 31.6.1 → 31.8.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 +1 -1
- package/package.json +5 -5
- package/src/declared-index.ts +3 -2
- package/src/describe.ts +3 -2
- package/src/index-identity.ts +13 -8
- package/src/index-init.ts +3 -0
- package/src/lifecycle.ts +8 -0
- package/src/memory-lifecycle.ts +7 -1
- package/src/registry.ts +4 -3
- package/src/search-repo.ts +16 -0
- package/src/types.ts +5 -2
package/README.md
CHANGED
|
@@ -619,7 +619,7 @@ export const find = async (orgId: string, q: string) => {
|
|
|
619
619
|
|
|
620
620
|
| Rule | Detail |
|
|
621
621
|
|---|---|
|
|
622
|
-
| `.search(term)` asks the installed index | built-in: the `matches` predicate in the statement (today's SQL). Any other: ids first
|
|
622
|
+
| `.search(term)` asks the installed index | built-in: the `matches` predicate in the statement (today's SQL). Any other: ids first — 10,000 (`MAX_PAGE_SIZE`) or more hits are refused with `X_INVARIANT_VIOLATED`, never truncated — then the chain reads those rows with its own tenancy, soft delete, order and cursor |
|
|
623
623
|
| a document has a **version** | the row's `onUpdateNow()` instant, epoch ms. A searchable entity indexed by anything declares one — `updatedAt: timestamp().defaultNow().onUpdateNow()` — or `searchDocumentOf` and `reindex()` refuse (`X_INVARIANT_VIOLATED`) |
|
|
624
624
|
| the version is STRICTLY increasing per row | a write of a searchable entity — a soft delete included — stores its version no earlier than the stored one + 1 ms: Postgres `greatest($now, updated_at + interval '1 millisecond')` in the `update` and the upsert's `on conflict` set; the memory driver in its merge (`advanceVersion`); a `defineDriver` store in ITS write, atomically per row — `DriverRepo.update`'s `advance` argument, and `advanceVersion(entity, stored, merged, (p) => plan.set.includes(p))` in its own `upsert` (`entityDriverConformance` refuses a store that skips either). So two writes inside one millisecond are two versions, and the index keeps the second. The column may run ahead of the clock by a millisecond per same-ms write. Other entities' `onUpdateNow()` columns are the clock's reading, unchanged |
|
|
625
625
|
| keep only newer | an index applies a document only when its version is newer than what it holds for that id: a document, or the tombstone `remove(scope, ids, version)` leaves (a delete's: past the row's last version and no earlier than its instant). Equal = replay. EXCEPT a `committed` document — the write path's, `index(docs, { committed: true })` — which replaces a tombstone whatever its version: a key deleted and written again is live at once, inside one millisecond too. `reindex`'s reads are not `committed`, so a stale read never brings a deleted row back |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ultimat3/entity",
|
|
3
|
-
"version": "31.
|
|
3
|
+
"version": "31.8.0",
|
|
4
4
|
"description": "A table + its domain type + invariants the database also enforces",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ultimate",
|
|
@@ -44,9 +44,9 @@
|
|
|
44
44
|
"test": "bun test"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@ultimat3/core": "31.
|
|
48
|
-
"@ultimat3/db": "31.
|
|
49
|
-
"@ultimat3/schema": "31.
|
|
50
|
-
"@ultimat3/time": "31.
|
|
47
|
+
"@ultimat3/core": "31.8.0",
|
|
48
|
+
"@ultimat3/db": "31.8.0",
|
|
49
|
+
"@ultimat3/schema": "31.8.0",
|
|
50
|
+
"@ultimat3/time": "31.8.0"
|
|
51
51
|
}
|
|
52
52
|
}
|
package/src/declared-index.ts
CHANGED
|
@@ -66,7 +66,8 @@ export const declaredIndexOf = <C extends ColumnMap>(
|
|
|
66
66
|
// Absent stays absent: `btree` written out would be a field every existing snapshot lacks,
|
|
67
67
|
// and `indexMethodOf` reads the two the same way precisely so nothing regenerates.
|
|
68
68
|
...(index.using === undefined || index.using === 'btree' ? {} : { using: index.using }),
|
|
69
|
-
// How it is BUILT, so not part of the name
|
|
70
|
-
|
|
69
|
+
// How it is BUILT, so not part of the name. Absent stays absent — the default every existing
|
|
70
|
+
// index already projects — and only an explicit ask, either way, rides along.
|
|
71
|
+
...(index.concurrently === undefined ? {} : { concurrently: index.concurrently }),
|
|
71
72
|
};
|
|
72
73
|
};
|
package/src/describe.ts
CHANGED
|
@@ -363,8 +363,9 @@ export const describeEntity = <Row>(input: DescribeInput<Row>): EntityDescriptio
|
|
|
363
363
|
// Spread, never `?? null`: absent is what `@ultimat3/db` reads as the btree it always was,
|
|
364
364
|
// and a written-out `null` would be a field no existing snapshot carries.
|
|
365
365
|
...(index.using === undefined ? {} : { using: index.using }),
|
|
366
|
-
//
|
|
367
|
-
|
|
366
|
+
// Absent stays absent for the same reason — `x verify` hashes this projection, and an index
|
|
367
|
+
// that asks nothing must hash what it always did. An explicit ask, `true` or `false`, rides.
|
|
368
|
+
...(index.concurrently === undefined ? {} : { concurrently: index.concurrently }),
|
|
368
369
|
})),
|
|
369
370
|
tags: input.tags,
|
|
370
371
|
cacheTag: input.cacheTag,
|
package/src/index-identity.ts
CHANGED
|
@@ -28,8 +28,9 @@ const identity = (index: IndexDef): string =>
|
|
|
28
28
|
* definition keeps both, and two `create index` statements sharing a name is `42P07` on the next
|
|
29
29
|
* migration.
|
|
30
30
|
*
|
|
31
|
-
* A duplicate
|
|
32
|
-
*
|
|
31
|
+
* A duplicate's ask about how to build is handed to the one kept: `true` over everything, since
|
|
32
|
+
* dropping it would build a hot table's index inside a transaction after all, then an explicit
|
|
33
|
+
* `false` over the default it opted out of.
|
|
33
34
|
*/
|
|
34
35
|
export function distinctIndexes(declared: readonly IndexDef[]): readonly IndexDef[] {
|
|
35
36
|
return declared
|
|
@@ -37,10 +38,14 @@ export function distinctIndexes(declared: readonly IndexDef[]): readonly IndexDe
|
|
|
37
38
|
(index, position) =>
|
|
38
39
|
declared.findIndex((other) => identity(other) === identity(index)) === position,
|
|
39
40
|
)
|
|
40
|
-
.map((index) =>
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
41
|
+
.map((index) => {
|
|
42
|
+
const asks = declared
|
|
43
|
+
.filter((other) => identity(other) === identity(index))
|
|
44
|
+
.map((other) => other.concurrently);
|
|
45
|
+
const ask = asks.includes(true) ? true : asks.includes(false) ? false : undefined;
|
|
46
|
+
// `undefined` means no duplicate asked, so `index` itself asked nothing either.
|
|
47
|
+
return ask === undefined || ask === index.concurrently
|
|
48
|
+
? index
|
|
49
|
+
: { ...index, concurrently: ask };
|
|
50
|
+
});
|
|
46
51
|
}
|
package/src/index-init.ts
CHANGED
|
@@ -36,6 +36,9 @@ export interface IndexInit<C extends ColumnMap> {
|
|
|
36
36
|
* `<id>_concurrently.sql` beside the migration (or as the migration, when it is the only change),
|
|
37
37
|
* marked `-- transaction: false`; an index on a table the same migration creates is still built
|
|
38
38
|
* inside that migration's transaction — the table is empty and nobody else can see it.
|
|
39
|
+
*
|
|
40
|
+
* Absent is the default: concurrent on a table that already exists; `false` is the plain build
|
|
41
|
+
* `x db gen` marks `-- lock-heavy: true`.
|
|
39
42
|
*/
|
|
40
43
|
readonly concurrently?: boolean;
|
|
41
44
|
}
|
package/src/lifecycle.ts
CHANGED
|
@@ -13,6 +13,7 @@ import type { EntityCore } from './entity';
|
|
|
13
13
|
import { invariantViolated } from './errors';
|
|
14
14
|
import { type HistoryEntity, type HistoryInit, historyEntity, historyIgnored } from './history';
|
|
15
15
|
import type { Retain } from './retain';
|
|
16
|
+
import { sealedFields } from './sealed';
|
|
16
17
|
import type { ColumnMap } from './types';
|
|
17
18
|
|
|
18
19
|
/** What `$describe()` gains, each absent unless declared so no other entity's hash moves. */
|
|
@@ -68,6 +69,12 @@ function exactColumns(core: EntityCore): Pick<HistoryDescriptionLike, 'text' | '
|
|
|
68
69
|
};
|
|
69
70
|
}
|
|
70
71
|
|
|
72
|
+
/** Sealed columns never enter an image (`HistoryDescriptionLike.sealed`). Absent when none. */
|
|
73
|
+
function sealedColumns(core: EntityCore): Pick<HistoryDescriptionLike, 'sealed'> {
|
|
74
|
+
const sealed = sealedFields(core).map((field) => field.column);
|
|
75
|
+
return sealed.length === 0 ? {} : { sealed };
|
|
76
|
+
}
|
|
77
|
+
|
|
71
78
|
const physical = (core: EntityCore, property: string): string => {
|
|
72
79
|
const column = core.$columns[property];
|
|
73
80
|
return column === undefined ? property : columnName(property, column.$meta);
|
|
@@ -96,6 +103,7 @@ export function describeLifecycle(
|
|
|
96
103
|
]),
|
|
97
104
|
],
|
|
98
105
|
...exactColumns(core),
|
|
106
|
+
...sealedColumns(core),
|
|
99
107
|
},
|
|
100
108
|
}),
|
|
101
109
|
...(counters.length === 0 ? {} : { counts: counters.map(describeCounter) }),
|
package/src/memory-lifecycle.ts
CHANGED
|
@@ -11,6 +11,7 @@ import { SOFT_DELETE_COLUMN } from './entity';
|
|
|
11
11
|
import { historyIgnored } from './history';
|
|
12
12
|
import type { MemoryTables } from './memory-same-tenant';
|
|
13
13
|
import type { Tx } from './repo';
|
|
14
|
+
import { sealedFields } from './sealed';
|
|
14
15
|
|
|
15
16
|
const field = (row: unknown, property: string): unknown =>
|
|
16
17
|
typeof row === 'object' && row !== null ? (row as Record<string, unknown>)[property] : undefined;
|
|
@@ -18,10 +19,15 @@ const field = (row: unknown, property: string): unknown =>
|
|
|
18
19
|
const jsonValue = (value: unknown): unknown =>
|
|
19
20
|
value instanceof Date ? value.toISOString() : value === undefined ? null : value;
|
|
20
21
|
|
|
21
|
-
/**
|
|
22
|
+
/**
|
|
23
|
+
* The stored row as the trigger spells it: physical names, money as its columns, and no sealed
|
|
24
|
+
* column — its stored string is ciphertext, and a lookup one is deterministic: an equality oracle.
|
|
25
|
+
*/
|
|
22
26
|
export function imageOf(entity: EntityCore, row: unknown): Record<string, unknown> {
|
|
23
27
|
const image = new Map<string, unknown>();
|
|
28
|
+
const sealed = new Set(sealedFields(entity).map((each) => each.property));
|
|
24
29
|
for (const [property, column] of Object.entries(entity.$columns)) {
|
|
30
|
+
if (sealed.has(property)) continue;
|
|
25
31
|
const value = field(row, property);
|
|
26
32
|
if (column.$meta.kind !== 'money') {
|
|
27
33
|
image.set(columnName(property, column.$meta), jsonValue(value));
|
package/src/registry.ts
CHANGED
|
@@ -138,10 +138,11 @@ export interface IndexDescription {
|
|
|
138
138
|
*/
|
|
139
139
|
readonly using?: IndexMethod | undefined;
|
|
140
140
|
/**
|
|
141
|
-
*
|
|
142
|
-
*
|
|
141
|
+
* How the index is built, when the entity asked: `true` for `create index concurrently` outside a
|
|
142
|
+
* transaction, `false` for the plain build `x db gen` marks `-- lock-heavy: true`. Absent is the
|
|
143
|
+
* default (concurrently on an existing table), so an entity that never asked hashes as before.
|
|
143
144
|
*/
|
|
144
|
-
readonly concurrently?:
|
|
145
|
+
readonly concurrently?: boolean | undefined;
|
|
145
146
|
}
|
|
146
147
|
|
|
147
148
|
export interface EntityDescription {
|
package/src/search-repo.ts
CHANGED
|
@@ -38,6 +38,21 @@ const keyOf = (entity: EntityCore<unknown>): string => {
|
|
|
38
38
|
return key;
|
|
39
39
|
};
|
|
40
40
|
|
|
41
|
+
/**
|
|
42
|
+
* A full answer is refused, never read: the index stops at `MAX_PAGE_SIZE` hits, so a full list
|
|
43
|
+
* cannot say whether more matched, and an `in` over it would count, group and page a sample as
|
|
44
|
+
* if it were every match — where the built-in index counts them all. Not `MAX_PAGE_SIZE + 1`: that
|
|
45
|
+
* is past the limit every adapter accepts (`searchLimitOf`), so it would break the ones that check
|
|
46
|
+
* it. The cost is exactly `MAX_PAGE_SIZE` matches being refused too. The index is asked by term and
|
|
47
|
+
* tenant alone, so the chain's other filters cannot narrow what it answers: the term must.
|
|
48
|
+
*/
|
|
49
|
+
const tooManyHits = (entity: EntityCore<unknown>, indexName: string): EntityError =>
|
|
50
|
+
new EntityError({
|
|
51
|
+
code: 'X_INVARIANT_VIOLATED',
|
|
52
|
+
cause: `${entity.$name}.search(<term>) matched ${MAX_PAGE_SIZE} or more rows in the '${indexName}' search index — it answers at most ${MAX_PAGE_SIZE} ids, so a count, a breakdown or a page over them would be a sample, not every match`,
|
|
53
|
+
fix: `${entity.$name}.search('<a more specific term>') # every word must match, so each one added narrows the hits; or installSearchIndex(postgresSearchIndex()) to match inside the statement, unbounded`,
|
|
54
|
+
});
|
|
55
|
+
|
|
41
56
|
/** `args` with every `matches` predicate replaced by the ids the installed index answers. */
|
|
42
57
|
const resolved = async (entity: EntityCore<unknown>, args: FindManyArgs): Promise<FindManyArgs> => {
|
|
43
58
|
const where = args.where ?? [];
|
|
@@ -56,6 +71,7 @@ const resolved = async (entity: EntityCore<unknown>, args: FindManyArgs): Promis
|
|
|
56
71
|
term: String(predicate.value ?? ''),
|
|
57
72
|
limit: MAX_PAGE_SIZE,
|
|
58
73
|
});
|
|
74
|
+
if (hits.length >= MAX_PAGE_SIZE) throw tooManyHits(entity, index.name);
|
|
59
75
|
swapped.push({ column: keyOf(entity), op: 'in', value: hits.map((hit) => hit.id) });
|
|
60
76
|
}
|
|
61
77
|
return { ...args, where: swapped };
|
package/src/types.ts
CHANGED
|
@@ -489,6 +489,9 @@ export interface IndexDef {
|
|
|
489
489
|
* one fact that this release exists to stop.
|
|
490
490
|
*/
|
|
491
491
|
readonly using?: IndexMethod;
|
|
492
|
-
/**
|
|
493
|
-
|
|
492
|
+
/**
|
|
493
|
+
* How it is built (`IndexInit`). Absent is the default: concurrently on a table that already
|
|
494
|
+
* exists. `false` is the opt-out, a plain build `x db gen` marks `-- lock-heavy: true`.
|
|
495
|
+
*/
|
|
496
|
+
readonly concurrently?: boolean;
|
|
494
497
|
}
|