You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 6, 2025. It is now read-only.
(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.
Copy file name to clipboardExpand all lines: docs/getting-started/add-custom-code.md
+18-11Lines changed: 18 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,14 @@
1
1
---
2
2
id: add-custom-code
3
3
title: Adding Custom Code to Your Amplication Service
4
-
sidebar_label: Adding Custom Code
4
+
sidebar_label: Add Custom Code To Your Service
5
5
slug: /add-custom-code-to-your-service
6
6
---
7
7
8
-
# Adding Custom Code to Your Amplication Service
8
+
# Add Custom Code To Your Service
9
9
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.
11
12
12
13
## Prerequisites
13
14
@@ -20,12 +21,13 @@ Before you begin, make sure you know to:
20
21
5.[Commit changes and build a new version](/commit-and-build-new-versions/)
21
22
22
23
:::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/).
24
25
:::
25
26
26
-
## Adding Custom Code: A Simple Example
27
+
## Adding Custom Code: Retrieve The User's Full Name
27
28
28
29
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.
29
31
30
32
### Step 1: Create A New Feature Branch
31
33
@@ -41,9 +43,9 @@ Next, create a new branch from the main branch to make your custom code changes:
41
43
git checkout -b feature/user-full-name
42
44
```
43
45
44
-
### Step 2: Locate the Correct Files
46
+
### Step 2: Locate the Correct File
45
47
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:
47
49
48
50
```
49
51
src
@@ -58,11 +60,16 @@ src
58
60
└── user.service.ts
59
61
```
60
62
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
+
:::
62
69
63
70
### Step 3: Add Custom Logic to the Service
64
71
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.
66
73
67
74
```typescript
68
75
import { Injectable } from"@nestjs/common";
@@ -77,11 +84,11 @@ export class UserService extends UserServiceBase {
77
84
}
78
85
```
79
86
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.
81
88
82
89
### Step 4: Push Your Changes
83
90
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:
Amplication allows seamless integration of your custom code with generated code through our [Smart Git Sync](/smart-git-sync) feature.
12
-
13
12
This lets you add custom business logic while continuing to use Amplication to update your data models, permissions, roles, plugins, and more.
14
13
14
+
This guide is an in-depth explanation on how custom code works in your Amplication project.
15
+
15
16
## How Custom Code Integration Works
16
17
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.
22
22
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
+
:::
24
26
25
27
## Folder Structure
26
28
@@ -36,9 +38,13 @@ Each entity has a dedicated folder under the `src` folder:
36
38
37
39
Within each entity folder, files are split into two groups:
38
40
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
40
46
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.
42
48
43
49
```
44
50
src
@@ -52,43 +58,38 @@ src
52
58
└── ...
53
59
```
54
60
61
+
Your custom code, whether it's in base or non-file files, is always preserved and respected across builds.
62
+
55
63
## Smart Git Sync
56
64
57
65
Amplication uses [Smart Git Sync](/smart-git-sync/) to seamlessly integrate changes, preserving your custom code while updating the generated parts. This feature:
58
66
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.
61
68
62
69
:::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.
64
71
:::
65
72
66
73
## Best Practices for Custom Code
67
74
68
75
When adding custom code to your Amplication service, keep these best practices in mind:
69
76
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`).
78
81
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
80
83
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.
84
85
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.
86
87
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.
89
91
90
92
## Next Steps
91
93
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