@ultimat3/entity 31.45.0 → 31.47.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 +194 -9
- package/package.json +5 -5
- package/src/containment.ts +4 -6
- package/src/predicate-node.ts +5 -7
- package/src/rows-of.ts +5 -6
- package/src/sealed-repo.ts +3 -0
- package/src/search-index-sync.ts +3 -4
package/README.md
CHANGED
|
@@ -7,9 +7,11 @@ admin screen, cache tags and the manifest are all projections of that one call.
|
|
|
7
7
|
`bun add @ultimat3/entity` · guide: [Entities And Migrations](https://github.com/developerz-ai/ultimate/wiki/Entities-And-Migrations)
|
|
8
8
|
|
|
9
9
|
```ts
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
import { entity, enumerated, integer, invariant, text, timestamp, url, uuid } from '@ultimat3/entity';
|
|
11
|
+
|
|
12
|
+
const POST_STATUSES = ['draft', 'published'] as const;
|
|
13
|
+
|
|
14
|
+
const orgs = entity('orgs', { columns: { id: uuid().primaryKey() } });
|
|
13
15
|
|
|
14
16
|
export const posts = entity('posts', {
|
|
15
17
|
tenant: 'orgId',
|
|
@@ -283,11 +285,20 @@ the physical names alone and would deliver `ghLogin` where the repository says `
|
|
|
283
285
|
## Branded ids
|
|
284
286
|
|
|
285
287
|
```ts
|
|
288
|
+
import { database, entity, uuid } from '@ultimat3/entity';
|
|
289
|
+
|
|
290
|
+
type PostId = string & { readonly __brand: 'PostId' };
|
|
291
|
+
type UserId = string & { readonly __brand: 'UserId' };
|
|
292
|
+
|
|
293
|
+
const users = entity('users', { columns: { id: uuid().primaryKey() } });
|
|
294
|
+
declare const postId: PostId;
|
|
295
|
+
|
|
286
296
|
export const posts = entity('posts', {
|
|
287
297
|
columns: { id: uuid<PostId>().primaryKey(), authorId: uuid<UserId>().references(() => users.id) },
|
|
288
298
|
});
|
|
289
299
|
|
|
290
|
-
const
|
|
300
|
+
const db = database({ posts });
|
|
301
|
+
const post = await db.posts.where({ id: postId }).one(); // PostId — a UserId here is a compile error
|
|
291
302
|
```
|
|
292
303
|
|
|
293
304
|
The brand is declared once, on the column, and carried by the whole chain: `RowOf`, `Insertable`,
|
|
@@ -349,6 +360,15 @@ database, and `x verify` reports the other as a rule the database does not know.
|
|
|
349
360
|
## One typed handle
|
|
350
361
|
|
|
351
362
|
```ts
|
|
363
|
+
import { database, entity, timestamp, uuid } from '@ultimat3/entity';
|
|
364
|
+
|
|
365
|
+
const orgs = entity('orgs', { columns: { id: uuid().primaryKey() } });
|
|
366
|
+
const posts = entity('posts', {
|
|
367
|
+
tenant: 'orgId',
|
|
368
|
+
columns: { id: uuid().primaryKey(), orgId: uuid(), createdAt: timestamp().defaultNow() },
|
|
369
|
+
});
|
|
370
|
+
declare const orgId: string;
|
|
371
|
+
|
|
352
372
|
export const db = database({ orgs, posts });
|
|
353
373
|
|
|
354
374
|
db.posts.where({ orgId }).orderBy('createdAt').limit(50).page(); // { rows, nextCursor, hasMore }
|
|
@@ -412,6 +432,33 @@ the terminal, not something the caller writes around `page()`:
|
|
|
412
432
|
|
|
413
433
|
```ts
|
|
414
434
|
// One statement per batch, one page of rows in memory at a time.
|
|
435
|
+
import { database, entity, text, uuid } from '@ultimat3/entity';
|
|
436
|
+
import { type Ctx } from '@ultimat3/core';
|
|
437
|
+
|
|
438
|
+
declare const checkpoint: string | null;
|
|
439
|
+
declare const deadline: Date;
|
|
440
|
+
declare const search: { index(rows: readonly object[]): Promise<void> };
|
|
441
|
+
|
|
442
|
+
const members = entity('members', {
|
|
443
|
+
tenant: 'orgId',
|
|
444
|
+
columns: { id: uuid().primaryKey(), orgId: uuid() },
|
|
445
|
+
});
|
|
446
|
+
const posts = entity('posts', {
|
|
447
|
+
tenant: 'orgId',
|
|
448
|
+
columns: {
|
|
449
|
+
id: uuid().primaryKey(),
|
|
450
|
+
orgId: uuid(),
|
|
451
|
+
authorId: uuid().references(() => members.id),
|
|
452
|
+
},
|
|
453
|
+
});
|
|
454
|
+
const checkpoints = entity('checkpoints', {
|
|
455
|
+
columns: { id: uuid().primaryKey(), cursor: text().nullable() },
|
|
456
|
+
});
|
|
457
|
+
const db = database({ checkpoints, members, posts });
|
|
458
|
+
declare const id: string;
|
|
459
|
+
declare const orgId: string;
|
|
460
|
+
declare const ctx: Ctx;
|
|
461
|
+
|
|
415
462
|
for await (const batch of db.posts.where({ orgId }).preload('author').inBatches(500)) {
|
|
416
463
|
await search.index(batch);
|
|
417
464
|
}
|
|
@@ -813,6 +860,20 @@ asks N times. `countBy(column)` is that whole loop as one statement, keyed by th
|
|
|
813
860
|
|
|
814
861
|
```ts
|
|
815
862
|
// One statement for every post in `ids`, not one `select count(*)` each.
|
|
863
|
+
import { database, entity, integer, uuid } from '@ultimat3/entity';
|
|
864
|
+
|
|
865
|
+
const posts = entity('posts', {
|
|
866
|
+
tenant: 'orgId',
|
|
867
|
+
columns: { id: uuid().primaryKey(), orgId: uuid(), likeCount: integer().default(0) },
|
|
868
|
+
});
|
|
869
|
+
const likes = entity('likes', {
|
|
870
|
+
tenant: 'orgId',
|
|
871
|
+
columns: { id: uuid().primaryKey(), orgId: uuid(), postId: uuid() },
|
|
872
|
+
});
|
|
873
|
+
const db = database({ likes, posts });
|
|
874
|
+
declare const orgId: string;
|
|
875
|
+
declare const ids: readonly string[];
|
|
876
|
+
|
|
816
877
|
const counts = await db.likes.where({ orgId }).andWhere('postId', 'in', ids).countBy('postId');
|
|
817
878
|
for (const id of ids) await db.posts.update(id, { likeCount: counts.get(id) ?? 0 });
|
|
818
879
|
```
|
|
@@ -886,6 +947,47 @@ one row at a time: a `for … of` deleting or patching one row per iteration is
|
|
|
886
947
|
not `n`.
|
|
887
948
|
|
|
888
949
|
```ts
|
|
950
|
+
import { database, entity, integer, text, timestamp, uuid } from '@ultimat3/entity';
|
|
951
|
+
|
|
952
|
+
const members = entity('members', {
|
|
953
|
+
tenant: 'orgId',
|
|
954
|
+
columns: { id: uuid().primaryKey(), orgId: uuid() },
|
|
955
|
+
});
|
|
956
|
+
const posts = entity('posts', {
|
|
957
|
+
tenant: 'orgId',
|
|
958
|
+
columns: {
|
|
959
|
+
id: uuid().primaryKey(),
|
|
960
|
+
orgId: uuid(),
|
|
961
|
+
authorId: uuid().references(() => members.id),
|
|
962
|
+
title: text(),
|
|
963
|
+
status: text().default('draft'),
|
|
964
|
+
rank: integer().default(0),
|
|
965
|
+
publishedAt: timestamp().nullable(),
|
|
966
|
+
},
|
|
967
|
+
});
|
|
968
|
+
const likes = entity('likes', {
|
|
969
|
+
tenant: 'orgId',
|
|
970
|
+
columns: { id: uuid().primaryKey(), orgId: uuid(), postId: uuid(), userId: uuid() },
|
|
971
|
+
});
|
|
972
|
+
const participants = entity('participants', {
|
|
973
|
+
columns: {
|
|
974
|
+
id: uuid().primaryKey(),
|
|
975
|
+
conversationId: uuid(),
|
|
976
|
+
userId: uuid(),
|
|
977
|
+
lastReadAt: timestamp().nullable(),
|
|
978
|
+
},
|
|
979
|
+
});
|
|
980
|
+
const db = database({ likes, participants, posts });
|
|
981
|
+
declare const id: string;
|
|
982
|
+
declare const ids: readonly string[];
|
|
983
|
+
declare const title: string;
|
|
984
|
+
declare const postId: string;
|
|
985
|
+
declare const userId: string;
|
|
986
|
+
declare const authorId: string;
|
|
987
|
+
declare const conversationId: string;
|
|
988
|
+
declare const lastReadAt: Date;
|
|
989
|
+
declare const rows: readonly { readonly id: string; readonly rank: number }[];
|
|
990
|
+
|
|
889
991
|
db.posts.delete(id); // by a single primary key
|
|
890
992
|
db.posts.update(id, { title }); // ditto; named columns validate as insert does
|
|
891
993
|
|
|
@@ -1078,14 +1180,36 @@ export const members = entity('members', {
|
|
|
1078
1180
|
Each call is all or nothing, `As of 2026-11-02`.
|
|
1079
1181
|
|
|
1080
1182
|
```ts
|
|
1183
|
+
import { database, entity, integer, text, uuid } from '@ultimat3/entity';
|
|
1184
|
+
|
|
1185
|
+
const tags = entity('tags', {
|
|
1186
|
+
tenant: 'orgId',
|
|
1187
|
+
columns: { id: uuid().primaryKey(), orgId: uuid(), name: text() },
|
|
1188
|
+
});
|
|
1189
|
+
const likes = entity('likes', {
|
|
1190
|
+
tenant: 'orgId',
|
|
1191
|
+
columns: { id: uuid().primaryKey(), orgId: uuid(), postId: uuid(), memberId: uuid() },
|
|
1192
|
+
indexes: [{ on: ['orgId', 'postId', 'memberId'], unique: true }], // what onConflict names
|
|
1193
|
+
});
|
|
1194
|
+
const counters = entity('counters', {
|
|
1195
|
+
tenant: 'orgId',
|
|
1196
|
+
columns: { id: uuid().primaryKey(), orgId: uuid(), day: text(), total: integer() },
|
|
1197
|
+
indexes: [{ on: ['orgId', 'day'], unique: true }],
|
|
1198
|
+
});
|
|
1199
|
+
const db = database({ counters, likes, tags });
|
|
1200
|
+
declare const orgId: string;
|
|
1201
|
+
declare const names: readonly string[];
|
|
1202
|
+
declare const likeRows: Parameters<typeof db.likes.upsertAll>[0];
|
|
1203
|
+
declare const counterRows: Parameters<typeof db.counters.upsertAll>[0];
|
|
1204
|
+
|
|
1081
1205
|
await db.tags.insertAll(names.map((name) => ({ orgId, name }))); // one statement, n rows
|
|
1082
1206
|
|
|
1083
|
-
await db.likes.upsertAll(
|
|
1207
|
+
await db.likes.upsertAll(likeRows, { // insert, or leave what is there
|
|
1084
1208
|
onConflict: ['orgId', 'postId', 'memberId'],
|
|
1085
1209
|
onMatch: 'nothing',
|
|
1086
1210
|
});
|
|
1087
1211
|
|
|
1088
|
-
await db.counters.upsertAll(
|
|
1212
|
+
await db.counters.upsertAll(counterRows, { onConflict: ['orgId', 'day'] }); // insert, or overwrite
|
|
1089
1213
|
```
|
|
1090
1214
|
|
|
1091
1215
|
`insertAll` is `insert` in bulk and nothing else: rows are `Insertable`, each one goes through
|
|
@@ -1114,7 +1238,9 @@ caller counts what it actually inserted.
|
|
|
1114
1238
|
## Relations are the foreign keys, read twice
|
|
1115
1239
|
|
|
1116
1240
|
```ts
|
|
1117
|
-
relationMap
|
|
1241
|
+
import { relationMap, relationNamed, relationsFor } from '@ultimat3/entity';
|
|
1242
|
+
|
|
1243
|
+
relationMap()['posts'];
|
|
1118
1244
|
// { org: { kind: 'belongsTo', to: 'orgs', localKey: 'orgId', remoteKey: 'id' },
|
|
1119
1245
|
// author: { kind: 'belongsTo', to: 'members', localKey: 'authorId', remoteKey: 'id' },
|
|
1120
1246
|
// likes: { kind: 'hasMany', to: 'likes', localKey: 'id', remoteKey: 'postId' } }
|
|
@@ -1157,11 +1283,28 @@ loop it causes — reach for `preload()` to carry the relation from the start, w
|
|
|
1157
1283
|
either pattern to trigger it:
|
|
1158
1284
|
|
|
1159
1285
|
```ts
|
|
1286
|
+
import { database, entity, text, uuid } from '@ultimat3/entity';
|
|
1287
|
+
|
|
1288
|
+
const orgs = entity('orgs', { columns: { id: uuid().primaryKey(), name: text() } });
|
|
1289
|
+
const members = entity('members', {
|
|
1290
|
+
tenant: 'orgId',
|
|
1291
|
+
columns: { id: uuid().primaryKey(), orgId: uuid(), name: text() },
|
|
1292
|
+
});
|
|
1293
|
+
const posts = entity('posts', {
|
|
1294
|
+
tenant: 'orgId',
|
|
1295
|
+
columns: {
|
|
1296
|
+
id: uuid().primaryKey(),
|
|
1297
|
+
orgId: uuid(),
|
|
1298
|
+
authorId: uuid().references(() => members.id),
|
|
1299
|
+
},
|
|
1300
|
+
});
|
|
1301
|
+
declare const orgId: string;
|
|
1302
|
+
|
|
1160
1303
|
export const db = database({ orgs, posts, members });
|
|
1161
1304
|
|
|
1162
1305
|
// Two statements: the page, then one `select … where "id" in (…)` over its authors.
|
|
1163
1306
|
const page = await db.posts.where({ orgId }).preload('author').page();
|
|
1164
|
-
page.rows[0]
|
|
1307
|
+
page.rows[0]?.author; // the member row, or null — always present
|
|
1165
1308
|
```
|
|
1166
1309
|
|
|
1167
1310
|
| | |
|
|
@@ -1177,6 +1320,14 @@ page.rows[0].author; // the member row, or null — always present
|
|
|
1177
1320
|
## Two drivers, one meaning
|
|
1178
1321
|
|
|
1179
1322
|
```ts
|
|
1323
|
+
import { database, entity, postgresDriver, uuid } from '@ultimat3/entity';
|
|
1324
|
+
|
|
1325
|
+
const orgs = entity('orgs', { columns: { id: uuid().primaryKey() } });
|
|
1326
|
+
const posts = entity('posts', {
|
|
1327
|
+
tenant: 'orgId',
|
|
1328
|
+
columns: { id: uuid().primaryKey(), orgId: uuid() },
|
|
1329
|
+
});
|
|
1330
|
+
|
|
1180
1331
|
database({ orgs, posts }); // memoryDriver() — the default
|
|
1181
1332
|
database({ orgs, posts }, { driver: postgresDriver() }); // production
|
|
1182
1333
|
```
|
|
@@ -1237,7 +1388,13 @@ never a transaction split across two stores. The scaffolded handle's line is
|
|
|
1237
1388
|
object — the one seam a test harness needs, `As of 2026-08`:
|
|
1238
1389
|
|
|
1239
1390
|
```ts
|
|
1240
|
-
import { defaultDriver } from '@ultimat3/entity';
|
|
1391
|
+
import { defaultDriver, entity, uuid } from '@ultimat3/entity';
|
|
1392
|
+
|
|
1393
|
+
const posts = entity('posts', {
|
|
1394
|
+
tenant: 'orgId',
|
|
1395
|
+
columns: { id: uuid().primaryKey(), orgId: uuid() },
|
|
1396
|
+
});
|
|
1397
|
+
declare const row: typeof posts.$row;
|
|
1241
1398
|
|
|
1242
1399
|
defaultDriver().repo(posts).insert(row); // seeds what database({ posts }) reads
|
|
1243
1400
|
defaultDriver().reset?.(); // between tests; `?.` because Postgres has no reset
|
|
@@ -1337,6 +1494,15 @@ store was constructed (`driverStores()`). The meanings a custom store borrows
|
|
|
1337
1494
|
`As of 2026-08`:
|
|
1338
1495
|
|
|
1339
1496
|
```ts
|
|
1497
|
+
import { entity, postgresRepo, uuid } from '@ultimat3/entity';
|
|
1498
|
+
|
|
1499
|
+
const users = entity('users', {
|
|
1500
|
+
tenant: 'orgId',
|
|
1501
|
+
columns: { id: uuid().primaryKey(), orgId: uuid() },
|
|
1502
|
+
});
|
|
1503
|
+
declare const orgId: string;
|
|
1504
|
+
declare const posts: readonly { readonly authorId: string }[];
|
|
1505
|
+
|
|
1340
1506
|
const repo = postgresRepo(users);
|
|
1341
1507
|
// One statement, not one per post: the lookups issued in this microtask are one `in`.
|
|
1342
1508
|
const authors = await Promise.all(posts.map((post) => repo.findById(post.authorId, { orgId })));
|
|
@@ -1362,6 +1528,23 @@ next lookup exists. So a page leaves its foreign key values behind, and the firs
|
|
|
1362
1528
|
one of them resolves that key for every row of the page:
|
|
1363
1529
|
|
|
1364
1530
|
```ts
|
|
1531
|
+
import { entity, postgresRepo, uuid } from '@ultimat3/entity';
|
|
1532
|
+
|
|
1533
|
+
const users = entity('users', { columns: { id: uuid().primaryKey() } });
|
|
1534
|
+
const members = entity('members', {
|
|
1535
|
+
tenant: 'orgId',
|
|
1536
|
+
columns: { id: uuid().primaryKey(), orgId: uuid() },
|
|
1537
|
+
});
|
|
1538
|
+
const posts = entity('posts', {
|
|
1539
|
+
tenant: 'orgId',
|
|
1540
|
+
columns: {
|
|
1541
|
+
id: uuid().primaryKey(),
|
|
1542
|
+
orgId: uuid(),
|
|
1543
|
+
authorId: uuid().references(() => members.id),
|
|
1544
|
+
},
|
|
1545
|
+
});
|
|
1546
|
+
declare const orgId: string;
|
|
1547
|
+
|
|
1365
1548
|
const page = await postgresRepo(posts).findMany({ orgId });
|
|
1366
1549
|
for (const post of page.rows) {
|
|
1367
1550
|
// Two statements for the whole loop: the page, then one `in` over every author on it.
|
|
@@ -1387,6 +1570,8 @@ is what an author is handed when it did not. It takes a repeated statement — t
|
|
|
1387
1570
|
reached, never a count this package keeps — and returns the error every surface renders:
|
|
1388
1571
|
|
|
1389
1572
|
```ts
|
|
1573
|
+
import { nPlusOne } from '@ultimat3/entity';
|
|
1574
|
+
|
|
1390
1575
|
nPlusOne({ kind: 'read', subject: 'members.findById', count: 50, entity: 'members', op: 'findById' });
|
|
1391
1576
|
// X_N_PLUS_ONE_QUERY: a read repeated once per row
|
|
1392
1577
|
// cause: members.findById ran 50 times in one request — one read per row
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ultimat3/entity",
|
|
3
|
-
"version": "31.
|
|
3
|
+
"version": "31.47.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.47.0",
|
|
48
|
+
"@ultimat3/db": "31.47.0",
|
|
49
|
+
"@ultimat3/schema": "31.47.0",
|
|
50
|
+
"@ultimat3/time": "31.47.0"
|
|
51
51
|
}
|
|
52
52
|
}
|
package/src/containment.ts
CHANGED
|
@@ -9,13 +9,11 @@
|
|
|
9
9
|
// Every rule here is Postgres', reproduced rather than approximated. Where the two could not be
|
|
10
10
|
// made to agree the operator is refused instead (`memory-match.ts`), never guessed at.
|
|
11
11
|
|
|
12
|
+
import { isJsonObject } from '@ultimat3/core';
|
|
12
13
|
import { isNullish as isNull } from './is-null';
|
|
13
14
|
|
|
14
|
-
const isRecord = (value: unknown): value is Readonly<Record<string, unknown>> =>
|
|
15
|
-
typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
16
|
-
|
|
17
15
|
/** Neither an object nor an array — which is exactly the set the top-level exception below covers. */
|
|
18
|
-
const isScalar = (value: unknown): boolean => !
|
|
16
|
+
const isScalar = (value: unknown): boolean => !isJsonObject(value) && !Array.isArray(value);
|
|
19
17
|
|
|
20
18
|
/**
|
|
21
19
|
* Two ELEMENTS, equal. `===` plus the one case it gets wrong here: an `arrayOf(timestamp())` row
|
|
@@ -63,7 +61,7 @@ const contains = (left: unknown, right: unknown, top: boolean): boolean => {
|
|
|
63
61
|
}
|
|
64
62
|
return top && isScalar(right) && left.some((candidate) => sameElement(candidate, right));
|
|
65
63
|
}
|
|
66
|
-
if (
|
|
64
|
+
if (isJsonObject(left) && isJsonObject(right)) {
|
|
67
65
|
return Object.keys(right).every(
|
|
68
66
|
(key) => Object.hasOwn(left, key) && contains(left[key], right[key], false),
|
|
69
67
|
);
|
|
@@ -107,6 +105,6 @@ export const arrayOverlaps = (left: readonly unknown[], right: readonly unknown[
|
|
|
107
105
|
export const jsonHasKey = (value: unknown, key: unknown): boolean => {
|
|
108
106
|
if (typeof key !== 'string' || isNull(value)) return false;
|
|
109
107
|
if (Array.isArray(value)) return value.some((item) => item === key);
|
|
110
|
-
if (
|
|
108
|
+
if (isJsonObject(value)) return Object.hasOwn(value, key);
|
|
111
109
|
return value === key;
|
|
112
110
|
};
|
package/src/predicate-node.ts
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
// is not an `Operator`, so an executor that does not know it refuses the op rather than silently
|
|
4
4
|
// dropping the filter.
|
|
5
5
|
|
|
6
|
+
import { isJsonObject } from '@ultimat3/core';
|
|
6
7
|
import { EntityError } from './entity-error';
|
|
7
8
|
import { checkedPredicate, type Operator } from './operator';
|
|
8
9
|
import type { Predicate } from './tenancy';
|
|
@@ -91,16 +92,13 @@ export const predicateNodeInvalid = (entityName: string, detail: string): Entity
|
|
|
91
92
|
fix: `andWhereNode({ or: [{ column, op, value }, …] }) # or { via: { column, entity, target }, where: [ … ] }: entity is a registered entity name, column and target its declared properties, at most ${MAX_PREDICATE_DEPTH} levels deep`,
|
|
92
93
|
});
|
|
93
94
|
|
|
94
|
-
const isRecord = (value: unknown): value is Readonly<Record<string, unknown>> =>
|
|
95
|
-
typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
96
|
-
|
|
97
95
|
/**
|
|
98
96
|
* The tree as plan predicates, its shape checked on the line the author wrote. Columns are judged
|
|
99
97
|
* later, by `screenPredicates`, against the entity each level is about — a `via`'s inner level is
|
|
100
98
|
* the target's, which only the registry knows.
|
|
101
99
|
*/
|
|
102
100
|
export const nodePredicate = (entityName: string, node: unknown, depth = 1): Predicate => {
|
|
103
|
-
if (!
|
|
101
|
+
if (!isJsonObject(node)) throw predicateNodeInvalid(entityName, 'a node is not an object');
|
|
104
102
|
const group = node['or'] !== undefined ? 'or' : node['and'] !== undefined ? 'and' : undefined;
|
|
105
103
|
const via = node['via'];
|
|
106
104
|
if ((group !== undefined || via !== undefined) && depth > MAX_PREDICATE_DEPTH) {
|
|
@@ -125,9 +123,9 @@ const viaPredicate = (
|
|
|
125
123
|
where: unknown,
|
|
126
124
|
depth: number,
|
|
127
125
|
): Predicate => {
|
|
128
|
-
const column =
|
|
129
|
-
const entity =
|
|
130
|
-
const target =
|
|
126
|
+
const column = isJsonObject(via) ? via['column'] : undefined;
|
|
127
|
+
const entity = isJsonObject(via) ? via['entity'] : undefined;
|
|
128
|
+
const target = isJsonObject(via) ? via['target'] : undefined;
|
|
131
129
|
if (typeof column !== 'string' || typeof entity !== 'string' || typeof target !== 'string') {
|
|
132
130
|
throw predicateNodeInvalid(entityName, '`via` needs { column, entity, target }, each a string');
|
|
133
131
|
}
|
package/src/rows-of.ts
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
// branches that can hold a row, so an output with none costs one WeakMap read per call.
|
|
4
4
|
|
|
5
5
|
import type { Row } from '@ultimat3/core';
|
|
6
|
+
import { isJsonObject } from '@ultimat3/core';
|
|
6
7
|
import { isSchemaNode, nodeOf, type SchemaNode } from '@ultimat3/schema';
|
|
7
8
|
import { projectionOf, type RecordProjection } from './record-projection';
|
|
8
9
|
|
|
@@ -60,9 +61,6 @@ export const projectionsIn = (schema: unknown): readonly RecordProjection[] => {
|
|
|
60
61
|
return [...found.values()];
|
|
61
62
|
};
|
|
62
63
|
|
|
63
|
-
const isObject = (value: unknown): value is Readonly<Record<string, unknown>> =>
|
|
64
|
-
typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
65
|
-
|
|
66
64
|
/**
|
|
67
65
|
* Inside a union the IR cannot say which arm a value took, so a branded arm claims the value only
|
|
68
66
|
* when it carries every column of that row as an OWN key — `$parse` writes every column, `null`
|
|
@@ -100,16 +98,17 @@ const walk = (node: SchemaNode, value: unknown, found: Found, inUnion: boolean):
|
|
|
100
98
|
if (projection !== undefined) {
|
|
101
99
|
// A row's own columns are never walked: a column node carries no brand, and a `json()` value
|
|
102
100
|
// shaped like a row is data, not a record.
|
|
103
|
-
if (
|
|
101
|
+
if (isJsonObject(value) && (!inUnion || fits(projection, value)))
|
|
102
|
+
collect(found, projection, value);
|
|
104
103
|
return;
|
|
105
104
|
}
|
|
106
105
|
if (node.kind === 'array' && node.items !== undefined && Array.isArray(value)) {
|
|
107
106
|
for (const item of value) walk(node.items, item, found, inUnion);
|
|
108
|
-
} else if (node.kind === 'object' && node.properties !== undefined &&
|
|
107
|
+
} else if (node.kind === 'object' && node.properties !== undefined && isJsonObject(value)) {
|
|
109
108
|
for (const [key, child] of Object.entries(node.properties)) {
|
|
110
109
|
if (Object.hasOwn(value, key)) walk(child, value[key], found, inUnion);
|
|
111
110
|
}
|
|
112
|
-
} else if (node.kind === 'record' && node.valueNode !== undefined &&
|
|
111
|
+
} else if (node.kind === 'record' && node.valueNode !== undefined && isJsonObject(value)) {
|
|
113
112
|
for (const entry of Object.values(value)) walk(node.valueNode, entry, found, inUnion);
|
|
114
113
|
} else if (node.kind === 'union') {
|
|
115
114
|
for (const arm of node.anyOf ?? []) walk(arm, value, found, true);
|
package/src/sealed-repo.ts
CHANGED
|
@@ -26,6 +26,9 @@ import { predicateColumns } from './write-where';
|
|
|
26
26
|
|
|
27
27
|
type Loose = Readonly<Record<string, unknown>>;
|
|
28
28
|
|
|
29
|
+
// why: NOT core's `isJsonObject` — an ARRAY passes here, so an object-shaped value always goes
|
|
30
|
+
// through the seal walk (and a row missing a required sealed column is refused) rather than past it
|
|
31
|
+
// unsealed. A seal fails closed. Pinned in `scripts/lib/helper-homes.ts`.
|
|
29
32
|
const isRecord = (value: unknown): value is Loose => typeof value === 'object' && value !== null;
|
|
30
33
|
|
|
31
34
|
/**
|
package/src/search-index-sync.ts
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
// or soft-stamped — removed at a tombstone no older than its last version. Ops are taken when the
|
|
4
4
|
// write runs and sent at COMMIT (`search-mirror-batch.ts`), so a rollback indexes nothing.
|
|
5
5
|
|
|
6
|
+
import { isJsonObject } from '@ultimat3/core';
|
|
6
7
|
import { entityNow } from './clock';
|
|
7
8
|
import type { EntityCore } from './entity';
|
|
8
9
|
import { SOFT_DELETE_COLUMN } from './entity';
|
|
@@ -29,8 +30,6 @@ export interface SearchMirror {
|
|
|
29
30
|
unnamed(op: string, count: number): void;
|
|
30
31
|
}
|
|
31
32
|
|
|
32
|
-
const isRecord = (row: unknown): row is Record_ => typeof row === 'object' && row !== null;
|
|
33
|
-
|
|
34
33
|
const tenantOf = (core: EntityCore<unknown>, row: Record_): string | null => {
|
|
35
34
|
const column = core.$tenantColumn;
|
|
36
35
|
const tenant = column === null ? null : row[column];
|
|
@@ -69,7 +68,7 @@ export const mirrorFor = (
|
|
|
69
68
|
return {
|
|
70
69
|
changed: (op, rows) =>
|
|
71
70
|
guarded(op, () => {
|
|
72
|
-
for (const row of rows.filter(
|
|
71
|
+
for (const row of rows.filter(isJsonObject)) {
|
|
73
72
|
const stamp = core.$softDelete ? row[SOFT_DELETE_COLUMN] : null;
|
|
74
73
|
if (stamp instanceof Date) {
|
|
75
74
|
removal(op, row, stamp.getTime());
|
|
@@ -81,7 +80,7 @@ export const mirrorFor = (
|
|
|
81
80
|
}),
|
|
82
81
|
deleted: (op, rows) =>
|
|
83
82
|
guarded(op, () => {
|
|
84
|
-
for (const row of rows.filter(
|
|
83
|
+
for (const row of rows.filter(isJsonObject)) removal(op, row, deletedAt);
|
|
85
84
|
}),
|
|
86
85
|
unnamed: (op, count) => guarded(op, () => batch.unnamed(entity, op, count)),
|
|
87
86
|
};
|