@nexigen/entity-normalizer 0.1.1 → 0.1.2
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/async/async-duck.d.ts +38 -0
- package/dist/async/async-duck.js +189 -0
- package/dist/async/async-duck.js.map +1 -0
- package/dist/async/create-duck.d.ts +2 -0
- package/dist/async/create-duck.js +6 -0
- package/dist/async/create-duck.js.map +1 -0
- package/dist/async/index.d.ts +1 -0
- package/dist/async/index.js +2 -0
- package/dist/async/index.js.map +1 -0
- package/dist/async/marker.d.ts +1 -0
- package/dist/async/marker.js +2 -0
- package/dist/async/marker.js.map +1 -0
- package/dist/async/public.d.ts +34 -0
- package/dist/async/public.js +2 -0
- package/dist/async/public.js.map +1 -0
- package/dist/async/retry.d.ts +3 -0
- package/dist/async/retry.js +36 -0
- package/dist/async/retry.js.map +1 -0
- package/dist/async/types.d.ts +24 -0
- package/dist/async/types.js +2 -0
- package/dist/async/types.js.map +1 -0
- package/dist/constants/time.d.ts +6 -0
- package/dist/constants/time.js +7 -0
- package/dist/constants/time.js.map +1 -0
- package/dist/constants/values.d.ts +1 -0
- package/dist/constants/values.js +2 -0
- package/dist/constants/values.js.map +1 -0
- package/dist/create/cleaner.d.ts +7 -0
- package/dist/create/cleaner.js +69 -0
- package/dist/create/cleaner.js.map +1 -0
- package/dist/create/create-store.d.ts +2 -0
- package/dist/create/create-store.js +5 -0
- package/dist/create/create-store.js.map +1 -0
- package/dist/create/extractor.d.ts +8 -0
- package/dist/create/extractor.js +64 -0
- package/dist/create/extractor.js.map +1 -0
- package/dist/create/index.d.ts +1 -0
- package/dist/create/index.js +2 -0
- package/dist/create/index.js.map +1 -0
- package/dist/create/manager.d.ts +9 -0
- package/dist/create/manager.js +39 -0
- package/dist/create/manager.js.map +1 -0
- package/dist/create/proxy.d.ts +11 -0
- package/dist/create/proxy.js +57 -0
- package/dist/create/proxy.js.map +1 -0
- package/dist/create/scanner.d.ts +19 -0
- package/dist/create/scanner.js +109 -0
- package/dist/create/scanner.js.map +1 -0
- package/dist/create/utils.d.ts +3 -0
- package/dist/create/utils.js +33 -0
- package/dist/create/utils.js.map +1 -0
- package/dist/create-store-hooks.d.ts +19 -0
- package/dist/create-store-hooks.js +23 -0
- package/dist/create-store-hooks.js.map +1 -0
- package/dist/di/index.d.ts +2 -0
- package/dist/di/index.js +3 -0
- package/dist/di/index.js.map +1 -0
- package/dist/di/register-root-store.d.ts +11 -0
- package/dist/di/register-root-store.js +23 -0
- package/dist/di/register-root-store.js.map +1 -0
- package/dist/entities/cleaner/bucket.d.ts +8 -0
- package/dist/entities/cleaner/bucket.js +31 -0
- package/dist/entities/cleaner/bucket.js.map +1 -0
- package/dist/entities/cleaner/index.d.ts +1 -0
- package/dist/entities/cleaner/index.js +2 -0
- package/dist/entities/cleaner/index.js.map +1 -0
- package/dist/entities/cleaner/store.d.ts +12 -0
- package/dist/entities/cleaner/store.js +73 -0
- package/dist/entities/cleaner/store.js.map +1 -0
- package/dist/entities/cleaner/walker.d.ts +6 -0
- package/dist/entities/cleaner/walker.js +34 -0
- package/dist/entities/cleaner/walker.js.map +1 -0
- package/dist/entities/collection/create-entity-collection.d.ts +9 -0
- package/dist/entities/collection/create-entity-collection.js +17 -0
- package/dist/entities/collection/create-entity-collection.js.map +1 -0
- package/dist/entities/collection/entity-collection.d.ts +49 -0
- package/dist/entities/collection/entity-collection.js +186 -0
- package/dist/entities/collection/entity-collection.js.map +1 -0
- package/dist/entities/collection/index.d.ts +3 -0
- package/dist/entities/collection/index.js +2 -0
- package/dist/entities/collection/index.js.map +1 -0
- package/dist/entities/collection/marker.d.ts +2 -0
- package/dist/entities/collection/marker.js +3 -0
- package/dist/entities/collection/marker.js.map +1 -0
- package/dist/entities/collection/multi-entity-collection.d.ts +24 -0
- package/dist/entities/collection/multi-entity-collection.js +95 -0
- package/dist/entities/collection/multi-entity-collection.js.map +1 -0
- package/dist/entities/collection/public.d.ts +50 -0
- package/dist/entities/collection/public.js +2 -0
- package/dist/entities/collection/public.js.map +1 -0
- package/dist/entities/collection/types.d.ts +39 -0
- package/dist/entities/collection/types.js +2 -0
- package/dist/entities/collection/types.js.map +1 -0
- package/dist/entities/constants.d.ts +4 -0
- package/dist/entities/constants.js +5 -0
- package/dist/entities/constants.js.map +1 -0
- package/dist/entities/create-entity-schema.d.ts +2 -0
- package/dist/entities/create-entity-schema.js +5 -0
- package/dist/entities/create-entity-schema.js.map +1 -0
- package/dist/entities/index.d.ts +4 -0
- package/dist/entities/index.js +5 -0
- package/dist/entities/index.js.map +1 -0
- package/dist/entities/normalize.d.ts +11 -0
- package/dist/entities/normalize.js +79 -0
- package/dist/entities/normalize.js.map +1 -0
- package/dist/entities/processor.d.ts +14 -0
- package/dist/entities/processor.js +156 -0
- package/dist/entities/processor.js.map +1 -0
- package/dist/entities/public.d.ts +102 -0
- package/dist/entities/public.js +7 -0
- package/dist/entities/public.js.map +1 -0
- package/dist/entities/record/create-entity-record.d.ts +15 -0
- package/dist/entities/record/create-entity-record.js +13 -0
- package/dist/entities/record/create-entity-record.js.map +1 -0
- package/dist/entities/record/entity-record.d.ts +37 -0
- package/dist/entities/record/entity-record.js +83 -0
- package/dist/entities/record/entity-record.js.map +1 -0
- package/dist/entities/record/index.d.ts +2 -0
- package/dist/entities/record/index.js +2 -0
- package/dist/entities/record/index.js.map +1 -0
- package/dist/entities/record/marker.d.ts +1 -0
- package/dist/entities/record/marker.js +2 -0
- package/dist/entities/record/marker.js.map +1 -0
- package/dist/entities/record/public.d.ts +25 -0
- package/dist/entities/record/public.js +2 -0
- package/dist/entities/record/public.js.map +1 -0
- package/dist/entities/record/types.d.ts +3 -0
- package/dist/entities/record/types.js +2 -0
- package/dist/entities/record/types.js.map +1 -0
- package/dist/entities/schema.d.ts +12 -0
- package/dist/entities/schema.js +31 -0
- package/dist/entities/schema.js.map +1 -0
- package/dist/entities/store.d.ts +22 -0
- package/dist/entities/store.js +154 -0
- package/dist/entities/store.js.map +1 -0
- package/dist/entities/types.d.ts +82 -0
- package/dist/entities/types.js +2 -0
- package/dist/entities/types.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/dist/root/RootStore.d.ts +24 -0
- package/dist/root/RootStore.js +134 -0
- package/dist/root/RootStore.js.map +1 -0
- package/dist/root/coreApi/create-core-api.d.ts +7 -0
- package/dist/root/coreApi/create-core-api.js +12 -0
- package/dist/root/coreApi/create-core-api.js.map +1 -0
- package/dist/root/coreApi/entities.d.ts +10 -0
- package/dist/root/coreApi/entities.js +57 -0
- package/dist/root/coreApi/entities.js.map +1 -0
- package/dist/root/coreApi/lifecycle.d.ts +5 -0
- package/dist/root/coreApi/lifecycle.js +9 -0
- package/dist/root/coreApi/lifecycle.js.map +1 -0
- package/dist/root/coreApi/public.d.ts +71 -0
- package/dist/root/coreApi/public.js +6 -0
- package/dist/root/coreApi/public.js.map +1 -0
- package/dist/root/coreApi/stores.d.ts +2 -0
- package/dist/root/coreApi/stores.js +31 -0
- package/dist/root/coreApi/stores.js.map +1 -0
- package/dist/root/coreApi/types.d.ts +68 -0
- package/dist/root/coreApi/types.js +2 -0
- package/dist/root/coreApi/types.js.map +1 -0
- package/dist/root/create-root-store.d.ts +3 -0
- package/dist/root/create-root-store.js +11 -0
- package/dist/root/create-root-store.js.map +1 -0
- package/dist/root/fallback.d.ts +2 -0
- package/dist/root/fallback.js +6 -0
- package/dist/root/fallback.js.map +1 -0
- package/dist/root/index.d.ts +3 -0
- package/dist/root/index.js +3 -0
- package/dist/root/index.js.map +1 -0
- package/dist/root/marker.d.ts +1 -0
- package/dist/root/marker.js +2 -0
- package/dist/root/marker.js.map +1 -0
- package/dist/root/public.d.ts +28 -0
- package/dist/root/public.js +2 -0
- package/dist/root/public.js.map +1 -0
- package/dist/root/types.d.ts +66 -0
- package/dist/root/types.js +2 -0
- package/dist/root/types.js.map +1 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +2 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/throttle-by-time.d.ts +1 -0
- package/dist/utils/throttle-by-time.js +11 -0
- package/dist/utils/throttle-by-time.js.map +1 -0
- package/package.json +3 -2
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { TEntitiesStore, EntityProcessOptions } from './types';
|
|
2
|
+
import type { TSchemaMap } from '../root/types';
|
|
3
|
+
export declare class EntityProcessor {
|
|
4
|
+
private entities;
|
|
5
|
+
private schemaMap;
|
|
6
|
+
private readonly getEntity;
|
|
7
|
+
constructor(entities: TEntitiesStore, schemaMap: TSchemaMap);
|
|
8
|
+
private ensureMeta;
|
|
9
|
+
private upsert;
|
|
10
|
+
hydrate(snapshot: Record<string, Record<string, any>> | null | undefined): void;
|
|
11
|
+
process<T>({ data, entityKey, sourceRefId, isCollection, }: EntityProcessOptions<T>): (string | number)[];
|
|
12
|
+
}
|
|
13
|
+
export declare function createEntityProcessor(entities: TEntitiesStore, schemaMap: TSchemaMap, options: EntityProcessOptions<any>): (string | number)[];
|
|
14
|
+
export declare function createEntityRestorer(entities: TEntitiesStore, schemaMap: TSchemaMap): (snapshot: any) => void;
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { PREFIX } from './constants';
|
|
2
|
+
import { createNormalizer } from './normalize';
|
|
3
|
+
import { META } from '../constants/values';
|
|
4
|
+
export class EntityProcessor {
|
|
5
|
+
constructor(entities, schemaMap) {
|
|
6
|
+
this.entities = entities;
|
|
7
|
+
this.schemaMap = schemaMap;
|
|
8
|
+
this.getEntity = entities.getEntity.bind(entities);
|
|
9
|
+
}
|
|
10
|
+
// ===== meta =====
|
|
11
|
+
ensureMeta(instance) {
|
|
12
|
+
let meta = instance[META];
|
|
13
|
+
if (!meta) {
|
|
14
|
+
meta = {
|
|
15
|
+
createdAt: Date.now(),
|
|
16
|
+
updatedAt: Date.now(),
|
|
17
|
+
accessedAt: Date.now(),
|
|
18
|
+
refSources: new Set(),
|
|
19
|
+
};
|
|
20
|
+
Object.defineProperty(instance, META, {
|
|
21
|
+
value: meta,
|
|
22
|
+
enumerable: false,
|
|
23
|
+
writable: true,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
return meta;
|
|
27
|
+
}
|
|
28
|
+
// ===== upsert =====
|
|
29
|
+
upsert(dto, schema, entityKey, id, opts) {
|
|
30
|
+
const existing = this.entities.getEntity(entityKey, id);
|
|
31
|
+
if (opts.hydrate) {
|
|
32
|
+
const instance = schema.model
|
|
33
|
+
? new schema.model(dto, this.getEntity)
|
|
34
|
+
: { ...dto };
|
|
35
|
+
const meta = this.ensureMeta(instance);
|
|
36
|
+
if (opts.snapshot) {
|
|
37
|
+
meta.createdAt = opts.snapshot.createdAt ?? meta.createdAt;
|
|
38
|
+
meta.updatedAt = opts.snapshot.updatedAt ?? meta.updatedAt;
|
|
39
|
+
meta.accessedAt = opts.snapshot.accessedAt ?? meta.accessedAt;
|
|
40
|
+
const restoredRefs = opts.snapshot.refSources ?? [];
|
|
41
|
+
for (const ref of restoredRefs) {
|
|
42
|
+
meta.refSources.add(ref);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return instance;
|
|
46
|
+
}
|
|
47
|
+
const processed = schema.process({ ...dto });
|
|
48
|
+
if (existing) {
|
|
49
|
+
if (existing.__partial) {
|
|
50
|
+
delete existing.__partial;
|
|
51
|
+
}
|
|
52
|
+
schema.merge(existing, processed);
|
|
53
|
+
const meta = this.ensureMeta(existing);
|
|
54
|
+
const now = Date.now();
|
|
55
|
+
meta.updatedAt = now;
|
|
56
|
+
meta.accessedAt = now;
|
|
57
|
+
if (opts.sourceRefId) {
|
|
58
|
+
meta.refSources.add(opts.sourceRefId);
|
|
59
|
+
}
|
|
60
|
+
return existing;
|
|
61
|
+
}
|
|
62
|
+
const instance = schema.model
|
|
63
|
+
? new schema.model(processed, this.getEntity)
|
|
64
|
+
: { ...processed };
|
|
65
|
+
const meta = this.ensureMeta(instance);
|
|
66
|
+
if (opts.sourceRefId) {
|
|
67
|
+
meta.refSources.add(opts.sourceRefId);
|
|
68
|
+
}
|
|
69
|
+
return instance;
|
|
70
|
+
}
|
|
71
|
+
// ===== hydrate =====
|
|
72
|
+
hydrate(snapshot) {
|
|
73
|
+
if (!snapshot) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
const out = {};
|
|
77
|
+
for (const entityKey of Object.keys(snapshot)) {
|
|
78
|
+
const schema = this.schemaMap[entityKey];
|
|
79
|
+
if (!schema) {
|
|
80
|
+
if (__DEV__) {
|
|
81
|
+
console.warn(`[hydrate] Schema not found for entityKey="${entityKey}". Skipping.`);
|
|
82
|
+
}
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
const bucket = snapshot[entityKey];
|
|
86
|
+
if (!bucket) {
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
const restored = {};
|
|
90
|
+
for (const id of Object.keys(bucket)) {
|
|
91
|
+
const full = bucket[id];
|
|
92
|
+
if (!full) {
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
const { [META]: meta, ...dto } = full;
|
|
96
|
+
restored[id] = this.upsert(dto, schema, entityKey, id, {
|
|
97
|
+
hydrate: true,
|
|
98
|
+
snapshot: meta,
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
out[entityKey] = restored;
|
|
102
|
+
}
|
|
103
|
+
this.entities.merge(out);
|
|
104
|
+
}
|
|
105
|
+
// ===== process =====
|
|
106
|
+
process({ data, entityKey, sourceRefId, isCollection, }) {
|
|
107
|
+
const schema = this.schemaMap[entityKey];
|
|
108
|
+
if (!schema) {
|
|
109
|
+
if (__DEV__) {
|
|
110
|
+
console.warn(`[process] Schema not found for entityKey="${entityKey}". Skipping.`);
|
|
111
|
+
}
|
|
112
|
+
return [];
|
|
113
|
+
}
|
|
114
|
+
const safeSourceRefId = sourceRefId && typeof sourceRefId === 'string' ? sourceRefId : 'unknown';
|
|
115
|
+
const refId = isCollection
|
|
116
|
+
? `${PREFIX.COLLECTION}${safeSourceRefId}`
|
|
117
|
+
: `${PREFIX.RECORD}${safeSourceRefId}`;
|
|
118
|
+
const { ids, map } = createNormalizer().normalize(data, schema);
|
|
119
|
+
const out = {};
|
|
120
|
+
for (const key of Object.keys(map)) {
|
|
121
|
+
const bucket = map[key];
|
|
122
|
+
if (!bucket) {
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
const schema = this.schemaMap[key];
|
|
126
|
+
if (!schema) {
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
const result = {};
|
|
130
|
+
for (const id of Object.keys(bucket)) {
|
|
131
|
+
result[id] = this.upsert(bucket[id], schema, key, id, {
|
|
132
|
+
hydrate: false,
|
|
133
|
+
sourceRefId: refId,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
out[key] = result;
|
|
137
|
+
}
|
|
138
|
+
this.entities.merge(out);
|
|
139
|
+
return ids;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
// ===== factories =====
|
|
143
|
+
export function createEntityProcessor(entities, schemaMap, options) {
|
|
144
|
+
if (!entities) {
|
|
145
|
+
throw new Error('createEntityProcessor: EntitiesStore is required.');
|
|
146
|
+
}
|
|
147
|
+
return new EntityProcessor(entities, schemaMap).process(options);
|
|
148
|
+
}
|
|
149
|
+
export function createEntityRestorer(entities, schemaMap) {
|
|
150
|
+
if (!entities) {
|
|
151
|
+
throw new Error('createEntityRestorer: EntitiesStore is required.');
|
|
152
|
+
}
|
|
153
|
+
const p = new EntityProcessor(entities, schemaMap);
|
|
154
|
+
return (snapshot) => p.hydrate(snapshot);
|
|
155
|
+
}
|
|
156
|
+
//# sourceMappingURL=processor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"processor.js","sourceRoot":"","sources":["../../src/entities/processor.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAA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OAAO,CAAC,QAAa,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;AAChD,CAAC"}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ============================
|
|
3
|
+
* Nexigen – Public Entity API
|
|
4
|
+
* ============================
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Primitive entity identifier
|
|
8
|
+
*/
|
|
9
|
+
export type EntityId = string | number;
|
|
10
|
+
/**
|
|
11
|
+
* ============================
|
|
12
|
+
* Entity Model Context
|
|
13
|
+
* ============================
|
|
14
|
+
*/
|
|
15
|
+
export type EntityGetter = <T = any>(entityKey: string, id: string | number) => T | undefined;
|
|
16
|
+
/**
|
|
17
|
+
* ============================
|
|
18
|
+
* Entity Model Constructor
|
|
19
|
+
* ============================
|
|
20
|
+
*
|
|
21
|
+
* User-defined domain model constructor.
|
|
22
|
+
*
|
|
23
|
+
* Example:
|
|
24
|
+
* class PostModel {
|
|
25
|
+
* constructor(dto: PostDto, ctx: EntityGetter) {}
|
|
26
|
+
* }
|
|
27
|
+
*/
|
|
28
|
+
export type EntityModelCtor<TDto, TModel> = new (dto: TDto, ctx: EntityGetter) => TModel;
|
|
29
|
+
/**
|
|
30
|
+
* ============================
|
|
31
|
+
* Public Entity Schema
|
|
32
|
+
* ============================
|
|
33
|
+
*
|
|
34
|
+
* Read-only interface exposed to userland.
|
|
35
|
+
* No normalization, no GC, no persistence details.
|
|
36
|
+
*/
|
|
37
|
+
export interface PublicEntitySchema<TDto, TModel> {
|
|
38
|
+
/**
|
|
39
|
+
* Unique entity key
|
|
40
|
+
*/
|
|
41
|
+
readonly key: string;
|
|
42
|
+
/**
|
|
43
|
+
* Extract entity id from DTO
|
|
44
|
+
*/
|
|
45
|
+
getId(input: TDto): EntityId;
|
|
46
|
+
/**
|
|
47
|
+
* Return id attribute key (string only)
|
|
48
|
+
*/
|
|
49
|
+
getIdKey(): string;
|
|
50
|
+
/**
|
|
51
|
+
* Optional preprocessing before normalization
|
|
52
|
+
*/
|
|
53
|
+
process(input: TDto): TDto;
|
|
54
|
+
/**
|
|
55
|
+
* Merge strategy for model updates
|
|
56
|
+
*/
|
|
57
|
+
merge(target: TModel, source: Partial<TModel>): TModel;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* ============================
|
|
61
|
+
* Entity Relations Definition
|
|
62
|
+
* ============================
|
|
63
|
+
*
|
|
64
|
+
* Used to describe nested schemas.
|
|
65
|
+
*
|
|
66
|
+
* Example:
|
|
67
|
+
* {
|
|
68
|
+
* author: userSchema,
|
|
69
|
+
* comments: [commentSchema],
|
|
70
|
+
* }
|
|
71
|
+
*/
|
|
72
|
+
export type EntitySchemaDefinition = Record<string, PublicEntitySchema<any, any> | [PublicEntitySchema<any, any>]>;
|
|
73
|
+
/**
|
|
74
|
+
* ============================
|
|
75
|
+
* Entity Schema Config
|
|
76
|
+
* ============================
|
|
77
|
+
*
|
|
78
|
+
* User-provided configuration.
|
|
79
|
+
*/
|
|
80
|
+
export interface EntitySchemaConfig<TDto, TModel> {
|
|
81
|
+
/**
|
|
82
|
+
* ID attribute or resolver
|
|
83
|
+
*
|
|
84
|
+
* Default: "id"
|
|
85
|
+
*/
|
|
86
|
+
idAttribute?: keyof TDto | ((dto: TDto) => EntityId);
|
|
87
|
+
/**
|
|
88
|
+
* Domain model constructor
|
|
89
|
+
*
|
|
90
|
+
* Example:
|
|
91
|
+
* model: PostModel
|
|
92
|
+
*/
|
|
93
|
+
model?: EntityModelCtor<TDto, TModel>;
|
|
94
|
+
/**
|
|
95
|
+
* Optional DTO preprocessing
|
|
96
|
+
*/
|
|
97
|
+
processStrategy?: (dto: TDto) => TDto;
|
|
98
|
+
/**
|
|
99
|
+
* Optional merge strategy
|
|
100
|
+
*/
|
|
101
|
+
mergeStrategy?: (target: TModel, source: Partial<TModel>) => TModel;
|
|
102
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"public.js","sourceRoot":"","sources":["../../src/entities/public.ts"],"names":[],"mappings":"AAAA;;;;GAIG"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { EntityRecord } from './entity-record';
|
|
2
|
+
import type { CoreEntitiesAPI } from '../../root/coreApi/types';
|
|
3
|
+
import type { EntityCleanerStore } from '../cleaner';
|
|
4
|
+
import type { TEntitiesStore } from '../types';
|
|
5
|
+
export declare function createEntityRecord<T extends {
|
|
6
|
+
id: string | number;
|
|
7
|
+
}, M>(options: {
|
|
8
|
+
entityKey: string;
|
|
9
|
+
recordId: string;
|
|
10
|
+
}, system: {
|
|
11
|
+
entities: TEntitiesStore;
|
|
12
|
+
entitiesCleaner: EntityCleanerStore;
|
|
13
|
+
notify: () => void;
|
|
14
|
+
entitiesApi: CoreEntitiesAPI;
|
|
15
|
+
}): EntityRecord<T, M>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { EntityRecord } from './entity-record';
|
|
2
|
+
export function createEntityRecord(options, system) {
|
|
3
|
+
if (__DEV__) {
|
|
4
|
+
if (!options.entityKey) {
|
|
5
|
+
console.warn('[EntityRecord] Missing "entityKey" in options');
|
|
6
|
+
}
|
|
7
|
+
if (!options.recordId) {
|
|
8
|
+
console.warn('[EntityRecord] Missing "recordId" in options');
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
return new EntityRecord(options, system);
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=create-entity-record.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-entity-record.js","sourceRoot":"","sources":["../../../src/entities/record/create-entity-record.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAO/C,MAAM,UAAU,kBAAkB,CAChC,OAAgD,EAChD,MAKC;IAED,IAAI,OAAO,EAAE,CAAC;QACZ,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,CAAC;YACvB,OAAO,CAAC,IAAI,CAAC,+CAA+C,CAAC,CAAC;QAChE,CAAC;QAED,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC;YACtB,OAAO,CAAC,IAAI,CAAC,8CAA8C,CAAC,CAAC;QAC/D,CAAC;IACH,CAAC;IAED,OAAO,IAAI,YAAY,CAAO,OAAO,EAAE,MAAM,CAAC,CAAC;AACjD,CAAC"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { RECORD_TAG } from './marker';
|
|
2
|
+
import type { CoreEntitiesAPI } from '../../root/coreApi/types';
|
|
3
|
+
import type { EntityCleanerStore } from '../cleaner';
|
|
4
|
+
import type { TEntitiesStore } from '../types';
|
|
5
|
+
import type { EntityRecordSnapshot } from './types';
|
|
6
|
+
export declare class EntityRecord<T extends {
|
|
7
|
+
id: string | number;
|
|
8
|
+
}, M> {
|
|
9
|
+
private entitiesApi;
|
|
10
|
+
[RECORD_TAG]: boolean;
|
|
11
|
+
private id;
|
|
12
|
+
private entityKey;
|
|
13
|
+
private entities;
|
|
14
|
+
private entitiesCleaner;
|
|
15
|
+
private notify;
|
|
16
|
+
recordId: string;
|
|
17
|
+
constructor(options: {
|
|
18
|
+
entityKey: string;
|
|
19
|
+
recordId: string;
|
|
20
|
+
}, system: {
|
|
21
|
+
entities: TEntitiesStore;
|
|
22
|
+
entitiesCleaner: EntityCleanerStore;
|
|
23
|
+
notify: () => void;
|
|
24
|
+
entitiesApi: CoreEntitiesAPI;
|
|
25
|
+
});
|
|
26
|
+
private process;
|
|
27
|
+
private sync;
|
|
28
|
+
set(item: T): void;
|
|
29
|
+
update(item: T): void;
|
|
30
|
+
reset(): void;
|
|
31
|
+
get data(): M | undefined;
|
|
32
|
+
get entityId(): string | number | null;
|
|
33
|
+
get exists(): boolean;
|
|
34
|
+
get isEmpty(): boolean;
|
|
35
|
+
getSnapshot(): EntityRecordSnapshot;
|
|
36
|
+
applySnapshot(snapshot: EntityRecordSnapshot): void;
|
|
37
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
var _a;
|
|
2
|
+
import { makeAutoObservable, runInAction } from 'mobx';
|
|
3
|
+
import { PREFIX } from '../constants';
|
|
4
|
+
import { RECORD_TAG } from './marker';
|
|
5
|
+
export class EntityRecord {
|
|
6
|
+
constructor(options, system) {
|
|
7
|
+
this[_a] = true;
|
|
8
|
+
this.id = null;
|
|
9
|
+
this.entityKey = options.entityKey;
|
|
10
|
+
this.recordId = options.recordId;
|
|
11
|
+
this.entities = system.entities;
|
|
12
|
+
this.entitiesCleaner = system.entitiesCleaner;
|
|
13
|
+
this.notify = system.notify;
|
|
14
|
+
this.entitiesApi = system.entitiesApi;
|
|
15
|
+
makeAutoObservable(this, {}, { autoBind: true });
|
|
16
|
+
}
|
|
17
|
+
// ---------------- NORMALIZATION ----------------
|
|
18
|
+
process(item) {
|
|
19
|
+
const ids = this.entitiesApi.process({
|
|
20
|
+
data: [item],
|
|
21
|
+
entityKey: this.entityKey,
|
|
22
|
+
sourceRefId: this.recordId,
|
|
23
|
+
});
|
|
24
|
+
return ids[0];
|
|
25
|
+
}
|
|
26
|
+
sync() {
|
|
27
|
+
this.notify();
|
|
28
|
+
}
|
|
29
|
+
// ---------------- PUBLIC API ----------------
|
|
30
|
+
set(item) {
|
|
31
|
+
this.id = this.process(item);
|
|
32
|
+
this.sync();
|
|
33
|
+
}
|
|
34
|
+
update(item) {
|
|
35
|
+
this.process(item);
|
|
36
|
+
this.sync();
|
|
37
|
+
}
|
|
38
|
+
reset() {
|
|
39
|
+
if (this.id === null) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
this.entitiesCleaner.deleteCascade(this.entityKey, [this.id], `${PREFIX.RECORD}${this.recordId}`);
|
|
43
|
+
this.id = null;
|
|
44
|
+
this.sync();
|
|
45
|
+
}
|
|
46
|
+
// ---------------- GETTERS ----------------
|
|
47
|
+
get data() {
|
|
48
|
+
if (!this.id) {
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
const entity = this.entities.getEntity(this.entityKey, this.id);
|
|
52
|
+
// lazy cleanup
|
|
53
|
+
if (!entity) {
|
|
54
|
+
queueMicrotask(() => {
|
|
55
|
+
runInAction(() => {
|
|
56
|
+
this.id = null;
|
|
57
|
+
this.sync();
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
return entity;
|
|
63
|
+
}
|
|
64
|
+
get entityId() {
|
|
65
|
+
return this.id;
|
|
66
|
+
}
|
|
67
|
+
get exists() {
|
|
68
|
+
return this.id !== null && this.data !== undefined;
|
|
69
|
+
}
|
|
70
|
+
get isEmpty() {
|
|
71
|
+
return this.id === null;
|
|
72
|
+
}
|
|
73
|
+
// ---------------- SNAPSHOT ----------------
|
|
74
|
+
getSnapshot() {
|
|
75
|
+
return { id: this.id };
|
|
76
|
+
}
|
|
77
|
+
applySnapshot(snapshot) {
|
|
78
|
+
this.id = snapshot.id;
|
|
79
|
+
this.sync();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
_a = RECORD_TAG;
|
|
83
|
+
//# sourceMappingURL=entity-record.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"entity-record.js","sourceRoot":"","sources":["../../../src/entities/record/entity-record.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,kBAAkB,EAAE,WAAW,EAAE,MAAM,MAAM,CAAC;AAEvD,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AACtC,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAOtC,MAAM,OAAO,YAAY;IAYvB,YACE,OAGC,EACD,MAKC;QAnBI,QAAY,GAAG,IAAI,CAAC;QAEnB,OAAE,GAA2B,IAAI,CAAC;QAmBxC,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC;QACnC,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;QACjC,IAAI,CAAC,QAAQ,GAAG,MAAM,C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,CAAC;IAED,4CAA4C;IAE5C,IAAI,IAAI;QACN,IAAI,CAAC,IAAI,CAAC,EAAE,EAAE,CAAC;YACb,OAAO,SAAS,CAAC;QACnB,CAAC;QAED,MAAM,MAAM,GAAG,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,EAAE,CAEjD,CAAC;QAEd,eAAe;QACf,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,cAAc,CAAC,GAAG,EAAE;gBAClB,WAAW,CAAC,GAAG,EAAE;oBACf,IAAI,CAAC,EAAE,GAAG,IAAI,CAAC;oBACf,IAAI,CAAC,IAAI,EAAE,CAAC;gBACd,CAAC,CAAC,CAAC;YACL,CAAC,CAAC,CAAC;YAEH,OAAO,SAAS,CAAC;QACnB,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,EAAE,CAAC;IACjB,CAAC;IAED,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,EAAE,KAAK,IAAI,IAAI,IAAI,CAAC,IAAI,KAAK,SAAS,CAAC;IACrD,CAAC;IAED,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,EAAE,KAAK,IAAI,CAAC;IAC1B,CAAC;IAED,6CAA6C;IAE7C,WAAW;QACT,OAAO,EAAE,EAAE,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC;IACzB,CAAC;IAED,aAAa,CAAC,QAA8B;QAC1C,IAAI,CAAC,EAAE,GAAG,QAAQ,CAAC,EAAE,CAAC;QACtB,IAAI,CAAC,IAAI,EAAE,CAAC;IACd,CAAC;CACF;KAzHS,UAAU"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/entities/record/index.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const RECORD_TAG: unique symbol;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"marker.js","sourceRoot":"","sources":["../../../src/entities/record/marker.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,UAAU,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export interface EntityRecordSnapshot {
|
|
2
|
+
id: string | number | null;
|
|
3
|
+
}
|
|
4
|
+
export interface EntityRecord<T, M> {
|
|
5
|
+
/** Current entity id or null */
|
|
6
|
+
readonly entityId: string | number | null;
|
|
7
|
+
/** Resolved entity model (may trigger lazy cleanup) */
|
|
8
|
+
readonly data: M | undefined;
|
|
9
|
+
/** True if record points to existing entity */
|
|
10
|
+
readonly exists: boolean;
|
|
11
|
+
/** True if record is empty (no id) */
|
|
12
|
+
readonly isEmpty: boolean;
|
|
13
|
+
/** Stable record identifier */
|
|
14
|
+
readonly recordId: string;
|
|
15
|
+
/** Normalize & set entity, replaces id */
|
|
16
|
+
set(item: T): void;
|
|
17
|
+
/** Normalize entity without changing id */
|
|
18
|
+
update(item: T): void;
|
|
19
|
+
/** Clear record and cascade delete */
|
|
20
|
+
reset(): void;
|
|
21
|
+
/** Serialize record state */
|
|
22
|
+
getSnapshot(): EntityRecordSnapshot;
|
|
23
|
+
/** Restore record state */
|
|
24
|
+
applySnapshot(snapshot: EntityRecordSnapshot): void;
|
|
25
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"public.js","sourceRoot":"","sources":["../../../src/entities/record/public.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/entities/record/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { EntitySchemaOptions, EntityModelCtor } from './types';
|
|
2
|
+
export declare class EntitySchema<TDto, TModel> {
|
|
3
|
+
key: string;
|
|
4
|
+
definition: Record<string, EntitySchema<any, any> | [EntitySchema<any, any>]>;
|
|
5
|
+
options: EntitySchemaOptions<TDto, TModel>;
|
|
6
|
+
constructor(key: string, definition?: Record<string, EntitySchema<any, any> | [EntitySchema<any, any>]>, options?: EntitySchemaOptions<TDto, TModel>);
|
|
7
|
+
get model(): EntityModelCtor<TDto, TModel> | undefined;
|
|
8
|
+
getId(input: TDto): any;
|
|
9
|
+
getIdKey(): string;
|
|
10
|
+
process(input: TDto): TDto;
|
|
11
|
+
merge(target: TModel, source: Partial<TModel>): any;
|
|
12
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export class EntitySchema {
|
|
2
|
+
constructor(key, definition = {}, options = {}) {
|
|
3
|
+
this.key = key;
|
|
4
|
+
this.definition = definition;
|
|
5
|
+
this.options = options;
|
|
6
|
+
}
|
|
7
|
+
get model() {
|
|
8
|
+
return this.options.model;
|
|
9
|
+
}
|
|
10
|
+
getId(input) {
|
|
11
|
+
const { idAttribute = 'id' } = this.options;
|
|
12
|
+
return typeof idAttribute === 'function'
|
|
13
|
+
? idAttribute(input)
|
|
14
|
+
: input[idAttribute];
|
|
15
|
+
}
|
|
16
|
+
getIdKey() {
|
|
17
|
+
const { idAttribute = 'id' } = this.options;
|
|
18
|
+
return typeof idAttribute === 'string' ? idAttribute : 'id';
|
|
19
|
+
}
|
|
20
|
+
process(input) {
|
|
21
|
+
return this.options.processStrategy
|
|
22
|
+
? this.options.processStrategy(input)
|
|
23
|
+
: { ...input };
|
|
24
|
+
}
|
|
25
|
+
merge(target, source) {
|
|
26
|
+
return this.options.mergeStrategy
|
|
27
|
+
? this.options.mergeStrategy(target, source)
|
|
28
|
+
: Object.assign(target, source);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../../src/entities/schema.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,YAAY;IACvB,YACS,GAAW,EACX,aAGH,EAAE,EACC,UAA6C,EAAE;QAL/C,QAAG,GAAH,GAAG,CAAQ;QACX,eAAU,GAAV,UAAU,CAGX;QACC,YAAO,GAAP,OAAO,CAAwC;IACrD,CAAC;IAEJ,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC;IAC5B,CAAC;IAED,KAAK,CAAC,KAAW;QACf,MAAM,EAAE,WAAW,GAAG,IAAI,EAAE,GAAG,IAAI,CAAC,OAAO,CAAC;QAC5C,OAAO,OAAO,WAAW,KAAK,UAAU;YACtC,CAAC,CAAC,WAAW,CAAC,KAAK,CAAC;YACpB,CAAC,CAAE,KAAa,CAAC,WAAW,CAAC,CAAC;IAClC,CAAC;IAED,QAAQ;QACN,MAAM,EAAE,WAAW,GAAG,IAAI,EAAE,GAAG,IAAI,CAAC,OAAO,CAAC;QAC5C,OAAO,OAAO,WAAW,KAAK,QAAQ,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC;IAC9D,CAAC;IAED,OAAO,CAAC,KAAW;QACjB,OAAO,IAAI,CAAC,OAAO,CAAC,eAAe;YACjC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,eAAe,CAAC,KAAK,CAAC;YACrC,CAAC,CAAC,EAAE,GAAG,KAAK,EAAE,CAAC;IACnB,CAAC;IAED,KAAK,CAAC,MAAc,EAAE,MAAuB;QAC3C,OAAO,IAAI,CAAC,OAAO,CAAC,aAAa;YAC/B,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,MAAM,EAAE,MAAM,CAAC;YAC5C,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,MAAa,EAAE,MAAM,CAAC,CAAC;IAC3C,CAAC;CACF"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { EntityBuckets, StoredEntity } from './types';
|
|
2
|
+
export declare class EntitiesStore {
|
|
3
|
+
private buckets;
|
|
4
|
+
private onMergeCallbacks;
|
|
5
|
+
constructor();
|
|
6
|
+
addOnMergeCallback(fn: () => void): void;
|
|
7
|
+
removeOnMergeCallback(fn: () => void): void;
|
|
8
|
+
private triggerMergeCallbacks;
|
|
9
|
+
private getBucket;
|
|
10
|
+
private ensureBucket;
|
|
11
|
+
merge(newEntities: Partial<EntityBuckets>): void;
|
|
12
|
+
hasEntity(entityKey: string, id: string | number): boolean;
|
|
13
|
+
getEntity<T = any>(entityKey: string, id: string | number): StoredEntity<T> | undefined;
|
|
14
|
+
getAll<T = any>(entityKey: string): StoredEntity<T>[];
|
|
15
|
+
getCount(entityKey: string): number;
|
|
16
|
+
remove(entityKey: string, id: string | number): void;
|
|
17
|
+
removeMany(toRemove: Record<string, Set<string>>): void;
|
|
18
|
+
reset(entityKey?: string): void;
|
|
19
|
+
get getSnapshot(): Record<string, any>;
|
|
20
|
+
get getSnapshotKeys(): string[];
|
|
21
|
+
getSnapshotByKey(entityKey: string): Record<string, any>;
|
|
22
|
+
}
|