@ultimat3/entity 31.10.0 → 31.12.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/CLAUDE.md CHANGED
@@ -141,6 +141,7 @@ A rule marked `†` has its full text in [`docs/history/entity-rules.md`](../../
141
141
  count; a JS-only invariant is judged
142
142
  in the statement's transaction (`judged-write.ts`), refused past `MAX_ASSERTED_ROWS`. Full rule: `docs/history/entity.md`.
143
143
  - **Sweep 5b seams** (`read-wrappers.test.ts`, `history-purge.test.ts`): `Repo.latestBy?`/`aggregates?`/`lockKeys?` optional, each forwarded by sealed (refuses sealed group/order/measure/filter columns, opens rows), search-repo and memory-observed; `countBy`'s 3rd arg `{ where }` forwarded everywhere. `defineDriver` composes filtered `countBy` and `aggregates` from existing capabilities; a versioned entity there takes the judged path, one CAS store write per row. `update(id)` calls `assertVersionedUpdate` in all three drivers. `purgingHistory` (`history-purge.ts`) is the only door out of a history table. `IndexDef` carries `expression`/`orders`/`include`/`operatorClass` (types.ts; checked in `declared-index-keys.ts`, rendered by db); one covering rule for implicit indexes (`coveringIndexOf`). Files: `latest-by*.ts`, `measures.ts`, `text-contains.ts`, `version-lock*.ts`, `lock-key.ts`, `counter-observed.ts`, `all-truncated.ts`.
144
+ - **`text().collate('C')`** (`sealed-column.ts`'s `makeTextColumn`): `ColumnMeta.collation`, `'C'` only (else `X_INVARIANT_VIOLATED`), projected on `ColumnDescription.collation` only when declared; db renders/diffs it. `@ultimat3/query`'s `live-collation.ts` exempts it from the live-order warning.
144
145
  - **A write past a coded abort is refused on both drivers**: `refuseAbandoned()` in `writing()` and each `memoryRepo` write. `write-abandon.test.ts`.
145
146
  - **Every instant the write path stamps comes from `entityNow()`** (`clock.ts`, `ctx.clock`).
146
147
  **`touch()` in `touch.ts` is the ONE place `onUpdateNow()` columns are stamped.**
package/README.md CHANGED
@@ -552,6 +552,28 @@ rebuilt by the next `x db migrate`. It is how the index is built, not what it is
552
552
  name, not recorded, and toggling it generates nothing. Every index REMOVED from `indexes` is dropped
553
553
  the same way, concurrently or not — `@ultimat3/db`'s README, "Indexes built concurrently".
554
554
 
555
+ ## Code-point collation
556
+
557
+ `text().collate('C')` declares `collate "C"`: the column orders and compares by code point, the
558
+ order the memory driver and a live window's matcher already keep. `'C'` is the only collation a
559
+ column may declare (another is `X_INVARIANT_VIOLATED` where written). A live query ordered by a text
560
+ column WITHOUT it is a `query.live.order_collation` warning in development and test
561
+ (`X_LIVE_ORDER_COLLATION`): Postgres orders `apple` before `Banana`, the matcher does not.
562
+
563
+ ```ts
564
+ import { entity, text, uuid } from '@ultimat3/entity';
565
+
566
+ export const tags = entity('tags', {
567
+ columns: {
568
+ id: uuid().primaryKey(),
569
+ name: text({ max: 40 }).collate('C'), // a live list may .orderBy('name')
570
+ },
571
+ });
572
+ ```
573
+
574
+ Declaring or dropping it on an existing column is `x db gen`: `alter column … type text collate "C"`,
575
+ in place, with its reverse in `down`.
576
+
555
577
  ## Full-text search
556
578
 
557
579
  `.searchable()` on a `text()` column puts it in the entity's **one** generated `tsvector`, with a
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ultimat3/entity",
3
- "version": "31.10.0",
3
+ "version": "31.12.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.10.0",
48
- "@ultimat3/db": "31.10.0",
49
- "@ultimat3/schema": "31.10.0",
50
- "@ultimat3/time": "31.10.0"
47
+ "@ultimat3/core": "31.12.0",
48
+ "@ultimat3/db": "31.12.0",
49
+ "@ultimat3/schema": "31.12.0",
50
+ "@ultimat3/time": "31.12.0"
51
51
  }
52
52
  }
