@domain-first/types 1.0.4 → 1.0.6

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 CHANGED
@@ -42,11 +42,7 @@ class UserId extends defineValueObject(
42
42
 
43
43
  class User extends defineEntity(
44
44
  // id schema
45
- z.custom<UserId>(
46
- // every Entity and Value Object has a static `is` method
47
- // for type-safe instance checks
48
- UserId.is,
49
- ),
45
+ z.instanceof(UserId),
50
46
  // model schema
51
47
  z.object({ name: z.string().nonempty() }),
52
48
  ) {}
package/dist/index.cjs CHANGED
@@ -30,6 +30,7 @@ __webpack_require__.r(__webpack_exports__);
30
30
  __webpack_require__.d(__webpack_exports__, {
31
31
  AsyncSchemaInSyncParsingError: ()=>AsyncSchemaInSyncParsingError,
32
32
  InvalidDataParsingError: ()=>InvalidDataParsingError,
33
+ defineEntity: ()=>defineEntity,
33
34
  defineValueObject: ()=>defineValueObject
34
35
  });
35
36
  const errors_namespaceObject = require("@domain-first/errors");
@@ -51,6 +52,41 @@ function parseSync(schema, value) {
51
52
  });
52
53
  return result.value;
53
54
  }
55
+ const defineEntity = (idSchema, modelSchema)=>{
56
+ const idSymbol = Symbol();
57
+ const modelSymbol = Symbol();
58
+ const classSymbol = Symbol();
59
+ const isThisClass = (target)=>!!target && 'object' == typeof target && classSymbol in target;
60
+ class Entity {
61
+ static idSchema = idSchema;
62
+ static modelSchema = 'function' == typeof modelSchema ? modelSchema(isThisClass) : modelSchema;
63
+ constructor(id, model){
64
+ const parsedId = parseSync(Entity.idSchema, id);
65
+ const parsedModelData = parseSync(Entity.modelSchema, model);
66
+ Object.defineProperty(this, idSymbol, {
67
+ value: parsedId
68
+ });
69
+ Object.defineProperty(this, modelSymbol, {
70
+ value: parsedModelData
71
+ });
72
+ Object.defineProperty(this, classSymbol, {
73
+ value: true
74
+ });
75
+ }
76
+ static is(target) {
77
+ return isThisClass(target);
78
+ }
79
+ get model() {
80
+ const thisWithSymbol = this;
81
+ return thisWithSymbol[modelSymbol];
82
+ }
83
+ get id() {
84
+ const thisWithSymbol = this;
85
+ return thisWithSymbol[idSymbol];
86
+ }
87
+ }
88
+ return Entity;
89
+ };
54
90
  const defineValueObject = (schema)=>{
55
91
  const modelSymbol = Symbol();
56
92
  const classSymbol = Symbol();
@@ -78,10 +114,12 @@ const defineValueObject = (schema)=>{
78
114
  };
79
115
  exports.AsyncSchemaInSyncParsingError = __webpack_exports__.AsyncSchemaInSyncParsingError;
80
116
  exports.InvalidDataParsingError = __webpack_exports__.InvalidDataParsingError;
117
+ exports.defineEntity = __webpack_exports__.defineEntity;
81
118
  exports.defineValueObject = __webpack_exports__.defineValueObject;
82
119
  for(var __rspack_i in __webpack_exports__)if (-1 === [
83
120
  "AsyncSchemaInSyncParsingError",
84
121
  "InvalidDataParsingError",
122
+ "defineEntity",
85
123
  "defineValueObject"
86
124
  ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
87
125
  Object.defineProperty(exports, '__esModule', {
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export * from './entity';
1
2
  export * from './errors';
2
3
  export type { DeepReadonly } from './utils';
3
4
  export * from './value-object';
package/dist/index.js CHANGED
@@ -17,6 +17,41 @@ function parseSync(schema, value) {
17
17
  });
18
18
  return result.value;
19
19
  }
20
+ const defineEntity = (idSchema, modelSchema)=>{
21
+ const idSymbol = Symbol();
22
+ const modelSymbol = Symbol();
23
+ const classSymbol = Symbol();
24
+ const isThisClass = (target)=>!!target && 'object' == typeof target && classSymbol in target;
25
+ class Entity {
26
+ static idSchema = idSchema;
27
+ static modelSchema = 'function' == typeof modelSchema ? modelSchema(isThisClass) : modelSchema;
28
+ constructor(id, model){
29
+ const parsedId = parseSync(Entity.idSchema, id);
30
+ const parsedModelData = parseSync(Entity.modelSchema, model);
31
+ Object.defineProperty(this, idSymbol, {
32
+ value: parsedId
33
+ });
34
+ Object.defineProperty(this, modelSymbol, {
35
+ value: parsedModelData
36
+ });
37
+ Object.defineProperty(this, classSymbol, {
38
+ value: true
39
+ });
40
+ }
41
+ static is(target) {
42
+ return isThisClass(target);
43
+ }
44
+ get model() {
45
+ const thisWithSymbol = this;
46
+ return thisWithSymbol[modelSymbol];
47
+ }
48
+ get id() {
49
+ const thisWithSymbol = this;
50
+ return thisWithSymbol[idSymbol];
51
+ }
52
+ }
53
+ return Entity;
54
+ };
20
55
  const defineValueObject = (schema)=>{
21
56
  const modelSymbol = Symbol();
22
57
  const classSymbol = Symbol();
@@ -42,4 +77,4 @@ const defineValueObject = (schema)=>{
42
77
  }
43
78
  return ValueObject;
44
79
  };
45
- export { AsyncSchemaInSyncParsingError, InvalidDataParsingError, defineValueObject };
80
+ export { AsyncSchemaInSyncParsingError, InvalidDataParsingError, defineEntity, defineValueObject };
package/package.json CHANGED
@@ -12,7 +12,7 @@
12
12
  "ddd",
13
13
  "domain-driven"
14
14
  ],
15
- "version": "1.0.4",
15
+ "version": "1.0.6",
16
16
  "type": "module",
17
17
  "repository": {
18
18
  "type": "git",