Skip to content

Commit 724171b

Browse files
committed
Refactor docs and add action/hook tests to core package
Removed several guide documents (logic-hooks-examples, sdk-reference, security-guide, visual-reporting) and updated logic-actions.md and logic-hooks.md for improved clarity and API alignment. Added jest config and new tests for actions and hooks in packages/core, including a MockDriver utility. Enhanced ObjectRepository with an execute method for custom actions. Updated metadata-format spec to document action definitions.
1 parent 62e0945 commit 724171b

12 files changed

Lines changed: 258 additions & 1237 deletions

docs/guide/logic-actions.md

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -72,32 +72,28 @@ Similar to Triggers, expected convention is `*.actions.ts`.
7272
### 3.1 Handler Signature
7373

7474
```typescript
75-
// objects/contracts/contracts.actions.ts
76-
import { defineAction } from '@objectql/core';
77-
78-
export default defineAction({
79-
object: 'contracts',
80-
name: 'terminate', // Must match metadata key
81-
82-
async handler({ ctx, id, params }) {
75+
// Registering action handler
76+
db.registerAction('contracts', 'terminate', async (ctx: ActionContext) => {
77+
const { id, params } = ctx;
78+
8379
// 1. Validate Business Logic
84-
const contract = await ctx.object('contracts').findOne(id);
85-
if (contract.status === 'terminated') {
86-
throw new Error("Contract is already terminated.");
87-
}
80+
// Access repository via db or generic find
81+
// ...
82+
83+
return { success: true };
84+
});
85+
```
8886

89-
// 2. Perform Updates (Atomic Transaction inherited from ctx)
90-
await ctx.object('contracts').update(id, {
91-
status: 'terminated',
92-
termination_reason: params.reason,
93-
termination_date: params.effective_date || new Date()
94-
});
95-
96-
// 3. Create Related Records
97-
if (params.penalty_amount > 0) {
98-
await ctx.object('invoices').create({
99-
contract_id: id,
100-
amount: params.penalty_amount,
87+
### 3.2 Invocation
88+
89+
You can execute actions using the Repository instance:
90+
91+
```typescript
92+
const result = await db.object('contracts').execute('terminate', 'contract_123', {
93+
reason: 'Client request',
94+
effective_date: '2023-12-31'
95+
});
96+
```
10197
type: 'penalty'
10298
});
10399
}

docs/guide/logic-hooks-examples.md

Lines changed: 0 additions & 68 deletions
This file was deleted.

docs/guide/logic-hooks.md

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,25 @@ The system provides a rich interception model (AOP) to inject business logic int
1111
Hooks receive a structured object containing the **Session Context** and the **Operation Payload**.
1212

1313
```typescript
14-
import { UnifiedQuery, ObjectQLContext } from './CORE_TYPES';
15-
16-
export interface HookContext<T = any> {
17-
// === 1. The Session Context ===
18-
// Automatically propagates userId, spaceId, and Transaction.
19-
ctx: ObjectQLContext;
20-
21-
// === 2. Operational Info ===
22-
entity: string;
23-
op: 'find' | 'create' | 'update' | 'delete' | 'count' | 'aggregate';
24-
25-
// === 3. Data Payload (Mutable) ===
26-
// - In beforeCreate/Update: The data to be written.
27-
// - In afterCreate/Update: The result record returned from DB.
28-
doc?: T;
29-
30-
// === 4. Query Context (Mutable, for 'find' only) ===
31-
// Complies strictly with the UnifiedQuery JSON-DSL (AST).
32-
// Developers can modify 'fields', 'sort', or wrap 'filters'.
33-
query?: UnifiedQuery;
34-
35-
// === 5. Helpers ===
36-
getPreviousDoc: () => Promise<T>;
37-
38-
// AST Manipulation Utilities
39-
utils: {
40-
/**
41-
* Safely injects a new filter criterion into the existing AST.
42-
* It wraps existing filters in a new group to preserve operator precedence.
43-
* * Logic: (Existing_Filters) AND (New_Filter)
44-
*/
45-
restrict: (criterion: [string, string, any]) => void;
46-
};
14+
import { UnifiedQuery, ObjectQLContext, HookContext } from '@objectql/types';
15+
16+
export interface HookContext extends ObjectQLContext {
17+
// === 1. Target Info ===
18+
objectName: string;
19+
20+
// === 2. Operational Data (Mutable) ===
21+
query?: UnifiedQuery; // For find/count
22+
doc?: any; // For create/update
23+
id?: string | number; // For update/delete/findOne
24+
25+
// === 3. Result (for after* hooks) ===
26+
result?: any;
27+
28+
// === 4. Shared Meta ===
29+
meta?: any; // To pass data between hooks or from main context
4730
}
4831

49-
export type HookFunction = (context: HookContext) => Promise<void>;
32+
export type HookHandler = (ctx: HookContext) => Promise<void> | void;
5033
```
5134

5235
## 3. Registering Hooks

0 commit comments

Comments
 (0)