-
-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from FoalTS/optional-service-declaration
Make service declaration optional
- Loading branch information
Showing
14 changed files
with
205 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,81 @@ | ||
# Modules | ||
|
||
Every app starts with a module. A module instantiates services and binds controllers to the request handler. It may import other ones. They're espacially interesting when a project grows up. | ||
Every app starts with a module. A module instantiates services and binds controllers to the request handler. It may also have pre-hooks (or post-hooks) executed before (or after) every controller. | ||
|
||
## Example | ||
|
||
```typescript | ||
import { rest } from '@foal/common'; | ||
import { FoalModule } from '@foal/core'; | ||
// module and service imports ... | ||
// module and service imports... | ||
|
||
const AppModule: FoaModule = { | ||
services: [ MyService1, MyService2, MyCRUDService ], | ||
controllers: [ | ||
rest.attachService('/my_resources', MyCRUDService) | ||
], | ||
imports: [ | ||
{ module: MyModule } | ||
{ module: Team1Module, path: '/team1' }, | ||
{ module: Team2Module, path: '/team2' }, | ||
hooks: [ | ||
myFirstPreHook(), | ||
mySecondPreHook(), | ||
myPostHook() | ||
] | ||
} | ||
``` | ||
|
||
## Dependencies | ||
## Nested modules | ||
|
||
![Schema](./module-dependencies.png) | ||
When your app grows up, you may be interested in splitting your app into several modules. Here's an example on how to embed your modules: | ||
|
||
```typescript | ||
const Module1: FoalModule = { | ||
controllers: [ | ||
rest.attachService('/my_resources', MyCRUDService) | ||
] | ||
}; | ||
|
||
const Module2: FoalModule = { | ||
controllers: [ | ||
rest.attachService('/my_resources2', MyCRUDService2) | ||
] | ||
}; | ||
|
||
const AppModule: FoalModule = { | ||
controllers: [ | ||
rest.attachService('/my_resources3', MyCRUDService3) | ||
], | ||
modules: [ | ||
{ module: Module1 } | ||
{ module: Module2, path: '/foo' }, | ||
] | ||
} | ||
|
||
/** | ||
* The app serves three REST endpoints: | ||
* - /my_resources3 | ||
* - /my_resources | ||
* - /foo/my_resources2 | ||
*/ | ||
``` | ||
|
||
Each service is instanciated per module. If you want to share the same instance of a service accross multiple modules, you must specify the service class in the `services` property of a parent module. | ||
|
||
```typescript | ||
const Module1: FoalModule = { | ||
controllers: [ | ||
rest.attachService('/my_resources', MySharedCRUDService) | ||
] | ||
}; | ||
|
||
const Module2: FoalModule = { | ||
controllers: [ | ||
rest.attachService('/my_resources2', MySharedCRUDService) | ||
] | ||
}; | ||
|
||
const AppModule: FoalModule = { | ||
modules: [ | ||
{ module: Module1 } | ||
{ module: Module2, path: '/foo' }, | ||
], | ||
services: [ MySharedCRUDService ] | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.