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/README.md +164 -0
- package/dist/s3db.cjs.js +44 -2
- package/dist/s3db.cjs.min.js +1 -1
- package/dist/s3db.d.ts +6 -0
- package/dist/s3db.es.js +44 -2
- package/dist/s3db.es.min.js +1 -1
- package/dist/s3db.iife.js +44 -2
- package/dist/s3db.iife.min.js +1 -1
- package/package.json +1 -1
- package/src/database.class.js +2 -1
- package/src/resource.class.js +49 -1
- package/src/s3db.d.ts +6 -0
package/dist/s3db.iife.js
CHANGED
|
@@ -11061,6 +11061,7 @@ ${JSON.stringify(validation, null, 2)}`,
|
|
|
11061
11061
|
* @param {Function} [config.idGenerator] - Custom ID generator function
|
|
11062
11062
|
* @param {number} [config.idSize=22] - Size for auto-generated IDs
|
|
11063
11063
|
* @param {boolean} [config.versioningEnabled=false] - Enable versioning for this resource
|
|
11064
|
+
* @param {Object} [config.events={}] - Event listeners to automatically add
|
|
11064
11065
|
* @example
|
|
11065
11066
|
* const users = new Resource({
|
|
11066
11067
|
* name: 'users',
|
|
@@ -11082,6 +11083,14 @@ ${JSON.stringify(validation, null, 2)}`,
|
|
|
11082
11083
|
* beforeInsert: [async (data) => {
|
|
11083
11084
|
* return data;
|
|
11084
11085
|
* }]
|
|
11086
|
+
* },
|
|
11087
|
+
* events: {
|
|
11088
|
+
* insert: (ev) => console.log('Inserted:', ev.id),
|
|
11089
|
+
* update: [
|
|
11090
|
+
* (ev) => console.warn('Update detected'),
|
|
11091
|
+
* (ev) => console.log('Updated:', ev.id)
|
|
11092
|
+
* ],
|
|
11093
|
+
* delete: (ev) => console.log('Deleted:', ev.id)
|
|
11085
11094
|
* }
|
|
11086
11095
|
* });
|
|
11087
11096
|
*
|
|
@@ -11134,7 +11143,8 @@ ${JSON.stringify(validation, null, 2)}`,
|
|
|
11134
11143
|
hooks = {},
|
|
11135
11144
|
idGenerator: customIdGenerator,
|
|
11136
11145
|
idSize = 22,
|
|
11137
|
-
versioningEnabled = false
|
|
11146
|
+
versioningEnabled = false,
|
|
11147
|
+
events = {}
|
|
11138
11148
|
} = config;
|
|
11139
11149
|
this.name = name;
|
|
11140
11150
|
this.client = client;
|
|
@@ -11176,6 +11186,19 @@ ${JSON.stringify(validation, null, 2)}`,
|
|
|
11176
11186
|
}
|
|
11177
11187
|
}
|
|
11178
11188
|
}
|
|
11189
|
+
if (events && Object.keys(events).length > 0) {
|
|
11190
|
+
for (const [eventName, listeners] of Object.entries(events)) {
|
|
11191
|
+
if (Array.isArray(listeners)) {
|
|
11192
|
+
for (const listener of listeners) {
|
|
11193
|
+
if (typeof listener === "function") {
|
|
11194
|
+
this.on(eventName, listener);
|
|
11195
|
+
}
|
|
11196
|
+
}
|
|
11197
|
+
} else if (typeof listeners === "function") {
|
|
11198
|
+
this.on(eventName, listeners);
|
|
11199
|
+
}
|
|
11200
|
+
}
|
|
11201
|
+
}
|
|
11179
11202
|
this._initMiddleware();
|
|
11180
11203
|
}
|
|
11181
11204
|
/**
|
|
@@ -13215,6 +13238,24 @@ ${JSON.stringify(validation, null, 2)}`,
|
|
|
13215
13238
|
}
|
|
13216
13239
|
}
|
|
13217
13240
|
}
|
|
13241
|
+
if (config.events !== void 0) {
|
|
13242
|
+
if (typeof config.events !== "object" || Array.isArray(config.events)) {
|
|
13243
|
+
errors.push("Resource 'events' must be an object");
|
|
13244
|
+
} else {
|
|
13245
|
+
for (const [eventName, listeners] of Object.entries(config.events)) {
|
|
13246
|
+
if (Array.isArray(listeners)) {
|
|
13247
|
+
for (let i = 0; i < listeners.length; i++) {
|
|
13248
|
+
const listener = listeners[i];
|
|
13249
|
+
if (typeof listener !== "function") {
|
|
13250
|
+
errors.push(`Resource 'events.${eventName}[${i}]' must be a function`);
|
|
13251
|
+
}
|
|
13252
|
+
}
|
|
13253
|
+
} else if (typeof listeners !== "function") {
|
|
13254
|
+
errors.push(`Resource 'events.${eventName}' must be a function or array of functions`);
|
|
13255
|
+
}
|
|
13256
|
+
}
|
|
13257
|
+
}
|
|
13258
|
+
}
|
|
13218
13259
|
return {
|
|
13219
13260
|
isValid: errors.length === 0,
|
|
13220
13261
|
errors
|
|
@@ -13598,7 +13639,8 @@ ${JSON.stringify(validation, null, 2)}`,
|
|
|
13598
13639
|
versioningEnabled: this.versioningEnabled,
|
|
13599
13640
|
map: config.map,
|
|
13600
13641
|
idGenerator: config.idGenerator,
|
|
13601
|
-
idSize: config.idSize
|
|
13642
|
+
idSize: config.idSize,
|
|
13643
|
+
events: config.events || {}
|
|
13602
13644
|
});
|
|
13603
13645
|
resource.database = this;
|
|
13604
13646
|
this.resources[name] = resource;
|