|
| 1 | +--- |
| 2 | +title: Date Macros |
| 3 | +description: Date Macros protocol schemas |
| 4 | +--- |
| 5 | + |
| 6 | +{/* ⚠️ AUTO-GENERATED — DO NOT EDIT. Run build-docs.ts to regenerate. Hand-written docs go in content/docs/guides/. */} |
| 7 | + |
| 8 | +Date Macro Tokens — the declarative placeholders the UI substitutes |
| 9 | + |
| 10 | +into filter values before sending a query to the data engine. |
| 11 | + |
| 12 | +# Why this lives in `spec` |
| 13 | + |
| 14 | +Filter values in dashboards, views, reports and pages travel as JSON. |
| 15 | + |
| 16 | +Because JSON cannot evaluate code, callers cannot write `daysAgo(30)` |
| 17 | + |
| 18 | +inline; they use a tiny placeholder grammar instead: |
| 19 | + |
| 20 | +\{ published_at: \{ $gte: '\{last_quarter_start\}' \} \} |
| 21 | + |
| 22 | +\{ signal_at: \{ $gte: '\{30_days_ago\}' \} \} |
| 23 | + |
| 24 | +The placeholders are expanded **client-side** by |
| 25 | + |
| 26 | +`resolveDateMacros()` in `@object-ui/core` immediately before the |
| 27 | + |
| 28 | +filter is handed to the data source. The data engine itself only |
| 29 | + |
| 30 | +sees ISO date / timestamp strings, never `\{tokens\}`. |
| 31 | + |
| 32 | +AI agents and template authors author these placeholders directly, |
| 33 | + |
| 34 | +so the **set of recognised tokens is part of the platform contract** |
| 35 | + |
| 36 | +and must live here next to the rest of the JSON-DSL schemas, not |
| 37 | + |
| 38 | +inside any single UI implementation. |
| 39 | + |
| 40 | +# Two flavours of token |
| 41 | + |
| 42 | +1. **Fixed tokens** — small, finite list (`\{today\}`, |
| 43 | + |
| 44 | +`\{current_quarter_start\}`, `\{last_year_end\}`, …). Enumerated by |
| 45 | + |
| 46 | +`DATE_MACRO_TOKENS` below. |
| 47 | + |
| 48 | +2. **Parameterised tokens** — `\{N_days_ago\}`, `\{N_weeks_from_now\}`, |
| 49 | + |
| 50 | +etc., where `N` is any non-negative integer. Matched by |
| 51 | + |
| 52 | +`DATE_MACRO_PARAM_RE`. Units: `minute(s)`, `hour(s)`, `day(s)`, |
| 53 | + |
| 54 | +`week(s)`, `month(s)`, `year(s)`. Directions: `ago`, `from_now`. |
| 55 | + |
| 56 | +# Out of scope |
| 57 | + |
| 58 | +- CEL expressions (`cel\`daysAgo(30)\``) run **server-side** in the |
| 59 | + |
| 60 | +formula engine. They are unrelated to these placeholders; see |
| 61 | + |
| 62 | +`@objectstack/formula`. |
| 63 | + |
| 64 | +- Token resolution semantics (week-start day, timezone, fiscal |
| 65 | + |
| 66 | +calendars) are defined by the resolver implementation; spec only |
| 67 | + |
| 68 | +freezes the **vocabulary**. |
| 69 | + |
| 70 | +<Callout type="info"> |
| 71 | +**Source:** `packages/spec/src/data/date-macros.zod.ts` |
| 72 | +</Callout> |
| 73 | + |
| 74 | +## TypeScript Usage |
| 75 | + |
| 76 | +```typescript |
| 77 | +import { DateMacroPlaceholder, DateMacroToken } from '@objectstack/spec/data'; |
| 78 | +import type { DateMacroPlaceholder, DateMacroToken } from '@objectstack/spec/data'; |
| 79 | + |
| 80 | +// Validate data |
| 81 | +const result = DateMacroPlaceholder.parse(data); |
| 82 | +``` |
| 83 | + |
| 84 | +--- |
| 85 | + |
| 86 | + |
| 87 | +--- |
| 88 | + |
| 89 | + |
| 90 | +--- |
| 91 | + |
0 commit comments