@entifix/business 0.20.3 → 0.22.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/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/repository/reference-labels.d.ts +60 -0
- package/dist/repository/reference-labels.d.ts.map +1 -0
- package/dist/repository/reference-labels.js +110 -0
- package/dist/repository/reference-labels.js.map +1 -0
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export * from './repository/entity-facet-counter.js';
|
|
|
7
7
|
export * from './repository/entity-link-resolver.repository.js';
|
|
8
8
|
export * from './repository/entity-mixins.repository.js';
|
|
9
9
|
export * from './repository/entity-repository-factory.js';
|
|
10
|
+
export * from './repository/reference-labels.js';
|
|
10
11
|
export * from './request/index.js';
|
|
11
12
|
export * from './resolve/index.js';
|
|
12
13
|
export * from './sessions/one-time-token-store.js';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAC;AACtC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,wBAAwB,CAAC;AACvC,cAAc,mCAAmC,CAAC;AAClD,cAAc,mCAAmC,CAAC;AAClD,cAAc,sCAAsC,CAAC;AACrD,cAAc,iDAAiD,CAAC;AAChE,cAAc,0CAA0C,CAAC;AACzD,cAAc,2CAA2C,CAAC;AAC1D,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,oCAAoC,CAAC;AACnD,cAAc,6BAA6B,CAAC;AAC5C,cAAc,iCAAiC,CAAC;AAChD,cAAc,oBAAoB,CAAC;AACnC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,yBAAyB,CAAC;AACxC,cAAc,0BAA0B,CAAC;AACzC,cAAc,0BAA0B,CAAC;AACzC,cAAc,6BAA6B,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAC;AACtC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,wBAAwB,CAAC;AACvC,cAAc,mCAAmC,CAAC;AAClD,cAAc,mCAAmC,CAAC;AAClD,cAAc,sCAAsC,CAAC;AACrD,cAAc,iDAAiD,CAAC;AAChE,cAAc,0CAA0C,CAAC;AACzD,cAAc,2CAA2C,CAAC;AAC1D,cAAc,kCAAkC,CAAC;AACjD,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,oCAAoC,CAAC;AACnD,cAAc,6BAA6B,CAAC;AAC5C,cAAc,iCAAiC,CAAC;AAChD,cAAc,oBAAoB,CAAC;AACnC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,yBAAyB,CAAC;AACxC,cAAc,0BAA0B,CAAC;AACzC,cAAc,0BAA0B,CAAC;AACzC,cAAc,6BAA6B,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -7,6 +7,7 @@ export * from './repository/entity-facet-counter.js';
|
|
|
7
7
|
export * from './repository/entity-link-resolver.repository.js';
|
|
8
8
|
export * from './repository/entity-mixins.repository.js';
|
|
9
9
|
export * from './repository/entity-repository-factory.js';
|
|
10
|
+
export * from './repository/reference-labels.js';
|
|
10
11
|
export * from './request/index.js';
|
|
11
12
|
export * from './resolve/index.js';
|
|
12
13
|
export * from './sessions/one-time-token-store.js';
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export * from './entity-api/index.js';\nexport * from './health/health-registry.js';\nexport * from './idempotency/index.js';\nexport * from './repository/config.repository.js';\nexport * from './repository/entity.repository.js';\nexport * from './repository/entity-facet-counter.js';\nexport * from './repository/entity-link-resolver.repository.js';\nexport * from './repository/entity-mixins.repository.js';\nexport * from './repository/entity-repository-factory.js';\nexport * from './request/index.js';\nexport * from './resolve/index.js';\nexport * from './sessions/one-time-token-store.js';\nexport * from './sessions/session-store.js';\nexport * from './shutdown/shutdown-registry.js';\nexport * from './tenancy/index.js';\nexport * from './tokens/token-failures.js';\nexport * from './tokens/token-service.js';\nexport * from './use-case/delete/index.js';\nexport * from './use-case/facets/index.js';\nexport * from './use-case/get/index.js';\nexport * from './use-case/load/index.js';\nexport * from './use-case/save/index.js';\nexport * from './wiring/wiring-registry.js';\n"],"names":[],"mappings":"AAAA,cAAc,wBAAwB;AACtC,cAAc,8BAA8B;AAC5C,cAAc,yBAAyB;AACvC,cAAc,oCAAoC;AAClD,cAAc,oCAAoC;AAClD,cAAc,uCAAuC;AACrD,cAAc,kDAAkD;AAChE,cAAc,2CAA2C;AACzD,cAAc,4CAA4C;AAC1D,cAAc,qBAAqB;AACnC,cAAc,qBAAqB;AACnC,cAAc,qCAAqC;AACnD,cAAc,8BAA8B;AAC5C,cAAc,kCAAkC;AAChD,cAAc,qBAAqB;AACnC,cAAc,6BAA6B;AAC3C,cAAc,4BAA4B;AAC1C,cAAc,6BAA6B;AAC3C,cAAc,6BAA6B;AAC3C,cAAc,0BAA0B;AACxC,cAAc,2BAA2B;AACzC,cAAc,2BAA2B;AACzC,cAAc,8BAA8B"}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export * from './entity-api/index.js';\nexport * from './health/health-registry.js';\nexport * from './idempotency/index.js';\nexport * from './repository/config.repository.js';\nexport * from './repository/entity.repository.js';\nexport * from './repository/entity-facet-counter.js';\nexport * from './repository/entity-link-resolver.repository.js';\nexport * from './repository/entity-mixins.repository.js';\nexport * from './repository/entity-repository-factory.js';\nexport * from './repository/reference-labels.js';\nexport * from './request/index.js';\nexport * from './resolve/index.js';\nexport * from './sessions/one-time-token-store.js';\nexport * from './sessions/session-store.js';\nexport * from './shutdown/shutdown-registry.js';\nexport * from './tenancy/index.js';\nexport * from './tokens/token-failures.js';\nexport * from './tokens/token-service.js';\nexport * from './use-case/delete/index.js';\nexport * from './use-case/facets/index.js';\nexport * from './use-case/get/index.js';\nexport * from './use-case/load/index.js';\nexport * from './use-case/save/index.js';\nexport * from './wiring/wiring-registry.js';\n"],"names":[],"mappings":"AAAA,cAAc,wBAAwB;AACtC,cAAc,8BAA8B;AAC5C,cAAc,yBAAyB;AACvC,cAAc,oCAAoC;AAClD,cAAc,oCAAoC;AAClD,cAAc,uCAAuC;AACrD,cAAc,kDAAkD;AAChE,cAAc,2CAA2C;AACzD,cAAc,4CAA4C;AAC1D,cAAc,mCAAmC;AACjD,cAAc,qBAAqB;AACnC,cAAc,qBAAqB;AACnC,cAAc,qCAAqC;AACnD,cAAc,8BAA8B;AAC5C,cAAc,kCAAkC;AAChD,cAAc,qBAAqB;AACnC,cAAc,6BAA6B;AAC3C,cAAc,4BAA4B;AAC1C,cAAc,6BAA6B;AAC3C,cAAc,6BAA6B;AAC3C,cAAc,0BAA0B;AACxC,cAAc,2BAA2B;AACzC,cAAc,2BAA2B;AACzC,cAAc,8BAA8B"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { type EntifixFailure, type Entity, type EntityConstructor, type LocalizedText } from '@entifix/core';
|
|
2
|
+
import { Context, Effect } from 'effect';
|
|
3
|
+
import { EntityRepositoryTag } from './entity.repository.js';
|
|
4
|
+
/**
|
|
5
|
+
* What a referenced record is called: a text, or a text per locale for the
|
|
6
|
+
* reader's locale to pick from.
|
|
7
|
+
*/
|
|
8
|
+
export type ReferenceLabel = string | LocalizedText;
|
|
9
|
+
/**
|
|
10
|
+
* Reads what the records a reference points at are called, by the entity's
|
|
11
|
+
* `domain:key` and their ids
|
|
12
|
+
* ([ADR 0030](../../../../../docs/adr/0030-a-reference-names-its-target-through-the-owners-read-port.md)).
|
|
13
|
+
*
|
|
14
|
+
* An id with no record — deleted, or one the reader may not read — is absent
|
|
15
|
+
* from the map rather than an error: one name that cannot be read does not
|
|
16
|
+
* fail a list that loaded. A failure is the read itself failing.
|
|
17
|
+
*/
|
|
18
|
+
export interface ReferenceLabels {
|
|
19
|
+
/** The entity keys this registry can name. */
|
|
20
|
+
readonly entities: ReadonlyMap<string, EntityConstructor<Entity>>;
|
|
21
|
+
resolve(entityKey: string, ids: readonly string[]): Effect.Effect<ReadonlyMap<string, ReferenceLabel>, EntifixFailure>;
|
|
22
|
+
}
|
|
23
|
+
declare const ReferenceLabelsTag_base: Context.ServiceClass<ReferenceLabelsTag, "ReferenceLabelsTag", ReferenceLabels>;
|
|
24
|
+
/** DI tag for the host's {@link ReferenceLabels} registry. */
|
|
25
|
+
export declare class ReferenceLabelsTag extends ReferenceLabelsTag_base {
|
|
26
|
+
}
|
|
27
|
+
/** How a registered target is named. */
|
|
28
|
+
export interface ReferenceLabelOptions {
|
|
29
|
+
/**
|
|
30
|
+
* The target's member a record is called by. Defaults to the target's own
|
|
31
|
+
* `labelMember`, and then to its record label's undeclared rung.
|
|
32
|
+
*/
|
|
33
|
+
readonly labelProperty?: string;
|
|
34
|
+
}
|
|
35
|
+
/** Maps a target entity to the repository adapter that reads it, and how its records are named. */
|
|
36
|
+
export type ReferenceLabelRegistration = readonly [
|
|
37
|
+
EntityConstructor<Entity>,
|
|
38
|
+
Context.Context<EntityRepositoryTag>,
|
|
39
|
+
ReferenceLabelOptions?
|
|
40
|
+
];
|
|
41
|
+
/**
|
|
42
|
+
* How many ids one read asks for. A page names far fewer; the bound keeps a
|
|
43
|
+
* filter's URL and a server's page-size cap out of reach for whatever does not.
|
|
44
|
+
*/
|
|
45
|
+
export declare const REFERENCE_BATCH_SIZE = 100;
|
|
46
|
+
/**
|
|
47
|
+
* Assembles a {@link ReferenceLabelsTag} from repository adapters that already
|
|
48
|
+
* exist, exactly as `createEntityLinkResolver` does — composed by the **host**,
|
|
49
|
+
* where every target's adapter is in reach.
|
|
50
|
+
*
|
|
51
|
+
* Each `resolve` is **one read per target entity**: a `load` filtered
|
|
52
|
+
* `id=in=(…)` over the distinct ids, page size equal to their count, through
|
|
53
|
+
* the owner's own read path. The target's `id` must be filterable.
|
|
54
|
+
*
|
|
55
|
+
* Registration throws on a target registered twice, on one with no
|
|
56
|
+
* `domain:key`, and on a label member that is not a scalar of the target.
|
|
57
|
+
*/
|
|
58
|
+
export declare function createReferenceLabels(registrations: ReadonlyArray<ReferenceLabelRegistration>): Context.Context<ReferenceLabelsTag>;
|
|
59
|
+
export {};
|
|
60
|
+
//# sourceMappingURL=reference-labels.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reference-labels.d.ts","sourceRoot":"","sources":["../../src/repository/reference-labels.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,cAAc,EACnB,KAAK,MAAM,EACX,KAAK,iBAAiB,EAMtB,KAAK,aAAa,EAEnB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEzC,OAAO,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AAE7D;;;GAGG;AACH,MAAM,MAAM,cAAc,GAAG,MAAM,GAAG,aAAa,CAAC;AAEpD;;;;;;;;GAQG;AACH,MAAM,WAAW,eAAe;IAC9B,8CAA8C;IAC9C,QAAQ,CAAC,QAAQ,EAAE,WAAW,CAAC,MAAM,EAAE,iBAAiB,CAAC,MAAM,CAAC,CAAC,CAAC;IAClE,OAAO,CACL,SAAS,EAAE,MAAM,EACjB,GAAG,EAAE,SAAS,MAAM,EAAE,GACrB,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,MAAM,EAAE,cAAc,CAAC,EAAE,cAAc,CAAC,CAAC;CACvE;;AAED,8DAA8D;AAC9D,qBAAa,kBAAmB,SAAQ,uBAGf;CAAG;AAE5B,wCAAwC;AACxC,MAAM,WAAW,qBAAqB;IACpC;;;OAGG;IACH,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC;CACjC;AAED,mGAAmG;AACnG,MAAM,MAAM,0BAA0B,GAAG,SAAS;IAChD,iBAAiB,CAAC,MAAM,CAAC;IACzB,OAAO,CAAC,OAAO,CAAC,mBAAmB,CAAC;IACpC,qBAAqB,CAAC;CACvB,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,oBAAoB,MAAM,CAAC;AAkExC;;;;;;;;;;;GAWG;AACH,wBAAgB,qBAAqB,CACnC,aAAa,EAAE,aAAa,CAAC,0BAA0B,CAAC,GACvD,OAAO,CAAC,OAAO,CAAC,kBAAkB,CAAC,CA8DrC"}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { describeEntityColumns, entityReferenceKey, extractMetaEntity, Internal, recordLabel } from '@entifix/core';
|
|
2
|
+
import { Context, Effect } from 'effect';
|
|
3
|
+
import { EntityRepositoryTag } from './entity.repository.js';
|
|
4
|
+
/** DI tag for the host's {@link ReferenceLabels} registry. */ export class ReferenceLabelsTag extends Context.Service()('ReferenceLabelsTag') {
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* How many ids one read asks for. A page names far fewer; the bound keeps a
|
|
8
|
+
* filter's URL and a server's page-size cap out of reach for whatever does not.
|
|
9
|
+
*/ export const REFERENCE_BATCH_SIZE = 100;
|
|
10
|
+
const NAMEABLE = [
|
|
11
|
+
'string',
|
|
12
|
+
'number',
|
|
13
|
+
'localized'
|
|
14
|
+
];
|
|
15
|
+
/**
|
|
16
|
+
* A label must be one hop: the member it reads is a text or a number on the
|
|
17
|
+
* target itself, never another reference — or naming one record would read a
|
|
18
|
+
* third.
|
|
19
|
+
*/ function labelDescriptor(entityConstructor, labelProperty) {
|
|
20
|
+
var _descriptors_find;
|
|
21
|
+
const descriptors = describeEntityColumns(entityConstructor);
|
|
22
|
+
// Read by `id=in=(…)`: a store refuses a filter on a member that is not.
|
|
23
|
+
if (((_descriptors_find = descriptors.find((column)=>column.name === 'id')) == null ? void 0 : _descriptors_find.filterable) !== true) {
|
|
24
|
+
throw new Error(`${entityConstructor.name}'s id must be filterable to be referenced — its names are read in one request, filtered by id.`);
|
|
25
|
+
}
|
|
26
|
+
const declared = labelProperty != null ? labelProperty : extractMetaEntity(entityConstructor).labelMember;
|
|
27
|
+
if (declared === undefined) return undefined;
|
|
28
|
+
const descriptor = descriptors.find((column)=>column.name === declared);
|
|
29
|
+
if (descriptor === undefined || descriptor.references !== undefined || !NAMEABLE.includes(descriptor.type)) {
|
|
30
|
+
throw new Error(`${entityConstructor.name} is named by "${declared}", which must be a visible string, number or localized member of it — a reference label is read in one hop.`);
|
|
31
|
+
}
|
|
32
|
+
return descriptor;
|
|
33
|
+
}
|
|
34
|
+
function nameOf(entityConstructor, descriptor, record) {
|
|
35
|
+
const value = descriptor === undefined ? (()=>{
|
|
36
|
+
const label = recordLabel(entityConstructor, record);
|
|
37
|
+
return label.kind === 'value' ? label.value : undefined;
|
|
38
|
+
})() : record[descriptor.name];
|
|
39
|
+
if (value === undefined || value === null || value === '') return undefined;
|
|
40
|
+
if (typeof value === 'object') {
|
|
41
|
+
const filled = Object.values(value).some((text)=>typeof text === 'string' && text !== '');
|
|
42
|
+
return filled ? value : undefined;
|
|
43
|
+
}
|
|
44
|
+
return String(value);
|
|
45
|
+
}
|
|
46
|
+
function chunks(items, size) {
|
|
47
|
+
const out = [];
|
|
48
|
+
for(let start = 0; start < items.length; start += size){
|
|
49
|
+
out.push(items.slice(start, start + size));
|
|
50
|
+
}
|
|
51
|
+
return out;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Assembles a {@link ReferenceLabelsTag} from repository adapters that already
|
|
55
|
+
* exist, exactly as `createEntityLinkResolver` does — composed by the **host**,
|
|
56
|
+
* where every target's adapter is in reach.
|
|
57
|
+
*
|
|
58
|
+
* Each `resolve` is **one read per target entity**: a `load` filtered
|
|
59
|
+
* `id=in=(…)` over the distinct ids, page size equal to their count, through
|
|
60
|
+
* the owner's own read path. The target's `id` must be filterable.
|
|
61
|
+
*
|
|
62
|
+
* Registration throws on a target registered twice, on one with no
|
|
63
|
+
* `domain:key`, and on a label member that is not a scalar of the target.
|
|
64
|
+
*/ export function createReferenceLabels(registrations) {
|
|
65
|
+
const entities = new Map();
|
|
66
|
+
const readers = new Map();
|
|
67
|
+
for (const [entityConstructor, repositoryContext, options] of registrations){
|
|
68
|
+
const key = entityReferenceKey(entityConstructor);
|
|
69
|
+
if (entities.has(key)) {
|
|
70
|
+
throw new Error(`"${key}" is registered twice — one target entity is read through one repository.`);
|
|
71
|
+
}
|
|
72
|
+
const repository = Context.get(repositoryContext, EntityRepositoryTag);
|
|
73
|
+
const descriptor = labelDescriptor(entityConstructor, options == null ? void 0 : options.labelProperty);
|
|
74
|
+
entities.set(key, entityConstructor);
|
|
75
|
+
readers.set(key, (ids)=>Effect.forEach(chunks(ids, REFERENCE_BATCH_SIZE), (batch)=>repository.load({
|
|
76
|
+
filtering: [
|
|
77
|
+
{
|
|
78
|
+
property: 'id',
|
|
79
|
+
operator: 'in',
|
|
80
|
+
values: batch
|
|
81
|
+
}
|
|
82
|
+
],
|
|
83
|
+
page: 1,
|
|
84
|
+
pageSize: batch.length
|
|
85
|
+
}), {
|
|
86
|
+
concurrency: 'unbounded'
|
|
87
|
+
}).pipe(Effect.map((pages)=>{
|
|
88
|
+
const labels = new Map();
|
|
89
|
+
for (const record of pages.flatMap((page)=>page.items)){
|
|
90
|
+
const name = nameOf(entityConstructor, descriptor, record);
|
|
91
|
+
if (name !== undefined) labels.set(String(record.id), name);
|
|
92
|
+
}
|
|
93
|
+
return labels;
|
|
94
|
+
})));
|
|
95
|
+
}
|
|
96
|
+
const labels = {
|
|
97
|
+
entities,
|
|
98
|
+
resolve (entityKey, ids) {
|
|
99
|
+
const read = readers.get(entityKey);
|
|
100
|
+
if (read === undefined) return Effect.fail(new Internal());
|
|
101
|
+
const distinct = [
|
|
102
|
+
...new Set(ids)
|
|
103
|
+
];
|
|
104
|
+
return distinct.length === 0 ? Effect.succeed(new Map()) : read(distinct);
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
return Context.make(ReferenceLabelsTag, labels);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
//# sourceMappingURL=reference-labels.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/repository/reference-labels.ts"],"sourcesContent":["import {\n describeEntityColumns,\n type EntifixFailure,\n type Entity,\n type EntityConstructor,\n type EntityFieldDescriptor,\n type EntityId,\n entityReferenceKey,\n extractMetaEntity,\n Internal,\n type LocalizedText,\n recordLabel,\n} from '@entifix/core';\nimport { Context, Effect } from 'effect';\n\nimport { EntityRepositoryTag } from './entity.repository.js';\n\n/**\n * What a referenced record is called: a text, or a text per locale for the\n * reader's locale to pick from.\n */\nexport type ReferenceLabel = string | LocalizedText;\n\n/**\n * Reads what the records a reference points at are called, by the entity's\n * `domain:key` and their ids\n * ([ADR 0030](../../../../../docs/adr/0030-a-reference-names-its-target-through-the-owners-read-port.md)).\n *\n * An id with no record — deleted, or one the reader may not read — is absent\n * from the map rather than an error: one name that cannot be read does not\n * fail a list that loaded. A failure is the read itself failing.\n */\nexport interface ReferenceLabels {\n /** The entity keys this registry can name. */\n readonly entities: ReadonlyMap<string, EntityConstructor<Entity>>;\n resolve(\n entityKey: string,\n ids: readonly string[],\n ): Effect.Effect<ReadonlyMap<string, ReferenceLabel>, EntifixFailure>;\n}\n\n/** DI tag for the host's {@link ReferenceLabels} registry. */\nexport class ReferenceLabelsTag extends Context.Service<\n ReferenceLabelsTag,\n ReferenceLabels\n>()('ReferenceLabelsTag') {}\n\n/** How a registered target is named. */\nexport interface ReferenceLabelOptions {\n /**\n * The target's member a record is called by. Defaults to the target's own\n * `labelMember`, and then to its record label's undeclared rung.\n */\n readonly labelProperty?: string;\n}\n\n/** Maps a target entity to the repository adapter that reads it, and how its records are named. */\nexport type ReferenceLabelRegistration = readonly [\n EntityConstructor<Entity>,\n Context.Context<EntityRepositoryTag>,\n ReferenceLabelOptions?,\n];\n\n/**\n * How many ids one read asks for. A page names far fewer; the bound keeps a\n * filter's URL and a server's page-size cap out of reach for whatever does not.\n */\nexport const REFERENCE_BATCH_SIZE = 100;\n\nconst NAMEABLE: readonly string[] = ['string', 'number', 'localized'];\n\n/**\n * A label must be one hop: the member it reads is a text or a number on the\n * target itself, never another reference — or naming one record would read a\n * third.\n */\nfunction labelDescriptor(\n entityConstructor: EntityConstructor<Entity>,\n labelProperty: string | undefined,\n): EntityFieldDescriptor | undefined {\n const descriptors = describeEntityColumns(entityConstructor);\n // Read by `id=in=(…)`: a store refuses a filter on a member that is not.\n if (descriptors.find(column => column.name === 'id')?.filterable !== true) {\n throw new Error(\n `${entityConstructor.name}'s id must be filterable to be referenced — its names are read in one request, filtered by id.`,\n );\n }\n const declared =\n labelProperty ?? extractMetaEntity(entityConstructor).labelMember;\n if (declared === undefined) return undefined;\n const descriptor = descriptors.find(column => column.name === declared);\n if (\n descriptor === undefined ||\n descriptor.references !== undefined ||\n !NAMEABLE.includes(descriptor.type)\n ) {\n throw new Error(\n `${entityConstructor.name} is named by \"${declared}\", which must be a visible string, number or localized member of it — a reference label is read in one hop.`,\n );\n }\n return descriptor;\n}\n\nfunction nameOf(\n entityConstructor: EntityConstructor<Entity>,\n descriptor: EntityFieldDescriptor | undefined,\n record: Entity,\n): ReferenceLabel | undefined {\n const value =\n descriptor === undefined\n ? (() => {\n const label = recordLabel(entityConstructor, record);\n return label.kind === 'value' ? label.value : undefined;\n })()\n : (record as unknown as Record<string, unknown>)[descriptor.name];\n if (value === undefined || value === null || value === '') return undefined;\n if (typeof value === 'object') {\n const filled = Object.values(value).some(\n text => typeof text === 'string' && text !== '',\n );\n return filled ? (value as LocalizedText) : undefined;\n }\n return String(value);\n}\n\nfunction chunks<T>(items: readonly T[], size: number): T[][] {\n const out: T[][] = [];\n for (let start = 0; start < items.length; start += size) {\n out.push(items.slice(start, start + size));\n }\n return out;\n}\n\n/**\n * Assembles a {@link ReferenceLabelsTag} from repository adapters that already\n * exist, exactly as `createEntityLinkResolver` does — composed by the **host**,\n * where every target's adapter is in reach.\n *\n * Each `resolve` is **one read per target entity**: a `load` filtered\n * `id=in=(…)` over the distinct ids, page size equal to their count, through\n * the owner's own read path. The target's `id` must be filterable.\n *\n * Registration throws on a target registered twice, on one with no\n * `domain:key`, and on a label member that is not a scalar of the target.\n */\nexport function createReferenceLabels(\n registrations: ReadonlyArray<ReferenceLabelRegistration>,\n): Context.Context<ReferenceLabelsTag> {\n const entities = new Map<string, EntityConstructor<Entity>>();\n const readers = new Map<\n string,\n (\n ids: readonly string[],\n ) => Effect.Effect<ReadonlyMap<string, ReferenceLabel>, EntifixFailure>\n >();\n\n for (const [entityConstructor, repositoryContext, options] of registrations) {\n const key = entityReferenceKey(entityConstructor);\n if (entities.has(key)) {\n throw new Error(\n `\"${key}\" is registered twice — one target entity is read through one repository.`,\n );\n }\n const repository = Context.get(repositoryContext, EntityRepositoryTag);\n const descriptor = labelDescriptor(\n entityConstructor,\n options?.labelProperty,\n );\n entities.set(key, entityConstructor);\n readers.set(key, ids =>\n Effect.forEach(\n chunks(ids, REFERENCE_BATCH_SIZE),\n batch =>\n repository.load<Entity>({\n filtering: [\n {\n property: 'id',\n operator: 'in',\n values: batch as EntityId[],\n },\n ],\n page: 1,\n pageSize: batch.length,\n }),\n { concurrency: 'unbounded' },\n ).pipe(\n Effect.map(pages => {\n const labels = new Map<string, ReferenceLabel>();\n for (const record of pages.flatMap(page => page.items)) {\n const name = nameOf(entityConstructor, descriptor, record);\n if (name !== undefined) labels.set(String(record.id), name);\n }\n return labels;\n }),\n ),\n );\n }\n\n const labels: ReferenceLabels = {\n entities,\n resolve(entityKey, ids) {\n const read = readers.get(entityKey);\n if (read === undefined) return Effect.fail(new Internal());\n const distinct = [...new Set(ids)];\n return distinct.length === 0 ? Effect.succeed(new Map()) : read(distinct);\n },\n };\n\n return Context.make(ReferenceLabelsTag, labels);\n}\n"],"names":["describeEntityColumns","entityReferenceKey","extractMetaEntity","Internal","recordLabel","Context","Effect","EntityRepositoryTag","ReferenceLabelsTag","Service","REFERENCE_BATCH_SIZE","NAMEABLE","labelDescriptor","entityConstructor","labelProperty","descriptors","find","column","name","filterable","Error","declared","labelMember","undefined","descriptor","references","includes","type","nameOf","record","value","label","kind","filled","Object","values","some","text","String","chunks","items","size","out","start","length","push","slice","createReferenceLabels","registrations","entities","Map","readers","repositoryContext","options","key","has","repository","get","set","ids","forEach","batch","load","filtering","property","operator","page","pageSize","concurrency","pipe","map","pages","labels","flatMap","id","resolve","entityKey","read","fail","distinct","Set","succeed","make"],"mappings":"AAAA,SACEA,qBAAqB,EAMrBC,kBAAkB,EAClBC,iBAAiB,EACjBC,QAAQ,EAERC,WAAW,QACN,gBAAgB;AACvB,SAASC,OAAO,EAAEC,MAAM,QAAQ,SAAS;AAEzC,SAASC,mBAAmB,QAAQ,yBAAyB;AA0B7D,4DAA4D,GAC5D,OAAO,MAAMC,2BAA2BH,QAAQI,OAAO,GAGnD;AAAuB;AAkB3B;;;CAGC,GACD,OAAO,MAAMC,uBAAuB,IAAI;AAExC,MAAMC,WAA8B;IAAC;IAAU;IAAU;CAAY;AAErE;;;;CAIC,GACD,SAASC,gBACPC,iBAA4C,EAC5CC,aAAiC;QAI7BC;IAFJ,MAAMA,cAAcf,sBAAsBa;IAC1C,yEAAyE;IACzE,IAAIE,EAAAA,oBAAAA,YAAYC,IAAI,CAACC,CAAAA,SAAUA,OAAOC,IAAI,KAAK,0BAA3CH,kBAAkDI,UAAU,MAAK,MAAM;QACzE,MAAM,IAAIC,MACR,GAAGP,kBAAkBK,IAAI,CAAC,8FAA8F,CAAC;IAE7H;IACA,MAAMG,WACJP,wBAAAA,gBAAiBZ,kBAAkBW,mBAAmBS,WAAW;IACnE,IAAID,aAAaE,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gE;AAC1C"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@entifix/business",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.22.0",
|
|
4
4
|
"description": "The ports every adapter implements — repository, session, token, health — and the generic get, load, save and delete use-cases.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -66,7 +66,7 @@
|
|
|
66
66
|
},
|
|
67
67
|
"dependencies": {
|
|
68
68
|
"@swc/helpers": "0.5.23",
|
|
69
|
-
"@entifix/core": "0.
|
|
69
|
+
"@entifix/core": "0.22.0"
|
|
70
70
|
},
|
|
71
71
|
"peerDependencies": {
|
|
72
72
|
"effect": "^4.0.7"
|