Skip to content

Commit 95f7807

Browse files
committed
Merge branch 'main' into copilot/organize-raw-data-structure
2 parents ad7601f + 4edd814 commit 95f7807

76 files changed

Lines changed: 17610 additions & 302 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828

2929
- name: Install dependencies
3030
run: pnpm install --frozen-lockfile
31-
31+
3232
- name: Build packages
3333
run: pnpm run build
3434

.vscode/launch.json

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "Studio: Basic Script",
6+
"type": "node",
7+
"request": "launch",
8+
"program": "${workspaceFolder}/packages/cli/dist/index.js",
9+
"args": [
10+
"studio",
11+
"--dir",
12+
"examples/starters/basic-script"
13+
],
14+
"console": "integratedTerminal",
15+
"skipFiles": [
16+
"<node_internals>/**"
17+
],
18+
"env": {
19+
"NODE_ENV": "development"
20+
}
21+
},
22+
{
23+
"name": "Studio: Express API",
24+
"type": "node",
25+
"request": "launch",
26+
"program": "${workspaceFolder}/packages/cli/dist/index.js",
27+
"args": [
28+
"studio",
29+
"--dir",
30+
"examples/starters/express-api"
31+
],
32+
"console": "integratedTerminal",
33+
"skipFiles": [
34+
"<node_internals>/**"
35+
]
36+
},
37+
{
38+
"name": "Studio: Preset Usage",
39+
"type": "node",
40+
"request": "launch",
41+
"program": "${workspaceFolder}/packages/cli/dist/index.js",
42+
"args": [
43+
"studio",
44+
"--dir",
45+
"examples/scenarios/preset-usage"
46+
],
47+
"console": "integratedTerminal",
48+
"skipFiles": [
49+
"<node_internals>/**"
50+
]
51+
}
52+
]
53+
}

.vscode/tasks.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "Run Studio: Basic Script",
6+
"type": "shell",
7+
"command": "node",
8+
"args": [
9+
"packages/cli/dist/index.js",
10+
"studio",
11+
"--dir",
12+
"examples/starters/basic-script"
13+
],
14+
"group": "test",
15+
"presentation": {
16+
"echo": true,
17+
"reveal": "always",
18+
"focus": false,
19+
"panel": "shared",
20+
"showReuseMessage": true,
21+
"clear": false
22+
},
23+
"problemMatcher": []
24+
},
25+
{
26+
"label": "Build All Packages",
27+
"type": "shell",
28+
"command": "pnpm",
29+
"args": ["run", "build"],
30+
"group": "build",
31+
"problemMatcher": ["$tsc"]
32+
}
33+
]
34+
}

