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/dist/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;
package/dist/s3db.es.js CHANGED
@@ -11070,6 +11070,7 @@ class Resource extends EventEmitter {
11070
11070
  * @param {Function} [config.idGenerator] - Custom ID generator function
11071
11071
  * @param {number} [config.idSize=22] - Size for auto-generated IDs
11072
11072
  * @param {boolean} [config.versioningEnabled=false] - Enable versioning for this resource
11073
+ * @param {Object} [config.events={}] - Event listeners to automatically add
11073
11074
  * @example
11074
11075
  * const users = new Resource({
11075
11076
  * name: 'users',
@@ -11091,6 +11092,14 @@ class Resource extends EventEmitter {
11091
11092
  * beforeInsert: [async (data) => {
11092
11093
  * return data;
11093
11094
  * }]
11095
+ * },
11096
+ * events: {
11097
+ * insert: (ev) => console.log('Inserted:', ev.id),
11098
+ * update: [
11099
+ * (ev) => console.warn('Update detected'),
11100
+ * (ev) => console.log('Updated:', ev.id)
11101
+ * ],
11102
+ * delete: (ev) => console.log('Deleted:', ev.id)
11094
11103
  * }
11095
11104
  * });
11096
11105
  *
@@ -11143,7 +11152,8 @@ class Resource extends EventEmitter {
11143
11152
  hooks = {},
11144
11153
  idGenerator: customIdGenerator,
11145
11154
  idSize = 22,
11146
- versioningEnabled = false
11155
+ versioningEnabled = false,
11156
+ events = {}
11147
11157
  } = config;
11148
11158
  this.name = name;
11149
11159
  this.client = client;
@@ -11185,6 +11195,19 @@ class Resource extends EventEmitter {
11185
11195
  }
11186
11196
  }
11187
11197
  }
11198
+ if (events && Object.keys(events).length > 0) {
11199
+ for (const [eventName, listeners] of Object.entries(events)) {
11200
+ if (Array.isArray(listeners)) {
11201
+ for (const listener of listeners) {
11202
+ if (typeof listener === "function") {
11203
+ this.on(eventName, listener);
11204
+ }
11205
+ }
11206
+ } else if (typeof listeners === "function") {
11207
+ this.on(eventName, listeners);
11208
+ }
11209
+ }
11210
+ }
11188
11211
  this._initMiddleware();
11189
11212
  }
11190
11213
  /**
@@ -13224,6 +13247,24 @@ function validateResourceConfig(config) {
13224
13247
  }
13225
13248
  }
13226
13249
  }
13250
+ if (config.events !== void 0) {
13251
+ if (typeof config.events !== "object" || Array.isArray(config.events)) {
13252
+ errors.push("Resource 'events' must be an object");
13253
+ } else {
13254
+ for (const [eventName, listeners] of Object.entries(config.events)) {
13255
+ if (Array.isArray(listeners)) {
13256
+ for (let i = 0; i < listeners.length; i++) {
13257
+ const listener = listeners[i];
13258
+ if (typeof listener !== "function") {
13259
+ errors.push(`Resource 'events.${eventName}[${i}]' must be a function`);
13260
+ }
13261
+ }
13262
+ } else if (typeof listeners !== "function") {
13263
+ errors.push(`Resource 'events.${eventName}' must be a function or array of functions`);
13264
+ }
13265
+ }
13266
+ }
13267
+ }
13227
13268
  return {
13228
13269
  isValid: errors.length === 0,
13229
13270
  errors
@@ -13607,7 +13648,8 @@ class Database extends EventEmitter {
13607
13648
  versioningEnabled: this.versioningEnabled,
13608
13649
  map: config.map,
13609
13650
  idGenerator: config.idGenerator,
13610
- idSize: config.idSize
13651
+ idSize: config.idSize,
13652
+ events: config.events || {}
13611
13653
  });
13612
13654
  resource.database = this;
13613
13655
  this.resources[name] = resource;