Skip to content
This repository was archived by the owner on Feb 6, 2025. It is now read-only.

Commit 25516bd

Browse files
committed
(docs): After the call, I have a much better understanding of this. Implementing Paz's feedback, making these pages much easier to read, and flow better.
1 parent face2c4 commit 25516bd

File tree

2 files changed

+49
-41
lines changed

2 files changed

+49
-41
lines changed

docs/getting-started/add-custom-code.md

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
---
22
id: add-custom-code
33
title: Adding Custom Code to Your Amplication Service
4-
sidebar_label: Adding Custom Code
4+
sidebar_label: Add Custom Code To Your Service
55
slug: /add-custom-code-to-your-service
66
---
77

8-
# Adding Custom Code to Your Amplication Service
8+
# Add Custom Code To Your Service
99

10-
While Amplication generates a robust, production-ready backend for your application, you'll often need to add custom business logic or additional functionality. This guide explains how Amplication manages custom code alongside generated code, and provides best practices for adding your own code to an Amplication-generated service.
10+
Amplication generates a robust, production-ready backend for your app, but you'll often need to add your own custom business logic with custom code.
11+
In this guide you'll learn how to add custom code to your Amplication service with a simple example.
1112

1213
## Prerequisites
1314

@@ -20,12 +21,13 @@ Before you begin, make sure you know to:
2021
5. [Commit changes and build a new version](/commit-and-build-new-versions/)
2122

2223
:::note
23-
For more a more in-depth explanation, read [Understanding Custom Code in Amplication](/custom-code-overview/)
24+
For more a more in-depth explanation of how custom code works, read [Understanding Custom Code in Amplication](/custom-code-overview/).
2425
:::
2526

26-
## Adding Custom Code: A Simple Example
27+
## Adding Custom Code: Retrieve The User's Full Name
2728

2829
Let's walk through a simple example of adding custom code to your service.
30+
In this example, we'll add a method with custom code to get the user's full name.
2931

3032
### Step 1: Create A New Feature Branch
3133

@@ -41,9 +43,9 @@ Next, create a new branch from the main branch to make your custom code changes:
4143
git checkout -b feature/user-full-name
4244
```
4345

44-
### Step 2: Locate the Correct Files
46+
### Step 2: Locate the Correct File
4547

46-
Navigate to your service's `src` folder and find the `user` folder:
48+
Navigate to the code of your generated service's `src` folder and find the `user` folder:
4749

4850
```
4951
src
@@ -58,11 +60,16 @@ src
5860
└── user.service.ts
5961
```
6062

61-
We'll be modifying `user.service.ts` to add our custom functionality.
63+
We'll modify the `user.service.ts` to add our custom functionality.
64+
65+
:::tip
66+
Notice that we're adding our changes to `user.service.ts` instead of `base/user.service.base.ts` file.
67+
To learn more why we recommend doing this, read [Understanding Custom Code in Amplication](/custom-code-overview/).
68+
:::
6269

6370
### Step 3: Add Custom Logic to the Service
6471

65-
Open `src/user/user.service.ts`. This file extends the base service and is where we'll add our custom method.
72+
Open `src/user/user.service.ts`. This file extends the base service and is where we'll add our custom method that retrieves the user's full name.
6673

6774
```typescript
6875
import { Injectable } from "@nestjs/common";
@@ -77,11 +84,11 @@ export class UserService extends UserServiceBase {
7784
}
7885
```
7986

80-
This example adds a simple method to get a user's full name. Note how it uses the `findOne` method from the base service.
87+
Note how it uses the `findOne` method from the base service.
8188

8289
### Step 4: Push Your Changes
8390

84-
After adding your custom code, commit the changes to your git repository:
91+
After adding your custom code, commit the changes to the git feature branch you created in Step 1:
8592

8693
```bash
8794
git add .

docs/how-to/add-custom-code.md

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,20 @@ pagination_next: getting-started/add-custom-code
99
# Understanding Custom Code in Amplication
1010

1111
Amplication allows seamless integration of your custom code with generated code through our [Smart Git Sync](/smart-git-sync) feature.
12-
1312
This lets you add custom business logic while continuing to use Amplication to update your data models, permissions, roles, plugins, and more.
1413

14+
This guide is an in-depth explanation on how custom code works in your Amplication project.
15+
1516
## How Custom Code Integration Works
1617

17-
1. Custom code can be added to _all files_ in your Amplication project.
18-
2. Amplication uses a specific folder structure to manage base and non-base files.
19-
3. The `base` folder contains generated files that will get updates only if there are relevant changes.
20-
4. Non-base files are intended for custom code.
21-
5. If necessary changes to non-base files are required (e.g., removing references to a deleted plugin), Amplication will make these changes automatically.
18+
1. Amplication uses a specific folder structure to manage base and non-base files.
19+
2. The `base` folder contains generated files that will get updates only if there are relevant changes (e.g., you add a new plugin).
20+
3. Non-base files are intended for custom code.
21+
4. Amplication will make necessary changes to both base and non-base files (e.g., updating interfaces, removing references to deleted plugins) while always preserving and respecting your custom code.
2222

