@harperfast/template-vue-ts-studio 1.12.12 → 1.13.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.
- package/.agents/skills/harper-best-practices/AGENTS.md +184 -151
- package/.agents/skills/harper-best-practices/rules/custom-resources.md +63 -29
- package/.agents/skills/harper-best-practices/rules/deploying-to-harper-fabric.md +56 -71
- package/.agents/skills/harper-best-practices/rules/programmatic-table-requests.md +71 -57
- package/README.md +0 -48
- package/agent/skills/harper-best-practices/AGENTS.md +184 -151
- package/agent/skills/harper-best-practices/rules/custom-resources.md +63 -29
- package/agent/skills/harper-best-practices/rules/deploying-to-harper-fabric.md +56 -71
- package/agent/skills/harper-best-practices/rules/programmatic-table-requests.md +71 -57
- package/package.json +1 -1
- package/skills-lock.json +1 -1
|
@@ -14,31 +14,37 @@ metadata:
|
|
|
14
14
|
reference/v5/resources/resource-api.md#`addTo(property: string, value:
|
|
15
15
|
number)`
|
|
16
16
|
- reference/v5/components/javascript-environment.md#Module Formats
|
|
17
|
-
sourceCommit:
|
|
18
|
-
inputHash:
|
|
17
|
+
sourceCommit: e73e5efb2823cc52caf7a6458d67fbcff3bbff2c
|
|
18
|
+
inputHash: bb360b0d6c0a7bd4
|
|
19
19
|
---
|
|
20
20
|
|
|
21
21
|
# Programmatic Table Requests
|
|
22
22
|
|
|
23
|
-
Instructions for the agent to
|
|
23
|
+
Instructions for the agent to follow when interacting with Harper tables programmatically using the `tables` object.
|
|
24
24
|
|
|
25
25
|
## When to Use
|
|
26
26
|
|
|
27
|
-
Apply this rule when writing server-side Harper component code that reads from or writes to tables
|
|
27
|
+
Apply this rule when writing server-side Harper component code that reads from or writes to tables using the programmatic API — for example, in HTTP handlers, background jobs, timers, or SSR entry points. Use it whenever you need to construct queries with `conditions`, `sort`, `select`, pagination, or transactions outside of the REST layer.
|
|
28
28
|
|
|
29
29
|
## How It Works
|
|
30
30
|
|
|
31
|
-
1. **Import `tables` (and other APIs) from `harper`**: Access every table defined in `schema.graphql` as a
|
|
31
|
+
1. **Import `tables` (and other APIs) from `harper`**: Access every table defined in `schema.graphql` as a property of `tables`. Each property is the table class implementing the Resource API.
|
|
32
32
|
|
|
33
33
|
```javascript
|
|
34
34
|
import { tables, transaction } from 'harper';
|
|
35
35
|
const { Product } = tables;
|
|
36
|
-
//
|
|
36
|
+
// same as: databases.data.Product
|
|
37
37
|
```
|
|
38
38
|
|
|
39
|
-
For
|
|
39
|
+
For components in their own directory, run:
|
|
40
40
|
|
|
41
|
-
|
|
41
|
+
```bash
|
|
42
|
+
npm link harper
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
All installed components have `harper` automatically linked.
|
|
46
|
+
|
|
47
|
+
2. **Define your schema with `@table`**: Every type you want to access via `tables` must carry the `@table` directive. Mark queryable fields with `@indexed`.
|
|
42
48
|
|
|
43
49
|
```graphql
|
|
44
50
|
type Product @table {
|
|
@@ -48,7 +54,7 @@ Apply this rule when writing server-side Harper component code that reads from o
|
|
|
48
54
|
}
|
|
49
55
|
```
|
|
50
56
|
|
|
51
|
-
3. **Query records
|
|
57
|
+
3. **Query records using `search(`**: Pass a Query object to `search()`. The query is an async iterable.
|
|
52
58
|
|
|
53
59
|
```javascript
|
|
54
60
|
const query = {
|
|
@@ -59,7 +65,7 @@ Apply this rule when writing server-side Harper component code that reads from o
|
|
|
59
65
|
}
|
|
60
66
|
```
|
|
61
67
|
|
|
62
|
-
4. **Build `conditions`**: Each condition object supports
|
|
68
|
+
4. **Build `conditions`**: Each condition object supports the following properties:
|
|
63
69
|
|
|
64
70
|
| Property | Description |
|
|
65
71
|
| ------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
@@ -69,53 +75,60 @@ Apply this rule when writing server-side Harper component code that reads from o
|
|
|
69
75
|
| `conditions` | Nested conditions array |
|
|
70
76
|
| `operator` | `and` (default) or `or` for the nested `conditions` |
|
|
71
77
|
|
|
72
|
-
5. **
|
|
78
|
+
5. **Apply `select` to shape results**: Pass an array of property names, a string for a single property, or nested objects for relationships.
|
|
73
79
|
|
|
74
80
|
```javascript
|
|
75
|
-
//
|
|
81
|
+
// Flat select
|
|
76
82
|
Product.search({ select: ['name', 'price'] });
|
|
77
83
|
|
|
78
|
-
//
|
|
84
|
+
// Nested relationship select
|
|
79
85
|
Book.get({ id: 42, select: ['id', 'title', { name: 'author', select: ['name'] }] });
|
|
80
86
|
```
|
|
81
87
|
|
|
82
88
|
Special `select` values: `$id`, `$updatedtime`, `$distance`.
|
|
83
89
|
|
|
84
|
-
6. **
|
|
85
|
-
|
|
86
|
-
```javascript
|
|
87
|
-
Product.search({ conditions: [...], limit: 20, offset: 40 });
|
|
88
|
-
```
|
|
90
|
+
6. **Apply `sort`**: Harper uses an index to provide sort order.
|
|
89
91
|
|
|
90
|
-
|
|
92
|
+
| Property | Description |
|
|
93
|
+
| ------------ | ---------------------------------------------------------- |
|
|
94
|
+
| `attribute` | Property name (or array for chained relationship property) |
|
|
95
|
+
| `descending` | Sort descending if `true` (default: `false`) |
|
|
96
|
+
| `next` | Secondary sort to resolve ties (same structure) |
|
|
97
|
+
- If the sort `attribute` is `@indexed`, no condition is required.
|
|
98
|
+
- If the sort `attribute` is not indexed, at least one entry in `conditions` (on **any** attribute) is required.
|
|
99
|
+
- Sorting by a non-indexed attribute with zero conditions raises:
|
|
100
|
+
> `HdbError: <attribute> is not indexed and not combined with any other conditions`
|
|
101
|
+
- The bare `@primaryKey` is treated as not indexed for sort purposes. To iterate in primary-key order, add an open-ended condition:
|
|
91
102
|
|
|
92
|
-
|
|
103
|
+
```javascript
|
|
104
|
+
Product.search({
|
|
105
|
+
conditions: [{ attribute: 'id', comparator: 'greater_than', value: '' }],
|
|
106
|
+
sort: { attribute: 'id' },
|
|
107
|
+
});
|
|
108
|
+
```
|
|
93
109
|
|
|
94
|
-
|
|
110
|
+
- Pass `allowFullScan: true` to permit an unconditional ordered scan, or omit `sort` entirely to iterate without an index requirement.
|
|
95
111
|
|
|
96
|
-
|
|
112
|
+
7. **Paginate with `limit` and `offset`**:
|
|
97
113
|
|
|
98
114
|
```javascript
|
|
99
|
-
Product.search({
|
|
100
|
-
conditions: [{ attribute: 'id', comparator: 'greater_than', value: '' }],
|
|
101
|
-
sort: { attribute: 'id' },
|
|
102
|
-
});
|
|
115
|
+
Product.search({ conditions: [...], limit: 20, offset: 40 });
|
|
103
116
|
```
|
|
104
117
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
8. **Debug query planning with `explain` and `enforceExecutionOrder`**:
|
|
118
|
+
8. **Debug with `explain` and `enforceExecutionOrder`**:
|
|
108
119
|
- `explain: true` — returns conditions reordered as Harper will execute them.
|
|
109
|
-
- `enforceExecutionOrder: true` — forces conditions to execute in the order supplied, disabling automatic re-ordering.
|
|
120
|
+
- `enforceExecutionOrder: true` — forces conditions to execute in the order supplied, disabling Harper's automatic re-ordering.
|
|
110
121
|
|
|
111
|
-
9. **Use `addTo` for concurrent-safe
|
|
122
|
+
9. **Use `addTo` for concurrent-safe increments**: `addTo(property, value)` uses CRDT incrementation, safe across threads and nodes.
|
|
112
123
|
|
|
113
124
|
```javascript
|
|
114
|
-
|
|
115
|
-
|
|
125
|
+
static async post(target, data) {
|
|
126
|
+
const record = await this.update(target.id);
|
|
127
|
+
record.addTo('quantity', -1);
|
|
128
|
+
}
|
|
116
129
|
```
|
|
117
130
|
|
|
118
|
-
10. **Wrap background work in `transaction()`**:
|
|
131
|
+
10. **Wrap background work in `transaction()`**: Harper auto-starts transactions for HTTP handlers. Use `transaction()` explicitly for timers, background jobs, or any code outside a natural transaction context.
|
|
119
132
|
|
|
120
133
|
```javascript
|
|
121
134
|
await transaction(async (txn) => {
|
|
@@ -127,18 +140,18 @@ Apply this rule when writing server-side Harper component code that reads from o
|
|
|
127
140
|
|
|
128
141
|
The `txn` object exposes:
|
|
129
142
|
|
|
130
|
-
| Member | Description |
|
|
131
|
-
| --------------------- | ------------------------------------------------------ |
|
|
132
|
-
| `commit()` | Commits the current transaction |
|
|
133
|
-
| `abort()` | Aborts and resets
|
|
134
|
-
| `resetReadSnapshot()` | Resets the read snapshot to the latest committed state |
|
|
135
|
-
| `timestamp` | Timestamp associated with the current transaction |
|
|
143
|
+
| Member | Type | Description |
|
|
144
|
+
| --------------------- | --------------- | ------------------------------------------------------ |
|
|
145
|
+
| `commit()` | `() => Promise` | Commits the current transaction |
|
|
146
|
+
| `abort()` | `() => void` | Aborts the transaction and resets it |
|
|
147
|
+
| `resetReadSnapshot()` | `() => void` | Resets the read snapshot to the latest committed state |
|
|
148
|
+
| `timestamp` | `number` | Timestamp associated with the current transaction |
|
|
136
149
|
|
|
137
|
-
|
|
150
|
+
On normal callback completion the transaction commits automatically. If the callback throws, the transaction is aborted.
|
|
138
151
|
|
|
139
|
-
|
|
152
|
+
11. **Understand atomicity**: Transactions span a single database. All tables within the same database share one transactional context — reads return a consistent snapshot and writes across multiple tables commit atomically. Cross-database operations get separate transactions with no cross-database atomicity guarantee.
|
|
140
153
|
|
|
141
|
-
|
|
154
|
+
12. **Keep `harper` external when bundling for SSR**: In `vite.config`, mark `harper` as external so it resolves to the runtime rather than being bundled.
|
|
142
155
|
|
|
143
156
|
```javascript
|
|
144
157
|
// vite.config
|
|
@@ -149,7 +162,7 @@ Apply this rule when writing server-side Harper component code that reads from o
|
|
|
149
162
|
|
|
150
163
|
## Examples
|
|
151
164
|
|
|
152
|
-
|
|
165
|
+
**Full CRUD flow:**
|
|
153
166
|
|
|
154
167
|
```javascript
|
|
155
168
|
import { tables } from 'harper';
|
|
@@ -161,19 +174,18 @@ const created = await Product.create({ name: 'Shirt', price: 9.5 });
|
|
|
161
174
|
// Patch
|
|
162
175
|
await Product.patch(created.id, { price: Math.round(created.price * 0.8 * 100) / 100 });
|
|
163
176
|
|
|
164
|
-
//
|
|
177
|
+
// Get by primary key
|
|
165
178
|
const record = await Product.get(created.id);
|
|
166
179
|
|
|
167
|
-
//
|
|
168
|
-
const
|
|
180
|
+
// Search with conditions
|
|
181
|
+
for await (const record of Product.search({
|
|
169
182
|
conditions: [{ attribute: 'price', comparator: 'less_than', value: 8.0 }],
|
|
170
|
-
}
|
|
171
|
-
for await (const record of Product.search(query)) {
|
|
183
|
+
})) {
|
|
172
184
|
// process record
|
|
173
185
|
}
|
|
174
186
|
```
|
|
175
187
|
|
|
176
|
-
|
|
188
|
+
**Nested conditions with `or`:**
|
|
177
189
|
|
|
178
190
|
```javascript
|
|
179
191
|
Product.search({
|
|
@@ -190,13 +202,13 @@ Product.search({
|
|
|
190
202
|
});
|
|
191
203
|
```
|
|
192
204
|
|
|
193
|
-
|
|
205
|
+
**Chained attribute reference (join):**
|
|
194
206
|
|
|
195
207
|
```javascript
|
|
196
208
|
Product.search({ conditions: [{ attribute: ['brand', 'name'], value: 'Harper' }] });
|
|
197
209
|
```
|
|
198
210
|
|
|
199
|
-
|
|
211
|
+
**Background job with `transaction()`:**
|
|
200
212
|
|
|
201
213
|
```javascript
|
|
202
214
|
import { isMainThread } from 'node:worker_threads';
|
|
@@ -224,7 +236,7 @@ if (isMainThread) {
|
|
|
224
236
|
}
|
|
225
237
|
```
|
|
226
238
|
|
|
227
|
-
|
|
239
|
+
**SSR with Harper data:**
|
|
228
240
|
|
|
229
241
|
```typescript
|
|
230
242
|
import { tables } from 'harper';
|
|
@@ -237,7 +249,9 @@ export async function render(url: string): Promise<string> {
|
|
|
237
249
|
|
|
238
250
|
## Notes
|
|
239
251
|
|
|
240
|
-
- `tables` and `databases`
|
|
241
|
-
-
|
|
242
|
-
- `
|
|
243
|
-
-
|
|
252
|
+
- `tables` and `databases` calls run in a trusted server-side context and do **not** automatically apply the target table's role permissions.
|
|
253
|
+
- Programmatic `update`, `patch`, and `delete` calls operate directly on stored data. Scope destructive operations with specific `conditions`, validate the affected set before writing, and gate them behind authorization controls.
|
|
254
|
+
- If `transaction()` is called with a context that already has an active transaction, it reuses that transaction — making it safe to call defensively.
|
|
255
|
+
- Always `await` the `transaction()` call and `catch` errors when outside a request context; an unawaited call means a failed write is never observed.
|
|
256
|
+
- Guard against timer overlap: if a job can outlast its interval, use a `running` flag to skip a tick rather than opening two transactions over the same rows.
|
|
257
|
+
- CommonJS is also supported: `const { tables, Resource } = require('harper');`
|
package/package.json
CHANGED
package/skills-lock.json
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
"source": "harperfast/skills",
|
|
6
6
|
"sourceType": "github",
|
|
7
7
|
"skillPath": "harper-best-practices/SKILL.md",
|
|
8
|
-
"computedHash": "
|
|
8
|
+
"computedHash": "078c9f668f6b3bed794bbc4d52f9ab85753ed000b25c61195ed5a24511f5bec9"
|
|
9
9
|
},
|
|
10
10
|
"harper-local-dev": {
|
|
11
11
|
"source": "harperfast/skills",
|