s3db.js 7.3.10 → 7.4.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "s3db.js",
3
- "version": "7.3.10",
3
+ "version": "7.4.0",
4
4
  "description": "Use AWS S3, the world's most reliable document storage, as a database with this ORM.",
5
5
  "main": "dist/s3db.cjs.js",
6
6
  "module": "dist/s3db.es.js",
@@ -471,7 +471,8 @@ export class Database extends EventEmitter {
471
471
  versioningEnabled: this.versioningEnabled,
472
472
  map: config.map,
473
473
  idGenerator: config.idGenerator,
474
- idSize: config.idSize
474
+ idSize: config.idSize,
475
+ events: config.events || {}
475
476
  });
476
477
  resource.database = this;
477
478
  this.resources[name] = resource;
@@ -40,6 +40,7 @@ export class Resource extends EventEmitter {
40
40
  * @param {Function} [config.idGenerator] - Custom ID generator function
41
41
  * @param {number} [config.idSize=22] - Size for auto-generated IDs
42
42
  * @param {boolean} [config.versioningEnabled=false] - Enable versioning for this resource
43
+ * @param {Object} [config.events={}] - Event listeners to automatically add
43
44
  * @example
44
45
  * const users = new Resource({
45
46
  * name: 'users',
@@ -61,6 +62,14 @@ export class Resource extends EventEmitter {
61
62
  * beforeInsert: [async (data) => {
62
63
  * return data;
63
64
  * }]
65
+ * },
66
+ * events: {
67
+ * insert: (ev) => console.log('Inserted:', ev.id),
68
+ * update: [
69
+ * (ev) => console.warn('Update detected'),
70
+ * (ev) => console.log('Updated:', ev.id)
71
+ * ],
72
+ * delete: (ev) => console.log('Deleted:', ev.id)
64
73
  * }
65
74
  * });
66
75
  *
@@ -117,7 +126,8 @@ export class Resource extends EventEmitter {
117
126
  hooks = {},
118
127
  idGenerator: customIdGenerator,
119
128
  idSize = 22,
120
- versioningEnabled = false
129
+ versioningEnabled = false,
130
+ events = {}
121
131
  } = config;
122
132
 
123
133
  // Set instance properties
@@ -177,6 +187,23 @@ export class Resource extends EventEmitter {
177
187
  }
178
188
  }
179
189
 
190
+ // Setup event listeners
191
+ if (events && Object.keys(events).length > 0) {
192
+ for (const [eventName, listeners] of Object.entries(events)) {
193
+ if (Array.isArray(listeners)) {
194
+ // Multiple listeners for this event
195
+ for (const listener of listeners) {
196
+ if (typeof listener === 'function') {
197
+ this.on(eventName, listener);
198
+ }
199
+ }
200
+ } else if (typeof listeners === 'function') {
201
+ // Single listener for this event
202
+ this.on(eventName, listeners);
203
+ }
204
+ }
205
+ }
206
+
180
207
  // --- MIDDLEWARE SYSTEM ---
181
208
  this._initMiddleware();
182
209
  // Debug: print method names and typeof update at construction
@@ -2617,6 +2644,27 @@ function validateResourceConfig(config) {
2617
2644
  }
2618
2645
  }
2619
2646
 
2647
+ // Validate events
2648
+ if (config.events !== undefined) {
2649
+ if (typeof config.events !== 'object' || Array.isArray(config.events)) {
2650
+ errors.push("Resource 'events' must be an object");
2651
+ } else {
2652
+ for (const [eventName, listeners] of Object.entries(config.events)) {
2653
+ if (Array.isArray(listeners)) {
2654
+ // Multiple listeners for this event
2655
+ for (let i = 0; i < listeners.length; i++) {
2656
+ const listener = listeners[i];
2657
+ if (typeof listener !== 'function') {
2658
+ errors.push(`Resource 'events.${eventName}[${i}]' must be a function`);
2659
+ }
2660
+ }
2661
+ } else if (typeof listeners !== 'function') {
2662
+ errors.push(`Resource 'events.${eventName}' must be a function or array of functions`);
2663
+ }
2664
+ }
2665
+ }
2666
+ }
2667
+
2620
2668
  return {
2621
2669
  isValid: errors.length === 0,
2622
2670
  errors
package/src/s3db.d.ts CHANGED
@@ -47,6 +47,7 @@ declare module 's3db.js' {
47
47
  idSize?: number;
48
48
  versioningEnabled?: boolean;
49
49
  map?: any;
50
+ events?: EventListenerConfig;
50
51
  }
51
52
 
52
53
  /** Partition configuration */
@@ -65,6 +66,11 @@ declare module 's3db.js' {
65
66
  afterDelete?: Function[];
66
67
  }
67
68
 
69
+ /** Event listener configuration */
70
+ export interface EventListenerConfig {
71
+ [eventName: string]: Function | Function[];
72
+ }
73
+
68
74
  /** Query options */
69
75
  export interface QueryOptions {
70
76
  limit?: number;