-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathdefault-namespace.ts
More file actions
40 lines (38 loc) · 1.75 KB
/
Copy pathdefault-namespace.ts
File metadata and controls
40 lines (38 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { contractError } from './contract-errors';
/**
* Reserved sentinel domain namespace id for the late-bound application-domain
* slot — the namespace a model lands in when it is authored without an explicit
* namespace. This is target-agnostic: targets that allow un-namespaced
* authoring (e.g. Mongo, SQLite) declare this id as their default on the target
* descriptor; the framework names the sentinel, never a target. Mirrors
* storage's `UNBOUND_NAMESPACE_ID` on the domain plane.
*/
export const UNBOUND_DOMAIN_NAMESPACE_ID = '__unbound__' as const;
/**
* Resolve the single domain namespace of a single-namespace contract.
*
* Bare-name access (`db.User`) reads "the contract's one namespace". Every
* contract in scope today declares exactly one domain namespace, so this is
* exact — there is nothing to infer. A contract that declares more than one
* namespace is ambiguous for a bare name, so rather than silently pick one this
* throws; cross-namespace selection is made explicit (TML-2550).
*/
export function soleDomainNamespaceId(domain: {
readonly namespaces: Readonly<Record<string, unknown>>;
}): string {
const [soleNamespaceId, ...rest] = Object.keys(domain.namespaces);
if (soleNamespaceId === undefined) {
throw contractError('CONTRACT.NAMESPACE_INVALID', 'domain has no namespaces', {
meta: { reason: 'no-domain-namespaces' },
});
}
if (rest.length > 0) {
const all = [soleNamespaceId, ...rest];
throw contractError(
'CONTRACT.NAMESPACE_INVALID',
`bare-name resolution requires exactly one domain namespace, found ${all.length} (${all.join(', ')}); select a namespace explicitly`,
{ meta: { reason: 'multiple-domain-namespaces', namespaceIds: all } },
);
}
return soleNamespaceId;
}