@stonyx/orm 0.2.1-beta.77 → 0.2.1-beta.78

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
@@ -17,6 +17,23 @@ 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
+
20
37
  ## Installation
21
38
 
22
39
  ```bash
@@ -417,7 +434,7 @@ Each hook receives a context object with comprehensive information:
417
434
  - It contains a deep copy of the record's state **before** the operation executes (captured before the `before` hook fires)
418
435
  - The deep copy is created via JSON serialization (`JSON.parse(JSON.stringify())`) to ensure complete isolation
419
436
  - For `delete` operations, `recordId` is provided in after hooks since the record may no longer exist in the store
420
- - `oldState` is captured from `record.__data` or the record itself, providing access to the raw data structure
437
+ - `oldState` is captured as a deep copy of the record's data before the operation, providing access to the previous field values
421
438
 
422
439
  ### Usage Examples
423
440
 
@@ -520,8 +537,8 @@ afterHook('update', 'animal', async (context) => {
520
537
 
521
538
  // Track multiple field changes
522
539
  const changedFields = [];
523
- for (const key in context.record.__data) {
524
- if (context.oldState[key] !== context.record.__data[key]) {
540
+ for (const key of Object.keys(context.oldState)) {
541
+ if (context.oldState[key] !== context.record[key]) {
525
542
  changedFields.push(key);
526
543
  }
527
544
  }
@@ -560,9 +577,9 @@ afterHook('update', 'animal', async (context) => {
560
577
  // Compare oldState with current record to capture exact changes
561
578
  const changes = {};
562
579
  if (context.oldState) {
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 };
580
+ for (const key of Object.keys(context.oldState)) {
581
+ if (context.oldState[key] !== context.record[key]) {
582
+ changes[key] = { from: context.oldState[key], to: context.record[key] };
566
583
  }
567
584
  }
568
585
  }
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "stonyx-async",
5
5
  "stonyx-module"
6
6
  ],
7
- "version": "0.2.1-beta.77",
7
+ "version": "0.2.1-beta.78",
8
8
  "description": "",
9
9
  "main": "src/main.js",
10
10
  "type": "module",
package/src/record.js CHANGED
@@ -3,12 +3,17 @@ 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 */
6
7
  __data = {};
8
+ /** @private */
7
9
  __relationships = {};
10
+ /** @private */
8
11
  __serialized = false;
9
12
 
10
13
  constructor(model, serializer) {
14
+ /** @private */
11
15
  this.__model = model;
16
+ /** @private */
12
17
  this.__serializer = serializer;
13
18
 
14
19
  }