Skip to content

Commit aaaf0fc

Browse files
Copilothotlong
andcommitted
Restore docs/guide directory (server-side documentation)
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent ecd12c5 commit aaaf0fc

24 files changed

+4558
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Architecture & Concepts
2+
3+
ObjectQL is built with a modular architecture that separates the data definition (Metadata) from the execution engine (Driver). This design allows applications to run on different database stacks (SQL vs NoSQL) without changing the business logic.
4+
5+
## High-Level Overview
6+
7+
An ObjectQL application consists of three main layers:
8+
9+
1. **Metadata Layer**: JSON/YAML files that define the shape of your data (`.object.yml`) and operations.
10+
2. **Core Engine**: The `ObjectQL` class that loads metadata, validates requests, and orchestrates execution.
11+
3. **Driver Layer**: Adapters that translate ObjectQL requests into database-specific queries (SQL, Mongo Protocol, etc.).
12+
13+
## Dependency Graph
14+
15+
The project is structured as a monorepo with strict dependency rules to ensure scalability and maintainability.
16+
17+
* **`@objectql/types`**: The shared contract. Contains all interfaces (`ObjectConfig`, `ObjectQLDriver`). Has **zero dependencies**.
18+
* **`@objectql/core`**: The main runtime. It depends on `types`.
19+
* **`@objectql/driver-*`**: Database adapters. They implement interfaces from `types` but do **not** depend on `core` (avoiding circular dependencies).
20+
21+
## Core Concepts
22+
23+
### 1. Metadata-First
24+
In traditional ORMs (like TypeORM or Prisma), you define classes/schema in code. In ObjectQL, the schema is data itself. This allows:
25+
* Dynamic schema generation at runtime.
26+
* Building "No-Code" table designers.
27+
* Hot-reloading of data structure without recompiling.
28+
29+
### 2. Universal Protocol
30+
ObjectQL uses a unified JSON-based query language (AST). This allows frontends, AI agents, or external systems to send complex queries (`filters`, `expand`, `aggregates`) in a safe, serializable format.
31+
32+
### 3. Logic: Actions & Hooks
33+
* **Hooks**: Intercept standard CRUD operations (e.g., "Before Create", "After Update") to enforce business rules.
34+
* **Actions**: Define custom RPC methods (e.g., "Approve Invoice") exposed via the API.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Why ObjectQL?
2+
3+
In the era of AI automation, the requirements for backend infrastructure have shifted. We are no longer just building for human users on web forms; we are building systems that **AI Agents** need to read, understand, and manipulate.
4+
5+
## The Problem with Traditional ORMs
6+
7+
Tools like TypeORM, Prisma, or Sequelize are fantastic for human developers. They rely heavily on:
8+
1. **Complex TypeScript Types**: Great for IDE autocomplete, but invisible to an LLM running in a production execution environment.
9+
2. **Chained Method Calls**: `db.users.where(...).include(...)`. This requires the AI to synthesize valid executable code, which is prone to syntax errors and hallucinations.
10+
3. **Code-First Schema**: The schema is buried in class definitions, making it hard to extract a simple "map" of the data for the AI context window.
11+
12+
## The ObjectQL Solution: Protocol-First
13+
14+
ObjectQL treats **Logic as Data**.
15+
16+
### 1. Schema is JSON (The Context)
17+
Instead of parsing TypeScript files, an AI Agent can simply read a JSON definition. This is the native tongue of LLMs.
18+
19+
```json
20+
{
21+
"name": "contact",
22+
"fields": { "email": { "type": "text", "required": true } }
23+
}
24+
```
25+
26+
This compact format fits perfectly in RAG (Retrieval-Augmented Generation) contexts.
27+
28+
### 2. Queries are ASTs (The Action)
29+
To fetch data, the AI doesn't need to write SQL or function code. It generates a JSON object (Abstract Syntax Tree).
30+
31+
```json
32+
{
33+
"op": "find",
34+
"from": "contact",
35+
"filters": [["email", "contains", "@gmail.com"]]
36+
}
37+
```
38+
* **Safe**: No SQL Injection possible. The Driver handles escaping.
39+
* **Deterministic**: It either parses or it fails. No subtle logic bugs from misplaced parentheses in code.
40+
* **Portability**: The same JSON runs on Mongo, Postgres, or a REST API.
41+
42+
## Comparison
43+
44+
| Feature | Traditional ORM | ObjectQL |
45+
| :--- | :--- | :--- |
46+
| **Schema Definition** | TypeScript Classes / Migration Files | JSON / YAML Metadata |
47+
| **Query Language** | Fluent API / Raw SQL | JSON AST |
48+
| **Runtime** | Node.js / Python Runtime | Universal Protocol (Any Language) |
49+
| **AI Friendliness** | Low (Requires Code Gen) | High (Requires JSON Gen) |
50+
| **Dynamic Fields** | Hard (Migration required) | Native (Metadata-driven) |
51+
52+
## Conclusion
53+
54+
If you are building a Copilot, an Autonomous Agent, or a Low-Code platform, ObjectQL provides the structured, safe, and descriptive layer that connects your LLM to your database.

