odac 1.3.0 → 1.4.1
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/.agent/rules/memory.md +10 -1
- package/.github/workflows/release.yml +1 -5
- package/AGENTS.md +47 -0
- package/CHANGELOG.md +58 -0
- package/README.md +11 -1
- package/bin/odac.js +359 -6
- package/client/odac.js +15 -11
- package/docs/ai/README.md +49 -0
- package/docs/ai/skills/SKILL.md +40 -0
- package/docs/ai/skills/backend/authentication.md +74 -0
- package/docs/ai/skills/backend/config.md +39 -0
- package/docs/ai/skills/backend/controllers.md +69 -0
- package/docs/ai/skills/backend/cron.md +57 -0
- package/docs/ai/skills/backend/database.md +37 -0
- package/docs/ai/skills/backend/forms.md +26 -0
- package/docs/ai/skills/backend/ipc.md +62 -0
- package/docs/ai/skills/backend/mail.md +41 -0
- package/docs/ai/skills/backend/migrations.md +80 -0
- package/docs/ai/skills/backend/request_response.md +42 -0
- package/docs/ai/skills/backend/routing.md +58 -0
- package/docs/ai/skills/backend/storage.md +50 -0
- package/docs/ai/skills/backend/streaming.md +41 -0
- package/docs/ai/skills/backend/structure.md +64 -0
- package/docs/ai/skills/backend/translations.md +49 -0
- package/docs/ai/skills/backend/utilities.md +31 -0
- package/docs/ai/skills/backend/validation.md +60 -0
- package/docs/ai/skills/backend/views.md +68 -0
- package/docs/ai/skills/frontend/core.md +73 -0
- package/docs/ai/skills/frontend/forms.md +28 -0
- package/docs/ai/skills/frontend/navigation.md +27 -0
- package/docs/ai/skills/frontend/realtime.md +54 -0
- package/docs/backend/08-database/04-migrations.md +258 -37
- package/docs/backend/10-authentication/01-user-logins-with-authjs.md +2 -0
- package/docs/backend/10-authentication/05-session-management.md +25 -3
- package/package.json +1 -1
- package/src/Auth.js +128 -17
- package/src/Config.js +1 -1
- package/src/Database/ConnectionFactory.js +69 -0
- package/src/Database/Migration.js +1203 -0
- package/src/Database.js +35 -35
- package/src/Route/Internal.js +21 -18
- package/src/Route/MimeTypes.js +56 -0
- package/src/Route.js +40 -63
- package/src/View/Form.js +91 -51
- package/src/View.js +8 -3
- package/template/schema/users.js +23 -0
- package/test/Auth.test.js +310 -0
- package/test/Client.test.js +29 -0
- package/test/Config.test.js +7 -0
- package/test/Database/ConnectionFactory.test.js +80 -0
- package/test/Migration.test.js +943 -0
- package/test/View/Form.test.js +37 -0
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# ODAC AI Agent Master Guide
|
|
2
|
+
|
|
3
|
+
Welcome! If you are reading this document, you are an AI Agent tasked with developing a project using the ODAC (Open Distributed Application Container) Framework.
|
|
4
|
+
|
|
5
|
+
ODAC is a high-performance, enterprise-grade, and zero-configuration Node.js framework. Your mission is to produce secure, scalable code while strictly adhering to the framework's principles.
|
|
6
|
+
|
|
7
|
+
## Core Mandate
|
|
8
|
+
When developing with ODAC, you must not only write code but also leverage the framework's optimized structures (Route, View, Database, Hub, etc.) in the most efficient way possible.
|
|
9
|
+
|
|
10
|
+
## Agent Principles
|
|
11
|
+
1. **Performance Focused**: Always aim for O(1) or O(n log n) complexity. Avoid inefficient queries like `SELECT *`.
|
|
12
|
+
2. **Security-by-Default**: Validate all input data using the `Validator`. Trust the auto-sanitizing View engine but maintain manual control in critical areas.
|
|
13
|
+
3. **Error Management**: Never swallow errors. Use framework-specific methods like `Odac.Request.error()` to return structured error messages.
|
|
14
|
+
4. **Clean Code**: Keep functions small (Single Responsibility) and adhere to ESM (ES Modules) standards.
|
|
15
|
+
|
|
16
|
+
## How to Use This Guide
|
|
17
|
+
The `skills/` directory contains a master `SKILL.md` that indexes specialized rules for each module. When adding a feature, you MUST refer to these rules and base your implementation on the "Reference Patterns".
|
|
18
|
+
|
|
19
|
+
## Core Rules
|
|
20
|
+
Detailed instructions are organized into Backend and Frontend categories:
|
|
21
|
+
|
|
22
|
+
### Backend
|
|
23
|
+
- `backend/authentication.md`: Sessions, Auth, and Realtime Hubs.
|
|
24
|
+
- `backend/config.md`: Configuration management and environment variables.
|
|
25
|
+
- `backend/controllers.md`: Request handling and Class-Based Controllers.
|
|
26
|
+
- `backend/cron.md`: Scheduled background tasks and automation.
|
|
27
|
+
- `backend/database.md`: High-performance query builder usage.
|
|
28
|
+
- `backend/forms.md`: Form processing and validation logic.
|
|
29
|
+
- `backend/ipc.md`: Inter-Process Communication and state sharing.
|
|
30
|
+
- `backend/mail.md`: Transactional email sending.
|
|
31
|
+
- `backend/request_response.md`: Handling Odac.Request and Odac.Response.
|
|
32
|
+
- `backend/routing.md`: Route definitions, Middlewares, and Error Pages.
|
|
33
|
+
- `backend/storage.md`: Persistent key-value storage (LMDB).
|
|
34
|
+
- `backend/streaming.md`: Server-Sent Events (SSE) and Streaming API.
|
|
35
|
+
- `backend/structure.md`: Project organization and Service Classes.
|
|
36
|
+
- `backend/translations.md`: Multi-language support (i18n).
|
|
37
|
+
- `backend/utilities.md`: Odac.Var (String Manipulation) and Flow Control.
|
|
38
|
+
- `backend/validation.md`: Input sanitization and security checks.
|
|
39
|
+
- `backend/views.md`: Template syntax, skeletons, and server-side JS.
|
|
40
|
+
|
|
41
|
+
### Frontend
|
|
42
|
+
- `frontend/core.md`: Framework lifecycle, page scoping, and storage.
|
|
43
|
+
- `frontend/forms.md`: AJAX form handling and API requests.
|
|
44
|
+
- `frontend/navigation.md`: AJAX Navigation (SPA) behavior.
|
|
45
|
+
- `frontend/realtime.md`: WebSocket Hubs and EventSource usage.
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
**Remember:** You are an ODAC expert. Every line of code you write should reflect the "Enterprise" spirit of the framework.
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: odac-framework-skill-catalog
|
|
3
|
+
description: Comprehensive ODAC backend and frontend AI skill index for architecture, security, and high-performance application development.
|
|
4
|
+
metadata:
|
|
5
|
+
tags: odac, skills, backend, frontend, architecture, security, performance
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## When to use
|
|
9
|
+
|
|
10
|
+
Use this skill whenever you are developing an application with the ODAC Framework. It provides domain-specific knowledge for both server-side logic and client-side interactions.
|
|
11
|
+
|
|
12
|
+
## How to use
|
|
13
|
+
|
|
14
|
+
Read the specific rule files based on whether you are working on the Backend or Frontend:
|
|
15
|
+
|
|
16
|
+
### Backend Rules
|
|
17
|
+
- [backend/authentication.md](backend/authentication.md) - Sessions, Auth, and Realtime Hubs
|
|
18
|
+
- [backend/config.md](backend/config.md) - Configuration management and environment variables
|
|
19
|
+
- [backend/controllers.md](backend/controllers.md) - Request handling and Class-Based Controllers
|
|
20
|
+
- [backend/cron.md](backend/cron.md) - Scheduled background tasks and automation
|
|
21
|
+
- [backend/database.md](backend/database.md) - Query Builder and DB best practices
|
|
22
|
+
- [backend/forms.md](backend/forms.md) - Form processing and Validation logic
|
|
23
|
+
- [backend/ipc.md](backend/ipc.md) - Inter-Process Communication and state sharing
|
|
24
|
+
- [backend/mail.md](backend/mail.md) - Transactional email sending
|
|
25
|
+
- [backend/migrations.md](backend/migrations.md) - Schema-first, auto-run, cluster-safe DB migrations
|
|
26
|
+
- [backend/request_response.md](backend/request_response.md) - Handling Odac.Request and Odac.Response
|
|
27
|
+
- [backend/routing.md](backend/routing.md) - Route definitions, Middlewares, and Error Pages
|
|
28
|
+
- [backend/storage.md](backend/storage.md) - Persistent key-value storage (LMDB)
|
|
29
|
+
- [backend/streaming.md](backend/streaming.md) - Server-Sent Events (SSE) and Streaming API
|
|
30
|
+
- [backend/structure.md](backend/structure.md) - Project organization and Service Classes
|
|
31
|
+
- [backend/translations.md](backend/translations.md) - Multi-language support (i18n)
|
|
32
|
+
- [backend/utilities.md](backend/utilities.md) - Odac.Var (String Manipulation) and Flow Control
|
|
33
|
+
- [backend/validation.md](backend/validation.md) - Input sanitization and security checks
|
|
34
|
+
- [backend/views.md](backend/views.md) - Template syntax, skeletons, and server-side JS
|
|
35
|
+
|
|
36
|
+
### Frontend Rules
|
|
37
|
+
- [frontend/core.md](frontend/core.md) - Framework lifecycle, page scoping, and storage
|
|
38
|
+
- [frontend/forms.md](frontend/forms.md) - AJAX form handling and API requests
|
|
39
|
+
- [frontend/navigation.md](frontend/navigation.md) - AJAX Navigation (SPA) behavior
|
|
40
|
+
- [frontend/realtime.md](frontend/realtime.md) - WebSocket Hubs and EventSource usage
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: backend-authentication-realtime-skill
|
|
3
|
+
description: Secure ODAC authentication patterns for sessions, guards, passwordless flows, and realtime channel protection.
|
|
4
|
+
metadata:
|
|
5
|
+
tags: backend, authentication, session, auth-guard, magic-link, realtime, security
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
# Backend Authentication & Realtime Skill
|
|
9
|
+
|
|
10
|
+
Secure user authentication, session management, and bidirectional communication.
|
|
11
|
+
|
|
12
|
+
## Architectural Approach
|
|
13
|
+
ODAC provides built-in drivers for session handling (Memory/Redis) and multiple authentication flows including standard password-based login and passwordless Magic Links.
|
|
14
|
+
|
|
15
|
+
## Core Rules
|
|
16
|
+
1. **Auth Guard**: Protect routes using `Odac.Route.auth`.
|
|
17
|
+
2. **Session Integrity**: `Odac.Request.setSession()` MUST be called before accessing `Odac.Request.session()`.
|
|
18
|
+
3. **Password Security**: The framework handles BCrypt hashing automatically via `Odac.Auth`.
|
|
19
|
+
4. **Magic Links**: Use `Odac.Auth.magic(email)` for passwordless flows.
|
|
20
|
+
5. **Token Rotation**: Enterprise-grade rotation is enabled by default. It uses a 60s grace period to prevent race conditions in SPAs.
|
|
21
|
+
6. **Persistence**: Cookies use the configured `maxAge` to persist beyond browser closure.
|
|
22
|
+
## Reference Patterns
|
|
23
|
+
|
|
24
|
+
### 1. Standard Login & Check
|
|
25
|
+
```javascript
|
|
26
|
+
// Check if user is logged in
|
|
27
|
+
if (Odac.Auth.check()) {
|
|
28
|
+
const user = Odac.Auth.user();
|
|
29
|
+
const userId = Odac.Auth.id();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Log in a user manually
|
|
33
|
+
await Odac.Auth.login(userId);
|
|
34
|
+
|
|
35
|
+
// Log out
|
|
36
|
+
await Odac.Auth.logout();
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### 2. Magic Links (Passwordless)
|
|
40
|
+
```javascript
|
|
41
|
+
// Generate and send a magic link
|
|
42
|
+
const result = await Odac.Auth.magic('user@example.com', {
|
|
43
|
+
redirect: '/dashboard',
|
|
44
|
+
subject: 'Login to MyApp'
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
if (result.success) {
|
|
48
|
+
return { message: 'Link sent!' };
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### 3. Session Management
|
|
53
|
+
```javascript
|
|
54
|
+
// Get/Set session data
|
|
55
|
+
Odac.session('key', 'value');
|
|
56
|
+
const val = Odac.session('key');
|
|
57
|
+
|
|
58
|
+
// Destroy session
|
|
59
|
+
Odac.session('key', null);
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### 4. Realtime Hubs (WebSockets)
|
|
63
|
+
```javascript
|
|
64
|
+
// Broadcast to everyone in a room
|
|
65
|
+
Odac.Hub.to('lobby').send('chat_message', { text: 'Hello!' });
|
|
66
|
+
|
|
67
|
+
// Targeted user broadcast
|
|
68
|
+
Odac.Hub.user(userId).send('notification', { text: 'New follower' });
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Security Best Practices
|
|
72
|
+
- **CSRF Protection**: Native `Odac.form` and `Odac.post` handle CSRF tokens automatically using `{{ TOKEN }}` in views.
|
|
73
|
+
- **Brute Force**: Always use `validator.brute()` on authentication endpoints.
|
|
74
|
+
- **Passwordless Preference**: Consider using Magic Links for enhanced security and better user experience.
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: backend-configuration-skill
|
|
3
|
+
description: ODAC configuration standards for odac.json usage, environment variable mapping, and secure runtime settings.
|
|
4
|
+
metadata:
|
|
5
|
+
tags: backend, configuration, odac-json, environment, secrets, settings
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
# Backend Configuration Skill
|
|
9
|
+
|
|
10
|
+
Managing application settings using `odac.json` and environment variables.
|
|
11
|
+
|
|
12
|
+
## Architectural Approach
|
|
13
|
+
ODAC uses `odac.json` for structure and `.env` for secrets. Values can be accessed via `Odac.Config` or the `Odac.env()` helper.
|
|
14
|
+
|
|
15
|
+
## Core Rules
|
|
16
|
+
1. **Direct Access**: Use `Odac.Config.key` for settings in `odac.json`.
|
|
17
|
+
2. **Environment Variables**: Use `${VAR_NAME}` in `odac.json` to map to `.env` values.
|
|
18
|
+
3. **Encapsulation**: Never hardcode credentials. Always use `.env`.
|
|
19
|
+
|
|
20
|
+
## Reference Patterns
|
|
21
|
+
### 1. Accessing Config
|
|
22
|
+
```javascript
|
|
23
|
+
// From odac.json: { "app": { "name": "My App" } }
|
|
24
|
+
const appName = Odac.Config.app.name;
|
|
25
|
+
|
|
26
|
+
// From .env: API_KEY=secret
|
|
27
|
+
const apiKey = Odac.env('API_KEY');
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### 2. odac.json Structure
|
|
31
|
+
```json
|
|
32
|
+
{
|
|
33
|
+
"mysql": {
|
|
34
|
+
"host": "${DB_HOST}",
|
|
35
|
+
"password": "${DB_PASSWORD}"
|
|
36
|
+
},
|
|
37
|
+
"debug": true
|
|
38
|
+
}
|
|
39
|
+
```
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: backend-controllers-skill
|
|
3
|
+
description: Best practices for ODAC controller architecture, class-based routing, and clean request handling boundaries.
|
|
4
|
+
metadata:
|
|
5
|
+
tags: backend, controllers, class-based, route-mapping, architecture, maintainability
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
# Backend Controllers Skill
|
|
9
|
+
|
|
10
|
+
Controllers are the bridge between routes and views/services. This skill covers how to write clean, professional controllers in ODAC.
|
|
11
|
+
|
|
12
|
+
## Architectural Approach
|
|
13
|
+
ODAC supports both simple function-based controllers and class-based controllers. For enterprise-grade applications, class-based controllers are strongly recommended for better organization.
|
|
14
|
+
|
|
15
|
+
## Core Rules
|
|
16
|
+
1. **Class-Based Controllers**: Use classes to group related logic. Each method handles a specific route.
|
|
17
|
+
2. **Naming Convention**: Controllers are usually PascalCase (e.g., `UserController.js`).
|
|
18
|
+
3. **Route Mapping**: Use the `ControllerName@MethodName` syntax in routes.
|
|
19
|
+
4. **Automatic Injection**: Methods receive the `Odac` instance as the first argument automatically.
|
|
20
|
+
|
|
21
|
+
## Reference Patterns
|
|
22
|
+
|
|
23
|
+
### 1. Class-Based Controller (Recommended)
|
|
24
|
+
```javascript
|
|
25
|
+
// controller/User.js
|
|
26
|
+
class User {
|
|
27
|
+
// GET /users
|
|
28
|
+
async index(Odac) {
|
|
29
|
+
const users = await Odac.Service.get('User').all();
|
|
30
|
+
return Odac.View.make('user.list', { users });
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// GET /users/{id}
|
|
34
|
+
async show(Odac) {
|
|
35
|
+
const id = Odac.Request.input('id');
|
|
36
|
+
const user = await Odac.Service.get('User').find(id);
|
|
37
|
+
return Odac.return(user);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// POST /users
|
|
41
|
+
async store(Odac) {
|
|
42
|
+
const data = Odac.Request.post();
|
|
43
|
+
// Logic to save user...
|
|
44
|
+
return { success: true };
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
module.exports = User;
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### 2. Simple Function-Based Controller
|
|
52
|
+
```javascript
|
|
53
|
+
// controller/Home.js
|
|
54
|
+
module.exports = function(Odac) {
|
|
55
|
+
return "Welcome to ODAC!";
|
|
56
|
+
};
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### 3. Usage in Routes
|
|
60
|
+
```javascript
|
|
61
|
+
// route/web.js
|
|
62
|
+
Odac.Route.get('/users', 'User@index');
|
|
63
|
+
Odac.Route.get('/home', 'Home'); // Auto-calls the exported function
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Best Practices
|
|
67
|
+
- **Keep it Thin**: Controllers should only handle request parsing and response formatting. Core logic belongs in **Services**.
|
|
68
|
+
- **Asynchronous Handling**: Always use `async/await` for database or network operations.
|
|
69
|
+
- **Structured Returns**: Use `Odac.return()` for JSON or `Odac.View.make()` for rendering.
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: backend-cron-jobs-skill
|
|
3
|
+
description: Reliable ODAC cron scheduling patterns for background automation, timing control, and safe execution design.
|
|
4
|
+
metadata:
|
|
5
|
+
tags: backend, cron, scheduler, background-jobs, automation, timing
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
# Backend Cron Jobs Skill
|
|
9
|
+
|
|
10
|
+
ODAC provides a built-in cron system for automating background tasks without external dependencies.
|
|
11
|
+
|
|
12
|
+
## Architectural Approach
|
|
13
|
+
Cron jobs are defined in routes and executed by the internal scheduler. They can use either external controller files (recommended) or inline functions.
|
|
14
|
+
|
|
15
|
+
## Core Rules
|
|
16
|
+
1. **Definition**: Use `Odac.Route.cron('name')` to start a definition.
|
|
17
|
+
2. **Scheduling**: Use fluent methods like `.everyMinute(n)`, `.at('HH:MM')`, `.day(n)`, etc.
|
|
18
|
+
3. **Controllers**: Cron controllers should be in `controller/cron/`. They receive the `Odac` instance.
|
|
19
|
+
4. **Wildcard Warning**: If you specify an hour but no minute, it will run every minute during that hour. Always use `.at()` or specify the minute.
|
|
20
|
+
|
|
21
|
+
## Reference Patterns
|
|
22
|
+
|
|
23
|
+
### 1. File-Based Cron (Recommended)
|
|
24
|
+
```javascript
|
|
25
|
+
// route/cron.js
|
|
26
|
+
Odac.Route.cron('cleanup').at('03:00'); // Runs daily at 03:00
|
|
27
|
+
|
|
28
|
+
// controller/cron/cleanup.js
|
|
29
|
+
module.exports = async (Odac) => {
|
|
30
|
+
console.log('Running nightly cleanup...');
|
|
31
|
+
await Odac.DB.table('logs').where('created_at', '<', 'NOW() - INTERVAL 30 DAY').delete();
|
|
32
|
+
};
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### 2. Inline Function Cron
|
|
36
|
+
```javascript
|
|
37
|
+
Odac.Route.cron(async () => {
|
|
38
|
+
const stats = await Odac.DB.table('orders').count();
|
|
39
|
+
console.log('Current orders:', stats);
|
|
40
|
+
}).everyHour(1);
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### 3. Raw Unix Cron Patterns
|
|
44
|
+
```javascript
|
|
45
|
+
// Run every 15 minutes
|
|
46
|
+
Odac.Route.cron('sync').raw('*/15 * * * *');
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Available Schedulers
|
|
50
|
+
- `.minute(0-59)`, `.hour(0-23)`, `.day(1-31)`, `.month(1-12)`, `.weekDay(0-6)`.
|
|
51
|
+
- `.everyMinute(n)`, `.everyHour(n)`, `.everyDay(n)`.
|
|
52
|
+
- `.at('HH:MM')`: Shorthand for setting specific hour and minute.
|
|
53
|
+
|
|
54
|
+
## Best Practices
|
|
55
|
+
- **Logging**: Always log the start and completion of background tasks to the console or a file.
|
|
56
|
+
- **Timeouts**: Be aware that long-running tasks might overlap if the interval is too short.
|
|
57
|
+
- **Service Injection**: Use Service Classes inside cron jobs to keep the logic clean.
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: backend-database-skill
|
|
3
|
+
description: High-performance ODAC database querying patterns using the built-in query builder with secure and efficient data access.
|
|
4
|
+
metadata:
|
|
5
|
+
tags: backend, database, query-builder, sql, indexing, performance, security
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
# Backend Database Skill
|
|
9
|
+
|
|
10
|
+
High-performance database operations using the ODAC Query Builder.
|
|
11
|
+
|
|
12
|
+
## Principles
|
|
13
|
+
1. **Directness**: Avoid ORM overhead. Use fluent Query Builder.
|
|
14
|
+
2. **Safety**: Always use parameterized queries (built-in).
|
|
15
|
+
3. **Efficiency**: Index foreign keys. No `SELECT *`.
|
|
16
|
+
|
|
17
|
+
## Patterns
|
|
18
|
+
```javascript
|
|
19
|
+
const user = await Odac.DB.table('users')
|
|
20
|
+
.select('id', 'name', 'email')
|
|
21
|
+
.where('status', 'active')
|
|
22
|
+
.first();
|
|
23
|
+
|
|
24
|
+
await Odac.DB.table('posts').insert({
|
|
25
|
+
title: 'Hello',
|
|
26
|
+
user_id: 1
|
|
27
|
+
});
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Migration Awareness
|
|
31
|
+
1. **Schema-First**: Structural DB changes must be defined in `schema/*.js`.
|
|
32
|
+
2. **Auto-Migrate**: Migrations run automatically at startup from `Database.init()`.
|
|
33
|
+
3. **Cluster-Safe**: Migration execution is limited to primary process (`cluster.isPrimary`).
|
|
34
|
+
4. **Indexes**: Keep index definitions in schema so add/drop is managed automatically.
|
|
35
|
+
5. **Data Changes**: Use `migration/*.js` only for one-time data transformation.
|
|
36
|
+
|
|
37
|
+
See: [migrations.md](./migrations.md)
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: backend-forms-validation-skill
|
|
3
|
+
description: Secure ODAC form processing workflow with validation, CSRF protection, and safe request-to-database handling.
|
|
4
|
+
metadata:
|
|
5
|
+
tags: backend, forms, validation, csrf, input-security, request-processing
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
# Backend Forms & Validation Skill
|
|
9
|
+
|
|
10
|
+
Processing form data securely and validating inputs.
|
|
11
|
+
|
|
12
|
+
## Rules
|
|
13
|
+
1. **Validator**: Always use `Odac.Validator` for input.
|
|
14
|
+
2. **Auto-save**: Use `Odac.DB.table().save(Odac.Request.post())` for quick inserts.
|
|
15
|
+
3. **CSRF**: Ensure `{{ TOKEN }}` is in your HTML forms.
|
|
16
|
+
|
|
17
|
+
## Patterns
|
|
18
|
+
```javascript
|
|
19
|
+
// Validation
|
|
20
|
+
const check = Odac.Validator.run(Odac.Request.post(), {
|
|
21
|
+
email: 'required|email',
|
|
22
|
+
password: 'required|min:8'
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
if (check.failed()) return Odac.Request.error(check.errors());
|
|
26
|
+
```
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: backend-ipc-skill
|
|
3
|
+
description: ODAC inter-process communication guidance for memory and Redis drivers, shared state, and distributed coordination.
|
|
4
|
+
metadata:
|
|
5
|
+
tags: backend, ipc, redis, cluster, distributed-state, synchronization
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
# Backend IPC (Inter-Process Communication) Skill
|
|
9
|
+
|
|
10
|
+
ODAC provides a built-in IPC system to share data and sync states across application workers or multiple servers.
|
|
11
|
+
|
|
12
|
+
## Architectural Approach
|
|
13
|
+
IPC abstracts the underlying driver, allowing seamless transition between `memory` (Node.js cluster) and `redis` (multi-server scaling).
|
|
14
|
+
|
|
15
|
+
## Core Rules
|
|
16
|
+
1. **Drivers**:
|
|
17
|
+
- `memory`: Shared across local workers via Main process (Default).
|
|
18
|
+
- `redis`: Shared across multiple servers/clusters.
|
|
19
|
+
2. **State Sharing**: Use `Odac.Ipc` for global key-value storage.
|
|
20
|
+
3. **Messaging**: Use Pub/Sub for worker-to-worker communication.
|
|
21
|
+
|
|
22
|
+
## Reference Patterns
|
|
23
|
+
|
|
24
|
+
### 1. Key-Value Storage
|
|
25
|
+
```javascript
|
|
26
|
+
// Set value with optional TTL (seconds)
|
|
27
|
+
await Odac.Ipc.set('maintenance_mode', true);
|
|
28
|
+
await Odac.Ipc.set('cache_key', { data: 123 }, 60);
|
|
29
|
+
|
|
30
|
+
// Get value
|
|
31
|
+
const status = await Odac.Ipc.get('maintenance_mode');
|
|
32
|
+
|
|
33
|
+
// Delete value
|
|
34
|
+
await Odac.Ipc.del('maintenance_mode');
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### 2. Pub/Sub Messaging
|
|
38
|
+
```javascript
|
|
39
|
+
// Subscribe to a channel (usually in a service or app start)
|
|
40
|
+
await Odac.Ipc.subscribe('notifications', (msg) => {
|
|
41
|
+
console.log('Received:', msg);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// Publish a message from any worker
|
|
45
|
+
await Odac.Ipc.publish('notifications', { type: 'alert', text: 'System update' });
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Configuration
|
|
49
|
+
In `odac.json` or a config provider:
|
|
50
|
+
```json
|
|
51
|
+
{
|
|
52
|
+
"ipc": {
|
|
53
|
+
"driver": "redis",
|
|
54
|
+
"redis": "default"
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Best Practices
|
|
60
|
+
- **Async First**: All IPC operations are asynchronous.
|
|
61
|
+
- **TTL Usage**: Always set a TTL for temporary cache data to prevent memory bloat.
|
|
62
|
+
- **Scaling**: Switch to `redis` driver when deploying across multiple containers or servers.
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: backend-mail-skill
|
|
3
|
+
description: Transactional email integration patterns in ODAC using fluent mail APIs, templating, and transport configuration.
|
|
4
|
+
metadata:
|
|
5
|
+
tags: backend, mail, smtp, transactional-email, templating, notifications
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
# Backend Mail Skill
|
|
9
|
+
|
|
10
|
+
Sending transactional emails using the fluent `Odac.Mail` service.
|
|
11
|
+
|
|
12
|
+
## Core Rules
|
|
13
|
+
1. **Transport**: Configured via `odac.json` (SMTP, etc.).
|
|
14
|
+
2. **Templating**: Combine with `Odac.Var().replace()` for dynamic content.
|
|
15
|
+
3. **Methods**: Use `Odac.Mail.send({ ... })`.
|
|
16
|
+
|
|
17
|
+
## Reference Patterns
|
|
18
|
+
### 1. Simple Email
|
|
19
|
+
```javascript
|
|
20
|
+
await Odac.Mail.send({
|
|
21
|
+
to: 'user@example.com',
|
|
22
|
+
subject: 'Welcome!',
|
|
23
|
+
body: '<h1>Hello!</h1>',
|
|
24
|
+
type: 'html' // default is text
|
|
25
|
+
});
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### 2. Template-based Email
|
|
29
|
+
```javascript
|
|
30
|
+
const template = 'Hello {{ name }}, welcome to {{ site }}!';
|
|
31
|
+
const body = Odac.Var(template).replace({
|
|
32
|
+
'{{ name }}': user.name,
|
|
33
|
+
'{{ site }}': 'ODAC'
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
await Odac.Mail.send({
|
|
37
|
+
to: user.email,
|
|
38
|
+
subject: 'Registration Success',
|
|
39
|
+
body: body
|
|
40
|
+
});
|
|
41
|
+
```
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: backend-migrations-skill
|
|
3
|
+
description: Schema-first ODAC migration strategy for deterministic database evolution, index sync, and cluster-safe execution.
|
|
4
|
+
metadata:
|
|
5
|
+
tags: backend, migrations, schema, database-evolution, indexes, cluster-safety
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
# Backend Migrations Skill
|
|
9
|
+
|
|
10
|
+
Schema-first, zero-config migration strategy for ODAC.
|
|
11
|
+
|
|
12
|
+
## Architectural Approach
|
|
13
|
+
ODAC migrations are **declarative**. The `schema/` directory is the single source of truth for final DB state. The migration engine diffs desired schema vs current DB and applies create/alter/drop operations automatically.
|
|
14
|
+
|
|
15
|
+
## Core Rules
|
|
16
|
+
1. **Source of Truth**: Always update `schema/*.js` files, not historical migration chains, for structural changes.
|
|
17
|
+
2. **Auto Execution**: Migrations run automatically during app startup via `Database.init()`.
|
|
18
|
+
3. **Cluster Safety**: Auto-migration runs only on `cluster.isPrimary` to prevent race conditions.
|
|
19
|
+
4. **Index Sync**: Define indexes in schema; engine adds/removes them automatically.
|
|
20
|
+
5. **Drop Behavior**: If a column/index is removed from schema, it is removed from DB on next startup.
|
|
21
|
+
6. **Seeds**: Use `seed` + `seedKey` for idempotent reference data.
|
|
22
|
+
7. **Data Transformations**: Use imperative files under `migration/` only for one-time data migration logic.
|
|
23
|
+
|
|
24
|
+
## Reference Patterns
|
|
25
|
+
### 1. Schema File (Final State)
|
|
26
|
+
```javascript
|
|
27
|
+
// schema/users.js
|
|
28
|
+
'use strict'
|
|
29
|
+
|
|
30
|
+
module.exports = {
|
|
31
|
+
columns: {
|
|
32
|
+
id: {type: 'increments'},
|
|
33
|
+
email: {type: 'string', length: 255, nullable: false},
|
|
34
|
+
role: {type: 'enum', values: ['admin', 'user'], default: 'user'},
|
|
35
|
+
timestamps: {type: 'timestamps'}
|
|
36
|
+
},
|
|
37
|
+
indexes: [
|
|
38
|
+
{columns: ['email'], unique: true}
|
|
39
|
+
],
|
|
40
|
+
seed: [
|
|
41
|
+
{email: 'admin@example.com', role: 'admin'}
|
|
42
|
+
],
|
|
43
|
+
seedKey: 'email'
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### 2. Multi-Database Layout
|
|
48
|
+
```
|
|
49
|
+
schema/
|
|
50
|
+
users.js # default DB
|
|
51
|
+
analytics/
|
|
52
|
+
events.js # analytics DB
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### 3. Imperative Data Migration (One-Time)
|
|
56
|
+
```javascript
|
|
57
|
+
// migration/20260225_001_backfill_roles.js
|
|
58
|
+
module.exports = {
|
|
59
|
+
async up(db) {
|
|
60
|
+
await db('users').whereNull('role').update({role: 'user'})
|
|
61
|
+
},
|
|
62
|
+
async down(db) {
|
|
63
|
+
await db('users').where('role', 'user').update({role: null})
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### 4. CLI Operations
|
|
69
|
+
```bash
|
|
70
|
+
npx odac migrate
|
|
71
|
+
npx odac migrate:status
|
|
72
|
+
npx odac migrate:rollback
|
|
73
|
+
npx odac migrate:snapshot
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Performance and Safety Notes
|
|
77
|
+
- Keep schema declarations deterministic and minimal.
|
|
78
|
+
- Prefer additive changes; drops are destructive and should be intentional.
|
|
79
|
+
- Ensure high-cardinality lookup columns are indexed in schema definitions.
|
|
80
|
+
- For very large tables, plan expensive column rewrites as dedicated data migrations.
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: backend-request-response-skill
|
|
3
|
+
description: ODAC request parsing and response composition patterns for consistent, secure, and predictable API behavior.
|
|
4
|
+
metadata:
|
|
5
|
+
tags: backend, request, response, headers, status-codes, json, api
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
# Backend Request & Response Skill
|
|
9
|
+
|
|
10
|
+
Handling incoming data and sending structured responses.
|
|
11
|
+
|
|
12
|
+
## Core Rules
|
|
13
|
+
1. **Request Data**:
|
|
14
|
+
- `Odac.request('key')`: Auto-detect GET/POST (Recommended).
|
|
15
|
+
- `Odac.Request.post('key')`: Targeted POST data.
|
|
16
|
+
- `Odac.Request.get('key')`: Targeted URL parameters.
|
|
17
|
+
2. **Metadata**: Access `Odac.Request.method`, `Odac.Request.ip`, `Odac.Request.header('name')`.
|
|
18
|
+
3. **Returning Data**:
|
|
19
|
+
- `return { ... }`: Returns JSON.
|
|
20
|
+
- `return Odac.return({ ... })`: Explicit JSON return.
|
|
21
|
+
- `Odac.Response.header('Key', 'Value')`: Set custom headers.
|
|
22
|
+
|
|
23
|
+
## Reference Patterns
|
|
24
|
+
### 1. Unified Request Handling
|
|
25
|
+
```javascript
|
|
26
|
+
module.exports = async function(Odac) {
|
|
27
|
+
const name = await Odac.request('name');
|
|
28
|
+
const method = Odac.Request.method;
|
|
29
|
+
|
|
30
|
+
if (method === 'POST') {
|
|
31
|
+
return { success: true, message: `Saved ${name}` };
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### 2. Header and Status Management
|
|
37
|
+
```javascript
|
|
38
|
+
module.exports = function(Odac) {
|
|
39
|
+
Odac.Response.header('Content-Type', 'text/plain');
|
|
40
|
+
return "Raw text response";
|
|
41
|
+
};
|
|
42
|
+
```
|