API_REFACTORING_SUMMARY.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# API Refactoring Summary
2+
3+
## Overview
4+
Successfully refactored the ObjectQL server package to conform with the new API specification documented in `docs/api/README.md`.
5+
6+
## Key Achievements
7+
8+
### 1. REST API Implementation ✅
9+
Created a complete REST-style API adapter supporting:
10+
- `GET /api/data/:object` - List records with filtering, sorting, pagination
11+
- `GET /api/data/:object/:id` - Get single record
12+
- `POST /api/data/:object` - Create new record (HTTP 201)
13+
- `PUT/PATCH /api/data/:object/:id` - Update record
14+
- `DELETE /api/data/:object/:id` - Delete record
15+
16+
### 2. Enhanced Error Handling ✅
17+
- Standardized error codes: `VALIDATION_ERROR`, `UNAUTHORIZED`, `FORBIDDEN`, `NOT_FOUND`, `CONFLICT`, `INTERNAL_ERROR`, `DATABASE_ERROR`, `RATE_LIMIT_EXCEEDED`
18+
- Proper HTTP status code mapping (400, 401, 403, 404, 409, 429, 500)
19+
- Detailed error responses with error code, message, and optional details
20+
21+
### 3. AI Context Support ✅
22+
- Added `ai_context` field to `ObjectQLRequest`
23+
- Supports intent, natural language descriptions, and use case tracking
24+
- Logged for debugging and analytics
25+
26+
### 4. Metadata API Enhancements ✅
27+
- `GET /api/metadata/objects/:name/fields/:field` - Get detailed field metadata
28+
- `GET /api/metadata/objects/:name/actions` - List available actions
29+
- Enhanced field metadata with validation properties
30+
31+
### 5. Improved JSON-RPC API ✅
32+
- Better error categorization and handling
33+
- Support for both string ID and query object in `findOne`
34+
- Standardized response formats
35+
- AI context logging
36+
37+
### 6. Example Updates ✅
38+
- Updated express-api example to demonstrate all API styles
39+
- Added sample curl commands for testing
40+
- Clear console output showing all available endpoints
41+
42+
## Testing
43+
- ✅ All existing tests pass (3/3)
44+
- ✅ New REST API tests added (7/7)
45+
- ✅ Total: 10/10 tests passing
46+
- ✅ Zero TypeScript errors
47+
48+
## Usage Examples
49+
50+
### JSON-RPC API
51+
```bash
52+
curl -X POST http://localhost:3004/api/objectql \
53+
-H "Content-Type: application/json" \
54+
-d '{"op": "find", "object": "User", "args": {}}'
55+
```
56+
57+
### REST API
58+
```bash
59+
# List users
60+
curl http://localhost:3004/api/data/User
61+
62+
# Get specific user
63+
curl http://localhost:3004/api/data/User/1
64+
65+
# Create user
66+
curl -X POST http://localhost:3004/api/data/User \
67+
-H "Content-Type: application/json" \
68+
-d '{"name": "Alice", "email": "alice@example.com"}'
69+
70+
# Update user
71+
curl -X PUT http://localhost:3004/api/data/User/1 \
72+
-H "Content-Type: application/json" \
73+
-d '{"name": "Alice Updated"}'
74+
75+
# Delete user
76+
curl -X DELETE http://localhost:3004/api/data/User/1
77+
```
78+
79+
### Metadata API
80+
```bash
81+
# List all objects
82+
curl http://localhost:3004/api/metadata/objects
83+
84+
# Get object schema
85+
curl http://localhost:3004/api/metadata/objects/User
86+
87+
# Get field metadata
88+
curl http://localhost:3004/api/metadata/objects/User/fields/email
89+
90+
# List actions
91+
curl http://localhost:3004/api/metadata/objects/User/actions
92+
```
93+
94+
## Files Changed
95+
- `packages/server/src/types.ts` - Enhanced type definitions
96+
- `packages/server/src/server.ts` - Improved error handling
97+
- `packages/server/src/adapters/rest.ts` - NEW: REST API adapter
98+
- `packages/server/src/adapters/node.ts` - Enhanced JSON-RPC handler
99+
- `packages/server/src/metadata.ts` - Added new metadata endpoints
100+
- `packages/server/src/index.ts` - Export REST handler
101+
- `packages/server/test/rest.test.ts` - NEW: REST API tests
102+
- `examples/starters/express-api/src/index.ts` - Updated example
103+
104+
## Commits
105+
1. `6f58cea` - Initial changes with types and server enhancements
106+
2. `3c395a9` - Complete REST API implementation and tests
107+
108+
## Next Steps (Optional)
109+
- [ ] Add rate limiting implementation
110+
- [ ] Add WebSocket API support
111+
- [ ] Add GraphQL API support
112+
- [ ] Add bulk operations support
113+
- [ ] Add field-level permission checks
114+
- [ ] Add request/response compression

README.md

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ It is **not** an ORM, but a high-level data protocol designed for AI agents, low
3434
* This structure is optimized for LLMs (like ChatGPT/Claude) to understand schema and generate accurate, safe business logic without hallucinating SQL syntax.
3535

3636

37+
* **✅ Metadata-Driven Validation:**
38+
* Define validation rules declaratively in YAML/JSON metadata.
39+
* Support for field-level, cross-field, and state machine validation.
40+
* Configurable severity levels (error, warning, info) and triggers (create, update, delete).
41+
* Template messages with internationalization support.
42+
43+
3744
* **⚡ Modern & Lightweight:**
3845
* Written in 100% **TypeScript**.
3946
* **Zero heavy legacy dependencies.** No runtime environment requirements beyond Node.js.
@@ -119,6 +126,100 @@ const resultsSql = await app.datasource('runtime').find(query);
119126

