@stonyx/orm 0.3.2-alpha.115 → 0.3.2-alpha.117

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.
Files changed (2) hide show
  1. package/README.md +20 -8
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -670,7 +670,7 @@ Each hook receives a context object with comprehensive information:
670
670
  - It contains a deep copy of the record's state **before** the operation executes (captured before the `before` hook fires)
671
671
  - The deep copy is created via JSON serialization (`JSON.parse(JSON.stringify())`) to ensure complete isolation
672
672
  - For `delete` operations, `recordId` is provided in after hooks since the record may no longer exist in the store
673
- - `oldState` is captured as a deep copy of the record's data before the operation, providing access to the previous field values
673
+ - `oldState` is captured as a deep copy of the record's data before the operation, providing access to the previous attribute values. Relationships are not in it: to detect a relationship change, capture the related id in a `before` hook (see Side Effects below)
674
674
  - `params.id` is the raw client text as a STRING (`/animals/05` gives `{ id: '05' }`). The id the ORM resolved the record by is `request.recordId`, and for a numeric-id model it is a NUMBER (`5` for both `/animals/5` and `/animals/05`) — abofs/stonyx-orm#270. Key lookups on `request.recordId`, never on `params.id`.
675
675
 
676
676
  ### Usage Examples
@@ -743,15 +743,23 @@ beforeHook('create', 'owner', (context) => {
743
743
  #### Side Effects
744
744
 
745
745
  ```javascript
746
- // Send notification after animal is adopted (using oldState to detect changes)
746
+ // Send notification after animal is adopted.
747
+ // `oldState` holds attributes only, and a belongsTo is the related Record, not
748
+ // its id: capture the previous owner's id before the update, compare ids after.
749
+ beforeHook('update', 'animal', (context) => {
750
+ context.state.previousOwnerId = store.get('animal', context.request.recordId)?.owner?.id;
751
+ });
752
+
747
753
  afterHook('update', 'animal', async (context) => {
748
- // Use oldState to compare before/after values
749
- if (context.oldState && context.oldState.owner !== context.record.owner) {
754
+ const previousOwner = context.state.previousOwnerId;
755
+ const newOwner = context.record?.owner?.id;
756
+
757
+ if (previousOwner !== newOwner) {
750
758
  await sendNotification({
751
759
  type: 'adoption',
752
760
  animalId: context.record.id,
753
- previousOwner: context.oldState.owner,
754
- newOwner: context.record.owner
761
+ previousOwner,
762
+ newOwner
755
763
  });
756
764
  }
757
765
  });
@@ -813,7 +821,10 @@ beforeHook('delete', 'animal', (context) => {
813
821
  const user = context.state.currentUser;
814
822
  const animal = store.get('animal', context.request.recordId);
815
823
 
816
- if (animal.owner !== user.id && !user.isAdmin) {
824
+ // `animal.owner` is the related Record, so compare its id. Refuse an unowned
825
+ // animal outright: `animal.owner?.id !== user.id` lets a user with no id
826
+ // delete it, because `undefined !== undefined` is false
827
+ if (!user.isAdmin && (!animal.owner || animal.owner.id !== user.id)) {
817
828
  return 403; // Forbidden
818
829
  }
819
830
  });
@@ -925,7 +936,8 @@ beforeHook('update', 'animal', (context) => {
925
936
  ```javascript
926
937
  // Update owner's pet count when animal is created
927
938
  afterHook('create', 'animal', async (context) => {
928
- const owner = store.get('owner', context.record.owner);
939
+ // A belongsTo is already the related Record; no store lookup needed
940
+ const { owner } = context.record;
929
941
  if (owner) {
930
942
  owner.petCount = (owner.petCount || 0) + 1;
931
943
  }
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "stonyx-async",
5
5
  "stonyx-module"
6
6
  ],
7
- "version": "0.3.2-alpha.115",
7
+ "version": "0.3.2-alpha.117",
8
8
  "description": "",
9
9
  "main": "dist/index.js",
10
10
  "type": "module",