@stonyx/orm 0.3.2-beta.287 → 0.3.2-beta.289
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 +20 -8
- package/package.json +3 -3
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
|
|
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
|
|
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
|
-
|
|
749
|
-
|
|
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
|
|
754
|
-
newOwner
|
|
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
|
-
|
|
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
|
-
|
|
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-beta.
|
|
7
|
+
"version": "0.3.2-beta.289",
|
|
8
8
|
"description": "",
|
|
9
9
|
"main": "dist/index.js",
|
|
10
10
|
"type": "module",
|
|
@@ -61,7 +61,7 @@
|
|
|
61
61
|
},
|
|
62
62
|
"homepage": "https://github.com/abofs/stonyx-orm#readme",
|
|
63
63
|
"dependencies": {
|
|
64
|
-
"@stonyx/cron": "0.2.1-beta.
|
|
64
|
+
"@stonyx/cron": "0.2.1-beta.151",
|
|
65
65
|
"@stonyx/events": "0.1.1-beta.68",
|
|
66
66
|
"@stonyx/utils": "0.2.3-beta.28",
|
|
67
67
|
"stonyx": "0.2.3-beta.101"
|
|
@@ -91,7 +91,7 @@
|
|
|
91
91
|
}
|
|
92
92
|
},
|
|
93
93
|
"devDependencies": {
|
|
94
|
-
"@stonyx/rest-server": "0.2.1-beta.
|
|
94
|
+
"@stonyx/rest-server": "0.2.1-beta.152",
|
|
95
95
|
"@types/node": "^25.6.0",
|
|
96
96
|
"mysql2": "^3.20.0",
|
|
97
97
|
"pg": "^8.20.0",
|