s3db.js 11.0.4 → 11.0.5

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.cjs.js CHANGED
@@ -6518,6 +6518,139 @@ async function getTopRecords(resourceName, field, options, fieldHandlers) {
6518
6518
  });
6519
6519
  return records.slice(0, limit);
6520
6520
  }
6521
+ async function getYearByDay(resourceName, field, year, options, fieldHandlers) {
6522
+ const startDate = `${year}-01-01`;
6523
+ const endDate = `${year}-12-31`;
6524
+ const data = await getAnalytics(resourceName, field, {
6525
+ period: "day",
6526
+ startDate,
6527
+ endDate
6528
+ }, fieldHandlers);
6529
+ if (options.fillGaps) {
6530
+ return fillGaps(data, "day", startDate, endDate);
6531
+ }
6532
+ return data;
6533
+ }
6534
+ async function getWeekByDay(resourceName, field, week, options, fieldHandlers) {
6535
+ const year = parseInt(week.substring(0, 4));
6536
+ const weekNum = parseInt(week.substring(6, 8));
6537
+ const jan4 = new Date(Date.UTC(year, 0, 4));
6538
+ const jan4Day = jan4.getUTCDay() || 7;
6539
+ const firstMonday = new Date(Date.UTC(year, 0, 4 - jan4Day + 1));
6540
+ const weekStart = new Date(firstMonday);
6541
+ weekStart.setUTCDate(weekStart.getUTCDate() + (weekNum - 1) * 7);
6542
+ const days = [];
6543
+ for (let i = 0; i < 7; i++) {
6544
+ const day = new Date(weekStart);
6545
+ day.setUTCDate(weekStart.getUTCDate() + i);
6546
+ days.push(day.toISOString().substring(0, 10));
6547
+ }
6548
+ const startDate = days[0];
6549
+ const endDate = days[6];
6550
+ const data = await getAnalytics(resourceName, field, {
6551
+ period: "day",
6552
+ startDate,
6553
+ endDate
6554
+ }, fieldHandlers);
6555
+ if (options.fillGaps) {
6556
+ return fillGaps(data, "day", startDate, endDate);
6557
+ }
6558
+ return data;
6559
+ }
6560
+ async function getWeekByHour(resourceName, field, week, options, fieldHandlers) {
6561
+ const year = parseInt(week.substring(0, 4));
6562
+ const weekNum = parseInt(week.substring(6, 8));
6563
+ const jan4 = new Date(Date.UTC(year, 0, 4));
6564
+ const jan4Day = jan4.getUTCDay() || 7;
6565
+ const firstMonday = new Date(Date.UTC(year, 0, 4 - jan4Day + 1));
6566
+ const weekStart = new Date(firstMonday);
6567
+ weekStart.setUTCDate(weekStart.getUTCDate() + (weekNum - 1) * 7);
6568
+ const weekEnd = new Date(weekStart);
6569
+ weekEnd.setUTCDate(weekEnd.getUTCDate() + 6);
6570
+ const startDate = weekStart.toISOString().substring(0, 10);
6571
+ const endDate = weekEnd.toISOString().substring(0, 10);
6572
+ const data = await getAnalytics(resourceName, field, {
6573
+ period: "hour",
6574
+ startDate,
6575
+ endDate
6576
+ }, fieldHandlers);
6577
+ if (options.fillGaps) {
6578
+ return fillGaps(data, "hour", startDate, endDate);
6579
+ }
6580
+ return data;
6581
+ }
6582
+ async function getLastNHours(resourceName, field, hours = 24, options, fieldHandlers) {
6583
+ const now = /* @__PURE__ */ new Date();
6584
+ const hoursAgo = new Date(now);
6585
+ hoursAgo.setHours(hoursAgo.getHours() - hours + 1);
6586
+ const startHour = hoursAgo.toISOString().substring(0, 13);
6587
+ const endHour = now.toISOString().substring(0, 13);
6588
+ const data = await getAnalytics(resourceName, field, {
6589
+ period: "hour",
6590
+ startDate: startHour,
6591
+ endDate: endHour
6592
+ }, fieldHandlers);
6593
+ if (options.fillGaps) {
6594
+ const result = [];
6595
+ const emptyRecord = { count: 0, sum: 0, avg: 0, min: 0, max: 0, recordCount: 0 };
6596
+ const dataMap = new Map(data.map((d) => [d.cohort, d]));
6597
+ const current = new Date(hoursAgo);
6598
+ for (let i = 0; i < hours; i++) {
6599
+ const cohort = current.toISOString().substring(0, 13);
6600
+ result.push(dataMap.get(cohort) || { cohort, ...emptyRecord });
6601
+ current.setHours(current.getHours() + 1);
6602
+ }
6603
+ return result;
6604
+ }
6605
+ return data;
6606
+ }
6607
+ async function getLastNWeeks(resourceName, field, weeks = 4, options, fieldHandlers) {
6608
+ const now = /* @__PURE__ */ new Date();
6609
+ const weeksAgo = new Date(now);
6610
+ weeksAgo.setDate(weeksAgo.getDate() - weeks * 7);
6611
+ const weekCohorts = [];
6612
+ const currentDate = new Date(weeksAgo);
6613
+ while (currentDate <= now) {
6614
+ const weekCohort = getCohortWeekFromDate(currentDate);
6615
+ if (!weekCohorts.includes(weekCohort)) {
6616
+ weekCohorts.push(weekCohort);
6617
+ }
6618
+ currentDate.setDate(currentDate.getDate() + 7);
6619
+ }
6620
+ const startWeek = weekCohorts[0];
6621
+ const endWeek = weekCohorts[weekCohorts.length - 1];
6622
+ const data = await getAnalytics(resourceName, field, {
6623
+ period: "week",
6624
+ startDate: startWeek,
6625
+ endDate: endWeek
6626
+ }, fieldHandlers);
6627
+ return data;
6628
+ }
6629
+ async function getLastNMonths(resourceName, field, months = 12, options, fieldHandlers) {
6630
+ const now = /* @__PURE__ */ new Date();
6631
+ const monthsAgo = new Date(now);
6632
+ monthsAgo.setMonth(monthsAgo.getMonth() - months + 1);
6633
+ const startDate = monthsAgo.toISOString().substring(0, 7);
6634
+ const endDate = now.toISOString().substring(0, 7);
6635
+ const data = await getAnalytics(resourceName, field, {
6636
+ period: "month",
6637
+ startDate,
6638
+ endDate
6639
+ }, fieldHandlers);
6640
+ if (options.fillGaps) {
6641
+ const result = [];
6642
+ const emptyRecord = { count: 0, sum: 0, avg: 0, min: 0, max: 0, recordCount: 0 };
6643
+ const dataMap = new Map(data.map((d) => [d.cohort, d]));
6644
+ const current = new Date(monthsAgo);
6645
+ for (let i = 0; i < months; i++) {
6646
+ const cohort = current.toISOString().substring(0, 7);
6647
+ result.push(dataMap.get(cohort) || { cohort, ...emptyRecord });
6648
+ current.setMonth(current.getMonth() + 1);
6649
+ }
6650
+ return result;
6651
+ }
6652
+ return data;
6653
+ }
6521
6654
 
