@quanticdigit/web-model 1.0.1 → 1.0.3
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 +53 -39
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,64 +1,78 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @quanticdigit/web-model
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Typed model reconstruction from JSON: `BaseModel` and the expose decorators for dates and nested
|
|
4
|
+
models.
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
This package holds the **machinery**, not the models. Your own model hierarchy lives in your product,
|
|
7
|
+
where your generator writes it from your DTOs — those are your entity conventions, and a shared
|
|
8
|
+
package that contained them would force the second consumer to adopt the first one's.
|
|
6
9
|
|
|
7
|
-
|
|
10
|
+
## Install
|
|
8
11
|
|
|
9
12
|
```bash
|
|
10
|
-
|
|
13
|
+
npm install @quanticdigit/web-model class-transformer reflect-metadata
|
|
11
14
|
```
|
|
12
15
|
|
|
13
|
-
|
|
16
|
+
`class-transformer` and `reflect-metadata` are peer dependencies.
|
|
14
17
|
|
|
15
|
-
|
|
16
|
-
ng generate --help
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
## Building
|
|
18
|
+
## Import reflect-metadata once
|
|
20
19
|
|
|
21
|
-
|
|
20
|
+
The decorators read type metadata. Without this import they silently find nothing:
|
|
22
21
|
|
|
23
|
-
```
|
|
24
|
-
|
|
22
|
+
```ts
|
|
23
|
+
// main.ts, before anything else
|
|
24
|
+
import 'reflect-metadata';
|
|
25
25
|
```
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
### Publishing the Library
|
|
30
|
-
|
|
31
|
-
Once the project is built, you can publish your library by following these steps:
|
|
27
|
+
## Writing a model
|
|
32
28
|
|
|
33
|
-
|
|
29
|
+
```ts
|
|
30
|
+
import { Exclude } from 'class-transformer';
|
|
31
|
+
import { BaseModel, ExposeDateOnly, ExposeDateTimeOffset, ExposeModel } from '@quanticdigit/web-model';
|
|
34
32
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
33
|
+
@Exclude()
|
|
34
|
+
export class Observation extends BaseModel {
|
|
35
|
+
@ExposeDateOnly() declare takenOn: Date;
|
|
36
|
+
@ExposeDateTimeOffset() declare createdAt: Date;
|
|
37
|
+
@ExposeModel(() => Species) declare species: Species;
|
|
38
38
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
39
|
+
public override init(): void {
|
|
40
|
+
this.takenOn = new Date();
|
|
41
|
+
this.createdAt = new Date();
|
|
42
|
+
this.species = new Species();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
```
|
|
43
46
|
|
|
44
|
-
|
|
47
|
+
Then a plain object from the wire becomes a typed instance, and back:
|
|
45
48
|
|
|
46
|
-
|
|
49
|
+
```ts
|
|
50
|
+
const model = new Observation({ takenOn: '2026-08-29', species: { code: 'ABI' } });
|
|
47
51
|
|
|
48
|
-
|
|
49
|
-
|
|
52
|
+
model.takenOn; // a Date
|
|
53
|
+
model.species; // a Species instance, not a plain object
|
|
54
|
+
model.toPlainObject(); // { takenOn: '2026-08-29', species: { code: 'ABI' } }
|
|
50
55
|
```
|
|
51
56
|
|
|
52
|
-
##
|
|
57
|
+
## The decorators
|
|
53
58
|
|
|
54
|
-
|
|
59
|
+
| Decorator | Wire format |
|
|
60
|
+
|---|---|
|
|
61
|
+
| `ExposeDateOnly` | `2026-08-29` |
|
|
62
|
+
| `ExposeTimeOnly` | `09:30:15` or `09:30:15.250` |
|
|
63
|
+
| `ExposeDateTimeOnly` | `2026-08-29T14:05:30.123`, no zone |
|
|
64
|
+
| `ExposeDateTimeOffset` | a full instant with zone |
|
|
65
|
+
| `ExposeTimeOffset` | a time of day carrying a zone |
|
|
66
|
+
| `ExposeModel` | a nested model, reconstructed as an instance |
|
|
55
67
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
68
|
+
Round trips are stable across time zones: a date-only value read east of Greenwich is written back as
|
|
69
|
+
the same day.
|
|
70
|
+
|
|
71
|
+
## The family
|
|
59
72
|
|
|
60
|
-
|
|
73
|
+
`web-utils`, `web-model`, `web-services`, `web-component`, `web-errors` and `web-logging` are released
|
|
74
|
+
together and always share the same version. Install them at the same version.
|
|
61
75
|
|
|
62
|
-
##
|
|
76
|
+
## License
|
|
63
77
|
|
|
64
|
-
|
|
78
|
+
Commercial. See `LICENSE.txt` in the package.
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quanticdigit/web-model",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.0.3",
|
|
4
|
+
"description": "Typed model reconstruction from JSON: BaseModel and the expose decorators for dates and nested models.",
|
|
5
5
|
"author": "Quantic Digit S.R.L.",
|
|
6
6
|
"license": "SEE LICENSE IN LICENSE.txt",
|
|
7
7
|
"homepage": "https://www.quanticdigit.it/",
|