@harperfast/template-vue-ts-studio 1.6.3 → 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.
Files changed (26) hide show
  1. package/.agents/skills/harper-best-practices/AGENTS.md +1686 -361
  2. package/.agents/skills/harper-best-practices/SKILL.md +25 -20
  3. package/.agents/skills/harper-best-practices/rules/adding-tables-with-schemas.md +2 -0
  4. package/.agents/skills/harper-best-practices/rules/automatic-apis.md +141 -18
  5. package/.agents/skills/harper-best-practices/rules/caching.md +134 -21
  6. package/.agents/skills/harper-best-practices/rules/checking-authentication.md +139 -148
  7. package/.agents/skills/harper-best-practices/rules/creating-a-fabric-account-and-cluster.md +2 -0
  8. package/.agents/skills/harper-best-practices/rules/creating-harper-apps.md +7 -2
  9. package/.agents/skills/harper-best-practices/rules/custom-resources.md +2 -0
  10. package/.agents/skills/harper-best-practices/rules/defining-relationships.md +2 -0
  11. package/.agents/skills/harper-best-practices/rules/deploying-to-harper-fabric.md +97 -77
  12. package/.agents/skills/harper-best-practices/rules/extending-tables.md +2 -0
  13. package/.agents/skills/harper-best-practices/rules/handling-binary-data.md +2 -0
  14. package/.agents/skills/harper-best-practices/rules/load-env.md +100 -0
  15. package/.agents/skills/harper-best-practices/rules/logging.md +154 -77
  16. package/.agents/skills/harper-best-practices/rules/programmatic-table-requests.md +32 -26
  17. package/.agents/skills/harper-best-practices/rules/querying-rest-apis.md +190 -15
  18. package/.agents/skills/harper-best-practices/rules/real-time-apps.md +80 -21
  19. package/.agents/skills/harper-best-practices/rules/schema-design-tooling.md +133 -40
  20. package/.agents/skills/harper-best-practices/rules/serving-web-content.md +2 -0
  21. package/.agents/skills/harper-best-practices/rules/typescript-type-stripping.md +48 -16
  22. package/.agents/skills/harper-best-practices/rules/using-blob-datatype.md +2 -0
  23. package/.agents/skills/harper-best-practices/rules/vector-indexing.md +85 -120
  24. package/.agents/skills/harper-best-practices/rules.manifest.yaml +302 -0
  25. package/package.json +1 -1
  26. package/skills-lock.json +1 -1
@@ -2,41 +2,8 @@
2
2
 
3
3
  Guidelines for building scalable, secure, and performant applications on Harper. These practices cover everything from initial schema design to advanced deployment strategies.
4
4
 