package/src/describe.ts CHANGED
@@ -298,6 +298,8 @@ const describeColumn = <Row>(
298
298
  property,
299
299
  column: physical,
300
300
  kind: sqlTypeOf(meta),
301
+ // Absent unless declared, like `default` below: no description written before it moves.
302
+ ...(meta.collation === undefined ? {} : { collation: meta.collation }),
301
303
  notNull: meta.notNull,
302
304
  primaryKey: meta.primaryKey || input.primaryKey.includes(property),
303
305
  unique: meta.unique,
package/src/registry.ts CHANGED
@@ -30,6 +30,11 @@ export interface ColumnDescription {
30
30
  readonly property: string;
31
31
  readonly column: string;
32
32
  readonly kind: string;
33
+ /**
34
+ * `text().collate('C')`, absent otherwise — so no description written before it moves the drift
35
+ * hash. Crosses to `@ultimat3/db` (tier 1) structurally, as `onDelete` does, or reaches no DDL.
36
+ */
37
+ readonly collation?: 'C';
33
38
  readonly notNull: boolean;
34
39
  readonly primaryKey: boolean;
35
40
  readonly unique: boolean;
@@ -5,6 +5,7 @@
5
5
 
6
6
  import { isSealed } from '@ultimat3/core';
7
7
  import { makeColumn } from './column';
8
+ import { refuseColumn } from './refuse';
8
9
  import { refuseSealed, sealedUniqueOpaque } from './sealed-errors';
9
10
  import type { Column, ColumnMeta, SealedColumn, SealedOptions, TextColumn } from './types';
10
11
 
@@ -103,6 +104,19 @@ const sealedMeta = (
103
104
  return { ...rest, sealed: { lookup, plaintext } };
104
105
  };
105
106
 
107
+ /**
108
+ * `'C'` is typed, and refused at runtime too: a JavaScript caller or a cast reaches here with any
109
+ * string, and an unknown collation is a `42704` inside `ROLE=migrate`, far from this line.
110
+ */
111
+ const declaredCollation = (collation: unknown): 'C' =>
112
+ collation === 'C'
113
+ ? collation
114
+ : refuseColumn(
115
+ 'collate',
116
+ `"${String(collation)}" is not a collation a column may declare: only 'C' (code-point order)`,
117
+ "text().collate('C') — or drop .collate() for the database's default collation",
118
+ );
119
+
106
120
  export const makeTextColumn = <T extends Text, Optional extends boolean>(
107
121
  meta: ColumnMeta,
108
122
  parse: (value: unknown) => T,
@@ -117,6 +131,12 @@ export const makeTextColumn = <T extends Text, Optional extends boolean>(
117
131
  unique: () => again(base.unique()),
118
132
  default: (value) => again(base.default(value)),
119
133
  column: (name) => again(base.column(name)),
134
+ collate: (collation) =>
135
+ makeTextColumn<T, Optional>(
136
+ { ...meta, collation: declaredCollation(collation) },
137
+ parse,
138
+ optional,
139
+ ),
120
140
  sealed: (options = {}) =>
121
141
  makeSealedColumn(sealedMeta(meta, options, parse), sealedParse(parse), optional),
122
142
  };
package/src/types.ts CHANGED
@@ -139,6 +139,8 @@ export interface ColumnMeta {
139
139
  */
140
140
  readonly name?: string;
141
141
  readonly length?: number;
142
+ /** `text().collate('C')`: absent is the database's default collation. `'C'` is code-point order. */
143
+ readonly collation?: 'C';
142
144
  /** `numeric(precision, scale)` — both, or neither. A bare `numeric` is unbounded on purpose. */
143
145
  readonly precision?: number;
144
146
  readonly numericScale?: number;
@@ -305,6 +307,11 @@ export interface TextColumn<T extends string | null = string, Optional extends b
305
307
  unique(): TextColumn<T, Optional>;
306
308
  default(value: T): TextColumn<T, true>;
307
309
  column(name: string): TextColumn<T, Optional>;
310
+ /**
311
+ * Order and compare this column by code point (`collate "C"`), the order a live window's matcher
312
+ * keeps — so a live query may order by it. `'C'` only; changing it is `x db gen`.
313
+ */
314
+ collate(collation: 'C'): TextColumn<T, Optional>;
308
315
  /**
309
316
  * Seal this column under the app's master key: the row still reads `string`, the database holds
310
317
  * `x1.<keyId>.<iv>.<ciphertext>`, and no view, `$schema`, record or predicate carries it.