@stonyx/orm 0.2.1-alpha.30 → 0.2.1-alpha.31
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 +6 -23
- package/package.json +1 -1
- package/src/record.js +0 -5
package/README.md
CHANGED
|
@@ -17,23 +17,6 @@ A lightweight ORM for Stonyx projects, featuring model definitions, serializers,
|
|
|
17
17
|
- **REST Server Integration**: Automatic route setup with customizable access control.
|
|
18
18
|
- **Lifecycle Hooks**: Middleware-based before/after hooks for validation, authorization, side effects, and auditing.
|
|
19
19
|
|
|
20
|
-
## Public API vs Internals
|
|
21
|
-
|
|
22
|
-
Records use a proxy that exposes model attributes as direct properties. Always use direct property access for reading and writing field values:
|
|
23
|
-
|
|
24
|
-
```js
|
|
25
|
-
// Correct: read/write via the proxy
|
|
26
|
-
const age = record.age;
|
|
27
|
-
record.age = 5;
|
|
28
|
-
|
|
29
|
-
// Correct: iterate fields using the record directly
|
|
30
|
-
for (const key of Object.keys(record.serialize())) {
|
|
31
|
-
console.log(key, record[key]);
|
|
32
|
-
}
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
All properties prefixed with `__` (`__data`, `__relationships`, `__model`, `__serializer`, `__serialized`) are **internal implementation details** and must not be accessed by consumer code. Bypassing the proxy by reading or writing `__data` directly skips type transforms and change tracking, which can lead to silent data corruption.
|
|
36
|
-
|
|
37
20
|
## Installation
|
|
38
21
|
|
|
39
22
|
```bash
|
|
@@ -434,7 +417,7 @@ Each hook receives a context object with comprehensive information:
|
|
|
434
417
|
- It contains a deep copy of the record's state **before** the operation executes (captured before the `before` hook fires)
|
|
435
418
|
- The deep copy is created via JSON serialization (`JSON.parse(JSON.stringify())`) to ensure complete isolation
|
|
436
419
|
- For `delete` operations, `recordId` is provided in after hooks since the record may no longer exist in the store
|
|
437
|
-
- `oldState` is captured
|
|
420
|
+
- `oldState` is captured from `record.__data` or the record itself, providing access to the raw data structure
|
|
438
421
|
|
|
439
422
|
### Usage Examples
|
|
440
423
|
|
|
@@ -537,8 +520,8 @@ afterHook('update', 'animal', async (context) => {
|
|
|
537
520
|
|
|
538
521
|
// Track multiple field changes
|
|
539
522
|
const changedFields = [];
|
|
540
|
-
for (const key
|
|
541
|
-
if (context.oldState[key] !== context.record[key]) {
|
|
523
|
+
for (const key in context.record.__data) {
|
|
524
|
+
if (context.oldState[key] !== context.record.__data[key]) {
|
|
542
525
|
changedFields.push(key);
|
|
543
526
|
}
|
|
544
527
|
}
|
|
@@ -577,9 +560,9 @@ afterHook('update', 'animal', async (context) => {
|
|
|
577
560
|
// Compare oldState with current record to capture exact changes
|
|
578
561
|
const changes = {};
|
|
579
562
|
if (context.oldState) {
|
|
580
|
-
for (const key of Object.
|
|
581
|
-
if (context.oldState[key] !==
|
|
582
|
-
changes[key] = { from: context.oldState[key], to:
|
|
563
|
+
for (const [key, newValue] of Object.entries(context.record.__data || context.record)) {
|
|
564
|
+
if (context.oldState[key] !== newValue) {
|
|
565
|
+
changes[key] = { from: context.oldState[key], to: newValue };
|
|
583
566
|
}
|
|
584
567
|
}
|
|
585
568
|
}
|
package/package.json
CHANGED
package/src/record.js
CHANGED
|
@@ -3,17 +3,12 @@ import { getComputedProperties } from "./serializer.js";
|
|
|
3
3
|
import { camelCaseToKebabCase } from '@stonyx/utils/string';
|
|
4
4
|
import { getPluralName } from './plural-registry.js';
|
|
5
5
|
export default class Record {
|
|
6
|
-
/** @private */
|
|
7
6
|
__data = {};
|
|
8
|
-
/** @private */
|
|
9
7
|
__relationships = {};
|
|
10
|
-
/** @private */
|
|
11
8
|
__serialized = false;
|
|
12
9
|
|
|
13
10
|
constructor(model, serializer) {
|
|
14
|
-
/** @private */
|
|
15
11
|
this.__model = model;
|
|
16
|
-
/** @private */
|
|
17
12
|
this.__serializer = serializer;
|
|
18
13
|
|
|
19
14
|
}
|