@schorts/shared-kernel 1.1.0 → 1.1.1
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/CHANGELOG
CHANGED
|
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [1.1.1] - 2025-09-30
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- Added `RegisterEntity` decorator to only decorate the entity to add it to the `EntityRegistry`.
|
|
13
|
+
|
|
8
14
|
## [1.1.0] - 2025-09-30
|
|
9
15
|
|
|
10
16
|
### Changed
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { RegisterEntity, EntityRegistry, Entity, EntityNotRegistered } from "../../src/entities";
|
|
2
|
+
import { UUIDValue } from "../../src/value-objects";
|
|
3
|
+
|
|
4
|
+
type MockModel = {
|
|
5
|
+
id: string;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
class ID extends UUIDValue {
|
|
9
|
+
attributeName = "id";
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
class MockEntity extends Entity<ID, MockModel> {
|
|
13
|
+
constructor(id: ID) {
|
|
14
|
+
super(id);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
toPrimitives(): MockModel {
|
|
18
|
+
return {
|
|
19
|
+
id: this.id.value,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
static fromPrimitives<MockModel>(model: MockModel): MockEntity {
|
|
24
|
+
return new MockEntity(new ID(model["id"]));
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
RegisterEntity("mock")(MockEntity);
|
|
29
|
+
|
|
30
|
+
describe("RegisterEntity", () => {
|
|
31
|
+
beforeEach(() => {
|
|
32
|
+
(EntityRegistry as any).registry.clear();
|
|
33
|
+
RegisterEntity("mock")(MockEntity);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("registers the entity constructor under the given type", () => {
|
|
37
|
+
const resolved = EntityRegistry.resolve<MockModel>("mock");
|
|
38
|
+
expect(resolved).toBe(MockEntity);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("creates an entity instance from registered type", () => {
|
|
42
|
+
const model = { id: 'abc-123' };
|
|
43
|
+
const entity = EntityRegistry.create("mock", model);
|
|
44
|
+
|
|
45
|
+
expect(entity).toBeInstanceOf(MockEntity);
|
|
46
|
+
expect(entity.id.value).toBe("abc-123");
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("throws if trying to create an unregistered entity", () => {
|
|
50
|
+
const model = { id: '123' };
|
|
51
|
+
|
|
52
|
+
expect(() => EntityRegistry.create("unknown", model)).toThrow(EntityNotRegistered);
|
|
53
|
+
});
|
|
54
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@schorts/shared-kernel",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "A modular, type-safe foundation for building expressive, maintainable applications. This package provides core abstractions for domain modeling, HTTP integration, authentication, state management, and more — designed to be framework-agnostic and highly extensible.",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -16,7 +16,7 @@ export class EntityRegistry {
|
|
|
16
16
|
|
|
17
17
|
static resolve<Model extends BaseModel>(type: string): EntityConstructor<Model> | null {
|
|
18
18
|
return (this.registry.get(type) || null) as EntityConstructor<Model> | null;
|
|
19
|
-
}
|
|
19
|
+
}
|
|
20
20
|
|
|
21
21
|
static create<Model extends BaseModel>(type: string, model: Model): Entity<ValueObject, Model> {
|
|
22
22
|
const entity = this.resolve<Model>(type);
|
package/src/entities/index.ts
CHANGED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { EntityRegistry } from "./entity-registry";
|
|
2
|
+
import { BaseModel } from "../models";
|
|
3
|
+
import { Entity as BaseEntity } from "./entity";
|
|
4
|
+
import { ValueObject } from "../value-objects";
|
|
5
|
+
|
|
6
|
+
type EntityConstructor<Model extends BaseModel = BaseModel> = {
|
|
7
|
+
fromPrimitives(model: Model): BaseEntity<ValueObject, Model>;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export function RegisterEntity<Model extends BaseModel = BaseModel>(type: string) {
|
|
11
|
+
return function <Entity extends EntityConstructor<Model>>(entity: Entity): Entity {
|
|
12
|
+
EntityRegistry.register(type, entity);
|
|
13
|
+
|
|
14
|
+
return entity;
|
|
15
|
+
};
|
|
16
|
+
}
|