@molecule/api-resource-readings 1.0.0 → 1.0.2
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 +345 -0
- package/package.json +14 -7
package/README.md
ADDED
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
AUTO-GENERATED — DO NOT EDIT THIS FILE.
|
|
3
|
+
Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
|
|
4
|
+
Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
|
|
5
|
+
To change this document, edit the module-level JSDoc in src/index.ts.
|
|
6
|
+
Generated: 2026-08-04T01:49:19.514Z
|
|
7
|
+
-->
|
|
8
|
+
|
|
9
|
+
# @molecule/api-resource-readings
|
|
10
|
+
|
|
11
|
+
> **Auto-generated, AI-first package reference** for the [molecule.dev](https://molecule.dev) ecosystem.
|
|
12
|
+
> It is written to be read by coding agents as much as by people, and is generated from this
|
|
13
|
+
> package's source — edit `src/index.ts` JSDoc, not this file.
|
|
14
|
+
|
|
15
|
+
`@molecule/api-resource-readings` — generic time-series sensor data.
|
|
16
|
+
|
|
17
|
+
Ingest readings via `POST /` or `POST /bulk`, then query raw or
|
|
18
|
+
aggregated (5min / hour / day rollups) via `GET /?granularity=…`.
|
|
19
|
+
|
|
20
|
+
Extracted from the energy-monitoring flagship — the pattern works
|
|
21
|
+
for any time-series surface (IoT sensors, app metrics, financial
|
|
22
|
+
tick data, etc.).
|
|
23
|
+
|
|
24
|
+
## Quick Start
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { createReadingsRouter } from '@molecule/api-resource-readings'
|
|
28
|
+
|
|
29
|
+
// Mount behind your global auth middleware — every route requires a session.
|
|
30
|
+
app.use('/readings', createReadingsRouter())
|
|
31
|
+
// POST /readings — ingest one reading
|
|
32
|
+
// POST /readings/bulk — ingest up to 10 000 readings
|
|
33
|
+
// GET /readings?granularity=raw|5min|hour|day&sensor_id=…&metric=…&from=…&to=…
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
import { ingestReading, listAggregatedReadings } from '@molecule/api-resource-readings'
|
|
38
|
+
|
|
39
|
+
await ingestReading(userId, { sensor_id: 'meter-1', metric: 'kwh', value: 1.42 })
|
|
40
|
+
const hourly = await listAggregatedReadings(userId, { granularity: 'hour', metric: 'kwh' })
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Type
|
|
44
|
+
|
|
45
|
+
`resource`
|
|
46
|
+
|
|
47
|
+
## Installation
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
npm install @molecule/api-resource-readings @molecule/api-bonds-default-express @molecule/api-database @molecule/api-i18n @molecule/api-middleware-validation express zod
|
|
51
|
+
npm install -D @types/express
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## API
|
|
55
|
+
|
|
56
|
+
### Interfaces
|
|
57
|
+
|
|
58
|
+
#### `AggregatedPoint`
|
|
59
|
+
|
|
60
|
+
A time-bucketed aggregate of sensor readings (min/max/avg/sum/count per bucket).
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
interface AggregatedPoint {
|
|
64
|
+
bucket_at: string
|
|
65
|
+
metric: string
|
|
66
|
+
sensor_id: string
|
|
67
|
+
min: number
|
|
68
|
+
max: number
|
|
69
|
+
avg: number
|
|
70
|
+
sum: number
|
|
71
|
+
count: number
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
#### `ReadingPoint`
|
|
76
|
+
|
|
77
|
+
A single sensor reading data point returned from a query.
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
interface ReadingPoint {
|
|
81
|
+
recorded_at: string
|
|
82
|
+
value: number
|
|
83
|
+
metric: string
|
|
84
|
+
sensor_id: string
|
|
85
|
+
unit: string | null
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
#### `ReadingRow`
|
|
90
|
+
|
|
91
|
+
Raw database row shape for a single sensor reading record.
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
interface ReadingRow {
|
|
95
|
+
id: string
|
|
96
|
+
owner_id: string
|
|
97
|
+
sensor_id: string
|
|
98
|
+
metric: string
|
|
99
|
+
value: number
|
|
100
|
+
unit: string | null
|
|
101
|
+
recorded_at: string | Date
|
|
102
|
+
metadata: Record<string, unknown> | null
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Types
|
|
107
|
+
|
|
108
|
+
#### `Granularity`
|
|
109
|
+
|
|
110
|
+
Time-bucket granularity options for aggregating sensor readings.
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
type Granularity = 'raw' | '5min' | 'hour' | 'day'
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### Functions
|
|
117
|
+
|
|
118
|
+
#### `countReadings(ownerId)`
|
|
119
|
+
|
|
120
|
+
Return the total number of readings stored for the given owner.
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
function countReadings(ownerId: string): Promise<number>
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
#### `createReadingsRouter()`
|
|
127
|
+
|
|
128
|
+
Creates and returns the Express router for the readings resource endpoints.
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
function createReadingsRouter(): Router
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
#### `ingestBulk(ownerId, readings)`
|
|
135
|
+
|
|
136
|
+
Persist multiple sensor readings in sequence, returning the number successfully inserted.
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
function ingestBulk(
|
|
140
|
+
ownerId: string,
|
|
141
|
+
readings: {
|
|
142
|
+
sensor_id: string
|
|
143
|
+
metric: string
|
|
144
|
+
value: number
|
|
145
|
+
unit?: string | null
|
|
146
|
+
recorded_at?: string
|
|
147
|
+
metadata?: Record<string, unknown>
|
|
148
|
+
}[],
|
|
149
|
+
): Promise<number>
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
#### `ingestReading(ownerId, data)`
|
|
153
|
+
|
|
154
|
+
Persist a single sensor reading row for the given owner.
|
|
155
|
+
|
|
156
|
+
```typescript
|
|
157
|
+
function ingestReading(
|
|
158
|
+
ownerId: string,
|
|
159
|
+
data: {
|
|
160
|
+
sensor_id: string
|
|
161
|
+
metric: string
|
|
162
|
+
value: number
|
|
163
|
+
unit?: string | null
|
|
164
|
+
recorded_at?: string
|
|
165
|
+
metadata?: Record<string, unknown>
|
|
166
|
+
},
|
|
167
|
+
): Promise<ReadingRow>
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
#### `listAggregatedReadings(ownerId, opts)`
|
|
171
|
+
|
|
172
|
+
Group readings into time buckets and aggregate per bucket. Returns
|
|
173
|
+
`min/max/avg/sum/count` per `(bucket, metric, sensor_id)`.
|
|
174
|
+
|
|
175
|
+
```typescript
|
|
176
|
+
function listAggregatedReadings(
|
|
177
|
+
ownerId: string,
|
|
178
|
+
opts: {
|
|
179
|
+
granularity: Exclude<Granularity, 'raw'>
|
|
180
|
+
sensor_id?: string
|
|
181
|
+
metric?: string
|
|
182
|
+
from?: string
|
|
183
|
+
to?: string
|
|
184
|
+
limit?: number
|
|
185
|
+
},
|
|
186
|
+
): Promise<AggregatedPoint[]>
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
#### `listRawReadings(ownerId, opts?)`
|
|
190
|
+
|
|
191
|
+
Fetch raw, unaggregated reading points for an owner, optionally filtered by sensor, metric, and time range.
|
|
192
|
+
|
|
193
|
+
```typescript
|
|
194
|
+
function listRawReadings(
|
|
195
|
+
ownerId: string,
|
|
196
|
+
opts?: { sensor_id?: string; metric?: string; from?: string; to?: string; limit?: number },
|
|
197
|
+
): Promise<ReadingPoint[]>
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### Constants
|
|
201
|
+
|
|
202
|
+
#### `GRANULARITIES`
|
|
203
|
+
|
|
204
|
+
Supported time-bucket granularities for reading aggregation queries.
|
|
205
|
+
|
|
206
|
+
```typescript
|
|
207
|
+
const GRANULARITIES: readonly ['raw', '5min', 'hour', 'day']
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
#### `readingBulkSchema`
|
|
211
|
+
|
|
212
|
+
Zod schema for validating a bulk readings creation payload (1–10 000 entries).
|
|
213
|
+
|
|
214
|
+
```typescript
|
|
215
|
+
const readingBulkSchema: z.ZodObject<
|
|
216
|
+
{
|
|
217
|
+
readings: z.ZodArray<
|
|
218
|
+
z.ZodObject<
|
|
219
|
+
{
|
|
220
|
+
sensor_id: z.ZodString
|
|
221
|
+
metric: z.ZodString
|
|
222
|
+
value: z.ZodNumber
|
|
223
|
+
unit: z.ZodOptional<z.ZodNullable<z.ZodString>>
|
|
224
|
+
recorded_at: z.ZodOptional<z.ZodString>
|
|
225
|
+
metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>
|
|
226
|
+
},
|
|
227
|
+
z.core.$strip
|
|
228
|
+
>
|
|
229
|
+
>
|
|
230
|
+
},
|
|
231
|
+
z.core.$strip
|
|
232
|
+
>
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
#### `readingCreateSchema`
|
|
236
|
+
|
|
237
|
+
Zod schema for validating a single reading creation payload.
|
|
238
|
+
|
|
239
|
+
```typescript
|
|
240
|
+
const readingCreateSchema: z.ZodObject<
|
|
241
|
+
{
|
|
242
|
+
sensor_id: z.ZodString
|
|
243
|
+
metric: z.ZodString
|
|
244
|
+
value: z.ZodNumber
|
|
245
|
+
unit: z.ZodOptional<z.ZodNullable<z.ZodString>>
|
|
246
|
+
recorded_at: z.ZodOptional<z.ZodString>
|
|
247
|
+
metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>
|
|
248
|
+
},
|
|
249
|
+
z.core.$strip
|
|
250
|
+
>
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
#### `readingQuerySchema`
|
|
254
|
+
|
|
255
|
+
Zod schema for validating reading list/query filter parameters.
|
|
256
|
+
|
|
257
|
+
```typescript
|
|
258
|
+
const readingQuerySchema: z.ZodObject<
|
|
259
|
+
{
|
|
260
|
+
sensor_id: z.ZodOptional<z.ZodString>
|
|
261
|
+
metric: z.ZodOptional<z.ZodString>
|
|
262
|
+
from: z.ZodOptional<z.ZodString>
|
|
263
|
+
to: z.ZodOptional<z.ZodString>
|
|
264
|
+
granularity: z.ZodOptional<z.ZodEnum<{ raw: 'raw'; '5min': '5min'; hour: 'hour'; day: 'day' }>>
|
|
265
|
+
limit: z.ZodOptional<z.ZodCoercedNumber<unknown>>
|
|
266
|
+
},
|
|
267
|
+
z.core.$strip
|
|
268
|
+
>
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
## Injection Notes
|
|
272
|
+
|
|
273
|
+
### Requirements
|
|
274
|
+
|
|
275
|
+
Peer dependencies:
|
|
276
|
+
|
|
277
|
+
- `@molecule/api-bonds-default-express` ^1.0.1
|
|
278
|
+
- `@molecule/api-database` ^1.0.1
|
|
279
|
+
- `@molecule/api-i18n` ^1.0.1
|
|
280
|
+
- `@molecule/api-middleware-validation` ^1.0.1
|
|
281
|
+
- `express` ^5.0.0
|
|
282
|
+
- `zod` ^4.0.0
|
|
283
|
+
|
|
284
|
+
### Runtime Dependencies
|
|
285
|
+
|
|
286
|
+
- `@molecule/api-bonds-default-express`
|
|
287
|
+
- `@molecule/api-database`
|
|
288
|
+
- `@molecule/api-i18n`
|
|
289
|
+
- `@molecule/api-middleware-validation`
|
|
290
|
+
- `express`
|
|
291
|
+
- `zod`
|
|
292
|
+
|
|
293
|
+
Unlike declarative-route resources, this package ships an Express Router
|
|
294
|
+
FACTORY (`createReadingsRouter()`) — there is no `routes` /
|
|
295
|
+
`requestHandlerMap` export for `mlcl inject`; mount the router yourself.
|
|
296
|
+
Every route reads the caller via `requireUser(res)`
|
|
297
|
+
(`res.locals.session.userId`, 401 fail-closed), so it must sit behind the
|
|
298
|
+
global auth middleware, and every query/insert is scoped to that owner —
|
|
299
|
+
never accept a client-supplied owner id.
|
|
300
|
+
|
|
301
|
+
Aggregated queries (`granularity` ≠ `raw`) run raw SQL using `date_trunc`,
|
|
302
|
+
`::int` casts, and `interval` literals — **PostgreSQL-only**. On the
|
|
303
|
+
SQLite/MySQL bonds use `granularity=raw` (DataStore-based, portable) and
|
|
304
|
+
bucket in application code, or supply your own dialect's aggregation.
|
|
305
|
+
`ingestBulk` inserts sequentially (one INSERT per reading, max 10 000 per
|
|
306
|
+
request).
|
|
307
|
+
|
|
308
|
+
Tables: `src/__setup__/readings.sql` creates `readings` (owner-scoped via
|
|
309
|
+
`owner_id`). An mlcl-scaffolded API replays `__setup__/*.sql` automatically
|
|
310
|
+
on migrate; anywhere else run it once — nothing at runtime creates them.
|
|
311
|
+
|
|
312
|
+
## E2E Tests
|
|
313
|
+
|
|
314
|
+
Integration checklist — drive the real UI (live preview, no mocks), adapt
|
|
315
|
+
each item to this app's actual screens/flows, and check every box off one
|
|
316
|
+
by one. A box you can't check is a correctness bug to fix — not a skip:
|
|
317
|
+
|
|
318
|
+
- [ ] Recording a reading (POST /) persists its value, unit, metric, and
|
|
319
|
+
recorded_at against the right sensor_id, and the chart/list reflects it:
|
|
320
|
+
the newest point is the LAST entry of the chronological raw query (readings
|
|
321
|
+
come back ordered by recorded_at ascending), so it shows the true latest
|
|
322
|
+
value for that sensor — not a stale or averaged one.
|
|
323
|
+
- [ ] A time-range query (from/to) returns ONLY readings inside the window,
|
|
324
|
+
in chronological order, with no gaps or duplicates — points outside the
|
|
325
|
+
range are absent and a point exactly on a boundary is present.
|
|
326
|
+
- [ ] An aggregated query (granularity=5min|hour|day) computes the correct
|
|
327
|
+
min/max/avg/sum/count for the readings in each bucket — spot-check one
|
|
328
|
+
bucket by hand against its raw points (avg = sum / count; min and max are
|
|
329
|
+
real members of the bucket; count matches the number of raw points).
|
|
330
|
+
Aggregation is Postgres-only; on SQLite/MySQL use granularity=raw and
|
|
331
|
+
verify the app buckets client-side instead.
|
|
332
|
+
- [ ] Every value renders as a real number with its unit (e.g. "1.42 kwh"),
|
|
333
|
+
never NaN, null, or undefined; a gap in the readings shows as a gap (or an
|
|
334
|
+
explicit "no data"), never as a fabricated 0.
|
|
335
|
+
- [ ] Malformed readings are rejected, not stored as truth: a non-numeric or
|
|
336
|
+
NaN value, or a missing sensor_id/metric, is refused by validation (400) and
|
|
337
|
+
never ingested; if the domain has a sane range, an out-of-range value is
|
|
338
|
+
flagged/rejected by the app, not silently recorded. If the readings are
|
|
339
|
+
health or otherwise personal, missing data is never shown as zero.
|
|
340
|
+
- [ ] AUTHORIZATION — readings are scoped to owner_id (the authenticated
|
|
341
|
+
session user via requireUser): a user sees ONLY their own readings, and no
|
|
342
|
+
sensor_id/from/to filter or guessed id surfaces another user's data.
|
|
343
|
+
Ingestion is authenticated (401 fail-closed) and the owner is taken from the
|
|
344
|
+
session, never the request body — a caller can't spoof readings onto another
|
|
345
|
+
owner or device.
|
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@molecule/api-resource-readings",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Generic time-series sensor readings with on-the-fly aggregation (raw/5min/hour/day rollups)",
|
|
5
|
+
"homepage": "https://www.molecule.dev/packages/api-resource-readings",
|
|
5
6
|
"type": "module",
|
|
6
7
|
"main": "dist/index.js",
|
|
7
8
|
"types": "dist/index.d.ts",
|
|
@@ -17,7 +18,8 @@
|
|
|
17
18
|
}
|
|
18
19
|
},
|
|
19
20
|
"files": [
|
|
20
|
-
"dist"
|
|
21
|
+
"dist",
|
|
22
|
+
"README.md"
|
|
21
23
|
],
|
|
22
24
|
"keywords": [
|
|
23
25
|
"molecule",
|
|
@@ -28,19 +30,24 @@
|
|
|
28
30
|
"resource"
|
|
29
31
|
],
|
|
30
32
|
"license": "Apache-2.0",
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "https://github.com/molecule-dev/molecule.git",
|
|
36
|
+
"directory": "packages/api/resources/readings"
|
|
37
|
+
},
|
|
31
38
|
"devDependencies": {
|
|
32
39
|
"@types/express": "5.0.6",
|
|
33
40
|
"@types/node": "26.1.2",
|
|
34
41
|
"express": "5.2.1",
|
|
35
42
|
"typescript": "6.0.3",
|
|
36
|
-
"vitest": "4.1.
|
|
43
|
+
"vitest": "4.1.11",
|
|
37
44
|
"zod": "4.4.3"
|
|
38
45
|
},
|
|
39
46
|
"peerDependencies": {
|
|
40
|
-
"@molecule/api-bonds-default-express": "^1.0.
|
|
41
|
-
"@molecule/api-database": "^1.0.
|
|
42
|
-
"@molecule/api-i18n": "^1.0.
|
|
43
|
-
"@molecule/api-middleware-validation": "^1.0.
|
|
47
|
+
"@molecule/api-bonds-default-express": "^1.0.1",
|
|
48
|
+
"@molecule/api-database": "^1.0.1",
|
|
49
|
+
"@molecule/api-i18n": "^1.0.1",
|
|
50
|
+
"@molecule/api-middleware-validation": "^1.0.1",
|
|
44
51
|
"express": "^5.0.0",
|
|
45
52
|
"zod": "^4.0.0"
|
|
46
53
|
}
|