s3db.js 9.2.0 → 9.2.2

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
@@ -1615,7 +1615,35 @@ await users.insert({ name: 'john', email: 'john@example.com' });
1615
1615
 
1616
1616
  ### 🎧 Event Listeners Configuration
1617
1617
 
1618
- s3db.js resources extend Node.js EventEmitter, providing a powerful event system for real-time monitoring and notifications. You can configure event listeners in **two ways**: programmatically using `.on()` or declaratively in the resource configuration.
1618
+ s3db.js resources extend Node.js EventEmitter, providing a powerful event system for real-time monitoring and notifications. **By default, events are emitted asynchronously** for better performance, but you can configure synchronous events when needed.
1619
+
1620
+ #### **Async vs Sync Events**
1621
+
1622
+ ```javascript
1623
+ // Async events (default) - Non-blocking, better performance
1624
+ const asyncResource = await s3db.createResource({
1625
+ name: "users",
1626
+ attributes: { name: "string", email: "string" },
1627
+ asyncEvents: true // Optional, this is the default
1628
+ });
1629
+
1630
+ // Sync events - Blocking, useful for testing or critical operations
1631
+ const syncResource = await s3db.createResource({
1632
+ name: "critical_ops",
1633
+ attributes: { name: "string", value: "number" },
1634
+ asyncEvents: false // Events will block until listeners complete
1635
+ });
1636
+
1637
+ // Runtime mode change
1638
+ asyncResource.setAsyncMode(false); // Switch to sync mode
1639
+ syncResource.setAsyncMode(true); // Switch to async mode
1640
+ ```
1641
+
1642
+ **When to use each mode:**
1643
+ - **Async (default)**: Best for production, logging, analytics, non-critical operations
1644
+ - **Sync**: Testing, critical validations, operations that must complete before continuing
1645
+
1646
+ You can configure event listeners in **two ways**: programmatically using `.on()` or declaratively in the resource configuration.
1619
1647
 
1620
1648
  #### **Programmatic Event Listeners**
1621
1649
  Traditional EventEmitter pattern using `.on()`, `.once()`, or `.off()`:
@@ -1771,7 +1799,8 @@ await users.insert({ name: 'John' });
1771
1799
  - **Declarative for core functionality**: Use the `events` config for essential listeners
1772
1800
  - **Programmatic for conditional/dynamic**: Use `.on()` for listeners that might change at runtime
1773
1801
  - **Error handling**: Listeners should handle their own errors to avoid breaking operations
1774
- - **Performance**: Keep listeners lightweight; use async operations sparingly
1802
+ - **Performance**: Keep listeners lightweight; async events (default) ensure non-blocking operations
1803
+ - **Testing**: Use `asyncEvents: false` in tests when you need predictable synchronous behavior
1775
1804
  - **Debugging**: Event listeners are excellent for debugging and monitoring
1776
1805
  - Middlewares are powerful and ideal for controlling or transforming operations.
1777
1806
  - You can safely combine both for maximum flexibility.