|
| 1 | +# Getting Started with ObjectQL |
| 2 | + |
| 3 | +**ObjectQL** is a universal, metadata-driven ORM designed for building dynamic business applications. Unlike traditional ORMs where you define schema in class files (like TypeORM entities), ObjectQL uses a **Metadata-First** approach. |
| 4 | + |
| 5 | +## Why ObjectQL? |
| 6 | + |
| 7 | +1. **Metadata-Driven**: Define your data models in JSON or YAML. Perfect for low-code platforms where schema changes at runtime. |
| 8 | +2. **Universal Protocol**: The query language is a JSON AST, making it easy for frontends or AI agents to generate queries. |
| 9 | +3. **Action & Hook System**: Built-in support for "Button Actions" (RPC) and "Triggers" (Hooks), allowing you to model **Behavior** alongside **Data**. |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +Install the core package and a driver (e.g., PostgreSQL or MongoDB). |
| 14 | + |
| 15 | +```bash |
| 16 | +npm install @objectql/core @objectql/driver-knex knex pg |
| 17 | +# or |
| 18 | +npm install @objectql/core @objectql/driver-mongo mongodb |
| 19 | +``` |
| 20 | + |
| 21 | +## Quick Start: The "Hello World" |
| 22 | + |
| 23 | +Let's build a simple **To-Do List** backend. |
| 24 | + |
| 25 | +### 1. Define Your Object |
| 26 | + |
| 27 | +In ObjectQL, everything is an "Object" (like a Table or Collection). |
| 28 | + |
| 29 | +```yaml |
| 30 | +# todo.object.yml |
| 31 | +name: todo |
| 32 | +label: To-Do Item |
| 33 | +fields: |
| 34 | + title: |
| 35 | + type: text |
| 36 | + label: Task Name |
| 37 | + completed: |
| 38 | + type: boolean |
| 39 | + default: false |
| 40 | +``` |
| 41 | +
|
| 42 | +### 2. Initialize the Engine |
| 43 | +
|
| 44 | +```typescript |
| 45 | +import { ObjectQL } from '@objectql/core'; |
| 46 | +import { KnexDriver } from '@objectql/driver-knex'; |
| 47 | + |
| 48 | +async function main() { |
| 49 | + // 1. Configure the engine |
| 50 | + const app = new ObjectQL({ |
| 51 | + datasources: { |
| 52 | + default: new KnexDriver({ |
| 53 | + client: 'pg', |
| 54 | + connection: 'postgres://user:pass@localhost:5432/mydb' |
| 55 | + }) |
| 56 | + }, |
| 57 | + objects: { |
| 58 | + // In a real app, you can load these from a directory using app.loadFromDirectory() |
| 59 | + todo: { |
| 60 | + name: 'todo', |
| 61 | + fields: { |
| 62 | + title: { type: 'text' }, |
| 63 | + completed: { type: 'boolean' } |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + }); |
| 68 | + |
| 69 | + await app.init(); |
| 70 | + |
| 71 | + // 2. Create Data (CRUD) |
| 72 | + // Create a context (representing a user request) |
| 73 | + const ctx = app.createContext({}); |
| 74 | + const todoRepo = ctx.object('todo'); |
| 75 | + |
| 76 | + const newTask = await todoRepo.create({ |
| 77 | + title: 'Learn ObjectQL', |
| 78 | + completed: false |
| 79 | + }); |
| 80 | + console.log('Created:', newTask); |
| 81 | + |
| 82 | + // 3. Query Data |
| 83 | + const tasks = await todoRepo.find({ |
| 84 | + filters: [['completed', '=', false]] |
| 85 | + }); |
| 86 | + console.log('Pending Tasks:', tasks); |
| 87 | +} |
| 88 | + |
| 89 | +main(); |
| 90 | +``` |
| 91 | + |
| 92 | +## Adding Business Logic |
| 93 | + |
| 94 | +ObjectQL shines when you need to add logic. |
| 95 | + |
| 96 | +### Adding a Hook |
| 97 | + |
| 98 | +Triggers logic automatically when data changes. |
| 99 | + |
| 100 | +```typescript |
| 101 | +app.on('beforeCreate', 'todo', async (ctx) => { |
| 102 | + if (ctx.doc.title === 'Sleep') { |
| 103 | + throw new Error("Cannot sleep yet!"); |
| 104 | + } |
| 105 | + // Auto-tagging |
| 106 | + ctx.doc.title = `[Task] ${ctx.doc.title}`; |
| 107 | +}); |
| 108 | +``` |
| 109 | + |
| 110 | +### Adding an Action |
| 111 | + |
| 112 | +Defines a custom operation (RPC) that frontends can call. |
| 113 | + |
| 114 | +```typescript |
| 115 | +// Define protocol |
| 116 | +app.registerAction('todo', 'mark_done', async (ctx) => { |
| 117 | + const { id } = ctx; |
| 118 | + await ctx.object('todo').update(id, { completed: true }); |
| 119 | + return { message: 'Good job!' }; |
| 120 | +}); |
| 121 | + |
| 122 | +// Invocation |
| 123 | +await ctx.object('todo').execute('mark_done', 'id_123', {}); |
| 124 | +``` |
| 125 | + |
| 126 | +## Next Steps |
| 127 | + |
| 128 | +* **[Data Modeling](./data-modeling.md)**: Learn about all field types (Select, Lookup, Date, etc.) |
| 129 | +* **[SDK Reference](./sdk-reference.md)**: Explore the full API. |
| 130 | +* **[Hooks](./logic-hooks.md)**: Deep dive into the event system. |
0 commit comments