Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepend changes #72

Merged
merged 5 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 57 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ The **[CDS-TS-Repository - BaseRepository](https://github.com/dxfrontier/cds-ts-
It simplifies the implementation by offering a set of ready-to-use actions for interacting with the database. These actions include:

- `.create()`: Create new records in the database.
- `.findAll()`: Retrieve all records from the database.
- `.getAll()`: Retrieve all records from the database.
- `.find()`: Query the database to find specific data.
- `.delete()`: Remove records from the database.
- `.exists()`: Check the existence of data in the database.
Expand Down Expand Up @@ -1467,10 +1467,10 @@ See also the official SAP JS **[CDS-Before](https://cap.cloud.sap/docs/node.js/c
> [!TIP]
> If `@odata.draft.enabled: true` to manage event handlers for draft version you can use
>
> - @BeforeCreateDraft()
> - @BeforeReadDraft()
> - @BeforeUpdateDraft()
> - @BeforeDeleteDraft()
> - `@BeforeCreateDraft()`
> - `@BeforeReadDraft()`
> - `@BeforeUpdateDraft()`
> - `@BeforeDeleteDraft()`

<p align="right">(<a href="#table-of-contents">back to top</a>)</p>

Expand Down Expand Up @@ -1612,7 +1612,11 @@ this.before('DELETE', MyEntity, async (req) => {

###### @BeforeAll

The `@BeforeAll` decorator will be triggered when **_any_** of the following CRUD event is triggered:
The `@BeforeAll` decorator is triggered whenever **_any CRUD (Create, Read, Update, Delete)_** event occurs, whether the entity is `active` or in `draft` mode.

`ACTIVE ENTITY`

For active entities, the @BeforeAll decorator will be triggered when at least *_one_* of the following events occurs:

- `CREATE` [@BeforeCreate()](#beforecreate), [@AfterCreate()](#aftercreate), [@OnCreate()](#oncreate)
- `READ` [@BeforeRead()](#beforeread), [@AfterRead()](#afterread), [@OnRead()](#onread)
Expand All @@ -1621,6 +1625,16 @@ The `@BeforeAll` decorator will be triggered when **_any_** of the following CRU
- `BOUND ACTIONS` [@OnBoundAction()](#onboundaction)
- `BOUND FUNCTIONS` [@OnBoundFunction()](#onboundfunction)

`DRAFT`

For draft entities, the @BeforeAll decorator will be triggered when at least *_one_* of the following events occurs:

- `CREATE` [@BeforeNewDraft()](#beforenewdraft), [@AfterNewDraft()](#afternewdraft), [@OnNewDraft()](#onnewdraft)
- `CANCEL` [@BeforeCancelDraft()](#beforecanceldraft), [@AfterCancelDraft()](#aftercanceldraft), [@OnCancelDraft()](#oncanceldraft)
- `EDIT` [@BeforeEditDraft()](#beforeeditdraft), [@AfterEditDraft()](#aftereditdraft), [@OnEditDraft()](#oneditdraft)
- `SAVE` [@BeforeSaveDraft()](#beforesavedraft), [@AfterSaveDraft()](#aftersavedraft), [@OnSaveDraft()](#onsavedraft)
- :heavy_plus_sign: All active entity [Before](#before), [After](#after), [On](#on) events which have a `Draft` variant.

**@BeforeAll**()

`Example`
Expand Down Expand Up @@ -1649,7 +1663,7 @@ this.before('*', MyEntity, async (req) => {
> Decorator `@BeforeAll()` will be triggered based on the [EntityHandler](#entityhandler) `argument` => `MyEntity`.

> [!TIP]
> If `@odata.draft.enabled: true` and you need to read the draft then you should use `@BeforeAllDraft()` decorator.
> If the entity has drafts enabled `@odata.draft.enabled: true`, the `@BeforeAll` decorator will still be triggered for draft events.

> [!NOTE]
> MyEntity was generated using [CDS-Typer](#generate-cds-typed-entities) and imported in the the class.
Expand Down Expand Up @@ -1855,7 +1869,11 @@ this.after('DELETE', MyEntity, async (deleted, req) => {

###### @AfterAll

The `@AfterAll` decorator will be triggered when **_any_** of the following CRUD event is triggered:
The `@AfterAll` decorator is triggered whenever **_any CRUD (Create, Read, Update, Delete)_** event occurs, whether the entity is `active` or in `draft` mode.

`ACTIVE ENTITY`

For active entities, the @BeforeAll decorator will be triggered when at least *_one_* of the following events occurs:

- `CREATE` [@BeforeCreate()](#beforecreate), [@AfterCreate()](#aftercreate), [@OnCreate()](#oncreate)
- `READ` [@BeforeRead()](#beforeread), [@AfterRead()](#afterread), [@OnRead()](#onread)
Expand All @@ -1864,6 +1882,16 @@ The `@AfterAll` decorator will be triggered when **_any_** of the following CRUD
- `BOUND ACTIONS` [@OnBoundAction()](#onboundaction)
- `BOUND FUNCTIONS` [@OnBoundFunction()](#onboundfunction)

`DRAFT`

For draft entities, the @BeforeAll decorator will be triggered when at least *_one_* of the following events occurs:

- `CREATE` [@BeforeNewDraft()](#beforenewdraft), [@AfterNewDraft()](#afternewdraft), [@OnNewDraft()](#onnewdraft)
- `CANCEL` [@BeforeCancelDraft()](#beforecanceldraft), [@AfterCancelDraft()](#aftercanceldraft), [@OnCancelDraft()](#oncanceldraft)
- `EDIT` [@BeforeEditDraft()](#beforeeditdraft), [@AfterEditDraft()](#aftereditdraft), [@OnEditDraft()](#oneditdraft)
- `SAVE` [@BeforeSaveDraft()](#beforesavedraft), [@AfterSaveDraft()](#aftersavedraft), [@OnSaveDraft()](#onsavedraft)
- :heavy_plus_sign: All active entity [Before](#before), [After](#after), [On](#on) events which have a `Draft` variant.

**@AfterAll**()

`Example`
Expand Down Expand Up @@ -1902,7 +1930,7 @@ this.after('*', MyEntity, async (result, req) => {
> Decorator `@AfterAll()` will be triggered based on the [EntityHandler](#entityhandler) `argument` => `MyEntity`.

> [!TIP]
> If `@odata.draft.enabled: true` and you need to read the draft then you should use `@AfterAll()` decorator.
> If the entity has drafts enabled `@odata.draft.enabled: true`, the `@AfterAll` decorator will still be triggered for draft events.

> [!NOTE]
> MyEntity was generated using [CDS-Typer](#generate-cds-typed-entities) and imported in the the class.
Expand Down Expand Up @@ -2317,14 +2345,28 @@ this.on(MyEntity.actions.AFunction, MyEntity, async (req) => {

**@OnAll**()

The `@OnAll` decorator will be triggered when one of the following CRUD event is called:
The `@OnAll` decorator is triggered whenever **_any CRUD (Create, Read, Update, Delete)_** event occurs, whether the entity is `active` or in `draft` mode.

`ACTIVE ENTITY`

For active entities, the @BeforeAll decorator will be triggered when at least *_one_* of the following events occurs:

- `CREATE` [@BeforeCreate()](#beforecreate), [@AfterCreate()](#aftercreate), [@OnCreate()](#oncreate)
- `READ` [@BeforeRead()](#beforeread), [@AfterRead()](#afterread), [@OnRead()](#onread)
- `UPDATE` [@BeforeUpdate()](#beforeupdate), [@AfterUpdate()](#afterupdate), [@OnUpdate()](#onupdate)
- `DELETE` [@BeforeDelete()](#beforedelete), [@AfterDelete()](#afterdelete), [@OnDelete()](#ondelete)
- `BOUND ACTIONS` [@OnBoundAction()](#onboundaction)
- `BOUND FUNCTIONS` [@OnBoundFunction()](#onboundfunction)

`DRAFT`

For draft entities, the @BeforeAll decorator will be triggered when at least *_one_* of the following events occurs:

- `CREATE` [@BeforeNewDraft()](#beforenewdraft), [@AfterNewDraft()](#afternewdraft), [@OnNewDraft()](#onnewdraft)
- `CANCEL` [@BeforeCancelDraft()](#beforecanceldraft), [@AfterCancelDraft()](#aftercanceldraft), [@OnCancelDraft()](#oncanceldraft)
- `EDIT` [@BeforeEditDraft()](#beforeeditdraft), [@AfterEditDraft()](#aftereditdraft), [@OnEditDraft()](#oneditdraft)
- `SAVE` [@BeforeSaveDraft()](#beforesavedraft), [@AfterSaveDraft()](#aftersavedraft), [@OnSaveDraft()](#onsavedraft)
- :heavy_plus_sign: All active entity [Before](#before), [After](#after), [On](#on) events which have a `Draft` variant.

> [!NOTE]
> Exception will be the following decorators [@OnEvent()](#onevent), [@OnError()](#onerror) and `UNBOUND ACTIONS` [@OnAction()](#onaction), `UNBOUND FUNCTIONS` [@OnFunction()](#onfunction) as these are bound to the service itself and not to an entity.
Expand Down Expand Up @@ -2352,12 +2394,12 @@ this.on('*', MyEntity, async (req, next) => {
});
```

> [!TIP]
> If `@odata.draft.enabled: true` and you need to read the draft then you should use `@OnAllDraft()` decorator.

> [!IMPORTANT]
> Decorator `@OnAll()` will be triggered based on the [EntityHandler](#entityhandler) `argument` => `MyEntity`.

> [!TIP]
> If the entity has drafts enabled `@odata.draft.enabled: true`, the `@OnAll` decorator will still be triggered for draft events.

> [!NOTE]
> MyEntity was generated using [CDS-Typer](#generate-cds-typed-entities) and imported in the the class.

Expand Down Expand Up @@ -2930,7 +2972,7 @@ import type { Request } from '@dxfrontier/cds-ts-dispatcher';
import { MyEntity } from 'YOUR_CDS_TYPER_ENTITIES_LOCATION';

@Prepend({ eventDecorator: 'AfterRead' })
public async prepend(@Req() req: Request): Promise<void> {
private async prepend(@Req() req: Request): Promise<void> {
req.locale = 'DE_de';
}

Expand All @@ -2954,7 +2996,7 @@ import type { TypedRequest } from '@dxfrontier/cds-ts-dispatcher';
import { MyEvent } from 'YOUR_CDS_TYPER_ENTITIES_LOCATION';

@Prepend({ eventDecorator: 'OnEvent', eventName: MyEvent })
public async prepend(@Req() req: TypedRequest<MyEvent>) {
private async prepend(@Req() req: TypedRequest<MyEvent>) {
req.locale = 'DE_de';
}

Expand Down
19 changes: 16 additions & 3 deletions lib/types/internalTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,13 @@ export type PrependDecorators = {
| 'AfterUpdate'
| 'AfterDelete'
| 'AfterAll'
//
| 'BeforeCreate'
| 'BeforeRead'
| 'BeforeUpdate'
| 'BeforeDelete'
| 'BeforeAll'
//
| 'OnCreate'
| 'OnRead'
| 'OnUpdate'
Expand All @@ -132,17 +134,28 @@ export type PrependDraftDecorators = {
| 'AfterReadDraftEachInstance'
| 'AfterUpdateDraft'
| 'AfterDeleteDraft'
| 'AfterAllDraft'
| 'AfterNewDraft'
| 'AfterCancelDraft'
| 'AfterEditDraft'
| 'AfterSaveDraft'
//
| 'BeforeCreateDraft'
| 'BeforeReadDraft'
| 'BeforeUpdateDraft'
| 'BeforeDeleteDraft'
| 'BeforeAllDraft'
| 'BeforeNewDraft'
| 'BeforeCancelDraft'
| 'BeforeEditDraft'
| 'BeforeSaveDraft'
//
| 'OnCreateDraft'
| 'OnReadDraft'
| 'OnUpdateDraft'
| 'OnDeleteDraft'
| 'OnAllDraft';
| 'OnNewDraft'
| 'OnCancelDraft'
| 'OnEditDraft'
| 'OnSaveDraft';
};

export type PrependDraftAction = {
Expand Down
15 changes: 12 additions & 3 deletions lib/util/decorators/decoratorsUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,28 @@ const decoratorsUtil = {
AfterReadDraftEachInstance: { event: 'each', eventKind: 'AFTER' },
AfterUpdateDraft: { event: 'UPDATE', eventKind: 'AFTER' },
AfterDeleteDraft: { event: 'DELETE', eventKind: 'AFTER' },
AfterAllDraft: { event: '*', eventKind: 'AFTER' },
AfterNewDraft: { event: 'NEW', eventKind: 'AFTER' },
AfterCancelDraft: { event: 'CANCEL', eventKind: 'AFTER' },
AfterEditDraft: { event: 'EDIT', eventKind: 'AFTER' },
AfterSaveDraft: { event: 'SAVE', eventKind: 'AFTER' },
//
BeforeCreateDraft: { event: 'CREATE', eventKind: 'BEFORE' },
BeforeReadDraft: { event: 'READ', eventKind: 'BEFORE' },
BeforeUpdateDraft: { event: 'UPDATE', eventKind: 'BEFORE' },
BeforeDeleteDraft: { event: 'DELETE', eventKind: 'BEFORE' },
BeforeAllDraft: { event: '*', eventKind: 'BEFORE' },
BeforeNewDraft: { event: 'NEW', eventKind: 'BEFORE' },
BeforeCancelDraft: { event: 'CANCEL', eventKind: 'BEFORE' },
BeforeEditDraft: { event: 'EDIT', eventKind: 'BEFORE' },
BeforeSaveDraft: { event: 'SAVE', eventKind: 'BEFORE' },
//
OnCreateDraft: { event: 'CREATE', eventKind: 'ON' },
OnReadDraft: { event: 'READ', eventKind: 'ON' },
OnUpdateDraft: { event: 'UPDATE', eventKind: 'ON' },
OnDeleteDraft: { event: 'DELETE', eventKind: 'ON' },
OnAllDraft: { event: '*', eventKind: 'ON' },
OnNewDraft: { event: 'NEW', eventKind: 'ON' },
OnCancelDraft: { event: 'CANCEL', eventKind: 'ON' },
OnEditDraft: { event: 'EDIT', eventKind: 'ON' },
OnSaveDraft: { event: 'SAVE', eventKind: 'ON' },
};

if (options.eventDecorator === 'OnBoundActionDraft' || options.eventDecorator === 'OnBoundFunctionDraft') {
Expand Down
Loading