@xano/developer-mcp 1.0.34 → 1.0.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -2
- package/dist/tools/index.d.ts +2 -15
- package/dist/tools/index.js +2 -2
- package/dist/tools/validate_xanoscript.d.ts +84 -9
- package/dist/tools/validate_xanoscript.js +345 -31
- package/dist/xanoscript.js +31 -1
- package/dist/xanoscript.test.js +6 -0
- package/dist/xanoscript_docs/README.md +56 -43
- package/dist/xanoscript_docs/addons.md +10 -0
- package/dist/xanoscript_docs/agents.md +15 -0
- package/dist/xanoscript_docs/apis.md +45 -24
- package/dist/xanoscript_docs/cheatsheet.md +252 -0
- package/dist/xanoscript_docs/database.md +37 -0
- package/dist/xanoscript_docs/docs_index.json +236 -0
- package/dist/xanoscript_docs/frontend.md +10 -0
- package/dist/xanoscript_docs/functions.md +18 -0
- package/dist/xanoscript_docs/integrations/cloud-storage.md +142 -0
- package/dist/xanoscript_docs/integrations/external-apis.md +201 -0
- package/dist/xanoscript_docs/integrations/redis.md +194 -0
- package/dist/xanoscript_docs/integrations/search.md +242 -0
- package/dist/xanoscript_docs/integrations/utilities.md +331 -0
- package/dist/xanoscript_docs/integrations.md +55 -901
- package/dist/xanoscript_docs/mcp-servers.md +10 -0
- package/dist/xanoscript_docs/performance.md +15 -0
- package/dist/xanoscript_docs/quickstart.md +288 -41
- package/dist/xanoscript_docs/run.md +10 -0
- package/dist/xanoscript_docs/security.md +26 -0
- package/dist/xanoscript_docs/streaming.md +10 -0
- package/dist/xanoscript_docs/syntax.md +78 -0
- package/dist/xanoscript_docs/tables.md +15 -0
- package/dist/xanoscript_docs/tasks.md +11 -0
- package/dist/xanoscript_docs/tools.md +15 -0
- package/dist/xanoscript_docs/triggers.md +57 -192
- package/dist/xanoscript_docs/types.md +18 -0
- package/package.json +1 -1
package/dist/xanoscript.js
CHANGED
|
@@ -16,6 +16,11 @@ export const XANOSCRIPT_DOCS_V2 = {
|
|
|
16
16
|
applyTo: [],
|
|
17
17
|
description: "XanoScript overview, workspace structure, and quick reference",
|
|
18
18
|
},
|
|
19
|
+
cheatsheet: {
|
|
20
|
+
file: "cheatsheet.md",
|
|
21
|
+
applyTo: ["**/*.xs"],
|
|
22
|
+
description: "Quick reference for 20 most common XanoScript patterns",
|
|
23
|
+
},
|
|
19
24
|
syntax: {
|
|
20
25
|
file: "syntax.md",
|
|
21
26
|
applyTo: ["**/*.xs"],
|
|
@@ -84,7 +89,32 @@ export const XANOSCRIPT_DOCS_V2 = {
|
|
|
84
89
|
integrations: {
|
|
85
90
|
file: "integrations.md",
|
|
86
91
|
applyTo: ["functions/**/*.xs", "apis/**/*.xs", "tasks/*.xs"],
|
|
87
|
-
description: "
|
|
92
|
+
description: "External service integrations index - see sub-topics for details",
|
|
93
|
+
},
|
|
94
|
+
"integrations/cloud-storage": {
|
|
95
|
+
file: "integrations/cloud-storage.md",
|
|
96
|
+
applyTo: [],
|
|
97
|
+
description: "AWS S3, Azure Blob, and GCP Storage operations",
|
|
98
|
+
},
|
|
99
|
+
"integrations/search": {
|
|
100
|
+
file: "integrations/search.md",
|
|
101
|
+
applyTo: [],
|
|
102
|
+
description: "Elasticsearch, OpenSearch, and Algolia search operations",
|
|
103
|
+
},
|
|
104
|
+
"integrations/redis": {
|
|
105
|
+
file: "integrations/redis.md",
|
|
106
|
+
applyTo: [],
|
|
107
|
+
description: "Redis caching, rate limiting, and queue operations",
|
|
108
|
+
},
|
|
109
|
+
"integrations/external-apis": {
|
|
110
|
+
file: "integrations/external-apis.md",
|
|
111
|
+
applyTo: [],
|
|
112
|
+
description: "HTTP requests with api.request patterns",
|
|
113
|
+
},
|
|
114
|
+
"integrations/utilities": {
|
|
115
|
+
file: "integrations/utilities.md",
|
|
116
|
+
applyTo: [],
|
|
117
|
+
description: "Local storage, email, zip, and Lambda utilities",
|
|
88
118
|
},
|
|
89
119
|
frontend: {
|
|
90
120
|
file: "frontend.md",
|
package/dist/xanoscript.test.js
CHANGED
|
@@ -10,6 +10,7 @@ describe("xanoscript module", () => {
|
|
|
10
10
|
it("should have all expected topics", () => {
|
|
11
11
|
const expectedTopics = [
|
|
12
12
|
"readme",
|
|
13
|
+
"cheatsheet",
|
|
13
14
|
"syntax",
|
|
14
15
|
"quickstart",
|
|
15
16
|
"types",
|
|
@@ -24,6 +25,11 @@ describe("xanoscript module", () => {
|
|
|
24
25
|
"mcp-servers",
|
|
25
26
|
"testing",
|
|
26
27
|
"integrations",
|
|
28
|
+
"integrations/cloud-storage",
|
|
29
|
+
"integrations/search",
|
|
30
|
+
"integrations/redis",
|
|
31
|
+
"integrations/external-apis",
|
|
32
|
+
"integrations/utilities",
|
|
27
33
|
"frontend",
|
|
28
34
|
"run",
|
|
29
35
|
"addons",
|
|
@@ -108,7 +108,20 @@ $db.table.field // Database field reference (in queries)
|
|
|
108
108
|
$this // Current item in loops/maps
|
|
109
109
|
```
|
|
110
110
|
|
|
111
|
-
**
|
|
111
|
+
**Reserved Variables:** The following cannot be used as variable names: `$response`, `$output`, `$input`, `$auth`, `$env`, `$db`, `$this`, `$result`.
|
|
112
|
+
|
|
113
|
+
### Type Names
|
|
114
|
+
|
|
115
|
+
XanoScript uses specific type names:
|
|
116
|
+
|
|
117
|
+
| Type | Description | Example |
|
|
118
|
+
|------|-------------|---------|
|
|
119
|
+
| `text` | String (not "string") | `text name` |
|
|
120
|
+
| `int` | Integer (not "integer") | `int count` |
|
|
121
|
+
| `bool` | Boolean (not "boolean") | `bool active` |
|
|
122
|
+
| `decimal` | Float/number | `decimal price` |
|
|
123
|
+
| `type[]` | Array (not "array" or "list") | `text[] tags` |
|
|
124
|
+
| `json` | Arbitrary JSON data | `json metadata` |
|
|
112
125
|
|
|
113
126
|
### Comments
|
|
114
127
|
|
|
@@ -164,69 +177,69 @@ Use `xanoscript_docs({ topic: "<topic>" })` to retrieve documentation.
|
|
|
164
177
|
|
|
165
178
|
### Core Language
|
|
166
179
|
|
|
167
|
-
| Topic | Description |
|
|
168
|
-
| ------------ | ---------------------------------------------------- |
|
|
169
|
-
| `quickstart` | Common patterns, quick examples, mistakes to avoid |
|
|
170
|
-
| `syntax` | Expressions, operators, filters, system variables |
|
|
171
|
-
| `types` | Data types, validation, input blocks |
|
|
172
|
-
| `functions` | Reusable function stacks, async, loops |
|
|
173
|
-
| `schema` | Runtime schema parsing and validation |
|
|
180
|
+
| Topic | Description | Key Sections |
|
|
181
|
+
| ------------ | ---------------------------------------------------- | ------------ |
|
|
182
|
+
| `quickstart` | Common patterns, quick examples, mistakes to avoid | Patterns, Common Mistakes |
|
|
183
|
+
| `syntax` | Expressions, operators, filters, system variables | Filters (L179-275), Error Handling (L411-477) |
|
|
184
|
+
| `types` | Data types, validation, input blocks | Validation Filters, Input Blocks |
|
|
185
|
+
| `functions` | Reusable function stacks, async, loops | Loops, Async Patterns |
|
|
186
|
+
| `schema` | Runtime schema parsing and validation | parse.object, parse.array |
|
|
174
187
|
|
|
175
188
|
### Data
|
|
176
189
|
|
|
177
|
-
| Topic | Description |
|
|
178
|
-
| ----------- | ---------------------------------------------------------- |
|
|
179
|
-
| `tables` | Database schema definitions with indexes and relationships |
|
|
180
|
-
| `database` | All db.\* operations: query, get, add, edit, patch, delete |
|
|
181
|
-
| `addons` | Reusable subqueries for fetching related data |
|
|
182
|
-
| `streaming` | Streaming data from files, requests, and responses |
|
|
190
|
+
| Topic | Description | Key Sections |
|
|
191
|
+
| ----------- | ---------------------------------------------------------- | ------------ |
|
|
192
|
+
| `tables` | Database schema definitions with indexes and relationships | Indexes, Foreign Keys |
|
|
193
|
+
| `database` | All db.\* operations: query, get, add, edit, patch, delete | Decision Tree (L11), Bulk Ops (L450-529) |
|
|
194
|
+
| `addons` | Reusable subqueries for fetching related data | Usage Patterns |
|
|
195
|
+
| `streaming` | Streaming data from files, requests, and responses | File Streams, API Streams |
|
|
183
196
|
|
|
184
197
|
### APIs & Endpoints
|
|
185
198
|
|
|
186
|
-
| Topic | Description |
|
|
187
|
-
| ---------- | --------------------------------------------------------------- |
|
|
188
|
-
| `apis` | HTTP endpoint definitions with authentication and CRUD patterns |
|
|
189
|
-
| `tasks` | Scheduled and cron jobs |
|
|
190
|
-
| `triggers` | Event-driven handlers (table, realtime, workspace, agent, MCP) |
|
|
191
|
-
| `realtime` | Real-time channels and events for push updates |
|
|
199
|
+
| Topic | Description | Key Sections |
|
|
200
|
+
| ---------- | --------------------------------------------------------------- | ------------ |
|
|
201
|
+
| `apis` | HTTP endpoint definitions with authentication and CRUD patterns | Decision Tree (L9), CRUD Examples (L220-350) |
|
|
202
|
+
| `tasks` | Scheduled and cron jobs | Cron Syntax, Input Handling |
|
|
203
|
+
| `triggers` | Event-driven handlers (table, realtime, workspace, agent, MCP) | Predefined Inputs, Event Types |
|
|
204
|
+
| `realtime` | Real-time channels and events for push updates | Channels, Events |
|
|
192
205
|
|
|
193
206
|
### AI & Agents
|
|
194
207
|
|
|
195
|
-
| Topic | Description |
|
|
196
|
-
| ------------- | --------------------------------------------------- |
|
|
197
|
-
| `agents` | AI agent configuration with LLM providers and tools |
|
|
198
|
-
| `tools` | AI tools for agents and MCP servers |
|
|
199
|
-
| `mcp-servers` | MCP server definitions exposing tools |
|
|
208
|
+
| Topic | Description | Key Sections |
|
|
209
|
+
| ------------- | --------------------------------------------------- | ------------ |
|
|
210
|
+
| `agents` | AI agent configuration with LLM providers and tools | LLM Config, Tool Binding |
|
|
211
|
+
| `tools` | AI tools for agents and MCP servers | Tool Schema, Parameters |
|
|
212
|
+
| `mcp-servers` | MCP server definitions exposing tools | Server Config, Tool Exposure |
|
|
200
213
|
|
|
201
214
|
### Integrations
|
|
202
215
|
|
|
203
|
-
| Topic | Description |
|
|
204
|
-
| -------------- | ------------------------------------------------- |
|
|
205
|
-
| `integrations` | Cloud storage, Redis, security, and external APIs |
|
|
216
|
+
| Topic | Description | Sub-topics |
|
|
217
|
+
| -------------- | ------------------------------------------------- | ---------- |
|
|
218
|
+
| `integrations` | Cloud storage, Redis, security, and external APIs | cloud-storage, search, redis, external-apis, utilities |
|
|
206
219
|
|
|
207
220
|
### Configuration
|
|
208
221
|
|
|
209
|
-
| Topic | Description |
|
|
210
|
-
| ------------ | ---------------------------------------------------------------------- |
|
|
211
|
-
| `workspace` | Workspace-level settings: environment variables, preferences, realtime |
|
|
212
|
-
| `branch` | Branch-level settings: middleware, history retention, visual styling |
|
|
213
|
-
| `middleware` | Request/response interceptors for functions, queries, tasks, and tools |
|
|
222
|
+
| Topic | Description | Key Sections |
|
|
223
|
+
| ------------ | ---------------------------------------------------------------------- | ------------ |
|
|
224
|
+
| `workspace` | Workspace-level settings: environment variables, preferences, realtime | Env Variables, Preferences |
|
|
225
|
+
| `branch` | Branch-level settings: middleware, history retention, visual styling | Middleware Config, History |
|
|
226
|
+
| `middleware` | Request/response interceptors for functions, queries, tasks, and tools | Pre/Post Hooks |
|
|
214
227
|
|
|
215
228
|
### Development
|
|
216
229
|
|
|
217
|
-
| Topic | Description |
|
|
218
|
-
| ----------- | ---------------------------------------------------------- |
|
|
219
|
-
| `testing` | Unit tests, mocks, and assertions |
|
|
220
|
-
| `debugging` | Logging, inspecting, and debugging XanoScript execution |
|
|
221
|
-
| `frontend` | Static frontend development and deployment |
|
|
222
|
-
| `run` | Run job and service configurations for the Xano Job Runner |
|
|
230
|
+
| Topic | Description | Key Sections |
|
|
231
|
+
| ----------- | ---------------------------------------------------------- | ------------ |
|
|
232
|
+
| `testing` | Unit tests, mocks, and assertions | Test Syntax, Assertions |
|
|
233
|
+
| `debugging` | Logging, inspecting, and debugging XanoScript execution | debug.log, Inspection |
|
|
234
|
+
| `frontend` | Static frontend development and deployment | File Structure |
|
|
235
|
+
| `run` | Run job and service configurations for the Xano Job Runner | Jobs, Services |
|
|
223
236
|
|
|
224
237
|
### Best Practices
|
|
225
238
|
|
|
226
|
-
| Topic | Description |
|
|
227
|
-
| ------------- | ------------------------------------------------------------ |
|
|
228
|
-
| `performance` | Performance optimization best practices |
|
|
229
|
-
| `security` | Security best practices for authentication and authorization |
|
|
239
|
+
| Topic | Description | Key Sections |
|
|
240
|
+
| ------------- | ------------------------------------------------------------ | ------------ |
|
|
241
|
+
| `performance` | Performance optimization best practices | Caching, Query Optimization |
|
|
242
|
+
| `security` | Security best practices for authentication and authorization | Auth Patterns, Token Handling |
|
|
230
243
|
|
|
231
244
|
---
|
|
232
245
|
|
|
@@ -254,3 +254,13 @@ addon/
|
|
|
254
254
|
3. **Use appropriate return types** - `list`, `single`, `count`, `exists`
|
|
255
255
|
4. **Limit nested queries** - Avoid N+1 query patterns
|
|
256
256
|
5. **Document inputs** - Add descriptions to input fields
|
|
257
|
+
|
|
258
|
+
---
|
|
259
|
+
|
|
260
|
+
## Related Topics
|
|
261
|
+
|
|
262
|
+
| Topic | Description |
|
|
263
|
+
|-------|-------------|
|
|
264
|
+
| `database` | db.query with addon usage |
|
|
265
|
+
| `apis` | Using addons in API responses |
|
|
266
|
+
| `functions` | Calling addons from functions |
|
|
@@ -6,6 +6,10 @@ applyTo: "agent/**/*.xs"
|
|
|
6
6
|
|
|
7
7
|
AI-powered agents that use LLMs to perform tasks autonomously.
|
|
8
8
|
|
|
9
|
+
> **TL;DR:** Define with `agent "name" { llm = { type: "provider", system_prompt: "...", prompt: "..." } tools = [...] }`. Providers: `xano-free`, `openai`, `anthropic`, `google-genai`. Tools give agents capabilities.
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
9
13
|
## Quick Reference
|
|
10
14
|
|
|
11
15
|
```xs
|
|
@@ -409,3 +413,14 @@ agent "Research Agent" {
|
|
|
409
413
|
5. **Use environment variables** - Never hardcode API keys
|
|
410
414
|
6. **Test with xano-free first** - Free for development
|
|
411
415
|
7. **Validate external MCP servers** - Check server_details before using
|
|
416
|
+
|
|
417
|
+
---
|
|
418
|
+
|
|
419
|
+
## Related Topics
|
|
420
|
+
|
|
421
|
+
| Topic | Description |
|
|
422
|
+
|-------|-------------|
|
|
423
|
+
| `tools` | Functions that agents can execute |
|
|
424
|
+
| `mcp-servers` | MCP server configuration |
|
|
425
|
+
| `triggers` | Agent triggers for events |
|
|
426
|
+
| `security` | API key management |
|
|
@@ -6,6 +6,30 @@ applyTo: "api/**/*.xs"
|
|
|
6
6
|
|
|
7
7
|
HTTP endpoint definitions in XanoScript.
|
|
8
8
|
|
|
9
|
+
> **TL;DR:** Use `query` to define endpoints. Require `api_group` for organization, `auth` for protected routes. Use appropriate HTTP verbs (GET/POST/PUT/PATCH/DELETE) and validate inputs with filters.
|
|
10
|
+
|
|
11
|
+
## Choosing an Approach
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
Building...
|
|
15
|
+
├── REST endpoint?
|
|
16
|
+
│ ├── GET (read) → query with verb=GET
|
|
17
|
+
│ ├── POST (create) → query with verb=POST
|
|
18
|
+
│ ├── PUT/PATCH (update) → query with verb=PUT or verb=PATCH
|
|
19
|
+
│ └── DELETE (remove) → query with verb=DELETE
|
|
20
|
+
├── Protected endpoint?
|
|
21
|
+
│ ├── Token auth? → auth = "user" (table with auth=true)
|
|
22
|
+
│ └── Public? → omit auth attribute
|
|
23
|
+
├── Input handling?
|
|
24
|
+
│ ├── Path params? → query "path/{id}" + input { int id }
|
|
25
|
+
│ ├── Query params? → GET with input block
|
|
26
|
+
│ └── Body params? → POST/PUT/PATCH with input block
|
|
27
|
+
├── Reusable logic?
|
|
28
|
+
│ └── Use function, call with function.run
|
|
29
|
+
└── Scheduled job?
|
|
30
|
+
└── Use task instead of query
|
|
31
|
+
```
|
|
32
|
+
|
|
9
33
|
## Quick Reference
|
|
10
34
|
|
|
11
35
|
```xs
|
|
@@ -116,26 +140,18 @@ query "products" verb=GET {
|
|
|
116
140
|
|
|
117
141
|
## Input Block
|
|
118
142
|
|
|
119
|
-
For complete type and
|
|
120
|
-
|
|
121
|
-
### Empty and Single-Input Blocks
|
|
122
|
-
|
|
123
|
-
Empty input blocks and single-input blocks can be written as one-liners. When there are two or more inputs, each must be on its own line.
|
|
143
|
+
> **Input block rules:** Empty and single-input blocks can be one-liners. Multiple inputs must be on separate lines. For complete type reference, validation filters, and schema definitions, see `xanoscript_docs({ topic: "types" })`.
|
|
124
144
|
|
|
125
145
|
```xs
|
|
126
|
-
// OK - empty input
|
|
127
|
-
|
|
128
|
-
api_group = "System"
|
|
129
|
-
input {}
|
|
130
|
-
stack { ... }
|
|
131
|
-
response = { status: "ok" }
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
// OK - single input as one-liner
|
|
146
|
+
// OK - empty or single input as one-liner
|
|
147
|
+
input {}
|
|
135
148
|
input { text query filters=trim }
|
|
136
149
|
|
|
137
|
-
//
|
|
138
|
-
input {
|
|
150
|
+
// Multiple inputs - each on own line
|
|
151
|
+
input {
|
|
152
|
+
text query filters=trim
|
|
153
|
+
int limit?=10
|
|
154
|
+
}
|
|
139
155
|
```
|
|
140
156
|
|
|
141
157
|
---
|
|
@@ -400,14 +416,7 @@ stack {
|
|
|
400
416
|
|
|
401
417
|
## Error Handling
|
|
402
418
|
|
|
403
|
-
For complete error handling
|
|
404
|
-
|
|
405
|
-
| Type | HTTP Status |
|
|
406
|
-
| -------------- | ----------- |
|
|
407
|
-
| `inputerror` | 400 |
|
|
408
|
-
| `accessdenied` | 403 |
|
|
409
|
-
| `notfound` | 404 |
|
|
410
|
-
| `standard` | 500 |
|
|
419
|
+
> **Error types:** Use `inputerror` (400), `accessdenied` (403), `notfound` (404), or `standard` (500). For complete error handling patterns including `precondition`, `try_catch`, and `throw`, see `xanoscript_docs({ topic: "syntax" })`.
|
|
411
420
|
|
|
412
421
|
---
|
|
413
422
|
|
|
@@ -437,3 +446,15 @@ When using `return = { type: "list", paging: {...} }`:
|
|
|
437
446
|
4. **Paginate lists** - Never return unbounded result sets
|
|
438
447
|
5. **Group by resource** - Organize endpoints in logical api groups
|
|
439
448
|
6. **Use specific canonicals** - Prefix canonicals to avoid instance-level collisions (e.g., `myapp-users` not `users`)
|
|
449
|
+
|
|
450
|
+
---
|
|
451
|
+
|
|
452
|
+
## Related Topics
|
|
453
|
+
|
|
454
|
+
| Topic | Description |
|
|
455
|
+
|-------|-------------|
|
|
456
|
+
| `types` | Input validation and filters |
|
|
457
|
+
| `functions` | Reusable logic called from API stacks |
|
|
458
|
+
| `database` | Database operations in API stacks |
|
|
459
|
+
| `security` | Authentication and authorization |
|
|
460
|
+
| `middleware` | Request interceptors |
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
---
|
|
2
|
+
applyTo: "**/*.xs"
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
# XanoScript Cheat Sheet
|
|
6
|
+
|
|
7
|
+
> **Purpose:** Quick reference for the 20 most common XanoScript patterns. For detailed documentation, use `xanoscript_docs({ topic: "<topic>" })`.
|
|
8
|
+
|
|
9
|
+
## Variable Declaration
|
|
10
|
+
|
|
11
|
+
```xs
|
|
12
|
+
var $name { value = "initial" }
|
|
13
|
+
var $count { value = 0 }
|
|
14
|
+
var $items { value = [] }
|
|
15
|
+
var $data { value = { key: "value" } }
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Conditionals
|
|
19
|
+
|
|
20
|
+
```xs
|
|
21
|
+
conditional {
|
|
22
|
+
if ($input.age >= 18) {
|
|
23
|
+
var $status { value = "adult" }
|
|
24
|
+
}
|
|
25
|
+
elseif ($input.age >= 13) {
|
|
26
|
+
var $status { value = "teen" }
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
var $status { value = "child" }
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
> **Note:** Use `elseif` (one word), not `else if`.
|
|
35
|
+
|
|
36
|
+
## Loops
|
|
37
|
+
|
|
38
|
+
```xs
|
|
39
|
+
// For each loop
|
|
40
|
+
each ($input.items as $item) {
|
|
41
|
+
debug.log { value = $item.name }
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// While loop (must be inside stack block)
|
|
45
|
+
stack {
|
|
46
|
+
var $counter { value = 0 }
|
|
47
|
+
while ($counter < 10) {
|
|
48
|
+
each {
|
|
49
|
+
var.update $counter { value = $counter + 1 }
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Map/filter for transformations
|
|
55
|
+
var $names { value = $items|map:$$.name }
|
|
56
|
+
var $active { value = $items|filter:$$.is_active }
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Database CRUD
|
|
60
|
+
|
|
61
|
+
```xs
|
|
62
|
+
// Get single record by field
|
|
63
|
+
db.get "user" {
|
|
64
|
+
field_name = "id"
|
|
65
|
+
field_value = $input.user_id
|
|
66
|
+
} as $user
|
|
67
|
+
|
|
68
|
+
// Query with filters
|
|
69
|
+
db.query "user" {
|
|
70
|
+
where = $db.user.is_active == true
|
|
71
|
+
sort = { created_at: "desc" }
|
|
72
|
+
return = { type: "list", paging: { page: 1, per_page: 25 } }
|
|
73
|
+
} as $users
|
|
74
|
+
|
|
75
|
+
// Insert record
|
|
76
|
+
db.add "user" {
|
|
77
|
+
data = { name: $input.name, email: $input.email, created_at: now }
|
|
78
|
+
} as $new_user
|
|
79
|
+
|
|
80
|
+
// Update record
|
|
81
|
+
db.edit "user" {
|
|
82
|
+
field_name = "id"
|
|
83
|
+
field_value = $input.user_id
|
|
84
|
+
data = { name: $input.name, updated_at: now }
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Delete record
|
|
88
|
+
db.del "user" { field_name = "id", field_value = $input.user_id }
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## API Requests
|
|
92
|
+
|
|
93
|
+
```xs
|
|
94
|
+
api.request {
|
|
95
|
+
url = "https://api.example.com/endpoint"
|
|
96
|
+
method = "POST"
|
|
97
|
+
params = { key: "value" } // Note: "params" for body, NOT "body"
|
|
98
|
+
headers = [
|
|
99
|
+
"Content-Type: application/json",
|
|
100
|
+
"Authorization: Bearer " ~ $env.API_KEY
|
|
101
|
+
]
|
|
102
|
+
timeout = 30
|
|
103
|
+
} as $api_result
|
|
104
|
+
|
|
105
|
+
// Response structure:
|
|
106
|
+
// $api_result.response.status → HTTP status code (200, 404, etc.)
|
|
107
|
+
// $api_result.response.result → Parsed response body
|
|
108
|
+
// $api_result.response.headers → Response headers
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Error Handling
|
|
112
|
+
|
|
113
|
+
```xs
|
|
114
|
+
// Precondition (stops execution if false)
|
|
115
|
+
precondition ($input.id > 0) {
|
|
116
|
+
error_type = "inputerror"
|
|
117
|
+
error = "ID must be positive"
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Try-catch
|
|
121
|
+
try_catch {
|
|
122
|
+
try {
|
|
123
|
+
// risky operation
|
|
124
|
+
}
|
|
125
|
+
catch {
|
|
126
|
+
debug.log { value = "Operation failed" }
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Throw custom error
|
|
131
|
+
throw {
|
|
132
|
+
name = "ValidationError"
|
|
133
|
+
value = "Custom error message"
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## Error Types
|
|
138
|
+
|
|
139
|
+
| Type | HTTP Status | Use Case |
|
|
140
|
+
|------|-------------|----------|
|
|
141
|
+
| `inputerror` | 400 | Invalid input data |
|
|
142
|
+
| `accessdenied` | 403 | Authorization failure |
|
|
143
|
+
| `notfound` | 404 | Resource doesn't exist |
|
|
144
|
+
| `standard` | 500 | General errors |
|
|
145
|
+
|
|
146
|
+
## Common Filters
|
|
147
|
+
|
|
148
|
+
```xs
|
|
149
|
+
// String
|
|
150
|
+
$text|trim // Remove whitespace
|
|
151
|
+
$text|to_lower // Lowercase
|
|
152
|
+
$text|to_upper // Uppercase
|
|
153
|
+
$text|substr:0:10 // Substring
|
|
154
|
+
$text|split:"," // Split to array
|
|
155
|
+
$text|contains:"x" // Check contains → bool
|
|
156
|
+
|
|
157
|
+
// Array
|
|
158
|
+
$arr|first // First element
|
|
159
|
+
$arr|last // Last element
|
|
160
|
+
$arr|count // Length
|
|
161
|
+
$arr|map:$$.field // Transform elements
|
|
162
|
+
$arr|filter:$$.active // Filter by condition
|
|
163
|
+
$arr|find:$$.id == 5 // Find first match
|
|
164
|
+
|
|
165
|
+
// Type conversion
|
|
166
|
+
$val|to_text // To string
|
|
167
|
+
$val|to_int // To integer
|
|
168
|
+
$val|to_bool // To boolean
|
|
169
|
+
$val|json_encode // To JSON string
|
|
170
|
+
$text|json_decode // From JSON string
|
|
171
|
+
|
|
172
|
+
// Object
|
|
173
|
+
$obj|get:"key" // Get property
|
|
174
|
+
$obj|get:"key":"default" // Get with default
|
|
175
|
+
$obj|set:"key":"value" // Set property
|
|
176
|
+
$obj|has:"key" // Check key exists
|
|
177
|
+
|
|
178
|
+
// Null handling
|
|
179
|
+
$val|first_notnull:"default" // Default if null
|
|
180
|
+
$val ?? "default" // Nullish coalescing
|
|
181
|
+
|
|
182
|
+
// Math
|
|
183
|
+
$num|round:2 // Round to 2 decimals
|
|
184
|
+
$num|abs // Absolute value
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## Authentication Check
|
|
188
|
+
|
|
189
|
+
```xs
|
|
190
|
+
precondition ($auth.id != null) {
|
|
191
|
+
error_type = "accessdenied"
|
|
192
|
+
error = "Authentication required"
|
|
193
|
+
}
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
## Function Call
|
|
197
|
+
|
|
198
|
+
```xs
|
|
199
|
+
function.run "my_function" {
|
|
200
|
+
input = { param1: "value" }
|
|
201
|
+
} as $result
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
## String Concatenation
|
|
205
|
+
|
|
206
|
+
```xs
|
|
207
|
+
// Basic
|
|
208
|
+
var $msg { value = "Hello, " ~ $input.name ~ "!" }
|
|
209
|
+
|
|
210
|
+
// With filters - MUST use parentheses
|
|
211
|
+
var $msg { value = ($status|to_text) ~ ": " ~ ($data|json_encode) }
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
## Common Type Names
|
|
215
|
+
|
|
216
|
+
| Use This | Not This |
|
|
217
|
+
|----------|----------|
|
|
218
|
+
| `text` | string |
|
|
219
|
+
| `int` | integer |
|
|
220
|
+
| `bool` | boolean |
|
|
221
|
+
| `decimal` | float, number |
|
|
222
|
+
| `type[]` | array, list |
|
|
223
|
+
|
|
224
|
+
## Reserved Variables (Cannot Use)
|
|
225
|
+
|
|
226
|
+
`$response`, `$output`, `$input`, `$auth`, `$env`, `$db`, `$this`, `$result`
|
|
227
|
+
|
|
228
|
+
## Input Block Syntax
|
|
229
|
+
|
|
230
|
+
```xs
|
|
231
|
+
input {
|
|
232
|
+
text name // Required
|
|
233
|
+
text nickname? // Optional (can be omitted)
|
|
234
|
+
text role?="user" // Optional with default
|
|
235
|
+
email contact filters=trim // With filters
|
|
236
|
+
text[] tags // Array type
|
|
237
|
+
object address { // Nested object
|
|
238
|
+
schema {
|
|
239
|
+
text street
|
|
240
|
+
text city
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
---
|
|
247
|
+
|
|
248
|
+
**Related:** For detailed documentation, use:
|
|
249
|
+
- `xanoscript_docs({ topic: "quickstart" })` - Common patterns and mistakes
|
|
250
|
+
- `xanoscript_docs({ topic: "syntax" })` - All filters and operators
|
|
251
|
+
- `xanoscript_docs({ topic: "database" })` - All db.* operations
|
|
252
|
+
- `xanoscript_docs({ topic: "types" })` - Type validation and schemas
|
|
@@ -6,6 +6,29 @@ applyTo: "function/**/*.xs, api/**/*.xs, task/**/*.xs, tool/**/*.xs"
|
|
|
6
6
|
|
|
7
7
|
Complete reference for XanoScript database operations.
|
|
8
8
|
|
|
9
|
+
> **TL;DR:** Use `db.get` for single record by ID, `db.query` for filtered lists, `db.has` to check existence, `db.add` to insert, `db.edit` for inline updates, `db.patch` for dynamic fields, `db.del` to delete.
|
|
10
|
+
|
|
11
|
+
## Choosing an Operation
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
Need to...
|
|
15
|
+
├── Read data?
|
|
16
|
+
│ ├── Single record by ID? → db.get
|
|
17
|
+
│ ├── Check if exists? → db.has
|
|
18
|
+
│ └── Filtered list? → db.query
|
|
19
|
+
├── Write data?
|
|
20
|
+
│ ├── New record? → db.add
|
|
21
|
+
│ ├── Update known fields? → db.edit
|
|
22
|
+
│ └── Update dynamic fields? → db.patch
|
|
23
|
+
├── Delete data?
|
|
24
|
+
│ ├── Single record? → db.del
|
|
25
|
+
│ └── All records? → db.truncate
|
|
26
|
+
└── Complex query?
|
|
27
|
+
├── Join tables? → db.query with join
|
|
28
|
+
├── Aggregate? → db.query with evals
|
|
29
|
+
└── Transaction? → db.transaction
|
|
30
|
+
```
|
|
31
|
+
|
|
9
32
|
## Quick Reference
|
|
10
33
|
|
|
11
34
|
| Operation | Purpose | Returns |
|
|
@@ -612,3 +635,17 @@ try_catch {
|
|
|
612
635
|
5. **Use null-safe operators** - `==?` for optional filters
|
|
613
636
|
6. **Use bulk operations for batch processing** - More efficient than loops
|
|
614
637
|
7. **Handle deadlocks gracefully** - Implement retry logic for concurrent writes
|
|
638
|
+
|
|
639
|
+
---
|
|
640
|
+
|
|
641
|
+
## Related Topics
|
|
642
|
+
|
|
643
|
+
Explore more with `xanoscript_docs({ topic: "<topic>" })`:
|
|
644
|
+
|
|
645
|
+
| Topic | Description |
|
|
646
|
+
|-------|-------------|
|
|
647
|
+
| `tables` | Database schema definitions with indexes and relationships |
|
|
648
|
+
| `syntax` | Query filters, operators, and expressions |
|
|
649
|
+
| `quickstart` | Common CRUD patterns and examples |
|
|
650
|
+
| `addons` | Reusable subqueries for fetching related data |
|
|
651
|
+
| `performance` | Query optimization best practices |
|