23-
Amplication always respects your custom code and syncs it with the generated code in both base and non-base files.
23+
:::tip
24+
While you can add custom code to _all files_, we recommend primarily adding custom code to non-base files (e.g., `user.service.ts` instead of `user.service.base.ts`) for easier maintenance.
25+
:::
2426

2527
## Folder Structure
2628

@@ -36,9 +38,13 @@ Each entity has a dedicated folder under the `src` folder:
3638

3739
Within each entity folder, files are split into two groups:
3840

39-
1. **Base files**: Located in the `base` folder, these are automatically generated by Amplication.
41+
### Base files
42+
43+
**Base files**: Located in the `base` folder, these are automatically generated by Amplication.
44+
45+
### Non-base Files
4046

41-
2. **Non-base files**: These inherit from the base files and are intended for your custom code. They reside directly in the entity folder. Your custom code is always preserved and respected across builds.
47+
**Non-base files**: These inherit from the base files and are intended for your custom code. They reside directly in the entity folder.
4248

4349
```
4450
src
@@ -52,43 +58,38 @@ src
5258
└── ...
5359
```
5460

61+
Your custom code, whether it's in base or non-file files, is always preserved and respected across builds.
62+
5563
## Smart Git Sync
5664

5765
Amplication uses [Smart Git Sync](/smart-git-sync/) to seamlessly integrate changes, preserving your custom code while updating the generated parts. This feature:
5866

59-
1. Preserves and always respects custom code in base and non-base files.
60-
2. Makes necessary updates to non-base ,files (e.g., removing references to deleted plugins) while maintaining your custom code.
67+
Makes necessary updates to both base and non-base files (e.g., updating interfaces, removing references to deleted plugins) while respecting your custom code.
6168

6269
:::note
63-
For a more in-depth explanation, please see the [Smart Git Sync](/smart-git-sync) page.
70+
For a more in-depth explanation, please read the [Smart Git Sync](/smart-git-sync) page.
6471
:::
6572

6673
## Best Practices for Custom Code
6774

6875
When adding custom code to your Amplication service, keep these best practices in mind:
6976

70-
1. Add custom code to non-base files (e.g., `user.service.ts` instead of `user.service.base.ts`).
71-
2. Use types and interfaces generated by Amplication to ensure type safety.
72-
3. Leverage existing services and utilities provided by Amplication.
73-
4. Document your custom code thoroughly.
74-
5. Create a new branch for significant custom code changes.
75-
6. Regularly pull and merge the latest Amplication-generated code from the `amplication` branch into your working branch.
76-
77-
## Handling Conflicts
77+
1. Use types and interfaces generated by Amplication to ensure type safety.
78+
2. Leverage existing services and utilities provided by Amplication.
79+
3. Create a new feature branch for significant custom code changes.
80+
4. Regularly pull and merge the latest Amplication-generated code from the `amplication` branch into your working branch (e.g., `main`).
7881

79-
While Amplication strives to preserve your custom code, conflicts may arise, especially with significant changes to your data model or entity structure. If conflicts occur:
82+
## How To Handle Conflicts
8083

81-
1. Amplication will provide clear indications of the conflicting areas using git diffs in your chosen git provider.
82-
2. You may need to manually resolve these conflicts in the `amplication` branch.
83-
3. After resolving conflicts, merging the updated `amplication` branch into the main branch.
84+
While Amplication strives to preserve your custom code, conflicts may arise, especially with significant changes to your data model or entity structure.
8485

85-
## Considerations
86+
If conflicts arise during this process, you'll need to resolve them manually in the `amplication` branch before merging into your main branch.
8687

87-
- While all code can be customized, we recommend adding custom code in the non-base files for easier maintenance.
88-
- Major changes to your data model or entity structure may require manual updates to your custom code.
88+
1. Amplication will provide clear indications of the conflicting areas using git diffs in your chosen git provider.
89+
2. You'll need to manually resolve these conflicts in the `amplication` branch.
90+
3. After resolving conflicts, merge the updated `amplication` branch into the main branch.
8991

9092
## Next Steps
9193

92-
Now that you understand how custom code works in Amplication, you're ready to start adding your own business logic and customizations. For a step-by-step guide, check out our [How To Add Custom Code To Your Service](/add-custom-code-to-your-service) guide.
93-
94-
Happy coding!
94+
Now that you understand how custom code works in Amplication, you're ready to start adding your own business logic.
95+
For a step-by-step guide, check out our [How To Add Custom Code To Your Service](/add-custom-code-to-your-service) guide.

0 commit comments

Comments
 (0)