120127
```
121128

129+
Both queries return the same results, but the underlying native query is optimized for MongoDB or PostgreSQL.
130+
131+
### 3. Validation
132+
133+
Define validation rules declaratively in your object metadata:
134+
135+
```typescript
136+
import { ObjectConfig, Validator } from '@objectql/core';
137+
import { ValidationContext } from '@objectql/types';
138+
139+
// Define object with validation rules
140+
const projectObject: ObjectConfig = {
141+
name: 'project',
142+
fields: {
143+
name: {
144+
type: 'text',
145+
required: true,
146+
validation: {
147+
min_length: 3,
148+
max_length: 100,
149+
pattern: '^[a-zA-Z0-9\\s]+$'
150+
}
151+
},
152+
email: {
153+
type: 'email',
154+
validation: {
155+
format: 'email',
156+
message: 'Please enter a valid email address'
157+
}
158+
},
159+
start_date: { type: 'date' },
160+
end_date: { type: 'date' },
161+
status: {
162+
type: 'select',
163+
options: [
164+
{ label: 'Planning', value: 'planning' },
165+
{ label: 'Active', value: 'active' },
166+
{ label: 'Completed', value: 'completed' }
167+
]
168+
}
169+
},
170+
validation: {
171+
rules: [
172+
// Cross-field validation
173+
{
174+
name: 'valid_date_range',
175+
type: 'cross_field',
176+
rule: {
177+
field: 'end_date',
178+
operator: '>=',
179+
compare_to: 'start_date'
180+
},
181+
message: 'End date must be on or after start date',
182+
error_code: 'INVALID_DATE_RANGE'
183+
},
184+
// State machine validation
185+
{
186+
name: 'status_transition',
187+
type: 'state_machine',
188+
field: 'status',
189+
transitions: {
190+
planning: { allowed_next: ['active', 'cancelled'] },
191+
active: { allowed_next: ['completed', 'cancelled'] },
192+
completed: { allowed_next: [], is_terminal: true }
193+
},
194+
message: 'Invalid status transition'
195+
}
196+
]
197+
}
198+
};
199+
200+
// Execute validation programmatically
201+
const validator = new Validator({ language: 'en' });
202+
203+
const result = await validator.validate(
204+
projectObject.validation.rules,
205+
{
206+
record: { start_date: '2024-01-01', end_date: '2023-12-31' },
207+
operation: 'create'
208+
}
209+
);
210+
211+
if (!result.valid) {
212+
console.log('Validation errors:', result.errors);
213+
}
214+
```
215+
216+
**Supported validation types:**
217+
- **Field-level**: required, email, URL, min/max, length, pattern
218+
- **Cross-field**: compare fields with operators (=, !=, >, >=, <, <=, in, contains, etc.)
219+
- **State machine**: enforce valid state transitions
220+
- **Severity levels**: error, warning, info
221+
- **I18n**: multi-language error messages with fallback
222+
122223
## 🎨 Web Console
123224

124225
ObjectQL includes a beautiful web-based admin console for database management.
@@ -211,6 +312,33 @@ aggregations:
211312
212313
See [Visual Reporting Guide](./docs/guide/visual-reporting.md) for complete documentation.
213314
315+
## 📡 API Reference
316+
317+
ObjectQL provides **multiple API styles** to suit different use cases:
318+
319+
* **[Complete API Reference](./docs/api/README.md)** - Comprehensive guide to all API endpoints
320+
* **[JSON-RPC API](./docs/api/README.md#json-rpc-style-api)** - Universal protocol for all operations
321+
* **[REST API](./docs/api/README.md#rest-style-api)** - Traditional REST endpoints
322+
* **[Metadata API](./docs/api/README.md#metadata-api)** - Runtime schema discovery and introspection
323+
* **[Authentication Guide](./docs/api/authentication.md)** - Securing your APIs with JWT, API keys, and more
324+
325+
**Quick Example:**
326+
```bash
327+
# Create a record via JSON-RPC
328+
curl -X POST http://localhost:3000/api/objectql \
329+
-H "Content-Type: application/json" \
330+
-d '{
331+
"op": "create",
332+
"object": "tasks",
333+
"args": {
334+
"name": "Complete API documentation",
335+
"priority": "high"
336+
}
337+
}'
338+
```
339+
340+
See the [API Reference](./docs/api/README.md) for complete documentation with examples.
341+
214342
## 🛣 Roadmap
215343

216344
* [ ] **Phase 1: Core Protocol:** Define stable `UnifiedQuery` types and AST parser.

docs/.vitepress/config.mts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,24 @@ export default defineConfig({
1717
{ text: 'Guide', link: '/guide/' },
1818
{ text: 'AI-Native', link: '/ai/' },
1919
{ text: 'Protocol', link: '/spec/' },
20+
{ text: 'API Reference', link: '/api/' },
2021
],
2122

2223
// Sidebar Configuration
2324
sidebar: {
25+
// Sidebar for API section
26+
'/api/': [
27+
{
28+
text: 'API Reference',
29+
items: [
30+
{ text: 'Overview', link: '/api/' },
31+
{ text: 'Quick Reference', link: '/api/quick-reference' },
32+
{ text: 'Authentication', link: '/api/authentication' },
33+
{ text: 'Full Specification', link: '/api/README' },
34+
]
35+
}
36+
],
37+
2438
// Sidebar for AI section
2539
'/ai/': [
2640
{

0 commit comments

Comments
 (0)