s3db.js 8.1.1 → 8.2.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.es.js CHANGED
@@ -1363,17 +1363,13 @@ class AuditPlugin extends plugin_class_default {
1363
1363
  const plugin = this;
1364
1364
  resource.deleteMany = async function(ids) {
1365
1365
  const objectsToDelete = [];
1366
- if (plugin.config.includeData) {
1367
- for (const id of ids) {
1368
- const [ok, err, fetched] = await try_fn_default(() => resource.get(id));
1369
- if (ok) {
1370
- objectsToDelete.push(fetched);
1371
- } else {
1372
- objectsToDelete.push({ id });
1373
- }
1366
+ for (const id of ids) {
1367
+ const [ok, err, fetched] = await try_fn_default(() => resource.get(id));
1368
+ if (ok) {
1369
+ objectsToDelete.push(fetched);
1370
+ } else {
1371
+ objectsToDelete.push({ id });
1374
1372
  }
1375
- } else {
1376
- objectsToDelete.push(...ids.map((id) => ({ id })));
1377
1373
  }
1378
1374
  const result = await originalDeleteMany(ids);
1379
1375
  for (const oldData of objectsToDelete) {
@@ -1478,18 +1474,37 @@ class AuditPlugin extends plugin_class_default {
1478
1474
  async getAuditLogs(options = {}) {
1479
1475
  if (!this.auditResource) return [];
1480
1476
  const { resourceName, operation, recordId, partition, startDate, endDate, limit = 100, offset = 0 } = options;
1481
- let query = {};
1482
- if (resourceName) query.resourceName = resourceName;
1483
- if (operation) query.operation = operation;
1484
- if (recordId) query.recordId = recordId;
1485
- if (partition) query.partition = partition;
1486
- if (startDate || endDate) {
1487
- query.timestamp = {};
1488
- if (startDate) query.timestamp.$gte = startDate;
1489
- if (endDate) query.timestamp.$lte = endDate;
1490
- }
1491
- const result = await this.auditResource.page({ query, limit, offset });
1492
- return result.items || [];
1477
+ const hasFilters = resourceName || operation || recordId || partition || startDate || endDate;
1478
+ let items = [];
1479
+ if (hasFilters) {
1480
+ const fetchSize = Math.min(1e4, Math.max(1e3, (limit + offset) * 20));
1481
+ const result = await this.auditResource.list({ limit: fetchSize });
1482
+ items = result || [];
1483
+ if (resourceName) {
1484
+ items = items.filter((log) => log.resourceName === resourceName);
1485
+ }
1486
+ if (operation) {
1487
+ items = items.filter((log) => log.operation === operation);
1488
+ }
1489
+ if (recordId) {
1490
+ items = items.filter((log) => log.recordId === recordId);
1491
+ }
1492
+ if (partition) {
1493
+ items = items.filter((log) => log.partition === partition);
1494
+ }
1495
+ if (startDate || endDate) {
1496
+ items = items.filter((log) => {
1497
+ const timestamp = new Date(log.timestamp);
1498
+ if (startDate && timestamp < new Date(startDate)) return false;
1499
+ if (endDate && timestamp > new Date(endDate)) return false;
1500
+ return true;
1501
+ });
1502
+ }
1503
+ return items.slice(offset, offset + limit);
1504
+ } else {
1505
+ const result = await this.auditResource.page({ size: limit, offset });
1506
+ return result.items || [];
1507
+ }
1493
1508
  }
1494
1509
  async getRecordHistory(resourceName, recordId) {
1495
1510
  return await this.getAuditLogs({ resourceName, recordId });
@@ -13503,7 +13518,7 @@ class Database extends EventEmitter {
13503
13518
  this.id = idGenerator(7);
13504
13519
  this.version = "1";
13505
13520
  this.s3dbVersion = (() => {
13506
- const [ok, err, version] = try_fn_default(() => true ? "8.1.1" : "latest");
13521
+ const [ok, err, version] = try_fn_default(() => true ? "8.2.0" : "latest");
13507
13522
  return ok ? version : "latest";
13508
13523
  })();
13509
13524
  this.resources = {};
@@ -13512,6 +13527,7 @@ class Database extends EventEmitter {
13512
13527
  this.verbose = options.verbose || false;
13513
13528
  this.parallelism = parseInt(options.parallelism + "") || 10;
13514
13529
  this.plugins = options.plugins || [];
13530
+ this.pluginRegistry = {};
13515
13531
  this.pluginList = options.plugins || [];
13516
13532
  this.cache = options.cache;
13517
13533
  this.passphrase = options.passphrase || "secret";
@@ -13804,6 +13820,8 @@ class Database extends EventEmitter {
13804
13820
  if (plugin.beforeSetup) await plugin.beforeSetup();
13805
13821
  await plugin.setup(db);
13806
13822
  if (plugin.afterSetup) await plugin.afterSetup();
13823
+ const pluginName = this._getPluginName(plugin);
13824
+ this.pluginRegistry[pluginName] = plugin;
13807
13825
  });
13808
13826
  await Promise.all(setupProms);
13809
13827
  const startProms = plugins.map(async (plugin) => {
@@ -13819,8 +13837,15 @@ class Database extends EventEmitter {
13819
13837
  * @param {Plugin} plugin - Plugin instance to register
13820
13838
  * @param {string} [name] - Optional name for the plugin (defaults to plugin.constructor.name)
13821
13839
  */
13840
+ /**
13841
+ * Get the normalized plugin name
13842
+ * @private
13843
+ */
13844
+ _getPluginName(plugin, customName = null) {
13845
+ return customName || plugin.constructor.name.replace("Plugin", "").toLowerCase();
13846
+ }
13822
13847
  async usePlugin(plugin, name = null) {
13823
- const pluginName = name || plugin.constructor.name.replace("Plugin", "").toLowerCase();
13848
+ const pluginName = this._getPluginName(plugin, name);
13824
13849
  this.plugins[pluginName] = plugin;
13825
13850
  if (this.isConnected()) {
13826
13851
  await plugin.setup(this);