@harperfast/skills 1.6.1 → 1.7.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.
@@ -143,7 +143,7 @@ Apply this rule when adding a vector index to a Harper table schema or writing s
143
143
 
144
144
  #### How It Works
145
145
 
146
- 1. **Declare a vector index on a `[Float]` field**: Add `@indexed(type: "HNSW")` to any `[Float]` attribute in a `@table` type. See [adding-tables-with-schemas.md](adding-tables-with-schemas.md) for general schema setup.
146
+ 1. **Declare the vector index on a `[Float]` field**: Add `@indexed(type: "HNSW")` to any `[Float]` attribute in a `@table` type. See [adding-tables-with-schemas.md](adding-tables-with-schemas.md) for general schema setup.
147
147
 
148
148
  ```graphql
149
149
  type Document @table {
@@ -152,7 +152,7 @@ Apply this rule when adding a vector index to a Harper table schema or writing s
152
152
  }
153
153
  ```
154
154
 
155
- 2. **Query by nearest neighbors using `sort`**: Call `Document.search()` with a `sort` object specifying `attribute` (the indexed field) and `target` (the query vector). Include `limit` to cap results.
155
+ 2. **Query by nearest neighbors using `sort`**: Call `Document.search()` with a `sort` object containing `attribute` (the indexed field name) and `target` (the query vector). Include `limit` to cap results.
156
156
 
157
157
  ```javascript
158
158
  let results = Document.search({
@@ -161,7 +161,7 @@ Apply this rule when adding a vector index to a Harper table schema or writing s
161
161
  });
162
162
  ```
163
163
 
164
- 3. **Combine HNSW with filter conditions**: Add a `conditions` array alongside `sort` to pre-filter records before ranking by similarity.
164
+ 3. **Combine with filter conditions**: Add a `conditions` array alongside `sort` to pre-filter records before ranking by similarity.
165
165
 
166
166
  ```javascript
167
167
  let results = Document.search({
@@ -171,7 +171,7 @@ Apply this rule when adding a vector index to a Harper table schema or writing s
171
171
  });
172
172
  ```
173
173
 
174
- 4. **Filter by distance threshold**: Place `target` directly on a condition (alongside `attribute`, `comparator`, and `value`) to return only records whose distance to the target vector is below a threshold. Use this form to bound result quality by a similarity cutoff rather than ranking.
174
+ 4. **Filter by distance threshold**: To return only records within a similarity cutoff (without ranking), place `target` directly on the condition alongside `comparator` and `value`. Omit `sort`.
175
175
 
176
176
  ```javascript
177
177
  let results = Document.search({
@@ -184,7 +184,7 @@ Apply this rule when adding a vector index to a Harper table schema or writing s
184
184
  });
185
185
  ```
186
186
 
187
- 5. **Include computed distance in results**: Add `'$distance'` to the `select` array to return the computed distance from the target vector alongside each record. `$distance` works in both `sort`-based and `conditions`-based queries.
187
+ 5. **Include computed distance in results**: Use the special `$distance` field in `select` to return the distance from the target vector. Works with both `sort`-based and `conditions`-based queries.
188
188
 
189
189
  ```javascript
190
190
  let results = Document.search({
@@ -194,7 +194,7 @@ Apply this rule when adding a vector index to a Harper table schema or writing s
194
194
  });
195
195
  ```
196
196
 
197
- 6. **Tune HNSW parameters**: Pass additional parameters to `@indexed(type: "HNSW", ...)` to control index quality and performance:
197
+ 6. **Tune HNSW parameters**: Pass additional parameters to `@indexed(type: "HNSW", ...)` to control index quality and performance.
198
198
 
199
199
  | Parameter | Default | Description |
200
200
  | ---------------------- | ----------------- | --------------------------------------------------------------------------------------------------- |
@@ -217,7 +217,7 @@ type Document @table {
217
217
  }
218
218
  ```
219
219
 
220
- **Nearest-neighbor search with distance output:**
220
+ **Nearest-neighbor search with distance score:**
221
221
 
222
222
  ```javascript