docs/guide/cli.md

Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
# CLI Guide
2+
3+
The ObjectQL CLI (`@objectql/cli`) is an essential tool for development, automating tasks like type generation and database migrations.
4+
5+
## 1. Installation
6+
7+
The CLI is typically installed as a dev dependency in your project.
8+
9+
```bash
10+
npm install -D @objectql/cli
11+
```
12+
13+
You can then run it via `npx objectql` or add scripts to your `package.json`.
14+
15+
## 2. Core Commands
16+
17+
### 2.1 `init` (Create Project)
18+
19+
Create a new ObjectQL project from a starter template.
20+
21+
```bash
22+
npx objectql init [options]
23+
```
24+
25+
**Options:**
26+
27+
| Option | Alias | Default | Description |
28+
| :--- | :--- | :--- | :--- |
29+
| `--template <template>` | `-t` | `basic` | Template to use (`basic`, `express-api`, `enterprise`). |
30+
| `--name <name>` | `-n` | - | Project name. |
31+
| `--dir <path>` | `-d` | - | Target directory. |
32+
| `--skip-install` | | `false` | Skip dependency installation. |
33+
| `--skip-git` | | `false` | Skip git initialization. |
34+
35+
**Example:**
36+
37+
```bash
38+
npx objectql init -t express-api -n my-app
39+
```
40+
41+
### 2.2 `generate` (Type Generation)
42+
43+
Scans your `*.object.yml` files and generates TypeScript interfaces. This is crucial for maintaining type safety in your Hooks and Actions.
44+
**Alias**: `g`
45+
46+
```bash
47+
npx objectql generate [options]
48+
```
49+
50+
**Options:**
51+
52+
| Option | Alias | Default | Description |
53+
| :--- | :--- | :--- | :--- |
54+
| `--source <path>` | `-s` | `.` | Root directory to search for object files. |
55+
| `--output <path>` | `-o` | `./src/generated` | Directory where `.ts` files will be generated. |
56+
57+
**Example:**
58+
59+
```bash
60+
# Generate types from /src/objects to /src/types
61+
npx objectql generate --source ./src/objects --output ./src/types
62+
```
63+
64+
### 2.3 `new` (Scaffold Metadata)
65+
66+
Generate a new metadata file (Object, View, Form, etc.) in the project.
67+
68+
```bash
69+
npx objectql new <type> <name> [options]
70+
```
71+
72+
**Arguments:**
73+
* `<type>`: The type of metadata to generate (e.g., `object`, `view`, `page`).
74+
* `<name>`: The name of the file/entity.
75+
76+
**Options:**
77+
78+
| Option | Alias | Default | Description |
79+
| :--- | :--- | :--- | :--- |
80+
| `--dir <path>` | `-d` | `.` | Output directory. |
81+
82+
**Example:**
83+
```bash
84+
npx objectql new object customer
85+
```
86+
87+
## 3. Development Tools
88+
89+
### 3.1 `serve` (Dev Server)
90+
91+
Start a standalone development server.
92+
**Alias**: `s`
93+
94+
```bash
95+
npx objectql serve [options]
96+
```
97+
98+
**Options:**
99+
100+
| Option | Alias | Default | Description |
101+
| :--- | :--- | :--- | :--- |
102+
| `--port <number>` | `-p` | `3000` | Port to listen on. |
103+
| `--dir <path>` | `-d` | `.` | Directory containing schema. |
104+
105+
### 3.2 `studio` (Admin UI)
106+
107+
Starts the web-based admin studio to browse data and view schema.
108+
**Alias**: `ui`
109+
110+
```bash
111+
npx objectql studio [options]
112+
```
113+
114+
**Options:**
115+
116+
| Option | Alias | Default | Description |
117+
| :--- | :--- | :--- | :--- |
118+
| `--port <number>` | `-p` | `3000` | Port to listen on. |
119+
| `--dir <path>` | `-d` | `.` | Directory containing schema. |
120+
| `--no-open` | | `false` | Do not open the browser automatically. |
121+
122+
### 3.3 `repl` (Interactive Shell)
123+
124+
Starts an interactive terminal similar to the MongoDB shell, allowing you to directly query your database using the ObjectQL API.
125+
**Alias**: `r`
126+
127+
```bash
128+
npx objectql repl [options]
129+
```
130+
131+
**Options:**
132+
133+
| Option | Alias | Default | Description |
134+
| :--- | :--- | :--- | :--- |
135+
| `--config <path>` | `-c` | - | Path to `objectql.config.ts/js`. |
136+
137+
**Example Session:**
138+
139+
```javascript
140+
objectql> await tasks.find({ status: 'todo' })
141+
[ { id: 1, title: 'Fix bug', status: 'todo' } ]
142+
```
143+
144+
## 4. Internationalization (i18n)
145+
146+
Tools for managing translations.
147+
148+
### 4.1 `i18n extract`
149+
150+
Extract translatable strings from metadata files into JSON.
151+
152+
```bash
153+
npx objectql i18n extract [options]
154+
```
155+
156+
**Options:**
157+
158+
| Option | Alias | Default | Description |
159+
| :--- | :--- | :--- | :--- |
160+
| `--source <path>` | `-s` | `.` | Source directory to scan. |
161+
| `--output <path>` | `-o` | `./src/i18n` | Output directory for translation files. |
162+
| `--lang <lang>` | `-l` | `en` | Language code. |
163+
164+
### 4.2 `i18n init`
165+
166+
Initialize i18n structure for a new language.
167+
168+
```bash
169+
npx objectql i18n init <lang> [options]
170+
```
171+
172+
**Options:**
173+
174+
| Option | Alias | Default | Description |
175+
| :--- | :--- | :--- | :--- |
176+
| `--base-dir <path>` | `-b` | `./src/i18n` | Base i18n directory. |
177+
178+
### 4.3 `i18n validate`
179+
180+
Validate translation completeness against a base language.
181+
182+
```bash
183+
npx objectql i18n validate <lang> [options]
184+
```
185+
186+
**Options:**
187+
188+
| Option | Alias | Default | Description |
189+
| :--- | :--- | :--- | :--- |
190+
| `--base-dir <path>` | `-b` | `./src/i18n` | Base i18n directory. |
191+
| `--base-lang <lang>` | | `en` | Base language to compare against. |
192+
193+
## 5. Database Migration
194+
195+
Manage database schema changes.
196+
197+
### 5.1 `migrate`
198+
199+
Run pending database migrations.
200+
201+
```bash
202+
npx objectql migrate [options]
203+
```
204+
205+
**Options:**
206+
207+
| Option | Alias | Default | Description |
208+
| :--- | :--- | :--- | :--- |
209+
| `--config <path>` | `-c` | - | Path to `objectql.config.ts/js`. |
210+
| `--dir <path>` | `-d` | `./migrations` | Migrations directory. |
211+
212+
### 5.2 `migrate create`
213+
214+
Create a new migration file.
215+
216+
```bash
217+
npx objectql migrate create <name> [options]
218+
```
219+
220+
**Options:**
221+
222+
| Option | Alias | Default | Description |
223+
| :--- | :--- | :--- | :--- |
224+
| `--dir <path>` | `-d` | `./migrations` | Migrations directory. |
225+
226+
### 5.3 `migrate status`
227+
228+
Show the status of migrations (applied/pending).
229+
230+
```bash
231+
npx objectql migrate status [options]
232+
```
233+
234+
**Options:**
235+
236+
| Option | Alias | Default | Description |
237+
| :--- | :--- | :--- | :--- |
238+
| `--config <path>` | `-c` | - | Path to `objectql.config.ts/js`. |
239+
| `--dir <path>` | `-d` | `./migrations` | Migrations directory. |
240+
241+
## 6. Integration with Workflow
242+
243+
We recommend adding the CLI commands to your lifecycle scripts.
244+
245+
**package.json:**
246+
247+
```json
248+
{
249+
"scripts": {
250+
"codegen": "objectql generate -s ./src -o ./src/generated",
251+
"build": "npm run codegen && tsc",
252+
"dev": "npm run codegen && ts-node src/index.ts",
253+
"studio": "objectql studio"
254+
}
255+
}
256+
```
257+
258+
This ensures that whenever you build or start your app, your TypeScript types are perfectly synced with your YAML definitions.