5
- ---
6
-
7
- ## Table of Contents
8
-
9
- 1. [Schema & Data Design](#1-schema--data-design) — **HIGH**
10
- - 1.1 [Adding Tables with Schemas](#11-adding-tables-with-schemas)
11
- - 1.2 [Schema Design & Tooling](#12-schema-design--tooling)
12
- - 1.3 [Defining Relationships](#13-defining-relationships)
13
- - 1.4 [Vector Indexing](#14-vector-indexing)
14
- - 1.5 [Using Blobs](#15-using-blobs)
15
- - 1.6 [Handling Binary Data](#16-handling-binary-data)
16
- 2. [API & Communication](#2-api--communication) — **HIGH**
17
- - 2.1 [Automatic REST APIs](#21-automatic-rest-apis)
18
- - 2.2 [Querying REST APIs](#22-querying-rest-apis)
19
- - 2.3 [Real-time Applications](#23-real-time-applications)
20
- - 2.4 [Checking Authentication](#24-checking-authentication)
21
- 3. [Logic & Extension](#3-logic--extension) — **MEDIUM**
22
- - 3.1 [Custom Resources](#31-custom-resources)
23
- - 3.2 [Extending Table Resources](#32-extending-table-resources)
24
- - 3.3 [Programmatic Table Requests](#33-programmatic-table-requests)
25
- - 3.4 [TypeScript Type Stripping](#34-typescript-type-stripping)
26
- - 3.5 [Caching](#35-caching)
27
- 4. [Infrastructure & Ops](#4-infrastructure--ops) — **MEDIUM**
28
- - 4.1 [Creating Harper Applications](#41-creating-harper-applications)
29
- - 4.2 [Creating a Fabric Account and Cluster](#42-creating-a-fabric-account-and-cluster)
30
- - 4.3 [Deploying to Harper Fabric](#43-deploying-to-harper-fabric)
31
- - 4.4 [Serving Web Content](#44-serving-web-content)
32
- - 4.5 [Logging Best Practices](#45-logging-best-practices)
33
-
34
- ---
35
-
36
5
  ## 1. Schema & Data Design
37
6
 
38
- **Impact: HIGH**
39
-
40
7
  ### 1.1 Adding Tables with Schemas
41
8
 
42
9
  Instructions for the agent to follow when adding tables to a Harper database.
@@ -49,11 +16,21 @@ Use this skill when you need to define new data structures or modify existing on
49
16
 
50
17
  1. **Create Dedicated Schema Files**: Prefer having a dedicated schema `.graphql` file for each table. Check the `config.yaml` file under `graphqlSchema.files` to see how it's configured. It typically accepts wildcards (e.g., `schemas/*.graphql`), but may be configured to point at a single file.
51
18
  2. **Use Directives**: All available directives for defining your schema are defined in `node_modules/harper/schema.graphql`. Common directives include `@table`, `@export`, `@primaryKey`, `@indexed`, and `@relationship`.
52
- 3. **Define Relationships**: Link tables together using the `@relationship` directive.
53
- 4. **Enable Automatic APIs**: If you add `@table @export` to a schema type, Harper automatically sets up REST and WebSocket APIs for basic CRUD operations against that table. **Important**: REST endpoints also require `rest: true` in `config.yaml` — without it, `@export`ed tables will not respond to HTTP requests.
54
- 5. **Consider Table Extensions**: If you are going to extend the table in your resources, then do not `@export` the table from the schema.
55
-
56
- #### Example
19
+ 3. **Define Relationships**: Link tables together using the `@relationship` directive. For more details, see the [Defining Relationships](defining-relationships.md) skill.
20
+ 4. **Enable Automatic APIs**: If you add `@table @export` to a schema type, Harper automatically sets up REST and WebSocket APIs for basic CRUD operations against that table. **Important**: REST endpoints also require `rest: true` in `config.yaml` — without it, `@export`ed tables will not respond to HTTP requests. For a detailed list of available endpoints and how to use them, see the [Automatic REST APIs](automatic-apis.md) skill.
21
+ - `GET /{TableName}`: Describes the schema itself.
22
+ - `GET /{TableName}/`: Lists all records (supports filtering, sorting, and pagination via query parameters). See the [Querying REST APIs](querying-rest-apis.md) skill for details.
23
+ - `GET /{TableName}/{id}`: Retrieves a single record by its ID.
24
+ - `POST /{TableName}/`: Creates a new record.
25
+ - `PUT /{TableName}/{id}`: Updates an existing record.
26
+ - `PATCH /{TableName}/{id}`: Performs a partial update on a record.
27
+ - `DELETE /{TableName}/`: Deletes all records or filtered records.
28
+ - `DELETE /{TableName}/{id}`: Deletes a single record by its ID.
29
+ 5. **Consider Table Extensions**: If you are going to [extend the table](./extending-tables.md) in your resources, then do not `@export` the table from the schema.
30
+
31
+ #### Examples
32
+
33
+ In a hypothetical `schemas/ExamplePerson.graphql`:
57
34
 
58
35
  ```graphql
59
36
  type ExamplePerson @table @export {
@@ -63,82 +40,165 @@ type ExamplePerson @table @export {
63
40
  }
64
41
  ```
65
42
 
66
- ### 1.2 Schema Design & Tooling
43
+ ### 1.2 Schema Design and Tooling
44
+
45
+ Instructions for the agent to follow when designing Harper schemas, applying core directives, and configuring GraphQL tooling.
46
+
47
+ #### When to Use
48
+
49
+ Apply this rule when creating or modifying Harper schema files, configuring `graphqlSchema` in `config.yaml`, or deciding which directives to apply to tables and fields. Use it any time a component needs tables, indexes, primary keys, or exported endpoints defined.
50
+
51
+ #### How It Works
52
+
53
+ 1. **Create a GraphQL schema file** with Harper-specific directives. Name it (e.g., `schema.graphql`) and place it in your component directory.
67
54
 
68
- Harper uses GraphQL schemas to define database tables, relationships, and APIs. To ensure the best development experience for both humans and AI agents, it's important to understand the core directives and configure your project tooling correctly.
55
+ ```graphql
56
+ type Dog @table {
57
+ id: Long @primaryKey
58
+ name: String
59
+ breed: String
60
+ age: Int
61
+ }
69
62
 
70
- #### Core Harper Directives
63
+ type Breed @table {
64
+ id: Long @primaryKey
65
+ name: String @indexed
66
+ }
67
+ ```
71
68
 
72
- Harper extends GraphQL with custom directives that define database behavior. These are typically defined in `node_modules/harper/schema.graphql`. If you don't have access to that file, here is a reference of the most important ones:
69
+ 2. **Register the schema in `config.yaml`** using the `graphqlSchema` plugin key:
73
70
 
74
- ##### Table Definition
71
+ ```yaml
72
+ graphqlSchema:
73
+ files: 'schema.graphql'
74
+ ```
75
75
 
76
- - `@table`: Marks a GraphQL type as a Harper database table.
77
- - `@export`: Automatically generates REST and WebSocket APIs for the table.
78
- - `@table(expiration: Int)`: Configures a time-to-expire for records in the table (useful for caching).
76
+ Both plugins and applications can specify schemas this way.
79
77
 
80
- ##### Attribute Constraints & Indexing
78
+ 3. **Mark every table type with `@table`**. The type name becomes the table name by default. Use optional arguments to override behavior:
81
79
 
82
- - `@primaryKey`: Specifies the unique identifier for the table.
83
- - `@indexed`: Creates a standard index on the field for faster lookups.
84
- - `@indexed(type: "HNSW", distance: "cosine" | "euclidean" | "dot")`: Creates a vector index for similarity search.
80
+ | Argument | Type | Default | Description |
81
+ | -------------- | --------- | ----------------------------- | ------------------------------------------------------------- |
82
+ | `table` | `String` | type name | Override the table name |
83
+ | `database` | `String` | `"data"` | Database to place the table in |
84
+ | `expiration` | `Int` | — | Seconds until a record goes stale |
85
+ | `eviction` | `Int` | `0` | Additional seconds after `expiration` before physical removal |
86
+ | `scanInterval` | `Int` | `(expiration + eviction) / 4` | Seconds between eviction scans |
87
+ | `replicate` | `Boolean` | `true` | Enable replication of this table |
85
88
 
86
- ##### Relationships
89
+ 4. **Designate a primary key on every table** using `@primaryKey`. Primary keys must be unique; duplicate-key inserts are rejected. If no key is provided on insert, Harper auto-generates one:
90
+ - `String` or `ID` → UUID string
91
+ - `Int`, `Long`, or `Any` → auto-incrementing integer
87
92
 
88
- - `@relationship(from: String)`: Defines a relationship to another table. `from` specifies the local field holding the foreign key.
93
+ Prefer `Long` or `Any` for auto-generated numeric keys; `Int` is 32-bit and may be insufficient for large tables.
89
94
 
90
- ##### Authentication & Authorization
95
+ 5. **Index fields that need fast querying** with `@indexed`. This is required for filtering by that attribute in REST queries, SQL, or NoSQL operations. If the field value is an array, each element is individually indexed.
91
96
 
92
- - `@auth(role: String)`: Restricts access to a table or field based on user roles.
97
+ ```graphql
98
+ type Product @table {
99
+ id: Long @primaryKey
100
+ category: String @indexed
101
+ price: Float @indexed
102
+ }
103
+ ```
93
104
 
94
- #### Configuring GraphQL Tooling
105
+ 6. **Expose a table as an external resource endpoint** with `@export`. This makes the table accessible via REST, MQTT, and other interfaces. The optional `name` parameter sets the URL path segment; without it, the type name is used.
95
106
 
96
- To get the best IDE support (autocompletion, validation) and to help AI agents understand your schema context, you should create a `graphql.config.yml` file in your project root.
107
+ ```graphql
108
+ type MyTable @table @export(name: "my-table") {
109
+ id: Long @primaryKey
110
+ }
111
+ ```
97
112
 
98
- This file tells GraphQL tools where to find Harper's built-in types and directives alongside your own schema files.
113
+ 7. **Restrict extra properties** with `@sealed` when records must not include attributes beyond those declared. By default, Harper allows additional properties.
99
114
 
100
- ##### Creating `graphql.config.yml`
115
+ ```graphql
116
+ type StrictRecord @table @sealed {
117
+ id: Long @primaryKey
118
+ name: String
119
+ }
120
+ ```
101
121
 
102
- Create a file named `graphql.config.yml` in your project root with the following content:
122
+ 8. **Configure expiration, eviction, and scan behavior** together when building caching tables. These three arguments control the full record lifecycle:
123
+ - `expiration` — record becomes stale; next request triggers a source fetch
124
+ - `eviction` — additional time after `expiration` before physical removal
125
+ - `scanInterval` — how often Harper scans for records to evict; clock-aligned, not startup-aligned
103
126
 
104
- ```yaml
105
- schema:
106
- - 'node_modules/harper/schema.graphql'
107
- - 'schema.graphql'
108
- - 'schemas/*.graphql'
127
+ #### Examples
128
+
129
+ **Caching table with tuned expiration:**
130
+
131
+ ```graphql
132
+ # Expire after 5 minutes, evict after 1 hour, scan every 10 minutes
133
+ type WeatherCache @table(expiration: 300, eviction: 3300, scanInterval: 600) {
134
+ id: ID @primaryKey
135
+ temperature: Float
136
+ }
137
+ ```
138
+
139
+ **Table in a named database with expiration and an indexed field:**
140
+
141
+ ```graphql
142
+ type Event @table(database: "analytics", expiration: 86400) {
143
+ id: Long @primaryKey
144
+ name: String @indexed
145
+ }
146
+ ```
147
+
148
+ **Session cache with auto-expiry:**
149
+
150
+ ```graphql
151
+ type Session @table(expiration: 3600) {
152
+ id: Long @primaryKey
153
+ userId: String
154
+ }
109
155
  ```
110
156
 
111
- ##### Why this is important:
157
+ **Table with audit timestamps:**
112
158
 
113
- 1. **Shared Directives**: It includes `@table`, `@primaryKey`, etc., so they aren't marked as "unknown directives".
114
- 2. **Context for Agents**: When an agent reads your project, seeing this config helps it locate the core Harper definitions, leading to more accurate code generation.
115
- 3. **Consistency**: The `npm create harper@latest` command includes this by default. Manually adding it to existing projects ensures they follow the same standards.
159
+ ```graphql
160
+ type Order @table @export(name: "orders") {
161
+ id: Long @primaryKey
162
+ createdAt: Long @createdTime
163
+ updatedAt: Long @updatedTime
164
+ status: String @indexed
165
+ }
166
+ ```
116
167
 
117
- #### Example Project Structure
168
+ **Overriding the table name and disabling replication:**
118
169
 
119
- A typical Harper project with proper schema tooling:
170
+ ```graphql
171
+ type Product @table(table: "products") {
172
+ id: Long @primaryKey
173
+ name: String
174
+ }
120
175
 
121
- ```text
122
- my-harper-app/
123
- ├── config.yaml
124
- ├── graphql.config.yml
125
- ├── package.json
126
- ├── schema.graphql
127
- └── resources.js
176
+ type LocalRecord @table(replicate: false) {
177
+ id: Long @primaryKey
178
+ value: String
179
+ }
128
180
  ```
129
181
 
182
+ #### Notes
183
+
184
+ - Use unique `database` names in plugins and applications to avoid table naming collisions, since all tables default to the `"data"` database.
185
+ - Eviction removes non-indexed record data but does **not** remove a record from its secondary indexes. Indexes remain functional for evicted records; Harper fetches the full record from the source on demand when a query matches an evicted record.
186
+ - `scanInterval` is clock-aligned to the server's local timezone. The server's startup time does not affect when eviction runs.
187
+ - If replication is disabled on a table and later re-enabled, it will not catch up on writes made while replication was disabled.
188
+ - Null values are indexed by `@indexed` fields, enabling queries such as `GET /Product/?category=null`.
189
+
130
190
  ### 1.3 Defining Relationships
131
191
 
132
- Using the `@relationship` directive to link tables.
192
+ Instructions for the agent to follow when defining relationships between Harper tables.
133
193
 
134
194
  #### When to Use
135
195
 
136
- Use this when you have two or more tables that need to be logically linked (e.g., a "Product" table and a "Category" table).
196
+ Use this skill when you need to link data across different tables, enabling automatic joins and efficient related-data fetching via REST APIs.
137
197
 
138
198
  #### How It Works
139
199
 
140
200
  1. **Identify the Relationship Type**: Determine if it's one-to-one, many-to-one, or one-to-many.
141
- 2. **Apply the `@relationship` Directive**: In your GraphQL schema, use the `@relationship` directive on the field that links to another table.
201
+ 2. **Use the `@relationship` Directive**: Apply it to a field in your GraphQL schema.
142
202
  - **Many-to-One (Current table holds FK)**: Use `from`.
143
203
  ```graphql
144
204
  type Book @table @export {
@@ -153,439 +213,1704 @@ Use this when you have two or more tables that need to be logically linked (e.g.
153
213
  }
154
214
  ```
155
215
  3. **Query with Relationships**: Use dot syntax in REST API calls for filtering or the `select()` operator for including related data.
156
-
157
- #### Example
158
-
159
- ```graphql
160
- type Product @table @export {
161
- id: ID @primaryKey
162
- name: String
163
- categoryId: ID
164
- category: Category @relationship(from: "categoryId")
165
- }
166
-
167
- type Category @table @export {
168
- id: ID @primaryKey
169
- name: String
170
- products: [Product] @relationship(to: "categoryId")
171
- }
172
- ```
216
+ - Example Filter: `GET /Book/?author.name=Harper`
217
+ - Example Select: `GET /Author/?select(name,books(title))`
173
218
 
174
219
  ### 1.4 Vector Indexing
175
220
 
176
- How to define and use vector indexes for efficient similarity search.
221
+ Instructions for the agent to follow when enabling and querying vector indexes for similarity search in Harper using the HNSW algorithm.
177
222
 
178
223
  #### When to Use
179
224
 
180
- Use this when you need to perform similarity searches on high-dimensional data, such as image embeddings, text embeddings, or any other numeric vectors.
225
+ Apply this rule when adding a vector index to a Harper table schema or writing similarity search queries against high-dimensional vector fields. Use it whenever you need approximate nearest-neighbor search, distance-threshold filtering, or distance-scored results.
181
226
 
182
227
  #### How It Works
183
228
 
184
- 1. **Define the Vector Field**: In your GraphQL schema, define a field with a list of floats (e.g., `[Float]`).
185
- 2. **Apply the `@indexed` Directive**: Use the `@indexed` directive on the vector field and specify the index type as `HNSW`.
186
- 3. **Configure the Index (Optional)**: You can provide additional configuration for the vector index, such as the distance metric (e.g., `cosine`, `euclidean`).
187
- 4. **Querying**: Use the `vector` operator in your REST or programmatic requests to perform similarity searches.
229
+ 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.
230
+
231
+ ```graphql
232
+ type Document @table {
233
+ id: Long @primaryKey
234
+ textEmbeddings: [Float] @indexed(type: "HNSW")
235
+ }
236
+ ```
237
+
238
+ 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.
239
+
240
+ ```javascript
241
+ let results = Document.search({
242
+ sort: { attribute: 'textEmbeddings', target: searchVector },
243
+ limit: 5,
244
+ });
245
+ ```
246
+
247
+ 3. **Combine with filter conditions**: Add a `conditions` array alongside `sort` to pre-filter records before ranking by similarity.
248
+
249
+ ```javascript
250
+ let results = Document.search({
251
+ conditions: [{ attribute: 'price', comparator: 'lt', value: 50 }],
252
+ sort: { attribute: 'textEmbeddings', target: searchVector },
253
+ limit: 5,
254
+ });
255
+ ```
256
+
257
+ 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`.
258
+
259
+ ```javascript
260
+ let results = Document.search({
261
+ conditions: {
262
+ attribute: 'textEmbeddings',
263
+ comparator: 'lt',
264
+ value: 0.1,
265
+ target: searchVector,
266
+ },
267
+ });
268
+ ```
269
+
270
+ 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.
271
+
272
+ ```javascript
273
+ let results = Document.search({
274
+ select: ['name', '$distance'],
275
+ sort: { attribute: 'textEmbeddings', target: searchVector },
276
+ limit: 5,
277
+ });
278
+ ```
279
+
280
+ 6. **Tune HNSW parameters**: Pass additional parameters to `@indexed(type: "HNSW", ...)` to control index quality and performance.
281
+
282
+ | Parameter | Default | Description |
283
+ | ---------------------- | ----------------- | --------------------------------------------------------------------------------------------------- |
284
+ | `distance` | `"cosine"` | Distance function: `"euclidean"` or `"cosine"` (negative cosine similarity) |
285
+ | `efConstruction` | `100` | Max nodes explored during index construction. Higher = better recall, lower = better performance |
286
+ | `M` | `16` | Preferred connections per graph layer. Higher = more space, better recall for high-dimensional data |
287
+ | `optimizeRouting` | `0.5` | Heuristic aggressiveness for omitting redundant connections (0 = off, 1 = most aggressive) |
288
+ | `mL` | computed from `M` | Normalization factor for level generation |
289
+ | `efSearchConstruction` | `50` | Max nodes explored during search |
290
+
291
+ #### Examples
188
292
 
189
- #### Example
293
+ **Schema with custom HNSW parameters:**
190
294
 
191
295
  ```graphql
192
- type Document @table @export {
193
- id: ID @primaryKey
194
- content: String
195
- embedding: [Float] @indexed(type: "HNSW")
296
+ type Document @table {
297
+ id: Long @primaryKey
298
+ textEmbeddings: [Float]
299
+ @indexed(type: "HNSW", distance: "euclidean", optimizeRouting: 0, efSearchConstruction: 100)
196
300
  }
197
301
  ```
198
302
 
199
- ### 1.5 Using Blobs
303
+ **Nearest-neighbor search with distance score:**
304
+
305
+ ```javascript
306
+ let results = Document.search({
307
+ select: ['name', '$distance'],
308
+ sort: { attribute: 'textEmbeddings', target: searchVector },
309
+ limit: 5,
310
+ });
311
+ ```
312
+
313
+ **Distance-threshold filter (no ranking):**
200
314
 
201
- How to store and retrieve large data in Harper.
315
+ ```javascript
316
+ let results = Document.search({
317
+ conditions: {
318
+ attribute: 'textEmbeddings',
319
+ comparator: 'lt',
320
+ value: 0.1,
321
+ target: searchVector,
322
+ },
323
+ });
324
+ ```
325
+
326
+ #### Notes
327
+
328
+ - The default `distance` function is `cosine`. Pass `distance: "euclidean"` to switch.
329
+ - `efConstruction` controls index build quality; raising it improves recall at the cost of build time.
330
+ - `$distance` is available in both `sort`-based ranking and `conditions`-based threshold queries.
331
+ - Use the threshold (`conditions` + `target`) form when you want to bound result quality by a similarity cutoff rather than ranking by similarity.
332
+
333
+ ### 1.5 Using Blob Datatype
334
+
335
+ Instructions for the agent to follow when working with the Blob data type in Harper.
202
336
 
203
337
  #### When to Use
204
338
 
205
- Use this when you need to store large, unstructured data such as files, images, or large text documents that exceed the typical size of a standard database field.
339
+ Use this skill when you need to store unstructured or large binary data (media, documents) that is too large for standard JSON fields. Blobs provide efficient storage and integrated streaming support.
206
340
 
207
341
  #### How It Works
208
342
 
209
- 1. **Define the Blob Field**: Use the `Blob` scalar type in your GraphQL schema.
210
- 2. **Storing Data**: Send the data as a buffer or a stream when creating or updating a record.
211
- 3. **Retrieving Data**: Access the blob field, which will return the data as a stream or buffer.
343
+ 1. **Define Blob Fields**: In your GraphQL schema, use the `Blob` type:
344
+ ```graphql
345
+ type MyTable @table {
346
+ id: ID @primaryKey
347
+ data: Blob
348
+ }
349
+ ```
350
+ 2. **Create and Store Blobs**: Use `createBlob()` from Harper's globals to wrap Buffers or Streams:
351
+ ```javascript
352
+ import { tables } from 'harper';
353
+ const blob = createBlob(largeBuffer);
354
+ await tables.MyTable.put('my-id', { data: blob });
355
+ ```
356
+ 3. **Use Streaming (Optional)**: For very large files, pass a stream to `createBlob()` to avoid loading the entire file into memory.
357
+ 4. **Read Blob Data**: Retrieve the record and use `.bytes()` or streaming interfaces on the blob field:
358
+ ```javascript
359
+ const record = await tables.MyTable.get('my-id');
360
+ const buffer = await record.data.bytes();
361
+ ```
362
+ 5. **Ensure Write Completion**: Use `saveBeforeCommit: true` in `createBlob` options if you need the blob fully written before the record is committed.
363
+ 6. **Handle Errors**: Attach error listeners to the blob object to handle streaming failures.
212
364
 
213
365
  ### 1.6 Handling Binary Data
214
366
 
215
- How to store and serve binary data like images or MP3s.
367
+ Instructions for the agent to follow when handling binary data in Harper.
216
368
 
217
369
  #### When to Use
218
370
 
219
- Use this when your application needs to handle binary files, particularly for storage and retrieval.
371
+ Use this skill when you need to store binary files (images, audio, etc.) in the database or serve them back to clients via REST endpoints.
220
372
 
221
373
  #### How It Works
222
374
 
223
- 1. **Use the `Blob` type**: As with general large data, the `Blob` type is best for binary files. Ensure you store and retrieve the appropriate MIME type (e.g., `image/jpeg`, `audio/mpeg`) for the data.
224
- 2. **Streaming**: For large files, use streaming to minimize memory usage during upload and download.
225
- 3. **MIME Types**: Store the MIME type alongside the binary data to ensure it is served correctly by your application logic.
375
+ 1. **Store Binary Data**: In your resource's `post` or `put` method, convert incoming data to Buffers and then to Blobs using `createBlob` from Harper's globals. Include the MIME type if available:
376
+
377
+ ```typescript
378
+ async post(target, record) {
379
+ if (record.data) {
380
+ record.data = createBlob(Buffer.from(record.data, record.encoding || 'base64'), {
381
+ type: record.contentType || 'application/octet-stream',
382
+ });
383
+ }
384
+ return super.post(target, record);
385
+ }
386
+ ```
226
387
 
227
- ---
388
+ 2. **Serve Binary Data**: In your resource's `get` method, return a response object with the appropriate `Content-Type` and the binary data in the `body`:
389
+ ```typescript
390
+ async get(target) {
391
+ const record = await super.get(target);
392
+ if (record?.data) {
393
+ return {
394
+ status: 200,
395
+ headers: { 'Content-Type': record.data.type || 'application/octet-stream' },
396
+ body: record.data,
397
+ };
398
+ }
399
+ return record;
400
+ }
401
+ ```
402
+ 3. **Use the Blob Type**: Ensure your GraphQL schema uses the `Blob` scalar for binary fields.
228
403
 
229
404
  ## 2. API & Communication
230
405
 
231
- **Impact: HIGH**
406
+ ### 2.1 Automatic APIs
232
407
 
233
- ### 2.1 Automatic REST APIs
408
+ Instructions for the agent to follow when enabling and using Harper's automatically generated REST and WebSocket APIs.
234
409
 
235
- Details on the CRUD endpoints automatically generated for exported tables.
410
+ #### When to Use
236
411
 
237
- #### Endpoints
412
+ 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.
238
413
 
239
- - `GET /{TableName}`: Describes the schema.
240
- - `GET /{TableName}/`: Lists records (supports filtering/sorting).
241
- - `GET /{TableName}/{id}`: Gets a record by ID.
242
- - `POST /{TableName}/`: Creates a record.
243
- - `PUT /{TableName}/{id}`: Updates a record.
244
- - `PATCH /{TableName}/{id}`: Partial update.
245
- - `DELETE /{TableName}/`: Deletes records.
246
- - `DELETE /{TableName}/{id}`: Deletes by ID.
414
+ #### How It Works
247
415
 
248
- ### 2.2 Querying REST APIs
416
+ 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.
249
417
 
250
- How to use filters, operators, sorting, and pagination in REST requests.
418
+ ```yaml
419
+ rest: true
420
+ ```
251
421
 
252
- #### Query Parameters
422
+ To configure optional behavior:
253
423
 
254
- - `limit(count)` or `limit(offset, count)`: Number of records to return and optional skip. Example: `?limit(10)`, `?limit(10, 20)`.
255
- - `sort(+field1, -field2)`: Fields to sort by. Use `+` for ascending and `-` for descending. Example: `?sort(+name)`, `?sort(-price, +name)`.
256
- - `select(field1, field2)`: Specific fields to return. Example: `?select(id, name)`.
257
- - `filter`: Advanced filtering using comparison operators and logic.
258
- - Operators: `gt`, `ge`, `lt`, `le`, `ne`. Example: `?price=gt=100`.
259
- - Logic: `&` (AND), `|` (OR), `()` (grouping). Example: `?(category=electronics|category=books)&price=lt=500`.
424
+ ```yaml
425
+ rest:
426
+ lastModified: true # enables Last-Modified response header support
427
+ webSocket: false # disables automatic WebSocket support (enabled by default)
428
+ ```
260
429
 
261
- ### 2.3 Real-time Applications
430
+ 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`).
262
431
 
263
- Implementing WebSockets and Pub/Sub for live data updates.
432
+ 3. **Use the correct URL structure**: The REST interface follows a consistent path convention.
264
433
 
265
- #### When to Use
434
+ | Path | Description |
435
+ | -------------------------------------------- | ---------------------------------------------------------------------------------- |
436
+ | `/my-resource` | Returns a description of the resource (e.g., table metadata) |
437
+ | `/my-resource/` | Trailing slash — represents the full collection; append query parameters to search |
438
+ | `/my-resource/record-id` | A specific record identified by its primary key |
439
+ | `/my-resource/record-id/` | Trailing slash — collection of records with the given id prefix |
440
+ | `/my-resource/record-id/with/multiple/parts` | Record id with multiple path segments |
266
441
 
267
- Use this for applications that require live updates, such as chat apps, live dashboards, or collaborative tools.
442
+ 4. **Map HTTP methods to operations**: Each HTTP method maps to a resource method and operation.
443
+ - **GET** — Retrieve a record or search. Calls `get()`.
268
444
 
269
- #### How It Works
445
+ ```
446
+ GET /MyTable/123
447
+ GET /MyTable/?name=Harper
448
+ GET /MyTable/123.propertyName
449
+ ```
270
450
 
271
- 1. **WebSocket Connection**: Connect to the Harper WebSocket endpoint. Use `wss://` for secure connections over HTTPS, or `ws://` for local development.
272
- 2. **Subscribing**: Subscribe to table updates or specific records.
273
- 3. **Pub/Sub**: Use the internal bus to publish and subscribe to custom events.
451
+ Responses include an `ETag` header. Clients may send `If-None-Match` to receive `304 Not Modified` when the record is unchanged.
274
452
 
275
- ### 2.4 Checking Authentication
453
+ - **PUT** — Create or replace a record (upsert). Calls `put(record)`. Properties not in the body are removed.
454
+
455
+ ```
456
+ PUT /MyTable/123
457
+ Content-Type: application/json
458
+
459
+ { "name": "some data" }
460
+ ```
461
+
462
+ - **POST** — Create a new record without specifying a primary key. Calls `post(data)`. The assigned key is returned in the `Location` response header.
463
+
464
+ ```
465
+ POST /MyTable/
466
+ Content-Type: application/json
467
+
468
+ { "name": "some data" }
469
+ ```
470
+
471
+ - **PATCH** — Partially update a record, merging only provided properties. Unspecified properties are preserved.
472
+
473
+ ```
474
+ PATCH /MyTable/123
475
+ Content-Type: application/json
476
+
477
+ { "status": "active" }
478
+ ```
479
+
480
+ - **DELETE** — Delete a record or all records matching a query.
481
+ ```
482
+ DELETE /MyTable/123
483
+ DELETE /MyTable/?status=archived
484
+ ```
485
+
486
+ 5. **Access the auto-generated OpenAPI spec**: Harper generates an OpenAPI specification for all exported resources. Retrieve it at:
487
+
488
+ ```
489
+ GET /openapi
490
+ ```
491
+
492
+ 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.
493
+
494
+ ```javascript
495
+ let ws = new WebSocket('wss://server/my-resource/341');
496
+ ws.onmessage = (event) => {
497
+ let data = JSON.parse(event.data);
498
+ };
499
+ ```
500
+
501
+ 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.
502
+
503
+ 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.
504
+
505
+ #### Examples
506
+
507
+ **Simple echo server using an async generator**:
508
+
509
+ ```javascript
510
+ export class Echo extends Resource {
511
+ async *connect(incomingMessages) {
512
+ for await (let message of incomingMessages) {
513
+ yield message; // echo each message back
514
+ }
515
+ }
516
+ }
517
+ ```
518
+
519
+ **Using the default `connect()` with event-style access and a timer**:
276
520
 
277
- How to use sessions to verify user identity and roles.
521
+ ```javascript
522
+ export class Example extends Resource {
523
+ connect(incomingMessages) {
524
+ let outgoingMessages = super.connect();
525
+
526
+ let timer = setInterval(() => {
527
+ outgoingMessages.send({ greeting: 'hi again!' });
528
+ }, 1000);
529
+
530
+ incomingMessages.on('data', (message) => {
531
+ outgoingMessages.send(message); // echo incoming messages
532
+ });
533
+
534
+ outgoingMessages.on('close', () => {
535
+ clearInterval(timer);
536
+ });
537
+
538
+ return outgoingMessages;
539
+ }
540
+ }
541
+ ```
542
+
543
+ **Minimal `config.yaml` enabling REST with WebSocket disabled**:
544
+
545
+ ```yaml
546
+ rest:
547
+ webSocket: false
548
+ ```
549
+
550
+ #### Notes
551
+
552
+ - Tables must be explicitly exported using `@export` in the schema — they are not exposed by default.
553
+ - `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.
554
+ - For full query syntax on `GET` and `DELETE` with query parameters, see [querying-rest-apis.md](querying-rest-apis.md).
555
+ - The default `connect()` returns an iterable with a `send(message)` method and a `close` event for cleanup on disconnect.
556
+ - For MQTT over WebSockets, set the sub-protocol header `Sec-WebSocket-Protocol: mqtt`.
557
+ - 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.
558
+ - Use the `Content-Type` request header to specify body format and the `Accept` header to request a specific response format.
559
+
560
+ ### 2.2 Querying REST APIs
561
+
562
+ Instructions for the agent to filter, sort, select, and paginate Harper REST API collections using URL query parameters.
278
563
 
279
564
  #### When to Use
280
565
 
281
- Use this to secure your application by ensuring that only authorized users can access certain resources or perform specific actions.
566
+ 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)).
282
567
 
283
568
  #### How It Works
284
569
 
285
- 1. **Session Handling**: Access the session object from the request context.
286
- 2. **Identity Verification**: Check for the presence of a user ID or token.
287
- 3. **Role Checks**: Verify if the user has the required roles for the action.
570
+ 1. **Filter by attribute**: Add query parameters matching attribute names and values. The queried attribute must be indexed.
288
571
 
289
- ---
572
+ ```
573
+ GET /Product/?category=software
574
+ GET /Product/?category=software&inStock=true
575
+ ```
290
576
 
291
- ## 3. Logic & Extension
577
+ 2. **Apply comparison operators (FIQL syntax)**: Use FIQL operators directly in query parameter values.
578
+
579
+ | Operator | Meaning |
580
+ | ------------ | -------------------------------------- |
581
+ | `==` | Equal |
582
+ | `=lt=` | Less than |
583
+ | `=le=` | Less than or equal |
584
+ | `=gt=` | Greater than |
585
+ | `=ge=` | Greater than or equal |
586
+ | `=ne=`, `!=` | Not equal |
587
+ | `=ct=` | Contains (strings) |
588
+ | `=sw=` | Starts with (strings) |
589
+ | `=ew=` | Ends with (strings) |
590
+ | `=`, `===` | Strict equality (no type conversion) |
591
+ | `!==` | Strict inequality (no type conversion) |
292
592
 
293
- **Impact: MEDIUM**
593
+ ```
594
+ GET /Product/?price=gt=100
595
+ GET /Product/?price=le=20
596
+ GET /Product/?name==Keyboard*
597
+ GET /Product/?category=software&price=gt=100&price=lt=200
598
+ ```
294
599
 
295
- ### 3.1 Custom Resources
600
+ For date fields, URL-encode colons as `%3A`:
296
601
 
297
- How to define custom REST endpoints using JavaScript or TypeScript.
602
+ ```
603
+ GET /Product/?listDate=gt=2017-03-08T09%3A30%3A00.000Z
604
+ ```
298
605
 
299
- #### How It Works
606
+ 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.
300
607
 
301
- 1. **Create Resource File**: Define your logic in a JS or TS file.
302
- 2. **Define Resource Class**: Export a class extending `Resource` from `harper`.
303
- 3. **Implement HTTP Methods**: Add methods like `get`, `post`, `put`, `patch`, or `delete` to handle corresponding requests.
304
- 4. **Route Nesting and Naming**: You can control the URL structure by how you export your resources:
305
- - **Direct Class Export**: `export class Foo extends Resource` creates endpoints at `/Foo/`. Class names are case-sensitive in the URL.
306
- - **Nested Objects**: `export const Bar = { Foo };` creates endpoints at `/Bar/Foo/`.
307
- - **Lowercase and Hyphens**: Use object keys to define custom paths: `export const bar = { 'foo-baz': Foo };` exposes endpoints at `/bar/foo-baz/`.
308
- 5. **Registration**: Ensure the resource is correctly registered in your application configuration.
608
+ ```
609
+ GET /Product/?price=gt=100&lt=200
610
+ ```
309
611
 
310
- ### 3.2 Extending Table Resources
612
+ 4. **Combine conditions with OR logic**: Use `|` instead of `&`.
311
613
 
312
- Adding custom logic to automatically generated table resources.
614
+ ```
615
+ GET /Product/?rating=5|featured=true
616
+ ```
313
617
 
314
- #### How It Works
618
+ 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 `]`.
315
619
 
316
- 1. **Define Extension**: Create a resource file that targets an existing table.
317
- 2. **Intercept Requests**: Use handlers to add custom validation or data transformation.
318
- 3. **No `@export`**: If extending, remember not to `@export` the table in the schema.
620
+ ```
621
+ GET /Product/?rating=5|(price=gt=100&price=lt=200)
622
+ GET /Product/?rating=5&[tag=fast|tag=scalable|tag=efficient]
623
+ ```
319
624
 
320
- ### 3.3 Programmatic Table Requests
625
+ Construct grouped queries from JavaScript:
321
626
 
322
- How to use filters, operators, sorting, and pagination in programmatic table requests.
627
+ ```javascript
628
+ let url = `/Product/?rating=5&[${tags.map(encodeURIComponent).join('|')}]`;
629
+ ```
323
630
 
324
- #### Usage
631
+ 6. **Select specific properties with `select(`**: Use `select()` to control which fields are returned.
325
632
 
326
- When writing custom resources, use the internal API to query tables with full support for advanced filtering and sorting.
633
+ | Syntax | Returns |
634
+ | -------------------------------------- | ------------------------------------------- |
635
+ | `?select(property)` | Values of a single property directly |
636
+ | `?select(property1,property2)` | Objects with only the specified properties |
637
+ | `?select([property1,property2])` | Arrays of property values |
638
+ | `?select(property1,)` | Objects with a single specified property |
639
+ | `?select(property{subProp1,subProp2})` | Nested objects with specific sub-properties |
327
640
 
328
- ### 3.4 TypeScript Type Stripping
641
+ ```
642
+ GET /Product/?category=software&select(name)
643
+ GET /Product/?brand.name=Microsoft&select(name,brand{name})
644
+ ```
329
645
 
330
- Using TypeScript directly without build tools via Node.js Type Stripping.
646
+ 7. **Limit results with `limit(`**: Use `limit(end)` or `limit(start,end)` to paginate.
331
647
 
332
- #### Configuration
648
+ ```
649
+ GET /Product/?rating=gt=3&inStock=true&select(rating,name)&limit(20)
650
+ GET /Product/?rating=gt=3&limit(10,30)
651
+ ```
333
652
 
334
- Harper supports native TypeScript type stripping, allowing you to run `.ts` files directly. Ensure your environment is configured to take advantage of this for faster development cycles.
653
+ 8. **Sort results with `sort(`**: Use `sort(property)` or `sort(+property,-property,...)`. Prefix `+` or no prefix = ascending; `-` = descending.
335
654
 
336
- ### 3.5 Caching
655
+ ```
656
+ GET /Product/?rating=gt=3&sort(+name)
657
+ GET /Product/?sort(+rating,-price)
658
+ ```
337
659
 
338
- How caching is defined and implemented in Harper applications.
660
+ 9. **Query across relationships**: Use dot-syntax to filter by related table attributes. Relationships must be defined in the schema using `@relation`.
339
661
 
340
- #### Strategies
662
+ ```
663
+ GET /Product/?brand.name=Microsoft
664
+ GET /Brand/?products.name=Keyboard
665
+ ```
341
666
 
342
- - **In-memory**: For fast access to frequently used data.
343
- - **Distributed**: For scaling across multiple nodes in Harper Fabric.
667
+ Use `select()` to include relationship attributes in the response (they are not included by default):
344
668
 
345
- ---
669
+ ```
670
+ GET /Product/?brand.name=Microsoft&select(name,brand{name})
671
+ ```
346
672
 
347
- ## 4. Infrastructure & Ops
673
+ 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.
674
+ ```
675
+ GET /MyTable/123.propertyName
676
+ ```
348
677
 
349
- **Impact: MEDIUM**
678
+ #### Examples
350
679
 
351
- ### 4.1 Creating Harper Applications
680
+ **Range filter with select and limit:**
352
681
 
353
- The fastest way to start a new Harper project is using the `create-harper` CLI tool. This command initializes a project with a standard folder structure, essential configuration files, and basic schema definitions.
682
+ ```
683
+ GET /Product/?category=software&price=gt=100&price=lt=200&select(name,price)&limit(20)
684
+ ```
354
685
 
355
- #### When to Use
686
+ **Sort descending with multiple fields:**
356
687
 
357
- Use this command when starting a new Harper application or adding a new Harper microservice to an existing architecture.
688
+ ```
689
+ GET /Product/?sort(+rating,-price)
690
+ ```
358
691
 
359
- #### Commands
692
+ **OR logic with grouping:**
360
693
 
361
- Initialize a project using your preferred package manager:
694
+ ```
695
+ GET /Product/?price=lt=100|[rating=5&[tag=fast|tag=scalable|tag=efficient]&inStock=true]
696
+ ```
362
697
 
363
- **NPM**
698
+ **Relationship join with nested select:**
364
699
 
365
- ```bash
366
- npm create harper@latest
700
+ ```
701
+ GET /Product/?brand.name=Microsoft&select(name,brand{name,id})
367
702
  ```
368
703
 
369
- **PNPM**
704
+ **Schema defining a relationship for join queries:**
370
705
 
371
- ```bash
372
- pnpm create harper@latest
706
+ ```graphql
707
+ type Product @table @export {
708
+ id: Long @primaryKey
709
+ name: String
710
+ brandId: Long @indexed
711
+ brand: Brand @relation(from: "brandId")
712
+ }
713
+ type Brand @table @export {
714
+ id: Long @primaryKey
715
+ name: String
716
+ products: [Product] @relation(to: "brandId")
717
+ }
373
718
  ```
374
719
 
375
- **Bun**
720
+ **Many-to-many relationship query:**
376
721
 
377
- ```bash
378
- bun create harper@latest
722
+ ```graphql
723
+ type Product @table @export {
724
+ id: Long @primaryKey
725
+ name: String
726
+ resellerIds: [Long] @indexed
727
+ resellers: [Reseller] @relation(from: "resellerId")
728
+ }
379
729
  ```
380
730
 
381
- ### 4.2 Creating a Fabric Account and Cluster
731
+ ```
732
+ GET /Product/?resellers.name=Cool Shop&select(id,name,resellers{name,id})
733
+ ```
382
734
 
383
- Follow these steps to set up your Harper Fabric environment for deployment.
735
+ **Type conversion with explicit prefix:**
384
736
 
385
- #### How It Works
737
+ ```
738
+ GET /Product/?price==number:123
739
+ GET /Product/?active==boolean:true
740
+ GET /Product/?listDate==date:2024-01-05T20%3A07%3A27.955Z
741
+ ```
386
742
 
387
- 1. **Sign Up/In**: Go to [https://fabric.harper.fast/](https://fabric.harper.fast/) and sign up or sign in.
388
- 2. **Create an Organization**: Create an organization (org) to manage your projects.
389
- 3. **Create a Cluster**: Create a new cluster. This can be on the free tier, no credit card required.
390
- 4. **Set Credentials**: During setup, set the cluster username and password to finish configuring it.
391
- 5. **Get Application URL**: Navigate to the **Config** tab and copy the **Application URL**.
392
- 6. **Configure Environment**: Update your `.env` file or GitHub Actions secrets with these cluster-specific credentials:
393
- ```bash
394
- CLI_TARGET_USERNAME='YOUR_CLUSTER_USERNAME'
395
- CLI_TARGET_PASSWORD='YOUR_CLUSTER_PASSWORD'
396
- CLI_TARGET='YOUR_CLUSTER_URL'
397
- ```
743
+ #### Notes
744
+
745
+ - 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.
746
+ - 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.
747
+ - FIQL comparators (`==`, `!=`, `=gt=`, etc.) apply automatic type conversion based on value syntax or schema-declared type. Strict operators (`=`, `===`, `!==`) skip automatic type conversion.
748
+ - 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.
749
+ - The array order of foreign key values in many-to-many relationships is preserved when resolving the relationship.
750
+ - See [automatic-apis.md](automatic-apis.md) for how Harper tables are automatically exposed as REST endpoints.
398
751
 
399
- ### 4.3 Deploying to Harper Fabric
752
+ ### 2.3 Real-Time Apps with WebSockets and Pub/Sub
400
753
 
401
- Globally scaling your Harper application.
754
+ Instructions for the agent to follow when building real-time features in Harper using WebSockets and Pub/Sub.
402
755
 
403
- #### Benefits
756
+ #### When to Use
404
757
 
405
- - **Global Distribution**: Low latency for users everywhere.
406
- - **Automatic Sync**: Data is synced across the fabric automatically.
407
- - **Free Tier**: Start for free and scale as you grow.
758
+ 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.
408
759
 
409
760
  #### How It Works
410
761
 
411
- 1. **Sign up**: Follow the [Creating a Fabric Account and Cluster](#42-creating-a-fabric-account-and-cluster) steps to create a Harper Fabric account, organization, and cluster.
412
- 2. **Configure Environment**: Add your cluster credentials and cluster application URL to `.env`:
413
- ```bash
414
- CLI_TARGET_USERNAME='YOUR_CLUSTER_USERNAME'
415
- CLI_TARGET_PASSWORD='YOUR_CLUSTER_PASSWORD'
416
- CLI_TARGET='YOUR_CLUSTER_URL'
762
+ 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:
763
+
764
+ ```yaml
765
+ rest:
766
+ webSocket: false
767
+ ```
768
+
769
+ 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.
770
+
771
+ ```javascript
772
+ let ws = new WebSocket('wss://server/my-resource/341');
773
+ ws.onmessage = (event) => {
774
+ let data = JSON.parse(event.data);
775
+ };
417
776
  ```
418
- 3. **Deploy From Local Environment**: Run `npm run deploy`.
419
- 4. **Set up CI/CD**: Configure `.github/workflows/deploy.yaml` and set repository secrets for automated deployments.
420
777
 
421
- #### Manual Setup for Existing Apps
778
+ `new WebSocket('wss://server/my-resource/341')` accesses the resource defined for `my-resource` with record id `341` and subscribes to it.
422
779
 
423
- If your application was not created with `npm create harper`, you'll need to manually configure the deployment scripts and CI/CD workflow.
780
+ 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.
424
781
 
425
- #### 1. Update `package.json`
782
+ 4. **Use the default `connect()` for event-style access**: Call `super.connect()` to get a streaming iterable that provides:
783
+ - A `send(message)` method for pushing outgoing messages
784
+ - A `close` event for cleanup on disconnect
426
785
 
427
- Add the following scripts and dependencies to your `package.json`:
786
+ 5. **Handle message ordering in distributed environments**: Harper delivers messages to local subscribers immediately without inter-node coordination delay.
428
787
 
429
- ```json
430
- {
431
- "scripts": {
432
- "deploy": "dotenv -- npm run deploy:component",
433
- "deploy:component": "harper deploy_component . restart=rolling replicated=true"
434
- },
435
- "devDependencies": {
436
- "dotenv-cli": "^11.0.0",
437
- "harper": "^5.0.0"
788
+ | Message Type | Behavior |
789
+ | -------------------------------------------------------- | ----------------------------------------------------------------------- |
790
+ | Non-retained (no `retain` flag) | Every message delivered in order received; suitable for chat |
791
+ | Retained (published with `retain`, or PUT/updated in DB) | Only the latest-timestamp message is kept; suitable for sensor readings |
792
+
793
+ 6. **Use MQTT over WebSockets** when needed by setting the sub-protocol header:
794
+ ```
795
+ Sec-WebSocket-Protocol: mqtt
796
+ ```
797
+
798
+ #### Examples
799
+
800
+ **Simple echo server** — override `connect(incomingMessages)` to yield each incoming message back to the client:
801
+
802
+ ```javascript
803
+ export class Echo extends Resource {
804
+ async *connect(incomingMessages) {
805
+ for await (let message of incomingMessages) {
806
+ yield message; // echo each message back
807
+ }
438
808
  }
439
809
  }
440
810
  ```
441
811
 
442
- #### Why split the scripts?
443
-
444
- 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.
812
+ **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:
445
813
 
446
- - `deploy`: Uses `dotenv-cli` to load environment variables (like `CLI_TARGET`, `CLI_TARGET_USERNAME`, and `CLI_TARGET_PASSWORD`) before executing the next command.
447
- - `deploy:component`: The actual command that performs the deployment.
814
+ ```javascript
815
+ export class Example extends Resource {
816
+ connect(incomingMessages) {
817
+ let outgoingMessages = super.connect();
448
818
 
449
- 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.
819
+ let timer = setInterval(() => {
820
+ outgoingMessages.send({ greeting: 'hi again!' });
821
+ }, 1000);
450
822
 
451
- #### 2. Configure GitHub Actions
823
+ incomingMessages.on('data', (message) => {
824
+ outgoingMessages.send(message); // echo incoming messages
825
+ });
452
826
 
453
- Create a `.github/workflows/deploy.yaml` file with the following content:
454
-
455
- ```yaml
456
- name: Deploy to Harper Fabric
457
- on:
458
- workflow_dispatch:
459
- # push:
460
- # branches:
461
- # - main
462
- concurrency:
463
- group: main
464
- cancel-in-progress: false
465
- jobs:
466
- deploy:
467
- runs-on: ubuntu-latest
468
- steps:
469
- - name: Checkout code
470
- uses: actions/checkout@v4
471
- - name: Set up Node.js
472
- uses: actions/setup-node@v4
473
- with:
474
- cache: 'npm'
475
- node-version: '20'
476
- - name: Install dependencies
477
- run: npm ci
478
- - name: Run unit tests
479
- run: npm test
480
- - name: Run lint
481
- run: npm run lint
482
- - name: Deploy
483
- run: npm run deploy
484
- env:
485
- CLI_TARGET: ${{ secrets.CLI_TARGET }}
486
- CLI_TARGET_USERNAME: ${{ secrets.CLI_TARGET_USERNAME }}
487
- CLI_TARGET_PASSWORD: ${{ secrets.CLI_TARGET_PASSWORD }}
488
- ```
489
-
490
- Be sure to set the following repository secrets in your GitHub repository:
491
-
492
- - `CLI_TARGET`
493
- - `CLI_TARGET_USERNAME`
494
- - `CLI_TARGET_PASSWORD`
827
+ outgoingMessages.on('close', () => {
828
+ clearInterval(timer);
829
+ });
495
830
 
496
- ### 4.4 Serving Web Content
831
+ return outgoingMessages;
832
+ }
833
+ }
834
+ ```
497
835
 
498
- Two ways to serve web content from a Harper application.
836
+ #### Notes
499
837
 
500
- #### Methods
838
+ - WebSocket connections target a resource URL path. By default, connecting to a resource subscribes to changes for that resource.
839
+ - The `connect(incomingMessages)` method **must** return an async iterable or generator; returning a plain value will not work.
840
+ - `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.
841
+ - For one-way real-time streaming without bidirectional communication, consider Server-Sent Events instead.
842
+ - For full pub/sub capabilities, Harper also supports MQTT; set `Sec-WebSocket-Protocol: mqtt` to use MQTT over WebSockets.
501
843
 
502
- 1. **Static Serving**: Serve HTML, CSS, and JS files directly. If using the Vite plugin for development, ensure Harper is running (e.g., `harper run .`) to allow for Hot Module Replacement (HMR).
503
- 2. **Dynamic Rendering**: Use custom resources to render content on the fly.
844
+ ### 2.4 Checking Authentication
504
845
 
505
- ### 4.5 Logging Best Practices
846
+ Instructions for the agent to follow when handling user authentication and session management inside Harper Resources.
506
847
 
507
- Harper provides a robust logging system that captures standard output and offers a granular, tagged logging interface for both local and deployed environments.
848
+ #### When to Use
508
849
 
509
- #### Standard Console Logging
850
+ 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.
510
851
 
511
- 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.
852
+ #### How It Works
512
853
 
513
- - `console.log(...)`: Captured as `stdout` level in Harper logs.
514
- - `console.warn(...)`: Captured as `stderr` level in Harper logs.
515
- - `console.error(...)`: Captured as `stderr` level in Harper logs.
516
- - `console.trace(...)`: Captured as `stdout` level in Harper logs (includes stack trace).
854
+ 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`.
517
855
 
518
- #### Harper Logger
856
+ ```javascript
857
+ async get(target) {
858
+ const user = this.getCurrentUser();
859
+ if (!user) return new Response(null, { status: 401 });
860
+ return { username: user.username, role: user.role };
861
+ }
862
+ ```
519
863
 
520
- 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.
864
+ The returned object exposes `username`, `role`, and `role.permission` flags.
521
865
 
522
- ##### Log Levels
866
+ 2. **Enable sessions** before using session-based login. Set `authentication.enableSessions: true` in `harperdb-config.yaml`:
523
867
 
524
- The Harper `logger` supports the following levels (ordered by increasing severity):
868
+ ```yaml
869
+ authentication:
870
+ enableSessions: true
871
+ ```
525
872
 
526
- - `trace`
527
- - `debug`
528
- - `info`
529
- - `warn`
530
- - `error`
531
- - `fatal`
532
- - `notify`
873
+ 3. **Access login and session helpers** via `getContext()`. The context object exposes `context.login` and `context.session` for sign-in/out flows.
874
+ - Call `context.login(username, password)` to verify credentials and establish a session cookie on success.
875
+ - To end a session, delete it via `context.session.delete(context.session.id)`.
876
+
877
+ 4. **Implement sign-in and sign-out Resources** using the context helpers:
878
+
879
+ ```javascript
880
+ export class SignIn extends Resource {
881
+ async post(_target, data) {
882
+ const context = this.getContext();
883
+ try {
884
+ await context.login(data.username, data.password);
885
+ } catch {
886
+ return new Response('Invalid credentials', { status: 403 });
887
+ }
888
+ return new Response('Logged in', { status: 200 });
889
+ }
890
+ }
891
+
892
+ export class SignOut extends Resource {
893
+ async post() {
894
+ const context = this.getContext();
895
+ if (!context.session) return new Response(null, { status: 401 });
896
+ await context.session.delete(context.session.id);
897
+ return new Response('Logged out', { status: 200 });
898
+ }
899
+ }
900
+ ```
533
901
 
534
- ##### Usage
902
+ 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()`:
903
+
904
+ ```javascript
905
+ import { Resource, server } from 'harper';
906
+
907
+ export class IssueTokens extends Resource {
908
+ static async get(_target, context) {
909
+ const { operation_token, refresh_token } = await server.operation(
910
+ { operation: 'create_authentication_tokens' },
911
+ context,
912
+ true,
913
+ );
914
+ return { operation_token, refresh_token };
915
+ }
916
+
917
+ static async post(_target, data) {
918
+ const { username, password } = await data;
919
+ if (!username || !password) {
920
+ return new Response('username and password required', { status: 400 });
921
+ }
922
+ const { operation_token, refresh_token } = await server.operation({
923
+ operation: 'create_authentication_tokens',
924
+ username,
925
+ password,
926
+ });
927
+ return { operation_token, refresh_token };
928
+ }
929
+ }
930
+
931
+ export class RefreshJWT extends Resource {
932
+ static async post(_target, data) {
933
+ const { refresh_token } = await data;
934
+ if (!refresh_token) {
935
+ return new Response('refresh_token required', { status: 400 });
936
+ }
937
+ const { operation_token } = await server.operation({
938
+ operation: 'refresh_operation_token',
939
+ refresh_token,
940
+ });
941
+ return { operation_token };
942
+ }
943
+ }
944
+ ```
535
945
 
536
- ```typescript
537
- import { logger, loggerWithTag } from 'harper';
946
+ 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.
538
947
 
539
- // Basic logging
540
- logger.info('Application started');
541
- logger.error('An error occurred', error);
948
+ 6. **Configure JWT token expiry** in `harperdb-config.yaml` under the `authentication` section:
542
949
 
543
- // Tagged logging for better filtering (Namespacing)
544
- const authLogger = loggerWithTag('auth');
545
- authLogger.debug('User login attempt', { userId: '123' });
546
- ```
950
+ ```yaml
951
+ authentication:
952
+ operationTokenTimeout: 1d
953
+ refreshTokenTimeout: 30d
954
+ ```
547
955
 
548
- 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.
956
+ Duration strings follow the `jsonwebtoken` package format (e.g., `1d`, `12h`, `60m`).
549
957
 
550
- #### Programmatic Log Retrieval
958
+ #### Examples
551
959
 
552
- 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.
960
+ **Protecting a resource endpoint and returning user info:**
553
961
 
554
- ##### `read_log` Operation
962
+ ```javascript
963
+ async get(target) {
964
+ const user = this.getCurrentUser();
965
+ if (!user) return new Response(null, { status: 401 });
966
+ return { username: user.username, role: user.role };
967
+ }
968
+ ```
555
969
 
556
- The `read_log` operation is a POST request to the Harper instance.
970
+ **Full session-based sign-in/sign-out flow:**
971
+
972
+ ```javascript
973
+ export class SignIn extends Resource {
974
+ async post(_target, data) {
975
+ const context = this.getContext();
976
+ try {
977
+ await context.login(data.username, data.password);
978
+ } catch {
979
+ return new Response('Invalid credentials', { status: 403 });
980
+ }
981
+ return new Response('Logged in', { status: 200 });
982
+ }
983
+ }
557
984
 
558
- **Example Request:**
985
+ export class SignOut extends Resource {
986
+ async post() {
987
+ const context = this.getContext();
988
+ if (!context.session) return new Response(null, { status: 401 });
989
+ await context.session.delete(context.session.id);
990
+ return new Response('Logged out', { status: 200 });
991
+ }
992
+ }
993
+ ```
559
994
 
560
- ```json
561
- {
562
- "operation": "read_log",
563
- "limit": 100,
564
- "start": 0,
565
- "level": "error",
566
- "order": "desc",
567
- "from": "2024-01-01T00:00:00.000Z",
568
- "until": "2024-01-02T00:00:00.000Z"
995
+ **JWT token refresh endpoint:**
996
+
997
+ ```javascript
998
+ export class RefreshJWT extends Resource {
999
+ static async post(_target, data) {
1000
+ const { refresh_token } = await data;
1001
+ if (!refresh_token) {
1002
+ return new Response('refresh_token required', { status: 400 });
1003
+ }
1004
+ const { operation_token } = await server.operation({
1005
+ operation: 'refresh_operation_token',
1006
+ refresh_token,
1007
+ });
1008
+ return { operation_token };
1009
+ }
569
1010
  }
570
1011
  ```
571
1012
 
572
- ##### Parameters
1013
+ #### Notes
573
1014
 
574
- - `limit`: Number of log entries to return.
575
- - `start`: Offset for pagination.
576
- - `level`: Filter by log level (`info`, `error`, `warn`, `debug`, `trace`, `notify`, `fatal`, `stdout`, `stderr`).
577
- - `from`: ISO 8601 timestamp to start reading from.
578
- - `until`: ISO 8601 timestamp to stop reading at.
579
- - `order`: Sort order, either `asc` or `desc`.
580
- - `replicated`: (Boolean) Include logs from replicated nodes in a cluster.
1015
+ - `getCurrentUser()` and `getContext()` are instance methods; call them with `this` inside non-static Resource methods.
1016
+ - `enableSessions` must be `true` in config before `context.login` or `context.session` will function.
1017
+ - Cookie-based sessions target browser clients. Use JWT issuance via `server.operation()` for all other client types.
1018
+ - When both `operation_token` and `refresh_token` have expired, the client must call `create_authentication_tokens` again with credentials.
1019
+
1020
+ ## 3. Logic & Extension
581
1021
 
582
- ##### Log Entry Structure
1022
+ ### 3.1 Custom Resources
1023
+
1024
+ Instructions for the agent to follow when creating custom resources in Harper.
1025
+
1026
+ #### When to Use
1027
+
1028
+ Use this skill when the automatic CRUD operations provided by `@table @export` are insufficient, and you need custom logic, third-party API integration, or specialized data handling for your REST endpoints.
1029
+
1030
+ #### How It Works
1031
+
1032
+ 1. **Check if a Custom Resource is Necessary**: Verify if [Automatic APIs](./automatic-apis.md) or [Extending Tables](./extending-tables.md) can satisfy the requirement first.
1033
+ 2. **Create the Resource File**: Create a `.ts` or `.js` file in the directory specified by `jsResource` in `config.yaml` (typically `resources/`).
1034
+ 3. **Define the Resource Class**: Export a class extending `Resource` from `harper`:
1035
+
1036
+ ```typescript
1037
+ import { type RequestTargetOrId, Resource } from 'harper';
1038
+
1039
+ export class MyResource extends Resource {
1040
+ async get(target?: RequestTargetOrId) {
1041
+ return { message: 'Hello from custom GET!' };
1042
+ }
1043
+ }
1044
+ ```
1045
+
1046
+ 4. **Implement HTTP Methods**: Add methods like `get`, `post`, `put`, `patch`, or `delete` to handle corresponding requests.
1047
+ 5. **Route Nesting and Naming**: You can control the URL structure by how you export your resources:
1048
+ - **Direct Class Export**: `export class Foo extends Resource` creates endpoints at `/Foo/`. Class names are case-sensitive in the URL.
1049
+ - **Nested Objects**: `export const Bar = { Foo };` creates endpoints at `/Bar/Foo/`.
1050
+ - **Lowercase and Hyphens**: Use object keys to define custom paths: `export const bar = { 'foo-baz': Foo };` exposes endpoints at `/bar/foo-baz/`.
1051
+ 6. **Access Tables (Optional)**: Import and use the `tables` object to interact with your data:
1052
+ ```typescript
1053
+ import { tables } from 'harper';
1054
+ // ... inside a method
1055
+ const results = await tables.MyTable.list();
1056
+ ```
1057
+ 7. **Configure Loading**: Ensure `config.yaml` points to your resource files (e.g., `jsResource: { files: 'resources/*.ts' }`).
1058
+
1059
+ ### 3.2 Extending Tables
1060
+
1061
+ Instructions for the agent to follow when extending table resources in Harper.
1062
+
1063
+ #### When to Use
1064
+
1065
+ Use this skill when you need to add custom validation, side effects (like webhooks), data transformation, or custom access control to the standard CRUD operations of a Harper table.
1066
+
1067
+ #### How It Works
1068
+
1069
+ 1. **Define the Table in GraphQL**: In your `.graphql` schema, define the table using the `@table` directive. **Do not** use `@export` if you plan to extend it.
1070
+ ```graphql
1071
+ type MyTable @table {
1072
+ id: ID @primaryKey
1073
+ name: String
1074
+ }
1075
+ ```
1076
+ 2. **Create the Extension File**: Create a `.ts` file in your `resources/` directory.
1077
+ 3. **Extend the Table Resource**: Export a class that extends `tables.YourTableName`:
1078
+
1079
+ ```typescript
1080
+ import { type RequestTargetOrId, tables } from 'harper';
1081
+
1082
+ export class MyTable extends tables.MyTable {
1083
+ async post(target: RequestTargetOrId, record: any) {
1084
+ // Custom logic here
1085
+ if (!record.name) {
1086
+ throw new Error('Name required');
1087
+ }
1088
+ return super.post(target, record);
1089
+ }
1090
+ }
1091
+ ```
1092
+
1093
+ 4. **Override Methods**: Override `get`, `post`, `put`, `patch`, or `delete` as needed. Always call `super[method]` to maintain default Harper functionality unless you intend to replace it entirely.
1094
+ 5. **Implement Logic**: Use overrides for validation, side effects, or transforming data before/after database operations.
1095
+
1096
+ ### 3.3 Programmatic Table Requests
1097
+
1098
+ Instructions for the agent to follow when interacting with Harper tables via code.
1099
+
1100
+ #### When to Use
1101
+
1102
+ Use this skill when you need to perform database operations (CRUD, search, subscribe) from within Harper Resources or scripts.
1103
+
1104
+ #### How It Works
1105
+
1106
+ 1. **Access the Table**: Use the global `tables` object followed by your table name (e.g., `tables.MyTable`).
1107
+ 2. **Perform CRUD Operations**:
1108
+ - **Get**: `await tables.MyTable.get(id)` for a single record or `await tables.MyTable.get({ conditions: [...] })` for multiple.
1109
+ - **Create**: `await tables.MyTable.post(record)` (auto-generates ID) or `await tables.MyTable.put(id, record)`.
1110
+ - **Update**: `await tables.MyTable.patch(id, partialRecord)` for partial updates.
1111
+ - **Delete**: `await tables.MyTable.delete(id)`.
1112
+ 3. **Use Updatable Records for Atomic Ops**: Call `update(id)` to get a reference, then use `addTo` or `subtractFrom` for atomic increments/decrements:
1113
+ ```typescript
1114
+ const stats = await tables.Stats.update('daily');
1115
+ stats.addTo('viewCount', 1);
1116
+ ```
1117
+ 4. **Search and Stream**: Use `search(query)` for efficient streaming of large result sets:
1118
+ ```typescript
1119
+ for await (const record of tables.MyTable.search({ conditions: [...] })) {
1120
+ // process record
1121
+ }
1122
+ ```
1123
+ See the [Query Conditions](#query-conditions) section below for the full query object reference.
1124
+ 5. **Real-time Subscriptions**: Use `subscribe(query)` to listen for changes:
1125
+ ```typescript
1126
+ for await (const event of tables.MyTable.subscribe(query)) {
1127
+ // handle event
1128
+ }
1129
+ ```
1130
+ 6. **Publish Events**: Use `publish(id, message)` to trigger subscriptions without necessarily persisting data.
1131
+
1132
+ #### Query Conditions
1133
+
1134
+ When passing a query to `search()`, `get()`, or `subscribe()`, use a query object with a `conditions` array.
1135
+
1136
+ ##### Condition Object Shape
1137
+
1138
+ | Property | Description |
1139
+ | ------------ | ------------------------------------------------------------------------------------------ |
1140
+ | `attribute` | Field name, or array of field names to traverse a relationship (e.g., `['brand', 'name']`) |
1141
+ | `value` | The value to compare against |
1142
+ | `comparator` | One of the comparator strings below (default: `equals`) |
1143
+ | `operator` | `and` (default) or `or` — applies to a nested `conditions` block |
1144
+ | `conditions` | Nested array of condition objects for complex AND/OR logic |
1145
+
1146
+ ##### Comparator Values
1147
+
1148
+ Use these exact strings — incorrect comparator names will silently fail or error:
1149
+
1150
+ | Comparator | Meaning |
1151
+ | -------------------- | ---------------------------------------------------------- |
1152
+ | `equals` | Exact match (default) |
1153
+ | `not_equal` | Not equal |
1154
+ | `greater_than` | `>` |
1155
+ | `greater_than_equal` | `>=` |
1156
+ | `less_than` | `<` |
1157
+ | `less_than_equal` | `<=` |
1158
+ | `starts_with` | String starts with value |
1159
+ | `contains` | String contains value |
1160
+ | `ends_with` | String ends with value |
1161
+ | `between` | Value is between two bounds (pass `value` as `[min, max]`) |
1162
+
1163
+ ##### Query Object Parameters
1164
+
1165
+ | Property | Description |
1166
+ | ------------ | ------------------------------------------------------------------------------------ |
1167
+ | `conditions` | Array of condition objects |
1168
+ | `limit` | Maximum number of records to return |
1169
+ | `offset` | Number of records to skip (for pagination) |
1170
+ | `select` | Array of attribute names to return; supports `$id` and `$updatedtime` |
1171
+ | `sort` | Object with `attribute`, `descending` (bool), and optional `next` for secondary sort |
1172
+
1173
+ ##### Examples
1174
+
1175
+ **Simple filter:**
1176
+
1177
+ ```javascript
1178
+ for await (const record of tables.Product.search({
1179
+ conditions: [{ attribute: 'price', comparator: 'less_than', value: 100 }],
1180
+ limit: 20,
1181
+ })) { ... }
1182
+ ```
1183
+
1184
+ **AND + nested OR:**
1185
+
1186
+ ```javascript
1187
+ for await (const record of tables.Product.search({
1188
+ conditions: [
1189
+ { attribute: 'price', comparator: 'less_than', value: 100 },
1190
+ {
1191
+ operator: 'or',
1192
+ conditions: [
1193
+ { attribute: 'rating', comparator: 'greater_than', value: 4 },
1194
+ { attribute: 'featured', value: true },
1195
+ ],
1196
+ },
1197
+ ],
1198
+ })) { ... }
1199
+ ```
1200
+
1201
+ **Relationship traversal:**
1202
+
1203
+ ```javascript
1204
+ for await (const record of tables.Book.search({
1205
+ conditions: [{ attribute: ['brand', 'name'], comparator: 'equals', value: 'Harper' }],
1206
+ })) { ... }
1207
+ ```
1208
+
1209
+ **Sort and paginate:**
1210
+
1211
+ ```javascript
1212
+ for await (const record of tables.Product.search({
1213
+ conditions: [{ attribute: 'inStock', value: true }],
1214
+ sort: { attribute: 'price', descending: false },
1215
+ limit: 10,
1216
+ offset: 20,
1217
+ })) { ... }
1218
+ ```
1219
+
1220
+ #### Cautions
1221
+
1222
+ Be very careful when performing updates and deletions! You may be dealing with live production data. The wrong request to delete, without approval from a human, could be devastating to a business. Always use the proper approval process.
1223
+
1224
+ ### 3.4 TypeScript Type Stripping in Harper
1225
+
1226
+ Instructions for the agent to run `.ts` files directly in Harper without a build step using Node.js's built-in type stripping.
1227
+
1228
+ #### When to Use
1229
+
1230
+ Apply this rule when writing Harper resource files in TypeScript. Use it any time you need to reference `.ts` source files from `config.yaml` or import between local TypeScript modules in a Harper project.
1231
+
1232
+ #### How It Works
1233
+
1234
+ 1. **Ensure Node.js version**: Require Node.js 22.6 or later. Type stripping is unavailable on earlier versions.
1235
+
1236
+ 2. **Point `jsResource` at `.ts` files**: The `jsResource` plugin loads both `.js` and `.ts` files. Set its `files` glob in `config.yaml` to target your `.ts` source files:
1237
+
1238
+ ```yaml
1239
+ jsResource:
1240
+ files: 'resources/*.ts'
1241
+ ```
1242
+
1243
+ 3. **Use explicit `.ts` extensions in local imports**: Node's loader does not resolve `'./helper'` to `'./helper.ts'`, so always include the full extension:
1244
+
1245
+ ```typescript
1246
+ import { helper } from './helper.ts';
1247
+ ```
1248
+
1249
+ 4. **Stay within type-stripping limits**: Only type annotations and declarations are removed. Do not use enums with runtime values, namespaces with runtime semantics, or any other features that require code transformation beyond type stripping.
1250
+
1251
+ #### Examples
1252
+
1253
+ A complete Harper resource written in TypeScript, using imports from the `harper` package:
1254
+
1255
+ ```typescript
1256
+ import { type RequestTargetOrId, Resource, tables } from 'harper';
1257
+
1258
+ export class MyResource extends Resource {
1259
+ async get(target?: RequestTargetOrId): Promise<{ message: string }> {
1260
+ return { message: 'Hello from TS' };
1261
+ }
1262
+ }
1263
+ ```
1264
+
1265
+ Paired `config.yaml` entry loading the file via `jsResource`:
1266
+
1267
+ ```yaml
1268
+ jsResource:
1269
+ files: 'resources/*.ts'
1270
+ ```
1271
+
1272
+ #### Notes
1273
+
1274
+ - No build step or transpiler is required — Harper runs `.ts` files directly.
1275
+ - Type imports (e.g., `import { type RequestTargetOrId }`) from the `harper` package work as usual.
1276
+ - Unsupported TypeScript features include: enums with runtime values, namespaces with runtime semantics, and anything requiring code transformation beyond simple type stripping.
1277
+
1278
+ ### 3.5 Caching External Data Sources in Harper
1279
+
1280
+ Instructions for the agent to implement integrated data caching in Harper by wrapping external sources with a cache table and `sourcedFrom`.
1281
+
1282
+ #### When to Use
1283
+
1284
+ 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.
1285
+
1286
+ #### How It Works
1287
+
1288
+ 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.
1289
+
1290
+ ```graphql
1291
+ type JokeCache @table(expiration: 60) @export {
1292
+ id: ID @primaryKey
1293
+ setup: String
1294
+ punchline: String
1295
+ }
1296
+ ```
1297
+
1298
+ 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.
1299
+
1300
+ ```javascript
1301
+ const jokeAPI = {
1302
+ async get(id) {
1303
+ const response = await fetch(`https://official-joke-api.appspot.com/jokes/${id}`);
1304
+ return response.json();
1305
+ },
1306
+ };
1307
+
1308
+ tables.JokeCache.sourcedFrom(jokeAPI);
1309
+ ```
1310
+
1311
+ Harper's caching behavior after `sourcedFrom` is registered:
1312
+ - A request arrives for `/JokeCache/1`.
1313
+ - Harper checks if the record with id `1` exists in `JokeCache` and is not stale.
1314
+ - If fresh, Harper returns it immediately.
1315
+ - If missing or stale, Harper calls `jokeAPI.get()`, stores the result in `JokeCache`, and returns it.
1316
+ - Multiple simultaneous requests for the same missing or stale record wait on a single upstream call — Harper prevents cache stampedes automatically.
1317
+
1318
+ 3. **Configure plugins in `config.yaml`**: Enable the schema, REST API, and JS resource plugins.
1319
+
1320
+ ```yaml
1321
+ graphqlSchema:
1322
+ files: 'schema.graphql'
1323
+ rest: true
1324
+ jsResource:
1325
+ files: 'resources.js'
1326
+ ```
1327
+
1328
+ 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.
1329
+
1330
+ ```bash
1331
+ curl -i 'http://localhost:9926/JokeCache/1' \
1332
+ -H 'If-None-Match: "abCDefGHij"'
1333
+ ```
1334
+
1335
+ 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.
1336
+
1337
+ ```bash
1338
+ curl -i 'http://localhost:9926/JokeCache/1' \
1339
+ -H 'Cache-Control: no-cache'
1340
+ ```
1341
+
1342
+ 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)`.
1343
+
1344
+ ```graphql
1345
+ type JokeCache @table(expiration: 60) {
1346
+ id: ID @primaryKey
1347
+ setup: String
1348
+ punchline: String
1349
+ }
1350
+ ```
1351
+
1352
+ ```javascript
1353
+ export class JokeCache extends tables.JokeCache {
1354
+ static async post(target, data) {
1355
+ const body = await data;
1356
+ if (body?.action === 'invalidate') {
1357
+ this.invalidate(target);
1358
+ return { status: 200, data: { message: 'invalidated' } };
1359
+ }
1360
+ }
1361
+ }
1362
+ ```
1363
+
1364
+ Trigger invalidation with a `POST`:
1365
+
1366
+ ```bash
1367
+ curl -X POST 'http://localhost:9926/JokeCache/1' \
1368
+ -H 'Content-Type: application/json' \
1369
+ -d '{"action": "invalidate"}'
1370
+ ```
1371
+
1372
+ The next `GET /JokeCache/1` will fetch fresh data from the upstream source regardless of TTL.
1373
+
1374
+ #### Examples
1375
+
1376
+ Complete `schema.graphql` and `resources.js` for a cached external API with on-demand invalidation:
1377
+
1378
+ ```graphql
1379
+ type JokeCache @table(expiration: 60) {
1380
+ id: ID @primaryKey
1381
+ setup: String
1382
+ punchline: String
1383
+ }
1384
+ ```
1385
+
1386
+ ```javascript
1387
+ // resources.js
1388
+
1389
+ const jokeAPI = {
1390
+ async get() {
1391
+ const id = this.getId();
1392
+ const response = await fetch(`https://official-joke-api.appspot.com/jokes/${id}`);
1393
+ return response.json();
1394
+ },
1395
+ };
1396
+
1397
+ tables.JokeCache.sourcedFrom(jokeAPI);
1398
+
1399
+ export class JokeCache extends tables.JokeCache {
1400
+ static async post(target, data) {
1401
+ const body = await data;
1402
+ if (body?.action === 'invalidate') {
1403
+ this.invalidate(target);
1404
+ return { status: 200, data: { message: 'invalidated' } };
1405
+ }
1406
+ }
1407
+ }
1408
+ ```
1409
+
1410
+ First request — cache miss, upstream is called, `200` returned:
1411
+
1412
+ ```bash
1413
+ curl -i 'http://localhost:9926/JokeCache/1'
1414
+ ```
1415
+
1416
+ Second request with ETag — cache hit, `304 Not Modified`:
1417
+
1418
+ ```bash
1419
+ curl -i 'http://localhost:9926/JokeCache/1' \
1420
+ -H 'If-None-Match: "abCDefGHij"'
1421
+ ```
1422
+
1423
+ #### Notes
1424
+
1425
+ - `expiration` is measured in seconds. Harper also supports separate `eviction` and `scanInterval` arguments on `@table` for fine-grained control over physical record removal.
1426
+ - 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.
1427
+ - 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.
1428
+ - ETag values include their double quotes as part of the value — include them verbatim when passing the value in `If-None-Match`.
1429
+ - `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`.
1430
+
1431
+ ## 4. Infrastructure & Ops
1432
+
1433
+ ### 4.1 Deploying to Harper Fabric
1434
+
1435
+ Instructions for the agent to follow when deploying a Harper application to the Harper Fabric cloud using the Harper CLI.
1436
+
1437
+ #### When to Use
1438
+
1439
+ 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.
1440
+
1441
+ #### How It Works
1442
+
1443
+ 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)).
1444
+
1445
+ ```bash
1446
+ harper login <Application URL>
1447
+ # Provide cluster username and password when prompted
1448
+ ```
1449
+
1450
+ 2. **Deploy the application**: Run `harper deploy` with the required parameters. After logging in, no credentials are needed inline.
1451
+
1452
+ ```bash
1453
+ harper deploy \
1454
+ project=<name> \
1455
+ package=<package> \
1456
+ target=<remote> \
1457
+ restart=true \
1458
+ replicated=true
1459
+ ```
1460
+
1461
+ 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.
1462
+
1463
+ | Value | Effect |
1464
+ | ---------------------------------------------------- | ------------------------------------------------ |
1465
+ | _(omitted)_ | Packages and deploys the current local directory |
1466
+ | `"@harperdb/status-check"` | npm package |
1467
+ | `"HarperDB/status-check"` | GitHub repo (short form) |
1468
+ | `"https://github.com/HarperDB/status-check"` | GitHub repo (full URL) |
1469
+ | `"git+ssh://git@github.com:HarperDB/secret-app.git"` | Private repo via SSH |
1470
+ | `"https://example.com/application.tar.gz"` | Remote tarball |
1471
+
1472
+ For git tags, use the `semver` directive for reliable versioning:
1473
+
1474
+ ```
1475
+ HarperDB/application-template#semver:v1.0.0
1476
+ ```
1477
+
1478
+ 4. **Authenticate for CI/CD pipelines**: Use environment variables instead of interactive login. Set credentials before running `harper deploy`.
1479
+
1480
+ ```bash
1481
+ export HARPER_CLI_USERNAME=<username>
1482
+ export HARPER_CLI_PASSWORD=<password>
1483
+ harper deploy \
1484
+ project=<name> \
1485
+ package=<package> \
1486
+ target=<remote> \
1487
+ restart=true \
1488
+ replicated=true
1489
+ ```
1490
+
1491
+ 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.
1492
+
1493
+ #### Examples
1494
+
1495
+ **Interactive login then deploy (recommended):**
1496
+
1497
+ ```bash
1498
+ # Log in once
1499
+ harper login <remote>
1500
+ # Provide your username and password when prompted
1501
+
1502
+ # Subsequently deploy without credentials
1503
+ harper deploy \
1504
+ project=<name> \
1505
+ package=<package> \
1506
+ target=<remote> \
1507
+ restart=true \
1508
+ replicated=true
1509
+ ```
1510
+
1511
+ **Deploy with inline credentials (not recommended for production):**
1512
+
1513
+ ```bash
1514
+ harper deploy \
1515
+ project=<name> \
1516
+ package=<package> \
1517
+ username=<username> \
1518
+ password=<password> \
1519
+ target=<remote> \
1520
+ restart=true \
1521
+ replicated=true
1522
+ ```
1523
+
1524
+ **Deploy a specific GitHub release by semver tag:**
1525
+
1526
+ ```bash
1527
+ harper deploy \
1528
+ project=my-app \
1529
+ package="HarperDB/application-template#semver:v1.0.0" \
1530
+ target=<remote> \
1531
+ restart=true \
1532
+ replicated=true
1533
+ ```
1534
+
1535
+ #### Notes
1536
+
1537
+ - 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.
1538
+ - 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.
1539
+ - Harper generates a `package.json` from component configurations and resolves dependencies using a form of `npm install`.
1540
+ - For SSH-based private repos, register keys with the Add SSH Key operation before deploying.
1541
+
1542
+ ### 4.2 Creating a Harper Fabric Account and Cluster
1543
+
1544
+ Follow these steps to set up your Harper Fabric environment for deployment.
1545
+
1546
+ #### How It Works
1547
+
1548
+ 1. **Sign Up/In**: Go to [https://fabric.harper.fast/](https://fabric.harper.fast/) and sign up or sign in.
1549
+ 2. **Create an Organization**: Create an organization (org) to manage your projects.
1550
+ 3. **Create a Cluster**: Create a new cluster. This can be on the free tier, no credit card required.
1551
+ 4. **Set Credentials**: During setup, set the cluster username and password to finish configuring it.
1552
+ 5. **Get Application URL**: Navigate to the **Config** tab and copy the **Application URL**.
1553
+ 6. **Configure Environment**: Update your `.env` file or GitHub Actions secrets with cluster-specific credentials.
1554
+ 7. **Next Steps**: See the [deploying-to-harper-fabric](deploying-to-harper-fabric.md) rule for detailed instructions on deploying your application successfully.
1555
+
1556
+ #### Examples
1557
+
1558
+ ##### Environment Configuration
1559
+
1560
+ ```bash
1561
+ CLI_TARGET_USERNAME='YOUR_CLUSTER_USERNAME'
1562
+ CLI_TARGET_PASSWORD='YOUR_CLUSTER_PASSWORD'
1563
+ CLI_TARGET='YOUR_CLUSTER_URL'
1564
+ ```
1565
+
1566
+ ### 4.3 Creating Harper Applications
1567
+
1568
+ The fastest way to start a new Harper project is using the `create-harper` CLI tool. This command
1569
+ initializes a project with a standard folder structure, essential configuration files, and basic
1570
+ schema definitions.
1571
+
1572
+ #### When to Use
1573
+
1574
+ Use this command when starting a new Harper application or adding a new Harper microservice to an
1575
+ existing architecture.
1576
+
1577
+ #### Commands
1578
+
1579
+ Initialize a project using your preferred package manager:
1580
+
1581
+ ##### NPM
1582
+
1583
+ ```bash
1584
+ npm create harper@latest
1585
+ ```
1586
+
1587
+ ##### PNPM
1588
+
1589
+ ```bash
1590
+ pnpm create harper@latest
1591
+ ```
1592
+
1593
+ ##### Bun
1594
+
1595
+ ```bash
1596
+ bun create harper@latest
1597
+ ```
1598
+
1599
+ #### Options
1600
+
1601
+ You can specify the project name and template directly:
1602
+
1603
+ ```bash
1604
+ npm create harper@latest my-app --template default
1605
+ ```
1606
+
1607
+ #### Next Steps
1608
+
1609
+ 1. **Configure Environment**: Set up your `.env` file with local or cloud credentials.
1610
+ 2. **Define Schema**: Modify `schema.graphql` to fit your application's data model.
1611
+ 3. **Start Development**: Run `npm run dev` to start the local Harper instance.
1612
+ 4. **Deploy**: Use `npm run deploy` to push your application to Harper Fabric.
1613
+
1614
+ ### 4.4 Serving Web Content
1615
+
1616
+ Instructions for the agent to follow when serving web content from Harper.
1617
+
1618
+ #### When to Use
1619
+
1620
+ Use this skill when you need to serve a frontend (HTML, CSS, JS, or a React app) directly from your Harper instance.
1621
+
1622
+ #### How It Works
1623
+
1624
+ 1. **Choose a Method**: Decide between the simple Static Plugin or the integrated Vite Plugin.
1625
+ 2. **Option A: Static Plugin (Simple)**:
1626
+ - Add to `config.yaml`:
1627
+ ```yaml
1628
+ static:
1629
+ files: 'web/*'
1630
+ ```
1631
+ - Place files in a `web/` folder in the project root.
1632
+ - Files are served at the root URL (e.g., `http://localhost:9926/index.html`).
1633
+ 3. **Option B: Vite Plugin (Advanced/Development)**:
1634
+ - Add to `config.yaml`:
1635
+ ```yaml
1636
+ '@harperfast/vite-plugin':
1637
+ package: '@harperfast/vite-plugin'
1638
+ ```
1639
+ - Ensure `vite.config.ts` and `index.html` are in the project root.
1640
+
1641
+ ```javascript
1642
+ import vue from '@vitejs/plugin-vue';
1643
+ import path from 'node:path';
1644
+ import { defineConfig } from 'vite';
1645
+
1646
+ // https://vite.dev/config/
1647
+ export default defineConfig({
1648
+ plugins: [vue()],
1649
+ resolve: {
1650
+ alias: {
1651
+ '@': path.resolve(import.meta.dirname, './src'),
1652
+ },
1653
+ },
1654
+ build: {
1655
+ outDir: 'web',
1656
+ emptyOutDir: true,
1657
+ rolldownOptions: {
1658
+ external: ['**/*.test.*', '**/*.spec.*'],
1659
+ },
1660
+ },
1661
+ });
1662
+ ```
1663
+
1664
+ - Install dependencies: `npm install --save-dev vite @harperfast/vite-plugin`.
1665
+ - Then `harper run .` will start up Harper and Vite with HMR. Vite does _not_ need to be executed separately.
1666
+
1667
+ 4. **Deploy for Production**: For Vite apps, use a build script to generate static files into a `web/` folder and deploy them using the static handler pattern. For example, these scripts in a package.json can perform the necessary steps:
1668
+ ```json
1669
+ "build": "vite build",
1670
+ "deploy": "rm -Rf deploy && npm run build && mkdir deploy && mv web deploy/ && cp -R deploy-template/* deploy/ && cp -R schemas resources deploy/ && (cd deploy && harper deploy_component . project=web restart=rolling replicated=true) && rm -Rf deploy",
1671
+ ```
1672
+ Then in production, the "Static Plugin" option will performantly and securely serve your assets. `npm create harper@latest` scaffolds all of this for you.
1673
+
1674
+ ### 4.5 Harper Logging
1675
+
1676
+ Instructions for the agent to follow when implementing logging in Harper applications, including direct logger usage, tagged loggers, and console capture behavior.
1677
+
1678
+ #### When to Use
1679
+
1680
+ 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.
1681
+
1682
+ #### How It Works
1683
+
1684
+ 1. **Use the `logger` global directly** — `logger` is available in all JavaScript components without any imports. Call the method matching the desired severity level:
1685
+
1686
+ ```javascript
1687
+ logger.trace('detailed trace message');
1688
+ logger.debug('debug info', { someContext: 'value' });
1689
+ logger.info('informational message');
1690
+ logger.warn('potential issue');
1691
+ logger.error('error occurred', error);
1692
+ logger.fatal('fatal error');
1693
+ logger.notify('server is ready');
1694
+ ```
1695
+
1696
+ Only entries at or above the configured `logging.level` (or `logging.external.level`) are written to `hdb.log`.
1697
+
1698
+ 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.
1699
+
1700
+ ```javascript
1701
+ const log = logger.withTag('my-resource');
1702
+ ```
1703
+
1704
+ Because `TaggedLogger` methods for disabled levels are `null`, always use optional chaining (`?.`) when calling them:
1705
+
1706
+ ```javascript
1707
+ log.debug?.('Fetching record', { id });
1708
+ log.warn?.('Record not found', { id });
1709
+ log.error?.('Failed to update record', err);
1710
+ ```
1711
+
1712
+ `TaggedLogger` does not have a `withTag()` method.
1713
+
1714
+ 3. **Understand the interface contracts** — `MainLogger` always has all methods defined:
1715
+
1716
+ ```typescript
1717
+ interface MainLogger {
1718
+ trace(...messages: any[]): void;
1719
+ debug(...messages: any[]): void;
1720
+ info(...messages: any[]): void;
1721
+ warn(...messages: any[]): void;
1722
+ error(...messages: any[]): void;
1723
+ fatal(...messages: any[]): void;
1724
+ notify(...messages: any[]): void;
1725
+ withTag(tag: string): TaggedLogger;
1726
+ }
1727
+ ```
1728
+
1729
+ `TaggedLogger` methods may be `null`:
1730
+
1731
+ ```typescript
1732
+ interface TaggedLogger {
1733
+ trace: ((...messages: any[]) => void) | null;
1734
+ debug: ((...messages: any[]) => void) | null;
1735
+ info: ((...messages: any[]) => void) | null;
1736
+ warn: ((...messages: any[]) => void) | null;
1737
+ error: ((...messages: any[]) => void) | null;
1738
+ fatal: ((...messages: any[]) => void) | null;
1739
+ notify: ((...messages: any[]) => void) | null;
1740
+ }
1741
+ ```
1742
+
1743
+ 4. **Know the log levels** — From least to most severe:
1744
+
1745
+ | Level | Description |
1746
+ | -------- | -------------------------------------------------------------------- |
1747
+ | `trace` | Highly detailed internal execution tracing. |
1748
+ | `debug` | Diagnostic information useful during development. |
1749
+ | `info` | General operational events. |
1750
+ | `warn` | Potential issues that don't prevent normal operation. |
1751
+ | `error` | Errors that affect specific operations. |
1752
+ | `fatal` | Critical errors causing process termination. |
1753
+ | `notify` | Important operational milestones. Always logged regardless of level. |
1754
+
1755
+ The default log level is `warn`. Setting a level includes that level and all more-severe levels.
1756
+
1757
+ 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.
1758
+
1759
+ 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`.
1760
+
1761
+ #### Examples
1762
+
1763
+ ##### Basic logging in a resource
1764
+
1765
+ ```javascript
1766
+ export class MyResource extends Resource {
1767
+ async get(id) {
1768
+ logger.debug('Fetching record', { id });
1769
+ const record = await super.get(id);
1770
+ if (!record) {
1771
+ logger.warn('Record not found', { id });
1772
+ }
1773
+ return record;
1774
+ }
1775
+
1776
+ async put(record) {
1777
+ logger.info('Updating record', { id: record.id });
1778
+ try {
1779
+ return await super.put(record);
1780
+ } catch (err) {
1781
+ logger.error('Failed to update record', err);
1782
+ throw err;
1783
+ }
1784
+ }
1785
+ }
1786
+ ```
1787
+
1788
+ ##### Tagged logging with `withTag()`
1789
+
1790
+ ```javascript
1791
+ const log = logger.withTag('my-resource');
1792
+
1793
+ export class MyResource extends Resource {
1794
+ async get(id) {
1795
+ log.debug?.('Fetching record', { id });
1796
+ const record = await super.get(id);
1797
+ if (!record) {
1798
+ log.warn?.('Record not found', { id });
1799
+ }
1800
+ return record;
1801
+ }
1802
+
1803
+ async put(record) {
1804
+ log.info?.('Updating record', { id: record.id });
1805
+ try {
1806
+ return await super.put(record);
1807
+ } catch (err) {
1808
+ log.error?.('Failed to update record', err);
1809
+ throw err;
1810
+ }
1811
+ }
1812
+ }
1813
+ ```
1814
+
1815
+ Tagged entries appear in `hdb.log` with the tag in the header:
1816
+
1817
+ ```
1818
+ 2023-03-09T14:25:05.269Z [info] [my-resource]: Updating record
1819
+ ```
1820
+
1821
+ #### Notes
1822
+
1823
+ - All log output is written to `<ROOTPATH>/log/hdb.log`. The `logger` global writes to this file at the configured `logging.external` level.
1824
+ - Log entry format for `logger`: `<timestamp> [<level>] [<thread>/<id>]: <message>`
1825
+ - Log entry format for `TaggedLogger`: `<timestamp> [<level>] [<tag>]: <message>`
1826
+ - `console.log` output is only forwarded to `hdb.log` when `logging.console: true` is explicitly set; it is not forwarded by default.
1827
+ - When logging to standard streams, run Harper in the foreground (`harper`, not `harper start`).
1828
+ - `TaggedLogger` is bound to the configured log level at creation time — always use `?.` on its methods.
1829
+
1830
+ ### 4.6 Load Environment Variables with loadEnv
1831
+
1832
+ Instructions for the agent to follow when loading environment variables from `.env` files into a Harper application using the `loadEnv` plugin.
1833
+
1834
+ #### When to Use
1835
+
1836
+ Apply this rule when a Harper application needs to load secrets or configuration values from `.env` files into `process.env` at startup. Use it whenever you need to configure `loadEnv` in `config.yaml`, control load order, handle multiple files, or manage override behavior.
1837
+
1838
+ #### How It Works
1839
+
1840
+ 1. **Declare `loadEnv` in `config.yaml`**: Add `loadEnv` to your `config.yaml` with a `files` key pointing to the `.env` file. `loadEnv` is built into Harper and does not need to be installed separately.
1841
+
1842
+ ```yaml
1843
+ loadEnv:
1844
+ files: '.env'
1845
+ ```
1846
+
1847
+ This loads the specified file from the root of your component directory into `process.env`.
1848
+
1849
+ 2. **Place `loadEnv` first**: Always declare `loadEnv` before any other components in `config.yaml` so environment variables are available before dependent components start. Because Harper is single-process, variables loaded onto `process.env` are shared across all components.
1850
+
1851
+ ```yaml
1852
+ # config.yaml — loadEnv must come first
1853
+ loadEnv:
1854
+ files: '.env'
1855
+
1856
+ rest: true
1857
+
1858
+ myApp:
1859
+ files: './src/*.js'
1860
+ ```
1861
+
1862
+ 3. **Control override behavior**: By default, existing shell or container environment variables take precedence over values in `.env` files. To force `.env` values to overwrite existing variables, set `override: true`.
1863
+
1864
+ ```yaml
1865
+ loadEnv:
1866
+ files: '.env'
1867
+ override: true
1868
+ ```
1869
+
1870
+ 4. **Load multiple files**: Provide a list of files or a glob pattern under `files`. Files are loaded in the order specified.
1871
+ ```yaml
1872
+ loadEnv:
1873
+ files:
1874
+ - '.env'
1875
+ - '.env.local'
1876
+ ```
1877
+ Or using a glob pattern:
1878
+ ```yaml
1879
+ loadEnv:
1880
+ files: 'env-vars/*'
1881
+ ```
1882
+
1883
+ #### Examples
1884
+
1885
+ A complete `config.yaml` using `loadEnv` with multiple files and override enabled:
1886
+
1887
+ ```yaml
1888
+ # config.yaml — loadEnv must come first
1889
+ loadEnv:
1890
+ files:
1891
+ - '.env'
1892
+ - '.env.local'
1893
+ override: true
1894
+
1895
+ rest: true
1896
+
1897
+ myApp:
1898
+ files: './src/*.js'
1899
+ ```
1900
+
1901
+ A minimal setup loading a single `.env` file:
1902
+
1903
+ ```yaml
1904
+ loadEnv:
1905
+ files: '.env'
1906
+
1907
+ myApp:
1908
+ files: './src/*.js'
1909
+ ```
583
1910
 
584
- Each log entry returned by `read_log` typically includes:
1911
+ #### Notes
585
1912
 
586
- - `level`: The severity level of the log.
587
- - `timestamp`: When the log was recorded.
588
- - `thread`: The execution thread.
589
- - `tags`: An array of tags (e.g., from `loggerWithTag`).
590
- - `node`: The node name in a Harper cluster.
591
- - `message`: The logged content.
1913
+ - `loadEnv` is built into Harper declare it in `config.yaml` only; do not install it as a separate package.
1914
+ - The `files` value accepts either a single string, a list of strings, or a glob pattern.
1915
+ - Without `override: true`, variables already present in the environment are never overwritten by values in `.env` files.
1916
+ - `process.env` is shared across all Harper components in the same process, so load order in `config.yaml` determines availability.