223
223
  let results = Document.search({
@@ -242,10 +242,10 @@ let results = Document.search({
242
242
 
243
243
  #### Notes
244
244
 
245
- - The default `distance` function is `cosine`. To use Euclidean distance, set `distance: "euclidean"` in the `@indexed` directive.
246
- - `efConstruction` controls index build quality; increase it to improve recall at the cost of slower indexing.
247
- - `$distance` is a special field prefix it with `$` exactly as shown; it is not a schema attribute.
248
- - `target` is required in both `sort`-based and threshold-based condition queries to identify the reference vector for distance computation.
245
+ - The default `distance` function is `cosine`. Pass `distance: "euclidean"` to switch.
246
+ - `efConstruction` controls index build quality; raising it improves recall at the cost of build time.
247
+ - `$distance` is available in both `sort`-based ranking and `conditions`-based threshold queries.
248
+ - Use the threshold (`conditions` + `target`) form when you want to bound result quality by a similarity cutoff rather than ranking by similarity.
249
249
 
250
250
  ### 1.5 Using Blob Datatype
251
251
 
@@ -322,298 +322,617 @@ Use this skill when you need to store binary files (images, audio, etc.) in the
322
322
 
323
323
  ### 2.1 Automatic APIs
324
324
 
325
- Instructions for the agent to follow when utilizing Harper's automatic APIs.
325
+ Instructions for the agent to follow when enabling and using Harper's automatically generated REST and WebSocket APIs.
326
326
 
327
327
  #### When to Use
328
328
 
329
- Use this skill when you want to interact with Harper tables via REST or WebSockets without writing custom resource logic. This is ideal for basic CRUD operations and real-time updates.
329
+ Apply this rule when adding REST or WebSocket API access to Harper tables or custom resources. Use it when configuring `config.yaml` to expose endpoints, mapping HTTP methods to resource operations, or implementing real-time WebSocket connections on a resource class.
330
330
 
331
331
  #### How It Works
332
332
 
333
- 1. **Enable REST in `config.yaml`**: REST endpoints are **not active by default**. You must explicitly enable them:
333
+ 1. **Enable the REST plugin**: Add `rest: true` to your application's `config.yaml`. This activates the HTTP REST interface and enables WebSocket support by default.
334
+
334
335
  ```yaml
335
336
  rest: true
336
337
  ```
337
- Without this, `@export`ed tables will not respond to HTTP requests.
338
- 2. **Enable Automatic APIs**: Ensure your GraphQL schema includes the `@export` directive for the table.
339
- 3. **Access REST Endpoints**: Use the standard endpoints for your table (Note: Paths are case-sensitive).
340
- 4. **Use Automatic WebSockets**: Connect to `wss://your-harper-instance/{TableName}` to receive events whenever updates are made to that table. This is the easiest way to add real-time capabilities. (Use `ws://` for local development without SSL). For more complex needs, see [Real-time Apps](real-time-apps.md).
341
- 5. **Apply Filtering and Querying**: Use query parameters with `GET /{TableName}/` and `DELETE /{TableName}/`. See the [Querying REST APIs](querying-rest-apis.md) skill for advanced details.
342
- 6. **Customize if Needed**: If the automatic APIs don't meet your requirements, [customize the resources](./custom-resources.md).
343
338
 
344
- #### Examples
339
+ To configure optional behavior:
345
340
 
346
- ##### Schema Configuration
341
+ ```yaml
342
+ rest:
343
+ lastModified: true # enables Last-Modified response header support
344
+ webSocket: false # disables automatic WebSocket support (enabled by default)
345
+ ```
347
346
 
348
- ```graphql
349
- type MyTable @table @export {
350
- id: ID @primaryKey
351
- name: String
352
- }
353
- ```
347
+ 2. **Export your resource in the schema**: Tables are not exposed by default. Use the `@export` directive in your schema definition to make a table available as a REST endpoint. The exported name defines the base URL path, served on the application HTTP server port (default `9926`).
354
348
 
355
- ##### Common REST Operations
349
+ 3. **Use the correct URL structure**: The REST interface follows a consistent path convention.
356
350
 
357
- - **List Records**: `GET /MyTable/`
358
- - **Create Record**: `POST /MyTable/`
359
- - **Update Record**: `PATCH /MyTable/{id}`
351
+ | Path | Description |
352
+ | -------------------------------------------- | ---------------------------------------------------------------------------------- |
353
+ | `/my-resource` | Returns a description of the resource (e.g., table metadata) |
354
+ | `/my-resource/` | Trailing slash — represents the full collection; append query parameters to search |
355
+ | `/my-resource/record-id` | A specific record identified by its primary key |
356
+ | `/my-resource/record-id/` | Trailing slash — collection of records with the given id prefix |
357
+ | `/my-resource/record-id/with/multiple/parts` | Record id with multiple path segments |
360
358
 
361
- ### 2.2 Querying REST APIs
359
+ 4. **Map HTTP methods to operations**: Each HTTP method maps to a resource method and operation.
360
+ - **GET** — Retrieve a record or search. Calls `get()`.
362
361
 
363
- Instructions for the agent to follow when querying Harper's REST APIs.
362
+ ```
363
+ GET /MyTable/123
364
+ GET /MyTable/?name=Harper
365
+ GET /MyTable/123.propertyName
366
+ ```
364
367
 
365
- #### When to Use
368
+ Responses include an `ETag` header. Clients may send `If-None-Match` to receive `304 Not Modified` when the record is unchanged.
366
369
 
367
- Use this skill when you need to perform advanced data retrieval (filtering, sorting, pagination, joins) using Harper's automatic REST endpoints.
370
+ - **PUT** Create or replace a record (upsert). Calls `put(record)`. Properties not in the body are removed.
368
371
 
369
- #### How It Works
372
+ ```
373
+ PUT /MyTable/123
374
+ Content-Type: application/json
370
375
 
371
- 1. **Basic Filtering**: Use attribute names as query parameters: `GET /Table/?key=value`.
372
- 2. **Use Comparison Operators**: Append operators like `gt`, `ge`, `lt`, `le`, `ne` using FIQL-style syntax: `GET /Table/?price=gt=100`.
373
- 3. **Apply Logic and Grouping**: Use `&` for AND, `|` for OR, and `()` for grouping: `GET /Table/?(rating=5|featured=true)&price=lt=50`.
374
- 4. **Select Specific Fields**: Use `select()` to limit returned attributes: `GET /Table/?select(name,price)`.
375
- 5. **Paginate Results**: Use `limit(count)` or `limit(offset, count)` to set the number of records to return and skip.
376
- - Example (first 10): `GET /Table/?limit(10)`
377
- - Example (skip 20, return 10): `GET /Table/?limit(20, 10)`
378
- 6. **Sort Results**: Use `sort()` with `+` (asc) or `-` (desc) before the field name. Avoid `sort=field` format.
379
- - Example (asc): `GET /Table/?sort(+name)`
380
- - Example (desc): `GET /Table/?sort(-price)`
381
- - Example (combined): `GET /Table/?sort(-price,+name)`
382
- 7. **Query Relationships**: Use dot syntax for tables linked with `@relationship`: `GET /Book/?author.name=Harper`.
376
+ { "name": "some data" }
377
+ ```
383
378
 
384
- ### 2.3 Real-time Applications
379
+ - **POST** — Create a new record without specifying a primary key. Calls `post(data)`. The assigned key is returned in the `Location` response header.
385
380
 
386
- Instructions for the agent to follow when building real-time applications in Harper.
381
+ ```
382
+ POST /MyTable/
383
+ Content-Type: application/json
387
384
 
388
- #### When to Use
385
+ { "name": "some data" }
386
+ ```
389
387
 
390
- Use this skill when you need to stream live updates to clients, implement chat features, or provide real-time data synchronization between the database and a frontend.
388
+ - **PATCH** Partially update a record, merging only provided properties. Unspecified properties are preserved.
391
389
 
392
- #### How It Works
390
+ ```
391
+ PATCH /MyTable/123
392
+ Content-Type: application/json
393
393
 
394
- 1. **Check Automatic WebSockets**: If you only need to stream table changes, use [Automatic APIs](automatic-apis.md) which provide a WebSocket endpoint for every `@export`ed table.
395
- 2. **Implement `connect` in a Resource**: For custom bi-directional logic, implement the `connect` method.
396
- 3. **Use Pub/Sub**: Use `tables.TableName.subscribe(query)` to listen for specific data changes and stream them to the client.
397
- 4. **Handle SSE**: Ensure your `connect` method gracefully handles cases where `incomingMessages` is null (Server-Sent Events).
398
- 5. **Connect from Client**: Use standard WebSockets (`new WebSocket('wss://...')`) to connect to your resource endpoint. Ensure you use the appropriate scheme (`ws://` for HTTP, `wss://` for HTTPS).
394
+ { "status": "active" }
395
+ ```
399
396
 
400
- #### Examples
397
+ - **DELETE** — Delete a record or all records matching a query.
398
+ ```
399
+ DELETE /MyTable/123
400
+ DELETE /MyTable/?status=archived
401
+ ```
402
+
403
+ 5. **Access the auto-generated OpenAPI spec**: Harper generates an OpenAPI specification for all exported resources. Retrieve it at:
401
404
 
402
- ##### Bi-directional WebSocket Resource
405
+ ```
406
+ GET /openapi
407
+ ```
403
408
 
404
- ```typescript
405
- import { Resource, tables } from 'harper';
409
+ 6. **Connect via WebSocket**: When `rest` is enabled, WebSocket support is on by default. Connect to a resource URL to subscribe to change events for that resource.
406
410
 
407
- export class MySocket extends Resource {
408
- async *connect(target, incomingMessages) {
409
- // Subscribe to table changes
410
- const subscription = await tables.MyTable.subscribe(target);
411
- if (!incomingMessages) {
412
- return subscription; // SSE mode
413
- }
411
+ ```javascript
412
+ let ws = new WebSocket('wss://server/my-resource/341');
413
+ ws.onmessage = (event) => {
414
+ let data = JSON.parse(event.data);
415
+ };
416
+ ```
417
+
418
+ Connecting to `wss://server/my-resource/341` accesses the `my-resource` resource with record id `341` and subscribes to it. When the record changes or a message is published to it, the WebSocket connection receives the update.
419
+
420
+ 7. **Implement a custom `connect()` handler**: Override `connect(incomingMessages)` on a resource class to control WebSocket behavior. The method must return an async iterable or generator that produces messages to send to the client.
421
+
422
+ #### Examples
423
+
424
+ **Simple echo server using an async generator**:
414
425
 
415
- // Handle incoming client messages
426
+ ```javascript
427
+ export class Echo extends Resource {
428
+ async *connect(incomingMessages) {
416
429
  for await (let message of incomingMessages) {
417
- yield { received: message };
430
+ yield message; // echo each message back
418
431
  }
419
432
  }
420
433
  }
421
434
  ```
422
435
 
423
- ### 2.4 Checking Authentication
436
+ **Using the default `connect()` with event-style access and a timer**:
437
+
438
+ ```javascript
439
+ export class Example extends Resource {
440
+ connect(incomingMessages) {
441
+ let outgoingMessages = super.connect();
442
+
443
+ let timer = setInterval(() => {
444
+ outgoingMessages.send({ greeting: 'hi again!' });
445
+ }, 1000);
446
+
447
+ incomingMessages.on('data', (message) => {
448
+ outgoingMessages.send(message); // echo incoming messages
449
+ });
450
+
451
+ outgoingMessages.on('close', () => {
452
+ clearInterval(timer);
453
+ });
454
+
455
+ return outgoingMessages;
456
+ }
457
+ }
458
+ ```
459
+
460
+ **Minimal `config.yaml` enabling REST with WebSocket disabled**:
461
+
462
+ ```yaml
463
+ rest:
464
+ webSocket: false
465
+ ```
424
466
 
425
- Instructions for the agent to follow when handling authentication and sessions.
467
+ #### Notes
468
+
469
+ - Tables must be explicitly exported using `@export` in the schema — they are not exposed by default.
470
+ - `rest: true` is the minimal configuration to enable both REST and WebSocket support. See [real-time-apps.md](real-time-apps.md) for patterns around real-time WebSocket usage.
471
+ - For full query syntax on `GET` and `DELETE` with query parameters, see [querying-rest-apis.md](querying-rest-apis.md).
472
+ - The default `connect()` returns an iterable with a `send(message)` method and a `close` event for cleanup on disconnect.
473
+ - For MQTT over WebSockets, set the sub-protocol header `Sec-WebSocket-Protocol: mqtt`.
474
+ - In distributed environments, non-retained messages are delivered in the order received per node; retained messages (PUT/updated records) keep only the latest-timestamp version as the winning record across the cluster.
475
+ - Use the `Content-Type` request header to specify body format and the `Accept` header to request a specific response format.
476
+
477
+ ### 2.2 Querying REST APIs
478
+
479
+ Instructions for the agent to filter, sort, select, and paginate Harper REST API collections using URL query parameters.
426
480
 
427
481
  #### When to Use
428
482
 
429
- Use this skill when you need to implement sign-in/sign-out functionality, protect specific resource endpoints, or identify the currently logged-in user in a Harper application.
483
+ Apply this rule when building or modifying code that queries Harper REST endpoints with filtering, sorting, field selection, or pagination. Use it whenever constructing URLs against collection paths exposed by Harper's automatic REST interface (see [automatic-apis.md](automatic-apis.md)).
430
484
 
431
485
  #### How It Works
432
486
 
433
- 1. **Configure Harper for Sessions**: Ensure `harper-config.yaml` has sessions enabled and local auto-authorization disabled for testing:
434
- ```yaml
435
- authentication:
436
- authorizeLocal: false
437
- enableSessions: true
487
+ 1. **Filter by attribute**: Add query parameters matching attribute names and values. The queried attribute must be indexed.
488
+
438
489
  ```
439
- 2. **Implement Sign In**: Use `this.getContext().login(username, password)` to create a session:
440
- ```typescript
441
- async post(_target, data) {
442
- const context = this.getContext();
443
- try {
444
- await context.login(data.username, data.password);
445
- } catch {
446
- return new Response('Invalid credentials', { status: 403 });
447
- }
448
- return new Response('Logged in', { status: 200 });
449
- }
490
+ GET /Product/?category=software
491
+ GET /Product/?category=software&inStock=true
450
492
  ```
451
- 3. **Identify Current User**: Use `this.getCurrentUser()` to access session data:
452
- ```typescript
453
- async get() {
454
- const user = this.getCurrentUser?.();
455
- if (!user) return new Response(null, { status: 401 });
456
- return { username: user.username, role: user.role };
457
- }
493
+
494
+ 2. **Apply comparison operators (FIQL syntax)**: Use FIQL operators directly in query parameter values.
495
+
496
+ | Operator | Meaning |
497
+ | ------------ | -------------------------------------- |
498
+ | `==` | Equal |
499
+ | `=lt=` | Less than |
500
+ | `=le=` | Less than or equal |
501
+ | `=gt=` | Greater than |
502
+ | `=ge=` | Greater than or equal |
503
+ | `=ne=`, `!=` | Not equal |
504
+ | `=ct=` | Contains (strings) |
505
+ | `=sw=` | Starts with (strings) |
506
+ | `=ew=` | Ends with (strings) |
507
+ | `=`, `===` | Strict equality (no type conversion) |
508
+ | `!==` | Strict inequality (no type conversion) |
509
+
458
510
  ```
459
- 4. **Implement Sign Out**: Use `this.getContext().logout()` or delete the session from context:
460
- ```typescript
461
- async post() {
462
- const context = this.getContext();
463
- await context.session?.delete?.(context.session.id);
464
- return new Response('Logged out', { status: 200 });
465
- }
511
+ GET /Product/?price=gt=100
512
+ GET /Product/?price=le=20
513
+ GET /Product/?name==Keyboard*
514
+ GET /Product/?category=software&price=gt=100&price=lt=200
515
+ ```
516
+
517
+ For date fields, URL-encode colons as `%3A`:
518
+
519
+ ```
520
+ GET /Product/?listDate=gt=2017-03-08T09%3A30%3A00.000Z
521
+ ```
522
+
523
+ 3. **Chain conditions for range queries**: Omit the attribute name on the second condition to apply it to the same attribute. Only `gt`/`ge` combined with `lt`/`le` is supported.
524
+
525
+ ```
526
+ GET /Product/?price=gt=100&lt=200
527
+ ```
528
+
529
+ 4. **Combine conditions with OR logic**: Use `|` instead of `&`.
530
+
531
+ ```
532
+ GET /Product/?rating=5|featured=true
533
+ ```
534
+
535
+ 5. **Group conditions**: Use parentheses or square brackets to control order of operations. Prefer square brackets when constructing queries from user input, since standard URI encoding safely encodes `[` and `]`.
536
+
537
+ ```
538
+ GET /Product/?rating=5|(price=gt=100&price=lt=200)
539
+ GET /Product/?rating=5&[tag=fast|tag=scalable|tag=efficient]
540
+ ```
541
+
542
+ Construct grouped queries from JavaScript:
543
+
544
+ ```javascript
545
+ let url = `/Product/?rating=5&[${tags.map(encodeURIComponent).join('|')}]`;
546
+ ```
547
+
548
+ 6. **Select specific properties with `select(`**: Use `select()` to control which fields are returned.
549
+
550
+ | Syntax | Returns |
551
+ | -------------------------------------- | ------------------------------------------- |
552
+ | `?select(property)` | Values of a single property directly |
553
+ | `?select(property1,property2)` | Objects with only the specified properties |
554
+ | `?select([property1,property2])` | Arrays of property values |
555
+ | `?select(property1,)` | Objects with a single specified property |
556
+ | `?select(property{subProp1,subProp2})` | Nested objects with specific sub-properties |
557
+
558
+ ```
559
+ GET /Product/?category=software&select(name)
560
+ GET /Product/?brand.name=Microsoft&select(name,brand{name})
561
+ ```
562
+
563
+ 7. **Limit results with `limit(`**: Use `limit(end)` or `limit(start,end)` to paginate.
564
+
565
+ ```
566
+ GET /Product/?rating=gt=3&inStock=true&select(rating,name)&limit(20)
567
+ GET /Product/?rating=gt=3&limit(10,30)
568
+ ```
569
+
570
+ 8. **Sort results with `sort(`**: Use `sort(property)` or `sort(+property,-property,...)`. Prefix `+` or no prefix = ascending; `-` = descending.
571
+
572
+ ```
573
+ GET /Product/?rating=gt=3&sort(+name)
574
+ GET /Product/?sort(+rating,-price)
466
575
  ```
467
- 5. **Protect Routes**: In your Resource, use `allowRead()`, `allowUpdate()`, etc., to enforce authorization logic based on `this.getCurrentUser()`. For privileged actions, verify `user.role.permission.super_user`.
576
+
577
+ 9. **Query across relationships**: Use dot-syntax to filter by related table attributes. Relationships must be defined in the schema using `@relation`.
578
+
579
+ ```
580
+ GET /Product/?brand.name=Microsoft
581
+ GET /Brand/?products.name=Keyboard
582
+ ```
583
+
584
+ Use `select()` to include relationship attributes in the response (they are not included by default):
585
+
586
+ ```
587
+ GET /Product/?brand.name=Microsoft&select(name,brand{name})
588
+ ```
589
+
590
+ 10. **Access a specific property by URL**: Append the property name with dot syntax to the record ID. Only works for properties declared in the schema.
591
+ ```
592
+ GET /MyTable/123.propertyName
593
+ ```
468
594
 
469
595
  #### Examples
470
596
 
471
- ##### Sign In Implementation
472
-
473
- ```typescript
474
- async post(_target, data) {
475
- const context = this.getContext();
476
- try {
477
- await context.login(data.username, data.password);
478
- } catch {
479
- return new Response('Invalid credentials', { status: 403 });
480
- }
481
- return new Response('Logged in', { status: 200 });
482
- }
597
+ **Range filter with select and limit:**
598
+
599
+ ```
600
+ GET /Product/?category=software&price=gt=100&price=lt=200&select(name,price)&limit(20)
483
601
  ```
484
602
 
485
- ##### Identify Current User
603
+ **Sort descending with multiple fields:**
486
604
 
487
- ```typescript
488
- async get() {
489
- const user = this.getCurrentUser?.();
490
- if (!user) return new Response(null, { status: 401 });
491
- return { username: user.username, role: user.role };
605
+ ```
606
+ GET /Product/?sort(+rating,-price)
607
+ ```
608
+
609
+ **OR logic with grouping:**
610
+
611
+ ```
612
+ GET /Product/?price=lt=100|[rating=5&[tag=fast|tag=scalable|tag=efficient]&inStock=true]
613
+ ```
614
+
615
+ **Relationship join with nested select:**
616
+
617
+ ```
618
+ GET /Product/?brand.name=Microsoft&select(name,brand{name,id})
619
+ ```
620
+
621
+ **Schema defining a relationship for join queries:**
622
+
623
+ ```graphql
624
+ type Product @table @export {
625
+ id: Long @primaryKey
626
+ name: String
627
+ brandId: Long @indexed
628
+ brand: Brand @relation(from: "brandId")
629
+ }
630
+ type Brand @table @export {
631
+ id: Long @primaryKey
632
+ name: String
633
+ products: [Product] @relation(to: "brandId")
492
634
  }
493
635
  ```
494
636
 
495
- ##### Sign Out Implementation
637
+ **Many-to-many relationship query:**
496
638
 
497
- ```typescript
498
- async post() {
499
- const context = this.getContext();
500
- await context.session?.delete?.(context.session.id);
501
- return new Response('Logged out', { status: 200 });
639
+ ```graphql
640
+ type Product @table @export {
641
+ id: Long @primaryKey
642
+ name: String
643
+ resellerIds: [Long] @indexed
644
+ resellers: [Reseller] @relation(from: "resellerId")
502
645
  }
503
646
  ```
504
647
 
505
- #### Status code conventions used here
648
+ ```
649
+ GET /Product/?resellers.name=Cool Shop&select(id,name,resellers{name,id})
650
+ ```
651
+
652
+ **Type conversion with explicit prefix:**
653
+
654
+ ```
655
+ GET /Product/?price==number:123
656
+ GET /Product/?active==boolean:true
657
+ GET /Product/?listDate==date:2024-01-05T20%3A07%3A27.955Z
658
+ ```
659
+
660
+ #### Notes
661
+
662
+ - Only indexed attributes can be used as the primary filter; additional unindexed attributes can be combined with `&` once at least one indexed attribute is present.
663
+ - For null value queries, use `?attribute=null`. Indexes must have been created with null indexing support; existing indexes must be removed and re-added to support null queries.
664
+ - FIQL comparators (`==`, `!=`, `=gt=`, etc.) apply automatic type conversion based on value syntax or schema-declared type. Strict operators (`=`, `===`, `!==`) skip automatic type conversion.
665
+ - Filtering by a related attribute produces INNER JOIN behavior (only records with a matching related record are returned). Using `select()` on a relationship without a filter produces LEFT JOIN behavior.
666
+ - The array order of foreign key values in many-to-many relationships is preserved when resolving the relationship.
667
+ - See [automatic-apis.md](automatic-apis.md) for how Harper tables are automatically exposed as REST endpoints.
668
+
669
+ ### 2.3 Real-Time Apps with WebSockets and Pub/Sub
670
+
671
+ Instructions for the agent to follow when building real-time features in Harper using WebSockets and Pub/Sub.
672
+
673
+ #### When to Use
674
+
675
+ Apply this rule when implementing any feature that requires real-time bidirectional communication, live data streaming, or push-based updates in a Harper application. This includes chat, live dashboards, sensor feeds, and any scenario where clients must receive resource changes as they happen.
676
+
677
+ #### How It Works
678
+
679
+ 1. **Enable WebSocket support**: WebSocket support is enabled automatically when the `rest` plugin is enabled. To explicitly disable it, set the following in your config:
506
680
 
507
- - 200: Successful operation. For `GET /me`, a `200` with empty body means “not signed in”.
508
- - 400: Missing required fields (e.g., username/password on sign-in).
509
- - 401: No current session for an action that requires one (e.g., sign out when not signed in).
510
- - 403: Authenticated but not authorized (bad credentials on login attempt, or insufficient privileges).
681
+ ```yaml
682
+ rest:
683
+ webSocket: false
684
+ ```
511
685
 
512
- #### Client considerations
686
+ 2. **Connect a client to a resource**: A WebSocket connection to a resource URL automatically subscribes to that resource. When the record changes or a message is published to it, the connection receives the update.
513
687
 
514
- - Sessions are cookie-based; the server handles setting and reading the cookie via Harper. If you make cross-origin requests, ensure the appropriate `credentials` mode and CORS settings.
515
- - If developing locally, double-check the server config still has `authentication.authorizeLocal: false` to avoid accidental superuser bypass.
688
+ ```javascript
689
+ let ws = new WebSocket('wss://server/my-resource/341');
690
+ ws.onmessage = (event) => {
691
+ let data = JSON.parse(event.data);
692
+ };
693
+ ```
516
694
 
517
- #### Token-based auth (JWT + refresh token) for non-browser clients
695
+ `new WebSocket('wss://server/my-resource/341')` accesses the resource defined for `my-resource` with record id `341` and subscribes to it.
518
696
 
519
- Cookie-backed sessions are great for browser flows. For CLI tools, mobile apps, or other non-browser clients, it’s often easier to use **explicit tokens**:
697
+ 3. **Implement a custom `connect()` handler**: Override the `connect(incomingMessages)` method on a resource class to control WebSocket behavior. The method must return an async iterable (or generator) that produces messages to send to the client. See [automatic-apis.md](automatic-apis.md) for more on defining resource classes.
520
698
 
521
- - **JWT (`operation_token`)**: short-lived bearer token used to authorize API requests.
522
- - **Refresh token (`refresh_token`)**: longer-lived token used to mint a new JWT when it expires.
699
+ 4. **Use the default `connect()` for event-style access**: Call `super.connect()` to get a streaming iterable that provides:
700
+ - A `send(message)` method for pushing outgoing messages
701
+ - A `close` event for cleanup on disconnect
523
702
 
524
- This project includes two Resource patterns for that flow:
703
+ 5. **Handle message ordering in distributed environments**: Harper delivers messages to local subscribers immediately without inter-node coordination delay.
525
704
 
526
- ##### Issuing tokens: `IssueTokens`
705
+ | Message Type | Behavior |
706
+ | -------------------------------------------------------- | ----------------------------------------------------------------------- |
707
+ | Non-retained (no `retain` flag) | Every message delivered in order received; suitable for chat |
708
+ | Retained (published with `retain`, or PUT/updated in DB) | Only the latest-timestamp message is kept; suitable for sensor readings |
527
709
 
528
- **Description / use case:** Generate `{ refreshToken, jwt }` either:
710
+ 6. **Use MQTT over WebSockets** when needed by setting the sub-protocol header:
711
+ ```
712
+ Sec-WebSocket-Protocol: mqtt
713
+ ```
529
714
 
530
- - with an existing Authorization token (either Basic Auth or a JWT) and you want to issue new tokens, or
531
- - from an explicit `{ username, password }` payload (useful for direct “login” from a CLI/mobile client).
715
+ #### Examples
716
+
717
+ **Simple echo server** — override `connect(incomingMessages)` to yield each incoming message back to the client:
532
718
 
533
719
  ```javascript
534
- export class IssueTokens extends Resource {
535
- static loadAsInstance = false;
536
-
537
- async get(target) {
538
- const { refresh_token: refreshToken, operation_token: jwt } =
539
- await databases.system.hdb_user.operation(
540
- { operation: 'create_authentication_tokens' },
541
- this.getContext(),
542
- );
543
- return { refreshToken, jwt };
720
+ export class Echo extends Resource {
721
+ async *connect(incomingMessages) {
722
+ for await (let message of incomingMessages) {
723
+ yield message; // echo each message back
724
+ }
544
725
  }
726
+ }
727
+ ```
545
728
 
546
- async post(target, data) {
547
- if (!data.username || !data.password) {
548
- throw new Error('username and password are required');
549
- }
729
+ **Custom connect with timer and event-style access** — use `super.connect()` to get the outgoing stream, push periodic messages, echo incoming messages, and clean up on disconnect:
550
730
 
551
- const { refresh_token: refreshToken, operation_token: jwt } =
552
- await databases.system.hdb_user.operation({
553
- operation: 'create_authentication_tokens',
554
- username: data.username,
555
- password: data.password,
556
- });
557
- return { refreshToken, jwt };
731
+ ```javascript
732
+ export class Example extends Resource {
733
+ connect(incomingMessages) {
734
+ let outgoingMessages = super.connect();
735
+
736
+ let timer = setInterval(() => {
737
+ outgoingMessages.send({ greeting: 'hi again!' });
738
+ }, 1000);
739
+
740
+ incomingMessages.on('data', (message) => {
741
+ outgoingMessages.send(message); // echo incoming messages
742
+ });
743
+
744
+ outgoingMessages.on('close', () => {
745
+ clearInterval(timer);
746
+ });
747
+
748
+ return outgoingMessages;
558
749
  }
559
750
  }
560
751
  ```
561
752
 
562
- **Recommended documentation notes to include:**
753
+ #### Notes
754
+
755
+ - WebSocket connections target a resource URL path. By default, connecting to a resource subscribes to changes for that resource.
756
+ - The `connect(incomingMessages)` method **must** return an async iterable or generator; returning a plain value will not work.
757
+ - `super.connect()` returns a streaming iterable with `send(message)` and a `close` event — use this when you need to push messages outside of the incoming message loop.
758
+ - For one-way real-time streaming without bidirectional communication, consider Server-Sent Events instead.
759
+ - For full pub/sub capabilities, Harper also supports MQTT; set `Sec-WebSocket-Protocol: mqtt` to use MQTT over WebSockets.
563
760
 
564
- - `GET` variant: intended for “I already have an Authorization token, give me new tokens”.
565
- - `POST` variant: intended for “I have credentials, give me tokens”.
566
- - Response shape:
567
- - `refreshToken`: store securely (long-lived).
568
- - `jwt`: attach to requests (short-lived).
761
+ ### 2.4 Checking Authentication
762
+
763
+ Instructions for the agent to follow when handling user authentication and session management inside Harper Resources.
569
764
 
570
- ##### Refreshing a JWT: `RefreshJWT`
765
+ #### When to Use
571
766
 
572
- **Description / use case:** When the JWT expires, the client uses the refresh token to get a new JWT without re-supplying username/password.
767
+ Apply this rule when implementing authentication checks, login/logout flows, or token issuance inside a custom Resource. Use it any time a Resource needs to identify the current user, establish a session, or issue JWTs to clients. See [custom-resources.md](custom-resources.md) for the general Resource authoring pattern.
768
+
769
+ #### How It Works
770
+
771
+ 1. **Check the current user** with `getCurrentUser()`. Call it inside any Resource method to retrieve the authenticated user or `undefined` if no user is authenticated. Guard protected endpoints by returning a `401` when the result is `undefined`.
772
+
773
+ ```javascript
774
+ async get(target) {
775
+ const user = this.getCurrentUser();
776
+ if (!user) return new Response(null, { status: 401 });
777
+ return { username: user.username, role: user.role };
778
+ }
779
+ ```
780
+
781
+ The returned object exposes `username`, `role`, and `role.permission` flags.
782
+
783
+ 2. **Enable sessions** before using session-based login. Set `authentication.enableSessions: true` in `harperdb-config.yaml`:
784
+
785
+ ```yaml
786
+ authentication:
787
+ enableSessions: true
788
+ ```
789
+
790
+ 3. **Access login and session helpers** via `getContext()`. The context object exposes `context.login` and `context.session` for sign-in/out flows.
791
+ - Call `context.login(username, password)` to verify credentials and establish a session cookie on success.
792
+ - To end a session, delete it via `context.session.delete(context.session.id)`.
793
+
794
+ 4. **Implement sign-in and sign-out Resources** using the context helpers:
795
+
796
+ ```javascript
797
+ export class SignIn extends Resource {
798
+ async post(_target, data) {
799
+ const context = this.getContext();
800
+ try {
801
+ await context.login(data.username, data.password);
802
+ } catch {
803
+ return new Response('Invalid credentials', { status: 403 });
804
+ }
805
+ return new Response('Logged in', { status: 200 });
806
+ }
807
+ }
808
+
809
+ export class SignOut extends Resource {
810
+ async post() {
811
+ const context = this.getContext();
812
+ if (!context.session) return new Response(null, { status: 401 });
813
+ await context.session.delete(context.session.id);
814
+ return new Response('Logged out', { status: 200 });
815
+ }
816
+ }
817
+ ```
818
+
819
+ 5. **Issue JWTs for non-browser clients** (CLI tools, mobile apps, service-to-service). Cookie-based sessions are intended for browser clients. For other clients, mint tokens programmatically using `server.operation()`:
820
+
821
+ ```javascript
822
+ import { Resource, server } from 'harper';
823
+
824
+ export class IssueTokens extends Resource {
825
+ static async get(_target, context) {
826
+ const { operation_token, refresh_token } = await server.operation(
827
+ { operation: 'create_authentication_tokens' },
828
+ context,
829
+ true,
830
+ );
831
+ return { operation_token, refresh_token };
832
+ }
833
+
834
+ static async post(_target, data) {
835
+ const { username, password } = await data;
836
+ if (!username || !password) {
837
+ return new Response('username and password required', { status: 400 });
838
+ }
839
+ const { operation_token, refresh_token } = await server.operation({
840
+ operation: 'create_authentication_tokens',
841
+ username,
842
+ password,
843
+ });
844
+ return { operation_token, refresh_token };
845
+ }
846
+ }
847
+
848
+ export class RefreshJWT extends Resource {
849
+ static async post(_target, data) {
850
+ const { refresh_token } = await data;
851
+ if (!refresh_token) {
852
+ return new Response('refresh_token required', { status: 400 });
853
+ }
854
+ const { operation_token } = await server.operation({
855
+ operation: 'refresh_operation_token',
856
+ refresh_token,
857
+ });
858
+ return { operation_token };
859
+ }
860
+ }
861
+ ```
862
+
863
+ Pass `true` as the third argument to `server.operation()` when the operation should run as the current authenticated user. Omit it or pass `false` when the operation supplies its own credentials.
864
+
865
+ 6. **Configure JWT token expiry** in `harperdb-config.yaml` under the `authentication` section:
866
+
867
+ ```yaml
868
+ authentication:
869
+ operationTokenTimeout: 1d
870
+ refreshTokenTimeout: 30d
871
+ ```
872
+
873
+ Duration strings follow the `jsonwebtoken` package format (e.g., `1d`, `12h`, `60m`).
874
+
875
+ #### Examples
876
+
877
+ **Protecting a resource endpoint and returning user info:**
573
878
 
574
879
  ```javascript
575
- export class RefreshJWT extends Resource {
576
- static loadAsInstance = false;
880
+ async get(target) {
881
+ const user = this.getCurrentUser();
882
+ if (!user) return new Response(null, { status: 401 });
883
+ return { username: user.username, role: user.role };
884
+ }
885
+ ```
886
+
887
+ **Full session-based sign-in/sign-out flow:**
577
888
 
578
- async post(target, data) {
579
- if (!data.refreshToken) {
580
- throw new Error('refreshToken is required');
889
+ ```javascript
890
+ export class SignIn extends Resource {
891
+ async post(_target, data) {
892
+ const context = this.getContext();
893
+ try {
894
+ await context.login(data.username, data.password);
895
+ } catch {
896
+ return new Response('Invalid credentials', { status: 403 });
581
897
  }
898
+ return new Response('Logged in', { status: 200 });
899
+ }
900
+ }
582
901
 
583
- const { operation_token: jwt } = await databases.system.hdb_user.operation({
584
- operation: 'refresh_operation_token',
585
- refresh_token: data.refreshToken,
586
- });
587
- return { jwt };
902
+ export class SignOut extends Resource {
903
+ async post() {
904
+ const context = this.getContext();
905
+ if (!context.session) return new Response(null, { status: 401 });
906
+ await context.session.delete(context.session.id);
907
+ return new Response('Logged out', { status: 200 });
588
908
  }
589
909
  }
590
910
  ```
591
911
 
592
- **Recommended documentation notes to include:**
593
-
594
- - Requires `refreshToken` in the request body.
595
- - Returns a new `{ jwt }`.
596
- - If refresh fails (expired/revoked), client must re-authenticate (e.g., call `IssueTokens.post` again).
597
-
598
- ##### Suggested client flow (high-level)
912
+ **JWT token refresh endpoint:**
599
913
 
600
- 1. **Sign in (token flow)**
601
- - POST /IssueTokens/ with a body of `{ "username": "your username", "password": "your password" }` or GET /IssueTokens/ with an existing Authorization token.
602
- - Receive `{ jwt, refreshToken }` in the response
603
- 2. **Call protected APIs**
604
- - Send the JWT with each request in the Authorization header (as your auth mechanism expects)
605
- 3. **JWT expires**
606
- - POST /RefreshJWT/ with a body of `{ "refreshToken": "your refresh token" }`.
607
- - Receive `{ jwt }` in the response and continue
914
+ ```javascript
915
+ export class RefreshJWT extends Resource {
916
+ static async post(_target, data) {
917
+ const { refresh_token } = await data;
918
+ if (!refresh_token) {
919
+ return new Response('refresh_token required', { status: 400 });
920
+ }
921
+ const { operation_token } = await server.operation({
922
+ operation: 'refresh_operation_token',
923
+ refresh_token,
924
+ });
925
+ return { operation_token };
926
+ }
927
+ }
928
+ ```
608
929
 
609
- #### Quick checklist
930
+ #### Notes
610
931
 
611
- - [ ] Public endpoints explicitly `allowRead`/`allowCreate` as needed.
612
- - [ ] Sign-in uses `context.login` and handles 400/403 correctly.
613
- - [ ] Protected routes call `ensureSuperUser(this.getCurrentUser())` (or another role check) before doing work.
614
- - [ ] Sign-out verifies a session and deletes it.
615
- - [ ] `authentication.authorizeLocal` is `false` and `enableSessions` is `true` in Harper config.
616
- - [ ] If using tokens: `IssueTokens` issues `{ jwt, refreshToken }`, `RefreshJWT` refreshes `{ jwt }` with a `refreshToken`.
932
+ - `getCurrentUser()` and `getContext()` are instance methods; call them with `this` inside non-static Resource methods.
933
+ - `enableSessions` must be `true` in config before `context.login` or `context.session` will function.
934
+ - Cookie-based sessions target browser clients. Use JWT issuance via `server.operation()` for all other client types.
935
+ - When both `operation_token` and `refresh_token` have expired, the client must call `create_authentication_tokens` again with credentials.
617
936
 
618
937
  ## 3. Logic & Extension
619
938
 
@@ -847,151 +1166,269 @@ Use this skill when you want to write Harper Resources in TypeScript and have th
847
1166
  files: 'resources/*.ts'
848
1167
  ```
849
1168
 
850
- ### 3.5 Caching
1169
+ ### 3.5 Caching External Data Sources in Harper
851
1170
 
852
- Instructions for the agent to follow when implementing caching in Harper.
1171
+ Instructions for the agent to implement integrated data caching in Harper by wrapping external sources with a cache table and `sourcedFrom`.
853
1172
 
854
1173
  #### When to Use
855
1174
 
856
- Use this skill when you need high-performance, low-latency storage for data from external sources. It's ideal for reducing API calls to third-party services, preventing cache stampedes, and making external data queryable as if it were native Harper tables.
1175
+ Apply this rule when a Harper application needs to cache responses from an external API, microservice, or database to avoid repeated slow or expensive upstream calls. Use it whenever you need to define TTL-based cache expiration, observe ETag-based conditional responses, or manually invalidate cached entries.
857
1176
 
858
1177
  #### How It Works
859
1178
 
860
- 1. **Configure a Cache Table**: Define a table in your `schema.graphql` with an `expiration` (in seconds).
861
- 2. **Define an External Source**: Create a Resource class that fetches the data from your source.
862
- 3. **Attach Source to Table**: Use `sourcedFrom` to link your resource to the table.
863
- 4. **Implement Active Caching (Optional)**: Use `subscribe()` for proactive updates. See [Real-Time Apps](real-time-apps.md).
864
- 5. **Implement Write-Through Caching (Optional)**: Define `put` or `post` in your resource to propagate updates upstream.
1179
+ 1. **Define a cache table with `expiration`**: In `schema.graphql`, add the `expiration` argument to `@table`. The value is in seconds. Any record older than this threshold is considered stale and will be re-fetched on next access.
1180
+
1181
+ ```graphql
1182
+ type JokeCache @table(expiration: 60) @export {
1183
+ id: ID @primaryKey
1184
+ setup: String
1185
+ punchline: String
1186
+ }
1187
+ ```
1188
+
1189
+ 2. **Wrap the external source in `resources.js`**: Create an object with a `get(id)` method that fetches from the upstream source. Then call `sourcedFrom` on the table to register it.
1190
+
1191
+ ```javascript
1192
+ const jokeAPI = {
1193
+ async get(id) {
1194
+ const response = await fetch(`https://official-joke-api.appspot.com/jokes/${id}`);
1195
+ return response.json();
1196
+ },
1197
+ };
1198
+
1199
+ tables.JokeCache.sourcedFrom(jokeAPI);
1200
+ ```
1201
+
1202
+ Harper's caching behavior after `sourcedFrom` is registered:
1203
+ - A request arrives for `/JokeCache/1`.
1204
+ - Harper checks if the record with id `1` exists in `JokeCache` and is not stale.
1205
+ - If fresh, Harper returns it immediately.
1206
+ - If missing or stale, Harper calls `jokeAPI.get()`, stores the result in `JokeCache`, and returns it.
1207
+ - Multiple simultaneous requests for the same missing or stale record wait on a single upstream call — Harper prevents cache stampedes automatically.
1208
+
1209
+ 3. **Configure plugins in `config.yaml`**: Enable the schema, REST API, and JS resource plugins.
1210
+
1211
+ ```yaml
1212
+ graphqlSchema:
1213
+ files: 'schema.graphql'
1214
+ rest: true
1215
+ jsResource:
1216
+ files: 'resources.js'
1217
+ ```
1218
+
1219
+ 4. **Observe caching via ETags**: Harper automatically computes an ETag from the record's last-modified timestamp. On the first request you receive a `200` with an `etag` header. Pass that value back in `If-None-Match` on subsequent requests; Harper returns `304 Not Modified` with an empty body if the record is unchanged.
1220
+
1221
+ ```bash
1222
+ curl -i 'http://localhost:9926/JokeCache/1' \
1223
+ -H 'If-None-Match: "abCDefGHij"'
1224
+ ```
1225
+
1226
+ 5. **Force a cache bypass**: Send `Cache-Control: no-cache` to make Harper skip the local cache and always call the upstream source, regardless of TTL.
1227
+
1228
+ ```bash
1229
+ curl -i 'http://localhost:9926/JokeCache/1' \
1230
+ -H 'Cache-Control: no-cache'
1231
+ ```
1232
+
1233
+ 6. **Invalidate a cache entry on demand**: Remove `@export` from the schema type, then export a class of the same name in `resources.js` that extends the table and implements a `post` handler calling `this.invalidate(target)`.
1234
+
1235
+ ```graphql
1236
+ type JokeCache @table(expiration: 60) {
1237
+ id: ID @primaryKey
1238
+ setup: String
1239
+ punchline: String
1240
+ }
1241
+ ```
1242
+
1243
+ ```javascript
1244
+ export class JokeCache extends tables.JokeCache {
1245
+ static async post(target, data) {
1246
+ const body = await data;
1247
+ if (body?.action === 'invalidate') {
1248
+ this.invalidate(target);
1249
+ return { status: 200, data: { message: 'invalidated' } };
1250
+ }
1251
+ }
1252
+ }
1253
+ ```
1254
+
1255
+ Trigger invalidation with a `POST`:
1256
+
1257
+ ```bash
1258
+ curl -X POST 'http://localhost:9926/JokeCache/1' \
1259
+ -H 'Content-Type: application/json' \
1260
+ -d '{"action": "invalidate"}'
1261
+ ```
1262
+
1263
+ The next `GET /JokeCache/1` will fetch fresh data from the upstream source regardless of TTL.
865
1264
 
866
1265
  #### Examples
867
1266
 
868
- ##### Schema Configuration
1267
+ Complete `schema.graphql` and `resources.js` for a cached external API with on-demand invalidation:
869
1268
 
870
1269
  ```graphql
871
- type MyCache @table(expiration: 3600) @export {
1270
+ type JokeCache @table(expiration: 60) {
872
1271
  id: ID @primaryKey
1272
+ setup: String
1273
+ punchline: String
873
1274
  }
874
1275
  ```
875
1276
 
876
- ##### Resource Implementation
877
-
878
- ```js
879
- import { Resource, tables } from 'harper';
1277
+ ```javascript
1278
+ // resources.js
880
1279
 
881
- export class ThirdPartyAPI extends Resource {
1280
+ const jokeAPI = {
882
1281
  async get() {
883
1282
  const id = this.getId();
884
- const response = await fetch(`https://api.example.com/items/${id}`);
885
- if (!response.ok) {
886
- throw new Error('Source fetch failed');
1283
+ const response = await fetch(`https://official-joke-api.appspot.com/jokes/${id}`);
1284
+ return response.json();
1285
+ },
1286
+ };
1287
+
1288
+ tables.JokeCache.sourcedFrom(jokeAPI);
1289
+
1290
+ export class JokeCache extends tables.JokeCache {
1291
+ static async post(target, data) {
1292
+ const body = await data;
1293
+ if (body?.action === 'invalidate') {
1294
+ this.invalidate(target);
1295
+ return { status: 200, data: { message: 'invalidated' } };
887
1296
  }
888
- return await response.json();
889
1297
  }
890
1298
  }
1299
+ ```
1300
+
1301
+ First request — cache miss, upstream is called, `200` returned:
891
1302
 
892
- // Attach source to table
893
- tables.MyCache.sourcedFrom(ThirdPartyAPI);
1303
+ ```bash
1304
+ curl -i 'http://localhost:9926/JokeCache/1'
1305
+ ```
1306
+
1307
+ Second request with ETag — cache hit, `304 Not Modified`:
1308
+
1309
+ ```bash
1310
+ curl -i 'http://localhost:9926/JokeCache/1' \
1311
+ -H 'If-None-Match: "abCDefGHij"'
894
1312
  ```
895
1313
 
1314
+ #### Notes
1315
+
1316
+ - `expiration` is measured in seconds. Harper also supports separate `eviction` and `scanInterval` arguments on `@table` for fine-grained control over physical record removal.
1317
+ - The `@export` directive on the schema type is not required when you export a Resource class of the same name from `resources.js` — the class export serves as the endpoint registration. See [custom-resources.md](custom-resources.md) for details on building Resource classes.
1318
+ - Harper's REST layer automatically exposes `@export`-ed tables and Resource classes as HTTP endpoints. See [automatic-apis.md](automatic-apis.md) for how endpoints are structured and named.
1319
+ - ETag values include their double quotes as part of the value — include them verbatim when passing the value in `If-None-Match`.
1320
+ - `sourcedFrom` must be called after the table reference (`tables.JokeCache`) is available, which is guaranteed when the call is at the top level of `resources.js`.
1321
+
896
1322
  ## 4. Infrastructure & Ops
897
1323
 
898
1324
  ### 4.1 Deploying to Harper Fabric
899
1325
 
900
- Instructions for the agent to follow when deploying to Harper Fabric.
1326
+ Instructions for the agent to follow when deploying a Harper application to the Harper Fabric cloud using the Harper CLI.
901
1327
 
902
1328
  #### When to Use
903
1329
 
904
- Use this skill when you are ready to move your Harper application from local development to a cloud-hosted environment.
1330
+ Apply this rule when deploying a Harper application to a remote Harper instance or Harper Fabric cluster. This covers interactive deployments, CI/CD pipelines, and any scenario where the agent must push a local or remote package to a target environment.
905
1331
 
906
1332
  #### How It Works
907
1333
 
908
- 1. **Sign up**: Follow the [creating-a-fabric-account-and-cluster](creating-a-fabric-account-and-cluster.md) rule to create a Harper Fabric account, organization, and cluster.
909
- 2. **Configure Environment**: Add your cluster credentials and cluster application URL to `.env`:
1334
+ 1. **Authenticate with the remote target**: Run `harper login` once to store an authentication token. The CLI writes `HARPER_CLI_TARGET` to a local `.env` so subsequent commands do not need credentials repeated. Find the **Application URL** on the cluster's **Config → Overview** page (see [creating-a-fabric-account-and-cluster.md](creating-a-fabric-account-and-cluster.md)).
1335
+
910
1336
  ```bash
911
- CLI_TARGET_USERNAME='YOUR_CLUSTER_USERNAME'
912
- CLI_TARGET_PASSWORD='YOUR_CLUSTER_PASSWORD'
913
- CLI_TARGET='YOUR_CLUSTER_URL'
1337
+ harper login <Application URL>
1338
+ # Provide cluster username and password when prompted
914
1339
  ```
915
- 3. **Deploy From Local Environment**: Run `npm run deploy`.
916
- 4. **Set up CI/CD**: Configure `.github/workflows/deploy.yaml` and set repository secrets for automated deployments.
917
1340
 
918
- #### Manual Setup for Existing Apps
1341
+ 2. **Deploy the application**: Run `harper deploy` with the required parameters. After logging in, no credentials are needed inline.
919
1342
 
920
- If your application was not created with `npm create harper`, you'll need to manually configure the deployment scripts and CI/CD workflow.
1343
+ ```bash
1344
+ harper deploy \
1345
+ project=<name> \
1346
+ package=<package> \
1347
+ target=<remote> \
1348
+ restart=true \
1349
+ replicated=true
1350
+ ```
921
1351
 
922
- ##### 1. Update `package.json`
1352
+ 3. **Choose a package source**: Set the `package` parameter to any valid npm dependency value, or omit it to package and deploy the current local directory.
923
1353
 
924
- Add the following scripts and dependencies to your `package.json`:
1354
+ | Value | Effect |
1355
+ | ---------------------------------------------------- | ------------------------------------------------ |
1356
+ | _(omitted)_ | Packages and deploys the current local directory |
1357
+ | `"@harperdb/status-check"` | npm package |
1358
+ | `"HarperDB/status-check"` | GitHub repo (short form) |
1359
+ | `"https://github.com/HarperDB/status-check"` | GitHub repo (full URL) |
1360
+ | `"git+ssh://git@github.com:HarperDB/secret-app.git"` | Private repo via SSH |
1361
+ | `"https://example.com/application.tar.gz"` | Remote tarball |
925
1362
 
926
- ```json
927
- {
928
- "scripts": {
929
- "deploy": "dotenv -- npm run deploy:component",
930
- "deploy:component": "harper deploy_component . restart=rolling replicated=true"
931
- },
932
- "devDependencies": {
933
- "dotenv-cli": "^11.0.0",
934
- "harper": "^5.0.0"
935
- }
936
- }
937
- ```
1363
+ For git tags, use the `semver` directive for reliable versioning:
938
1364
 
939
- ###### Why split the scripts?
1365
+ ```
1366
+ HarperDB/application-template#semver:v1.0.0
1367
+ ```
940
1368
 
941
- The `deploy` script is separated from `deploy:component` to ensure environment variables from your `.env` file are properly loaded and passed to the Harper CLI.
1369
+ 4. **Authenticate for CI/CD pipelines**: Use environment variables instead of interactive login. Set credentials before running `harper deploy`.
942
1370
 
943
- - `deploy`: Uses `dotenv-cli` to load environment variables (like `CLI_TARGET`, `CLI_TARGET_USERNAME`, and `CLI_TARGET_PASSWORD`) before executing the next command.
944
- - `deploy:component`: The actual command that performs the deployment.
1371
+ ```bash
1372
+ export HARPER_CLI_USERNAME=<username>
1373
+ export HARPER_CLI_PASSWORD=<password>
1374
+ harper deploy \
1375
+ project=<name> \
1376
+ package=<package> \
1377
+ target=<remote> \
1378
+ restart=true \
1379
+ replicated=true
1380
+ ```
945
1381
 
946
- By using `dotenv -- npm run deploy:component`, the environment variables are correctly set in the shell session before `harper deploy_component` is called, allowing it to authenticate with your cluster.
1382
+ 5. **Register SSH keys for private repos**: Before deploying from an SSH-based private repository, use the Add SSH Key operation to register the key with the remote instance.
947
1383
 
948
- ##### 2. Configure GitHub Actions
1384
+ #### Examples
949
1385
 
950
- Create a `.github/workflows/deploy.yaml` file with the following content:
1386
+ **Interactive login then deploy (recommended):**
951
1387
 
952
- ```yaml
953
- name: Deploy to Harper Fabric
954
- on:
955
- workflow_dispatch:
956
- # push:
957
- # branches:
958
- # - main
959
- concurrency:
960
- group: main
961
- cancel-in-progress: false
962
- jobs:
963
- deploy:
964
- runs-on: ubuntu-latest
965
- steps:
966
- - name: Checkout code
967
- uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
968
- with:
969
- fetch-depth: 0
970
- fetch-tags: true
971
- - name: Set up Node.js
972
- uses: actions/setup-node@395ad3262231945c25e8478fd5baf05154b1d79f # v6.1.0
973
- with:
974
- cache: 'npm'
975
- node-version-file: '.nvmrc'
976
- - name: Install dependencies
977
- run: npm ci
978
- - name: Run unit tests
979
- run: npm test
980
- - name: Run lint
981
- run: npm run lint
982
- - name: Deploy
983
- run: npm run deploy
984
- env:
985
- CLI_TARGET: ${{ secrets.CLI_TARGET }}
986
- CLI_TARGET_USERNAME: ${{ secrets.CLI_TARGET_USERNAME }}
987
- CLI_TARGET_PASSWORD: ${{ secrets.CLI_TARGET_PASSWORD }}
1388
+ ```bash
1389
+ # Log in once
1390
+ harper login <remote>
1391
+ # Provide your username and password when prompted
1392
+
1393
+ # Subsequently deploy without credentials
1394
+ harper deploy \
1395
+ project=<name> \
1396
+ package=<package> \
1397
+ target=<remote> \
1398
+ restart=true \
1399
+ replicated=true
988
1400
  ```
989
1401
 
990
- Be sure to set the following repository secrets in your GitHub repository's /settings/secrets/actions:
1402
+ **Deploy with inline credentials (not recommended for production):**
991
1403
 
992
- - `CLI_TARGET`
993
- - `CLI_TARGET_USERNAME`
994
- - `CLI_TARGET_PASSWORD`
1404
+ ```bash
1405
+ harper deploy \
1406
+ project=<name> \
1407
+ package=<package> \
1408
+ username=<username> \
1409
+ password=<password> \
1410
+ target=<remote> \
1411
+ restart=true \
1412
+ replicated=true
1413
+ ```
1414
+
1415
+ **Deploy a specific GitHub release by semver tag:**
1416
+
1417
+ ```bash
1418
+ harper deploy \
1419
+ project=my-app \
1420
+ package="HarperDB/application-template#semver:v1.0.0" \
1421
+ target=<remote> \
1422
+ restart=true \
1423
+ replicated=true
1424
+ ```
1425
+
1426
+ #### Notes
1427
+
1428
+ - Always prefer `harper login` for interactive use and environment variables (`HARPER_CLI_USERNAME`, `HARPER_CLI_PASSWORD`) for CI/CD. Avoid inline `username`/`password` parameters in production.
1429
+ - Omitting `package` causes the CLI to package the current local directory. Specifying a local file path creates a symlink, so changes are picked up between restarts without redeploying.
1430
+ - Harper generates a `package.json` from component configurations and resolves dependencies using a form of `npm install`.
1431
+ - For SSH-based private repos, register keys with the Add SSH Key operation before deploying.
995
1432
 
996
1433
  ### 4.2 Creating a Harper Fabric Account and Cluster
997
1434
 
@@ -1122,90 +1559,158 @@ Use this skill when you need to serve a frontend (HTML, CSS, JS, or a React app)
1122
1559
  ```
1123
1560
  Then in production, the "Static Plugin" option will performantly and securely serve your assets. `npm create harper@latest` scaffolds all of this for you.
1124
1561
 
1125
- ### 4.5 Logging Best Practices
1562
+ ### 4.5 Harper Logging
1126
1563
 
1127
- Harper provides a robust logging system that captures standard output and offers a granular, tagged logging interface for both local and deployed environments.
1564
+ Instructions for the agent to follow when implementing logging in Harper applications, including direct logger usage, tagged loggers, and console capture behavior.
1128
1565
 
1129
- #### Standard Console Logging
1566
+ #### When to Use
1130
1567
 
1131
- The simplest way to log in Harper is using standard JavaScript console methods. `console.log()`, `console.warn()`, `console.error()`, and `console.trace()` are automatically captured by Harper and can be viewed in the logs.
1568
+ Apply this rule when writing any JavaScript component, plugin, or resource that needs to emit structured log entries, filter logs by component, or capture existing `console.log` output into Harper's log system. Use it whenever you need to understand log levels, log entry format, or the `logger` global API.
1132
1569
 
1133
- - `console.log(...)`: Captured as `stdout` level in Harper logs.
1134
- - `console.warn(...)`: Captured as `stderr` level in Harper logs.
1135
- - `console.error(...)`: Captured as `stderr` level in Harper logs.
1136
- - `console.trace(...)`: Captured as `stdout` level in Harper logs (includes stack trace).
1570
+ #### How It Works
1137
1571
 
1138
- #### Harper Logger
1572
+ 1. **Use the `logger` global directly** — `logger` is available in all JavaScript components without any imports. Call the method matching the desired severity level:
1139
1573
 
1140
- For more granularity and better organization, use Harper's built-in `logger`. You can use the global `logger` object or import it from the `harper` package.
1574
+ ```javascript
1575
+ logger.trace('detailed trace message');
1576
+ logger.debug('debug info', { someContext: 'value' });
1577
+ logger.info('informational message');
1578
+ logger.warn('potential issue');
1579
+ logger.error('error occurred', error);
1580
+ logger.fatal('fatal error');
1581
+ logger.notify('server is ready');
1582
+ ```
1141
1583
 
1142
- ##### Log Levels
1584
+ Only entries at or above the configured `logging.level` (or `logging.external.level`) are written to `hdb.log`.
1143
1585
 
1144
- The Harper `logger` supports the following levels (ordered by increasing severity):
1586
+ 2. **Create a tagged logger with `withTag(`** — Call `logger.withTag(tag)` once per module or class to get a `TaggedLogger` scoped to that tag. This prefixes every log entry with the tag, making log output filterable by component.
1145
1587
 
1146
- - `trace`
1147
- - `debug`
1148
- - `info`
1149
- - `warn`
1150
- - `error`
1151
- - `fatal`
1152
- - `notify`
1588
+ ```javascript
1589
+ const log = logger.withTag('my-resource');
1590
+ ```
1591
+
1592
+ Because `TaggedLogger` methods for disabled levels are `null`, always use optional chaining (`?.`) when calling them:
1153
1593
 
1154
- ##### Usage
1594
+ ```javascript
1595
+ log.debug?.('Fetching record', { id });
1596
+ log.warn?.('Record not found', { id });
1597
+ log.error?.('Failed to update record', err);
1598
+ ```
1155
1599
 
1156
- ```typescript
1157
- import { logger, loggerWithTag } from 'harper';
1600
+ `TaggedLogger` does not have a `withTag()` method.
1158
1601
 
1159
- // Basic logging
1160
- logger.info('Application started');
1161
- logger.error('An error occurred', error);
1602
+ 3. **Understand the interface contracts** — `MainLogger` always has all methods defined:
1162
1603
 
1163
- // Tagged logging for better filtering (Namespacing)
1164
- const authLogger = loggerWithTag('auth');
1165
- authLogger.debug('User login attempt', { userId: '123' });
1166
- ```
1604
+ ```typescript
1605
+ interface MainLogger {
1606
+ trace(...messages: any[]): void;
1607
+ debug(...messages: any[]): void;
1608
+ info(...messages: any[]): void;
1609
+ warn(...messages: any[]): void;
1610
+ error(...messages: any[]): void;
1611
+ fatal(...messages: any[]): void;
1612
+ notify(...messages: any[]): void;
1613
+ withTag(tag: string): TaggedLogger;
1614
+ }
1615
+ ```
1616
+
1617
+ `TaggedLogger` methods may be `null`:
1618
+
1619
+ ```typescript
1620
+ interface TaggedLogger {
1621
+ trace: ((...messages: any[]) => void) | null;
1622
+ debug: ((...messages: any[]) => void) | null;
1623
+ info: ((...messages: any[]) => void) | null;
1624
+ warn: ((...messages: any[]) => void) | null;
1625
+ error: ((...messages: any[]) => void) | null;
1626
+ fatal: ((...messages: any[]) => void) | null;
1627
+ notify: ((...messages: any[]) => void) | null;
1628
+ }
1629
+ ```
1167
1630
 
1168
- Using `loggerWithTag` is highly recommended for grouping related logs, making them much easier to filter and analyze in the Harper Studio or via the API.
1631
+ 4. **Know the log levels** From least to most severe:
1169
1632
 
1170
- #### Programmatic Log Retrieval
1633
+ | Level | Description |
1634
+ | -------- | -------------------------------------------------------------------- |
1635
+ | `trace` | Highly detailed internal execution tracing. |
1636
+ | `debug` | Diagnostic information useful during development. |
1637
+ | `info` | General operational events. |
1638
+ | `warn` | Potential issues that don't prevent normal operation. |
1639
+ | `error` | Errors that affect specific operations. |
1640
+ | `fatal` | Critical errors causing process termination. |
1641
+ | `notify` | Important operational milestones. Always logged regardless of level. |
1171
1642
 
1172
- You can programmatically read logs from a deployed Harper instance using the `read_log` operation. This is useful for building custom monitoring tools or debugging dashboards.
1643
+ The default log level is `warn`. Setting a level includes that level and all more-severe levels.
1173
1644
 
1174
- ##### `read_log` Operation
1645
+ 5. **Enable console capture when porting existing code** — When `logging.console: true` is set, writes via `console.log`, `console.warn`, `console.error`, etc. are appended verbatim to `hdb.log`. Captured lines do **not** pass through `logger`'s level filter. Prefer `logger` directly in production code so that level filtering and tagging apply. Console capture is intended as a convenience for porting existing code and for debugging.
1646
+
1647
+ 6. **Know where logs are written** — All standard log output goes to `<ROOTPATH>/log/hdb.log` (default: `~/hdb/log/hdb.log`). To also log to `stdout`/`stderr`, set `logging.stdStreams: true`.
1648
+
1649
+ #### Examples
1175
1650
 
1176
- The `read_log` operation is a POST request to the Harper instance.
1651
+ ##### Basic logging in a resource
1177
1652
 
1178
- **Example Request:**
1653
+ ```javascript
1654
+ export class MyResource extends Resource {
1655
+ async get(id) {
1656
+ logger.debug('Fetching record', { id });
1657
+ const record = await super.get(id);
1658
+ if (!record) {
1659
+ logger.warn('Record not found', { id });
1660
+ }
1661
+ return record;
1662
+ }
1179
1663
 
1180
- ```json
1181
- {
1182
- "operation": "read_log",
1183
- "limit": 100,
1184
- "start": 0,
1185
- "level": "error",
1186
- "order": "desc",
1187
- "from": "2024-01-01T00:00:00.000Z",
1188
- "until": "2024-01-02T00:00:00.000Z"
1664
+ async put(record) {
1665
+ logger.info('Updating record', { id: record.id });
1666
+ try {
1667
+ return await super.put(record);
1668
+ } catch (err) {
1669
+ logger.error('Failed to update record', err);
1670
+ throw err;
1671
+ }
1672
+ }
1189
1673
  }
1190
1674
  ```
1191
1675
 
1192
- ##### Parameters
1676
+ ##### Tagged logging with `withTag()`
1193
1677
 
1194
- - `limit`: Number of log entries to return.
1195
- - `start`: Offset for pagination.
1196
- - `level`: Filter by log level (`info`, `error`, `warn`, `debug`, `trace`, `notify`, `fatal`, `stdout`, `stderr`).
1197
- - `from`: ISO 8601 timestamp to start reading from.
1198
- - `until`: ISO 8601 timestamp to stop reading at.
1199
- - `order`: Sort order, either `asc` or `desc`.
1200
- - `replicated`: (Boolean) Include logs from replicated nodes in a cluster.
1678
+ ```javascript
1679
+ const log = logger.withTag('my-resource');
1680
+
1681
+ export class MyResource extends Resource {
1682
+ async get(id) {
1683
+ log.debug?.('Fetching record', { id });
1684
+ const record = await super.get(id);
1685
+ if (!record) {
1686
+ log.warn?.('Record not found', { id });
1687
+ }
1688
+ return record;
1689
+ }
1201
1690
 
1202
- ##### Log Entry Structure
1691
+ async put(record) {
1692
+ log.info?.('Updating record', { id: record.id });
1693
+ try {
1694
+ return await super.put(record);
1695
+ } catch (err) {
1696
+ log.error?.('Failed to update record', err);
1697
+ throw err;
1698
+ }
1699
+ }
1700
+ }
1701
+ ```
1203
1702
 
1204
- Each log entry returned by `read_log` typically includes:
1703
+ Tagged entries appear in `hdb.log` with the tag in the header:
1704
+
1705
+ ```
1706
+ 2023-03-09T14:25:05.269Z [info] [my-resource]: Updating record
1707
+ ```
1708
+
1709
+ #### Notes
1205
1710
 
1206
- - `level`: The severity level of the log.
1207
- - `timestamp`: When the log was recorded.
1208
- - `thread`: The execution thread.
1209
- - `tags`: An array of tags (e.g., from `loggerWithTag`).
1210
- - `node`: The node name in a Harper cluster.
1211
- - `message`: The logged content.
1711
+ - All log output is written to `<ROOTPATH>/log/hdb.log`. The `logger` global writes to this file at the configured `logging.external` level.
1712
+ - Log entry format for `logger`: `<timestamp> [<level>] [<thread>/<id>]: <message>`
1713
+ - Log entry format for `TaggedLogger`: `<timestamp> [<level>] [<tag>]: <message>`
1714
+ - `console.log` output is only forwarded to `hdb.log` when `logging.console: true` is explicitly set; it is not forwarded by default.
1715
+ - When logging to standard streams, run Harper in the foreground (`harper`, not `harper start`).
1716
+ - `TaggedLogger` is bound to the configured log level at creation time — always use `?.` on its methods.