@harperfast/skills 1.0.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.md +7 -0
- package/LICENSE +201 -0
- package/README.md +26 -0
- package/harper-best-practices/AGENTS.md +258 -0
- package/harper-best-practices/SKILL.md +88 -0
- package/harper-best-practices/rules/adding-tables-with-schemas.md +40 -0
- package/harper-best-practices/rules/automatic-apis.md +34 -0
- package/harper-best-practices/rules/caching.md +46 -0
- package/harper-best-practices/rules/checking-authentication.md +165 -0
- package/harper-best-practices/rules/custom-resources.md +35 -0
- package/harper-best-practices/rules/defining-relationships.md +33 -0
- package/harper-best-practices/rules/deploying-to-harper-fabric.md +24 -0
- package/harper-best-practices/rules/extending-tables.md +37 -0
- package/harper-best-practices/rules/handling-binary-data.md +43 -0
- package/harper-best-practices/rules/programmatic-table-requests.md +39 -0
- package/harper-best-practices/rules/querying-rest-apis.md +22 -0
- package/harper-best-practices/rules/real-time-apps.md +37 -0
- package/harper-best-practices/rules/serving-web-content.md +34 -0
- package/harper-best-practices/rules/typescript-type-stripping.md +32 -0
- package/harper-best-practices/rules/using-blob-datatype.md +36 -0
- package/harper-best-practices/rules/vector-indexing.md +152 -0
- package/package.json +35 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: caching
|
|
3
|
+
description: How to implement integrated data caching in Harper from external sources.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Caching
|
|
7
|
+
|
|
8
|
+
Instructions for the agent to follow when implementing caching in Harper.
|
|
9
|
+
|
|
10
|
+
## When to Use
|
|
11
|
+
|
|
12
|
+
Use this skill when you need high-performance, low-latency storage for data from external sources. It's ideal for reducing API calls to third-party services, preventing cache stampedes, and making external data queryable as if it were native Harper tables.
|
|
13
|
+
|
|
14
|
+
## Steps
|
|
15
|
+
|
|
16
|
+
1. **Configure a Cache Table**: Define a table in your `schema.graphql` with an `expiration` (in seconds):
|
|
17
|
+
```graphql
|
|
18
|
+
type MyCache @table(expiration: 3600) @export {
|
|
19
|
+
id: ID @primaryKey
|
|
20
|
+
}
|
|
21
|
+
```
|
|
22
|
+
2. **Define an External Source**: Create a Resource class that fetches the data:
|
|
23
|
+
```js
|
|
24
|
+
import { Resource } from 'harperdb';
|
|
25
|
+
|
|
26
|
+
export class ThirdPartyAPI extends Resource {
|
|
27
|
+
async get() {
|
|
28
|
+
const id = this.getId();
|
|
29
|
+
const response = await fetch(`https://api.example.com/items/${id}`);
|
|
30
|
+
if (!response.ok) {
|
|
31
|
+
throw new Error('Source fetch failed');
|
|
32
|
+
}
|
|
33
|
+
return await response.json();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
3. **Attach Source to Table**: Use `sourcedFrom` to link your resource to the table:
|
|
38
|
+
```js
|
|
39
|
+
import { tables } from 'harperdb';
|
|
40
|
+
import { ThirdPartyAPI } from './ThirdPartyAPI.js';
|
|
41
|
+
|
|
42
|
+
const { MyCache } = tables;
|
|
43
|
+
MyCache.sourcedFrom(ThirdPartyAPI);
|
|
44
|
+
```
|
|
45
|
+
4. **Implement Active Caching (Optional)**: Use `subscribe()` for proactive updates. See [Real Time Apps](real-time-apps.md).
|
|
46
|
+
5. **Implement Write-Through Caching (Optional)**: Define `put` or `post` in your resource to propagate updates upstream.
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: checking-authentication
|
|
3
|
+
description: How to handle user authentication and sessions in Harper Resources.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Checking Authentication
|
|
7
|
+
|
|
8
|
+
Instructions for the agent to follow when handling authentication and sessions.
|
|
9
|
+
|
|
10
|
+
## When to Use
|
|
11
|
+
|
|
12
|
+
Use this skill when you need to implement sign-in/sign-out functionality, protect specific resource endpoints, or identify the currently logged-in user in a Harper application.
|
|
13
|
+
|
|
14
|
+
## Steps
|
|
15
|
+
|
|
16
|
+
1. **Configure Harper for Sessions**: Ensure `harperdb-config.yaml` has sessions enabled and local auto-authorization disabled for testing:
|
|
17
|
+
```yaml
|
|
18
|
+
authentication:
|
|
19
|
+
authorizeLocal: false
|
|
20
|
+
enableSessions: true
|
|
21
|
+
```
|
|
22
|
+
2. **Implement Sign In**: Use `this.getContext().login(username, password)` to create a session:
|
|
23
|
+
```ts
|
|
24
|
+
async post(_target, data) {
|
|
25
|
+
const context = this.getContext();
|
|
26
|
+
try {
|
|
27
|
+
await context.login(data.username, data.password);
|
|
28
|
+
} catch {
|
|
29
|
+
return new Response('Invalid credentials', { status: 403 });
|
|
30
|
+
}
|
|
31
|
+
return new Response('Logged in', { status: 200 });
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
3. **Identify Current User**: Use `this.getCurrentUser()` to access session data:
|
|
35
|
+
```ts
|
|
36
|
+
async get() {
|
|
37
|
+
const user = this.getCurrentUser?.();
|
|
38
|
+
if (!user) return new Response(null, { status: 401 });
|
|
39
|
+
return { username: user.username, role: user.role };
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
4. **Implement Sign Out**: Use `this.getContext().logout()` or delete the session from context:
|
|
43
|
+
```ts
|
|
44
|
+
async post() {
|
|
45
|
+
const context = this.getContext();
|
|
46
|
+
await context.session?.delete?.(context.session.id);
|
|
47
|
+
return new Response('Logged out', { status: 200 });
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
5. **Protect Routes**: In your Resource, use `allowRead()`, `allowUpdate()`, etc., to enforce authorization logic based on `this.getCurrentUser()`. For privileged actions, verify `user.role.permission.super_user`.
|
|
51
|
+
|
|
52
|
+
## Status code conventions used here
|
|
53
|
+
|
|
54
|
+
- 200: Successful operation. For `GET /me`, a `200` with empty body means “not signed in”.
|
|
55
|
+
- 400: Missing required fields (e.g., username/password on sign-in).
|
|
56
|
+
- 401: No current session for an action that requires one (e.g., sign out when not signed in).
|
|
57
|
+
- 403: Authenticated but not authorized (bad credentials on login attempt, or insufficient privileges).
|
|
58
|
+
|
|
59
|
+
## Client considerations
|
|
60
|
+
|
|
61
|
+
- Sessions are cookie-based; the server handles setting and reading the cookie via Harper. If you make cross-origin requests, ensure the appropriate `credentials` mode and CORS settings.
|
|
62
|
+
- If developing locally, double-check the server config still has `authentication.authorizeLocal: false` to avoid accidental superuser bypass.
|
|
63
|
+
|
|
64
|
+
## Token-based auth (JWT + refresh token) for non-browser clients
|
|
65
|
+
|
|
66
|
+
Cookie-backed sessions are great for browser flows. For CLI tools, mobile apps, or other non-browser clients, it’s often easier to use **explicit tokens**:
|
|
67
|
+
|
|
68
|
+
- **JWT (`operation_token`)**: short-lived bearer token used to authorize API requests.
|
|
69
|
+
- **Refresh token (`refresh_token`)**: longer-lived token used to mint a new JWT when it expires.
|
|
70
|
+
|
|
71
|
+
This project includes two Resource patterns for that flow:
|
|
72
|
+
|
|
73
|
+
### Issuing tokens: `IssueTokens`
|
|
74
|
+
|
|
75
|
+
**Description / use case:** Generate `{ refreshToken, jwt }` either:
|
|
76
|
+
|
|
77
|
+
- with an existing Authorization token (either Basic Auth or a JWT) and you want to issue new tokens, or
|
|
78
|
+
- from an explicit `{ username, password }` payload (useful for direct “login” from a CLI/mobile client).
|
|
79
|
+
|
|
80
|
+
```
|
|
81
|
+
js
|
|
82
|
+
export class IssueTokens extends Resource {
|
|
83
|
+
static loadAsInstance = false;
|
|
84
|
+
|
|
85
|
+
async get(target) {
|
|
86
|
+
const { refresh_token: refreshToken, operation_token: jwt } =
|
|
87
|
+
await databases.system.hdb_user.operation(
|
|
88
|
+
{ operation: 'create_authentication_tokens' },
|
|
89
|
+
this.getContext(),
|
|
90
|
+
);
|
|
91
|
+
return { refreshToken, jwt };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async post(target, data) {
|
|
95
|
+
if (!data.username || !data.password) {
|
|
96
|
+
throw new Error('username and password are required');
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const { refresh_token: refreshToken, operation_token: jwt } =
|
|
100
|
+
await databases.system.hdb_user.operation({
|
|
101
|
+
operation: 'create_authentication_tokens',
|
|
102
|
+
username: data.username,
|
|
103
|
+
password: data.password,
|
|
104
|
+
});
|
|
105
|
+
return { refreshToken, jwt };
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
**Recommended documentation notes to include:**
|
|
111
|
+
|
|
112
|
+
- `GET` variant: intended for “I already have an Authorization token, give me new tokens”.
|
|
113
|
+
- `POST` variant: intended for “I have credentials, give me tokens”.
|
|
114
|
+
- Response shape:
|
|
115
|
+
- `refreshToken`: store securely (long-lived).
|
|
116
|
+
- `jwt`: attach to requests (short-lived).
|
|
117
|
+
|
|
118
|
+
### Refreshing a JWT: `RefreshJWT`
|
|
119
|
+
|
|
120
|
+
**Description / use case:** When the JWT expires, the client uses the refresh token to get a new JWT without re-supplying username/password.
|
|
121
|
+
|
|
122
|
+
```
|
|
123
|
+
js
|
|
124
|
+
export class RefreshJWT extends Resource {
|
|
125
|
+
static loadAsInstance = false;
|
|
126
|
+
|
|
127
|
+
async post(target, data) {
|
|
128
|
+
if (!data.refreshToken) {
|
|
129
|
+
throw new Error('refreshToken is required');
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const { operation_token: jwt } = await databases.system.hdb_user.operation({
|
|
133
|
+
operation: 'refresh_operation_token',
|
|
134
|
+
refresh_token: data.refreshToken,
|
|
135
|
+
});
|
|
136
|
+
return { jwt };
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
**Recommended documentation notes to include:**
|
|
142
|
+
|
|
143
|
+
- Requires `refreshToken` in the request body.
|
|
144
|
+
- Returns a new `{ jwt }`.
|
|
145
|
+
- If refresh fails (expired/revoked), client must re-authenticate (e.g., call `IssueTokens.post` again).
|
|
146
|
+
|
|
147
|
+
### Suggested client flow (high-level)
|
|
148
|
+
|
|
149
|
+
1. **Sign in (token flow)**
|
|
150
|
+
- POST /IssueTokens/ with a body of `{ "username": "your username", "password": "your password" }` or GET /IssueTokens/ with an existing Authorization token.
|
|
151
|
+
- Receive `{ jwt, refreshToken }` in the response
|
|
152
|
+
2. **Call protected APIs**
|
|
153
|
+
- Send the JWT with each request in the Authorization header (as your auth mechanism expects)
|
|
154
|
+
3. **JWT expires**
|
|
155
|
+
- POST /RefreshJWT/ with a body of `{ "refreshToken": "your refresh token" }`.
|
|
156
|
+
- Receive `{ jwt }` in the response and continue
|
|
157
|
+
|
|
158
|
+
## Quick checklist
|
|
159
|
+
|
|
160
|
+
- [ ] Public endpoints explicitly `allowRead`/`allowCreate` as needed.
|
|
161
|
+
- [ ] Sign-in uses `context.login` and handles 400/403 correctly.
|
|
162
|
+
- [ ] Protected routes call `ensureSuperUser(this.getCurrentUser())` (or another role check) before doing work.
|
|
163
|
+
- [ ] Sign-out verifies a session and deletes it.
|
|
164
|
+
- [ ] `authentication.authorizeLocal` is `false` and `enableSessions` is `true` in Harper config.
|
|
165
|
+
- [ ] If using tokens: `IssueTokens` issues `{ jwt, refreshToken }`, `RefreshJWT` refreshes `{ jwt }` with a `refreshToken`.
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: custom-resources
|
|
3
|
+
description: How to define custom REST endpoints with JavaScript or TypeScript in Harper.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Custom Resources
|
|
7
|
+
|
|
8
|
+
Instructions for the agent to follow when creating custom resources in Harper.
|
|
9
|
+
|
|
10
|
+
## When to Use
|
|
11
|
+
|
|
12
|
+
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.
|
|
13
|
+
|
|
14
|
+
## Steps
|
|
15
|
+
|
|
16
|
+
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.
|
|
17
|
+
2. **Create the Resource File**: Create a `.ts` or `.js` file in the directory specified by `jsResource` in `config.yaml` (typically `resources/`).
|
|
18
|
+
3. **Define the Resource Class**: Export a class extending `Resource` from `harperdb`:
|
|
19
|
+
```typescript
|
|
20
|
+
import { type RequestTargetOrId, Resource } from 'harperdb';
|
|
21
|
+
|
|
22
|
+
export class MyResource extends Resource {
|
|
23
|
+
async get(target?: RequestTargetOrId) {
|
|
24
|
+
return { message: 'Hello from custom GET!' };
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
4. **Implement HTTP Methods**: Add methods like `get`, `post`, `put`, `patch`, or `delete` to handle corresponding requests. Note that paths are **case-sensitive** and match the class name.
|
|
29
|
+
5. **Access Tables (Optional)**: Import and use the `tables` object to interact with your data:
|
|
30
|
+
```typescript
|
|
31
|
+
import { tables } from 'harperdb';
|
|
32
|
+
// ... inside a method
|
|
33
|
+
const results = await tables.MyTable.list();
|
|
34
|
+
```
|
|
35
|
+
6. **Configure Loading**: Ensure `config.yaml` points to your resource files (e.g., `jsResource: { files: 'resources/*.ts' }`).
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: defining-relationships
|
|
3
|
+
description: How to define and use relationships between tables in Harper using GraphQL.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Defining Relationships
|
|
7
|
+
|
|
8
|
+
Instructions for the agent to follow when defining relationships between Harper tables.
|
|
9
|
+
|
|
10
|
+
## When to Use
|
|
11
|
+
|
|
12
|
+
Use this skill when you need to link data across different tables, enabling automatic joins and efficient related-data fetching via REST APIs.
|
|
13
|
+
|
|
14
|
+
## Steps
|
|
15
|
+
|
|
16
|
+
1. **Identify the Relationship Type**: Determine if it's one-to-one, many-to-one, or one-to-many.
|
|
17
|
+
2. **Use the `@relationship` Directive**: Apply it to a field in your GraphQL schema.
|
|
18
|
+
- **Many-to-One (Current table holds FK)**: Use `from`.
|
|
19
|
+
```graphql
|
|
20
|
+
type Book @table @export {
|
|
21
|
+
authorId: ID
|
|
22
|
+
author: Author @relationship(from: "authorId")
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
- **One-to-Many (Related table holds FK)**: Use `to` and an array type.
|
|
26
|
+
```graphql
|
|
27
|
+
type Author @table @export {
|
|
28
|
+
books: [Book] @relationship(to: "authorId")
|
|
29
|
+
}
|
|
30
|
+
```
|
|
31
|
+
3. **Query with Relationships**: Use dot syntax in REST API calls for filtering or the `select()` operator for including related data.
|
|
32
|
+
- Example Filter: `GET /Book/?author.name=Harper`
|
|
33
|
+
- Example Select: `GET /Author/?select(name,books(title))`
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: deploying-to-harper-fabric
|
|
3
|
+
description: How to deploy a Harper application to the Harper Fabric cloud.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Deploying to Harper Fabric
|
|
7
|
+
|
|
8
|
+
Instructions for the agent to follow when deploying to Harper Fabric.
|
|
9
|
+
|
|
10
|
+
## When to Use
|
|
11
|
+
|
|
12
|
+
Use this skill when you are ready to move your Harper application from local development to a cloud-hosted environment.
|
|
13
|
+
|
|
14
|
+
## Steps
|
|
15
|
+
|
|
16
|
+
1. **Sign up**: Create an account at [https://fabric.harper.fast/](https://fabric.harper.fast/) and create a cluster.
|
|
17
|
+
2. **Configure Environment**: Add your cluster credentials and cluster application URL to `.env`:
|
|
18
|
+
```bash
|
|
19
|
+
CLI_TARGET_USERNAME='YOUR_CLUSTER_USERNAME'
|
|
20
|
+
CLI_TARGET_PASSWORD='YOUR_CLUSTER_PASSWORD'
|
|
21
|
+
CLI_TARGET='YOUR_CLUSTER_URL'
|
|
22
|
+
```
|
|
23
|
+
3. **Deploy From Local Environment**: Run `npm run deploy`.
|
|
24
|
+
4. **Set up CI/CD**: Configure `.github/workflows/deploy.yaml` and set repository secrets for automated deployments.
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: extending-tables
|
|
3
|
+
description: How to add custom logic to automatically generated table resources in Harper.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Extending Tables
|
|
7
|
+
|
|
8
|
+
Instructions for the agent to follow when extending table resources in Harper.
|
|
9
|
+
|
|
10
|
+
## When to Use
|
|
11
|
+
|
|
12
|
+
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.
|
|
13
|
+
|
|
14
|
+
## Steps
|
|
15
|
+
|
|
16
|
+
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.
|
|
17
|
+
```graphql
|
|
18
|
+
type MyTable @table {
|
|
19
|
+
id: ID @primaryKey
|
|
20
|
+
name: String
|
|
21
|
+
}
|
|
22
|
+
```
|
|
23
|
+
2. **Create the Extension File**: Create a `.ts` file in your `resources/` directory.
|
|
24
|
+
3. **Extend the Table Resource**: Export a class that extends `tables.YourTableName`:
|
|
25
|
+
```typescript
|
|
26
|
+
import { type RequestTargetOrId, tables } from 'harperdb';
|
|
27
|
+
|
|
28
|
+
export class MyTable extends tables.MyTable {
|
|
29
|
+
async post(target: RequestTargetOrId, record: any) {
|
|
30
|
+
// Custom logic here
|
|
31
|
+
if (!record.name) { throw new Error('Name required'); }
|
|
32
|
+
return super.post(target, record);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
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.
|
|
37
|
+
5. **Implement Logic**: Use overrides for validation, side effects, or transforming data before/after database operations.
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: handling-binary-data
|
|
3
|
+
description: How to store and serve binary data like images or audio in Harper.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Handling Binary Data
|
|
7
|
+
|
|
8
|
+
Instructions for the agent to follow when handling binary data in Harper.
|
|
9
|
+
|
|
10
|
+
## When to Use
|
|
11
|
+
|
|
12
|
+
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.
|
|
13
|
+
|
|
14
|
+
## Steps
|
|
15
|
+
|
|
16
|
+
1. **Store Base64 as Blobs**: In your resource's `post` or `put` method, convert incoming base64 strings to Buffers and then to Blobs using `createBlob`:
|
|
17
|
+
```typescript
|
|
18
|
+
import { createBlob } from 'harperdb';
|
|
19
|
+
|
|
20
|
+
async post(target, record) {
|
|
21
|
+
if (record.data) {
|
|
22
|
+
record.data = createBlob(Buffer.from(record.data, 'base64'), {
|
|
23
|
+
type: 'image/jpeg',
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
return super.post(target, record);
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
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`:
|
|
30
|
+
```typescript
|
|
31
|
+
async get(target) {
|
|
32
|
+
const record = await super.get(target);
|
|
33
|
+
if (record?.data) {
|
|
34
|
+
return {
|
|
35
|
+
status: 200,
|
|
36
|
+
headers: { 'Content-Type': 'image/jpeg' },
|
|
37
|
+
body: record.data,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
return record;
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
3. **Use the Blob Type**: Ensure your GraphQL schema uses the `Blob` scalar for binary fields.
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: programmatic-table-requests
|
|
3
|
+
description: How to interact with Harper tables programmatically using the `tables` object.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Programmatic Table Requests
|
|
7
|
+
|
|
8
|
+
Instructions for the agent to follow when interacting with Harper tables via code.
|
|
9
|
+
|
|
10
|
+
## When to Use
|
|
11
|
+
|
|
12
|
+
Use this skill when you need to perform database operations (CRUD, search, subscribe) from within Harper Resources or scripts.
|
|
13
|
+
|
|
14
|
+
## Steps
|
|
15
|
+
|
|
16
|
+
1. **Access the Table**: Use the global `tables` object followed by your table name (e.g., `tables.MyTable`).
|
|
17
|
+
2. **Perform CRUD Operations**:
|
|
18
|
+
- **Get**: `await tables.MyTable.get(id)` for a single record or `await tables.MyTable.get({ conditions: [...] })` for multiple.
|
|
19
|
+
- **Create**: `await tables.MyTable.post(record)` (auto-generates ID) or `await tables.MyTable.put(id, record)`.
|
|
20
|
+
- **Update**: `await tables.MyTable.patch(id, partialRecord)` for partial updates.
|
|
21
|
+
- **Delete**: `await tables.MyTable.delete(id)`.
|
|
22
|
+
3. **Use Updatable Records for Atomic Ops**: Call `update(id)` to get a reference, then use `addTo` or `subtractFrom` for atomic increments/decrements:
|
|
23
|
+
```typescript
|
|
24
|
+
const stats = await tables.Stats.update('daily');
|
|
25
|
+
stats.addTo('viewCount', 1);
|
|
26
|
+
```
|
|
27
|
+
4. **Search and Stream**: Use `search(query)` for efficient streaming of large result sets:
|
|
28
|
+
```typescript
|
|
29
|
+
for await (const record of tables.MyTable.search({ conditions: [...] })) {
|
|
30
|
+
// process record
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
5. **Real-time Subscriptions**: Use `subscribe(query)` to listen for changes:
|
|
34
|
+
```typescript
|
|
35
|
+
for await (const event of tables.MyTable.subscribe(query)) {
|
|
36
|
+
// handle event
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
6. **Publish Events**: Use `publish(id, message)` to trigger subscriptions without necessarily persisting data.
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: querying-rest-apis
|
|
3
|
+
description: How to use query parameters to filter, sort, and paginate Harper REST APIs.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Querying REST APIs
|
|
7
|
+
|
|
8
|
+
Instructions for the agent to follow when querying Harper's REST APIs.
|
|
9
|
+
|
|
10
|
+
## When to Use
|
|
11
|
+
|
|
12
|
+
Use this skill when you need to perform advanced data retrieval (filtering, sorting, pagination, joins) using Harper's automatic REST endpoints.
|
|
13
|
+
|
|
14
|
+
## Steps
|
|
15
|
+
|
|
16
|
+
1. **Basic Filtering**: Use attribute names as query parameters: `GET /Table/?key=value`.
|
|
17
|
+
2. **Use Comparison Operators**: Append operators like `gt`, `ge`, `lt`, `le`, `ne` using FIQL-style syntax: `GET /Table/?price=gt=100`.
|
|
18
|
+
3. **Apply Logic and Grouping**: Use `&` for AND, `|` for OR, and `()` for grouping: `GET /Table/?(rating=5|featured=true)&price=lt=50`.
|
|
19
|
+
4. **Select Specific Fields**: Use `select()` to limit returned attributes: `GET /Table/?select(name,price)`.
|
|
20
|
+
5. **Paginate Results**: Use `limit(count)` or `limit(offset, count)`: `GET /Table/?limit(20, 10)`.
|
|
21
|
+
6. **Sort Results**: Use `sort()` with `+` (asc) or `-` (desc): `GET /Table/?sort(-price,+name)`.
|
|
22
|
+
7. **Query Relationships**: Use dot syntax for tables linked with `@relationship`: `GET /Book/?author.name=Harper`.
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: real-time-apps
|
|
3
|
+
description: How to build real-time features in Harper using WebSockets and Pub/Sub.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Real-time Applications
|
|
7
|
+
|
|
8
|
+
Instructions for the agent to follow when building real-time applications in Harper.
|
|
9
|
+
|
|
10
|
+
## When to Use
|
|
11
|
+
|
|
12
|
+
Use this skill when you need to stream live updates to clients, implement chat features, or provide real-time data synchronization between the database and a frontend.
|
|
13
|
+
|
|
14
|
+
## Steps
|
|
15
|
+
|
|
16
|
+
1. **Check Automatic WebSockets**: If you only need to stream table changes, use [Automatic APIs](automatic-apis.md) which provide a WebSocket endpoint for every `@export`ed table.
|
|
17
|
+
2. **Implement `connect` in a Resource**: For custom bi-directional logic, implement the `connect` method:
|
|
18
|
+
```typescript
|
|
19
|
+
import { Resource, tables } from 'harperdb';
|
|
20
|
+
|
|
21
|
+
export class MySocket extends Resource {
|
|
22
|
+
async *connect(target, incomingMessages) {
|
|
23
|
+
// Subscribe to table changes
|
|
24
|
+
const subscription = await tables.MyTable.subscribe(target);
|
|
25
|
+
if (!incomingMessages) { return subscription; // SSE mode
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Handle incoming client messages
|
|
29
|
+
for await (let message of incomingMessages) {
|
|
30
|
+
yield { received: message };
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
3. **Use Pub/Sub**: Use `tables.TableName.subscribe(query)` to listen for specific data changes and stream them to the client.
|
|
36
|
+
4. **Handle SSE**: Ensure your `connect` method gracefully handles cases where `incomingMessages` is null (Server-Sent Events).
|
|
37
|
+
5. **Connect from Client**: Use standard WebSockets (`new WebSocket('ws://...')`) to connect to your resource endpoint.
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: serving-web-content
|
|
3
|
+
description: How to serve static files and integrated Vite/React applications in Harper.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Serving Web Content
|
|
7
|
+
|
|
8
|
+
Instructions for the agent to follow when serving web content from Harper.
|
|
9
|
+
|
|
10
|
+
## When to Use
|
|
11
|
+
|
|
12
|
+
Use this skill when you need to serve a frontend (HTML, CSS, JS, or a React app) directly from your Harper instance.
|
|
13
|
+
|
|
14
|
+
## Steps
|
|
15
|
+
|
|
16
|
+
1. **Choose a Method**: Decide between the simple Static Plugin or the integrated Vite Plugin.
|
|
17
|
+
2. **Option A: Static Plugin (Simple)**:
|
|
18
|
+
- Add to `config.yaml`:
|
|
19
|
+
```yaml
|
|
20
|
+
static:
|
|
21
|
+
files: 'web/*'
|
|
22
|
+
```
|
|
23
|
+
- Place files in a `web/` folder in the project root.
|
|
24
|
+
- Files are served at the root URL (e.g., `http://localhost:9926/index.html`).
|
|
25
|
+
3. **Option B: Vite Plugin (Advanced/Development)**:
|
|
26
|
+
- Add to `config.yaml`:
|
|
27
|
+
```yaml
|
|
28
|
+
'@harperfast/vite-plugin':
|
|
29
|
+
package: '@harperfast/vite-plugin'
|
|
30
|
+
```
|
|
31
|
+
- Ensure `vite.config.ts` and `index.html` are in the project root.
|
|
32
|
+
- Install dependencies: `npm install --save-dev vite @harperfast/vite-plugin`.
|
|
33
|
+
- Use `npm run dev` for development with HMR.
|
|
34
|
+
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.
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: typescript-type-stripping
|
|
3
|
+
description: How to run TypeScript files directly in Harper without a build step.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# TypeScript Type Stripping
|
|
7
|
+
|
|
8
|
+
Instructions for the agent to follow when using TypeScript in Harper.
|
|
9
|
+
|
|
10
|
+
## When to Use
|
|
11
|
+
|
|
12
|
+
Use this skill when you want to write Harper Resources in TypeScript and have them execute directly in Node.js without an intermediate build or compilation step.
|
|
13
|
+
|
|
14
|
+
## Steps
|
|
15
|
+
|
|
16
|
+
1. **Verify Node.js Version**: Ensure you are using Node.js v22.6.0 or higher.
|
|
17
|
+
2. **Name Files with `.ts`**: Create your resource files in the `resources/` directory with a `.ts` extension.
|
|
18
|
+
3. **Use TypeScript Syntax**: Write your resource classes using standard TypeScript (interfaces, types, etc.).
|
|
19
|
+
```typescript
|
|
20
|
+
import { Resource } from 'harperdb';
|
|
21
|
+
export class MyResource extends Resource {
|
|
22
|
+
async get(): Promise<{ message: string }> {
|
|
23
|
+
return { message: 'Running TS directly!' };
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
4. **Use Explicit Extensions in Imports**: When importing other local modules, include the `.ts` extension: `import { helper } from './helper.ts'`.
|
|
28
|
+
5. **Configure `config.yaml`**: Ensure `jsResource` points to your `.ts` files:
|
|
29
|
+
```yaml
|
|
30
|
+
jsResource:
|
|
31
|
+
files: 'resources/*.ts'
|
|
32
|
+
```
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: using-blob-datatype
|
|
3
|
+
description: How to use the Blob data type for efficient binary storage in Harper.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Using Blob Datatype
|
|
7
|
+
|
|
8
|
+
Instructions for the agent to follow when working with the Blob data type in Harper.
|
|
9
|
+
|
|
10
|
+
## When to Use
|
|
11
|
+
|
|
12
|
+
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.
|
|
13
|
+
|
|
14
|
+
## Steps
|
|
15
|
+
|
|
16
|
+
1. **Define Blob Fields**: In your GraphQL schema, use the `Blob` type:
|
|
17
|
+
```graphql
|
|
18
|
+
type MyTable @table {
|
|
19
|
+
id: ID @primaryKey
|
|
20
|
+
data: Blob
|
|
21
|
+
}
|
|
22
|
+
```
|
|
23
|
+
2. **Create and Store Blobs**: Use `createBlob()` from `harperdb` to wrap Buffers or Streams:
|
|
24
|
+
```javascript
|
|
25
|
+
import { createBlob, tables } from 'harperdb';
|
|
26
|
+
const blob = createBlob(largeBuffer);
|
|
27
|
+
await tables.MyTable.put('my-id', { data: blob });
|
|
28
|
+
```
|
|
29
|
+
3. **Use Streaming (Optional)**: For very large files, pass a stream to `createBlob()` to avoid loading the entire file into memory.
|
|
30
|
+
4. **Read Blob Data**: Retrieve the record and use `.bytes()` or streaming interfaces on the blob field:
|
|
31
|
+
```javascript
|
|
32
|
+
const record = await tables.MyTable.get('my-id');
|
|
33
|
+
const buffer = await record.data.bytes();
|
|
34
|
+
```
|
|
35
|
+
5. **Ensure Write Completion**: Use `saveBeforeCommit: true` in `createBlob` options if you need the blob fully written before the record is committed.
|
|
36
|
+
6. **Handle Errors**: Attach error listeners to the blob object to handle streaming failures.
|