@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 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 as a deep copy of the record's data before the operation, providing access to the previous field values
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 of Object.keys(context.oldState)) {
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.keys(context.oldState)) {
581
- if (context.oldState[key] !== context.record[key]) {
582
- changes[key] = { from: context.oldState[key], to: context.record[key] };
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
@@ -4,7 +4,7 @@
4
4
  "stonyx-async",
5
5
  "stonyx-module"
6
6
  ],
7
- "version": "0.2.1-alpha.30",
7
+ "version": "0.2.1-alpha.31",
8
8
  "description": "",
9
9
  "main": "src/main.js",
10
10
  "type": "module",
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
  }