6522
6655
  function addHelperMethods(resource, plugin, config) {
6523
6656
  resource.set = async (id, field, value) => {
@@ -7174,6 +7307,72 @@ class EventualConsistencyPlugin extends Plugin {
7174
7307
  async getTopRecords(resourceName, field, options = {}) {
7175
7308
  return await getTopRecords(resourceName, field, options, this.fieldHandlers);
7176
7309
  }
7310
+ /**
7311
+ * Get analytics for entire year, broken down by days
7312
+ * @param {string} resourceName - Resource name
7313
+ * @param {string} field - Field name
7314
+ * @param {number} year - Year (e.g., 2025)
7315
+ * @param {Object} options - Options
7316
+ * @returns {Promise<Array>} Daily analytics for the year (up to 365/366 records)
7317
+ */
7318
+ async getYearByDay(resourceName, field, year, options = {}) {
7319
+ return await getYearByDay(resourceName, field, year, options, this.fieldHandlers);
7320
+ }
7321
+ /**
7322
+ * Get analytics for entire week, broken down by days
7323
+ * @param {string} resourceName - Resource name
7324
+ * @param {string} field - Field name
7325
+ * @param {string} week - Week in YYYY-Www format (e.g., '2025-W42')
7326
+ * @param {Object} options - Options
7327
+ * @returns {Promise<Array>} Daily analytics for the week (7 records)
7328
+ */
7329
+ async getWeekByDay(resourceName, field, week, options = {}) {
7330
+ return await getWeekByDay(resourceName, field, week, options, this.fieldHandlers);
7331
+ }
7332
+ /**
7333
+ * Get analytics for entire week, broken down by hours
7334
+ * @param {string} resourceName - Resource name
7335
+ * @param {string} field - Field name
7336
+ * @param {string} week - Week in YYYY-Www format (e.g., '2025-W42')
7337
+ * @param {Object} options - Options
7338
+ * @returns {Promise<Array>} Hourly analytics for the week (168 records)
7339
+ */
7340
+ async getWeekByHour(resourceName, field, week, options = {}) {
7341
+ return await getWeekByHour(resourceName, field, week, options, this.fieldHandlers);
7342
+ }
7343
+ /**
7344
+ * Get analytics for last N hours
7345
+ * @param {string} resourceName - Resource name
7346
+ * @param {string} field - Field name
7347
+ * @param {number} hours - Number of hours to look back (default: 24)
7348
+ * @param {Object} options - Options
7349
+ * @returns {Promise<Array>} Hourly analytics
7350
+ */
7351
+ async getLastNHours(resourceName, field, hours = 24, options = {}) {
7352
+ return await getLastNHours(resourceName, field, hours, options, this.fieldHandlers);
7353
+ }
7354
+ /**
7355
+ * Get analytics for last N weeks
7356
+ * @param {string} resourceName - Resource name
7357
+ * @param {string} field - Field name
7358
+ * @param {number} weeks - Number of weeks to look back (default: 4)
7359
+ * @param {Object} options - Options
7360
+ * @returns {Promise<Array>} Weekly analytics
7361
+ */
7362
+ async getLastNWeeks(resourceName, field, weeks = 4, options = {}) {
7363
+ return await getLastNWeeks(resourceName, field, weeks, options, this.fieldHandlers);
7364
+ }
7365
+ /**
7366
+ * Get analytics for last N months
7367
+ * @param {string} resourceName - Resource name
7368
+ * @param {string} field - Field name
7369
+ * @param {number} months - Number of months to look back (default: 12)
7370
+ * @param {Object} options - Options
7371
+ * @returns {Promise<Array>} Monthly analytics
7372
+ */
7373
+ async getLastNMonths(resourceName, field, months = 12, options = {}) {
7374
+ return await getLastNMonths(resourceName, field, months, options, this.fieldHandlers);
7375
+ }
7177
7376
  }
7178
7377
 
7179
7378
  class FullTextPlugin extends Plugin {
@@ -13192,7 +13391,7 @@ class Database extends EventEmitter {
13192
13391
  this.id = idGenerator(7);
13193
13392
  this.version = "1";
13194
13393
  this.s3dbVersion = (() => {
13195
- const [ok, err, version] = tryFn(() => true ? "11.0.4" : "latest");
13394
+ const [ok, err, version] = tryFn(() => true ? "11.0.5" : "latest");
13196
13395
  return ok ? version : "latest";
13197
13396
  })();
13198
13397
  this.resources = {};