docs/guide/configuration.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Configuration
2+
3+
ObjectQL is configured by passing an `ObjectQLConfig` object to the constructor.
4+
5+
## Basic Layout
6+
7+
```typescript
8+
// objectql.config.ts
9+
import { ObjectQL } from '@objectql/core';
10+
11+
export const db = new ObjectQL({
12+
connection: 'sqlite://data.db', // 1. Infrastructure
13+
presets: ['@objectql/preset-auth'], // 2. Base Capabilities
14+
source: ['src'], // 3. Application Logic
15+
plugins: [] // 4. Extensions
16+
});
17+
```
18+
19+
## Reference
20+
21+
### `connection` (string)
22+
The Connection String URI defining the database connection.
23+
* `sqlite://path/to/db`
24+
* `postgres://user:pass@host:5432/db`
25+
* `mongodb://host:27017/db`
26+
27+
The engine will automatically load the appropriate driver (`@objectql/driver-sql` or `@objectql/driver-mongo`).
28+
29+
### `source` (string | string[])
30+
One or more directory paths (relative or absolute) containing your schema files (`*.object.yml`).
31+
The loader scans these directories recursively.
32+
33+
### `presets` (string[])
34+
A list of NPM packages to load as presets.
35+
ObjectQL will try to resolve the package and load schema files from its directory.
36+
Useful for sharing common business objects (User, Role, File, etc.).
37+
38+
### `plugins` ((ObjectQLPlugin | string)[])
39+
A list of plugin instances OR package names to extend the core functionality.
40+
See [Plugin System](./plugins.html) for details.
41+
42+
### `objects` (Record<string, ObjectConfig>)
43+
(Advanced) In-memory definition of objects. Useful for dynamic runtime schema generation.
44+
Objects defined here take highest priority.

0 commit comments

Comments
 (0)