Skip to content

Commit a7f6124

Browse files
committed
doc(loopback4-dynamic-datasource): README.md update
1 parent da1d8cf commit a7f6124

16 files changed

+457
-110
lines changed

.github/CODE_OF_CONDUCT.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Code of Conduct
2+
3+
## Our Pledge
4+
5+
In the interest of fostering an open and welcoming environment, we as
6+
contributors and maintainers pledge to making participation in our project and
7+
our community a harassment-free experience for everyone, regardless of age, body
8+
size, disability, ethnicity, sex characteristics, gender identity and expression,
9+
level of experience, education, socio-economic status, nationality, personal
10+
appearance, race, religion, or sexual identity and orientation.
11+
12+
## Our Standards
13+
14+
Examples of behavior that contributes to creating a positive environment
15+
include:
16+
17+
- Using welcoming and inclusive language
18+
- Being respectful of differing viewpoints and experiences
19+
- Gracefully accepting constructive criticism
20+
- Focusing on what is best for the community
21+
- Showing empathy towards other community members
22+
23+
Examples of unacceptable behavior by participants include:
24+
25+
- The use of sexualized language or imagery and unwelcome sexual attention or
26+
advances
27+
- Trolling, insulting/derogatory comments, and personal or political attacks
28+
- Public or private harassment
29+
- Publishing others' private information, such as a physical or electronic
30+
address, without explicit permission
31+
- Other conduct which could reasonably be considered inappropriate in a
32+
professional setting
33+
34+
## Our Responsibilities
35+
36+
Project maintainers are responsible for clarifying the standards of acceptable
37+
behavior and are expected to take appropriate and fair corrective action in
38+
response to any instances of unacceptable behavior.
39+
40+
Project maintainers have the right and responsibility to remove, edit, or
41+
reject comments, commits, code, wiki edits, issues, and other contributions
42+
that are not aligned to this Code of Conduct, or to ban temporarily or
43+
permanently any contributor for other behaviors that they deem inappropriate,
44+
threatening, offensive, or harmful.
45+
46+
## Scope
47+
48+
This Code of Conduct applies within all project spaces, and it also applies when
49+
an individual is representing the project or its community in public spaces.
50+
Examples of representing a project or community include using an official
51+
project e-mail address, posting via an official social media account, or acting
52+
as an appointed representative at an online or offline event. Representation of
53+
a project may be further defined and clarified by project maintainers.
54+
55+
## Enforcement
56+
57+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
58+
reported by contacting the project team at support@sourcefuse.com. All
59+
complaints will be reviewed and investigated and will result in a response that
60+
is deemed necessary and appropriate to the circumstances. The project team is
61+
obligated to maintain confidentiality with regard to the reporter of an incident.
62+
Further details of specific enforcement policies may be posted separately.
63+
64+
Project maintainers who do not follow or enforce the Code of Conduct in good
65+
faith may face temporary or permanent repercussions as determined by other
66+
members of the project's leadership.
67+
68+
## Attribution
69+
70+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71+
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
72+
73+
[homepage]: https://www.contributor-covenant.org
74+
75+
For answers to common questions about this code of conduct, see
76+
https://www.contributor-covenant.org/faq

.github/CONTRIBUTING.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# loopback4-notifications
2+
3+
## Contributing
4+
5+
First off, thank you for considering contributing to the project. It's people like you that helps in keeping this extension useful.
6+
7+
### Where do I go from here ?
8+
9+
If you've noticed a bug or have a question, [search the issue tracker](https://github.com/sourcefuse/loopback4-notifications/issues) to see if
10+
someone else in the community has already created a ticket. If not, go ahead and
11+
[make one](https://github.com/sourcefuse/loopback4-notifications/issues/new/choose)!
12+
13+
### Fork & create a branch
14+
15+
If this is something you think you can fix, then [fork](https://help.github.com/articles/fork-a-repo) this repo and
16+
create a branch with a descriptive name.
17+
18+
A good branch name would be (where issue #325 is the ticket you're working on):
19+
20+
```sh
21+
git checkout -b 325-add-new-feature
22+
```
23+
24+
### Make a Pull Request
25+
26+
At this point, you should switch back to your master branch and make sure it's
27+
up to date with loopback4-notifications's master branch:
28+
29+
```sh
30+
git remote add upstream git@github.com:sourcefuse/loopback4-notifications.git
31+
git checkout master
32+
git pull upstream master
33+
```
34+
35+
Then update your feature branch from your local copy of master, and push it!
36+
37+
```sh
38+
git checkout 325-add-new-feature
39+
git rebase master
40+
git push --set-upstream origin 325-add-new-feature
41+
```
42+
43+
Finally, go to GitHub and [make a Pull Request](https://help.github.com/articles/creating-a-pull-request).
44+
45+
### Keeping your Pull Request updated
46+
47+
If a maintainer asks you to "rebase" your PR, they're saying that a lot of code
48+
has changed, and that you need to update your branch so it's easier to merge.
49+
50+
To learn more about rebasing in Git, there are a lot of [good][git rebasing]
51+
[resources][interactive rebase] but here's the suggested workflow:
52+
53+
```sh
54+
git checkout 325-add-new-feature
55+
git pull --rebase upstream master
56+
git push --force-with-lease 325-add-new-feature
57+
```
58+
59+
[git rebasing]: http://git-scm.com/book/en/Git-Branching-Rebasing
60+
[interactive rebase]: https://help.github.com/articles/interactive-rebase

.github/ISSUE_TEMPLATE/bug_report.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
name: Bug report
3+
about: Create a report to help us improve
4+
title: ''
5+
labels: ''
6+
assignees: ''
7+
8+
---
9+
10+
**Describe the bug**
11+
A clear and concise description of what the bug is.
12+
13+
**To Reproduce**
14+
Steps to reproduce the behavior:
15+
1. Go to '...'
16+
2. Click on '....'
17+
3. Scroll down to '....'
18+
4. See error
19+
20+
**Expected behavior**
21+
A clear and concise description of what you expected to happen.
22+
23+
**Screenshots**
24+
If applicable, add screenshots to help explain your problem.
25+
26+
**Additional context**
27+
Add any other context about the problem here.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
name: Feature request
3+
about: Suggest an idea for this project
4+
title: ''
5+
labels: ''
6+
assignees: ''
7+
8+
---
9+
10+
**Is your feature request related to a problem? Please describe.**
11+
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12+
13+
**Describe the solution you'd like**
14+
A clear and concise description of what you want to happen.
15+
16+
**Describe alternatives you've considered**
17+
A clear and concise description of any alternative solutions or features you've considered.
18+
19+
**Additional context**
20+
Add any other context or screenshots about the feature request here.

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) [2020] [SourceFuse]
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 122 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,135 @@
22

33
[![LoopBack](https://github.com/strongloop/loopback-next/raw/master/docs/site/imgs/branding/Powered-by-LoopBack-Badge-(blue)-@2x.png)](http://loopback.io/)
44

5-
## Installation
5+
This is a loopback 4 extension to connect dynamic datasources runtime. In todays world there can be a use case of multi tenant system in which we need the physical
6+
seperation of the databses so in loopback we need to connect to those datasources runtime and maintain and reuse those connection.
67

7-
Install Loopback4DynamicDatasourceComponent using `npm`;
8+
![Image of Architecture](https://github.com/sourcefuse/loopback4-dynamic-datasource/blob/master/architecture.jpg)
9+
## Install
810

911
```sh
10-
$ [npm install | yarn add] loopback4-dynamic-datasource
12+
npm install loopback4-dynamic-datasource
1113
```
1214

13-
## Basic Use
15+
## Usage
1416

15-
Configure and load Loopback4DynamicDatasourceComponent in the application constructor
16-
as shown below.
17+
In order to use this component into your LoopBack application, please follow below steps.
18+
19+
- Add component to application.
20+
21+
```ts
22+
this.component(Loopback4DynamicDatasourceComponent);
23+
```
24+
25+
- Now add action provider to the action based sequence (This step is not required in case of middleware based sequence)
26+
27+
```ts
28+
export class MySequence implements SequenceHandler {
29+
30+
constructor(
31+
...
32+
@inject(DynamicDatasourceBindings.DYNAMIC_DATASOURCE_ACTION)
33+
private readonly setupDatasource: SetupDatasourceFn,
34+
...
35+
) {}
36+
37+
async handle(context: RequestContext) {
38+
const requestTime = Date.now();
39+
try {
40+
...
41+
await this.setupDatasource(context);
42+
...
43+
} catch (err) {
44+
this.reject(context, err);
45+
}
46+
}
47+
48+
}
49+
```
50+
51+
- Now write a datasource identifier provider that is used to identify runtime which source we need to connect.
52+
In below example getting the identifier from the tenantId coming in query of the request.
53+
```ts
54+
import {DatasourceIdentifierFn} from 'loopback4-dynamic-datasouce';
55+
56+
export class CustomDatasourceIdentifierProvider implements Provider<DatasourceIdentifierFn> {
57+
constructor() {
58+
}
59+
60+
value(): DatasourceIdentifierFn {
61+
return async (requestCtx) => {
62+
const tenantId = requestCtx.request.query['tenantId'] as string;
63+
return tenantId == null ? null : {id: tenantId};
64+
};
65+
}
66+
}
67+
```
68+
Now bind that provider in application.ts
69+
```ts
70+
this.bind(DynamicDatasourceBindings.DATASOURCE_IDENTIFIER_PROVIDER).toProvider(CustomDatasourceIdentifierProvider);
71+
```
72+
- Now write a datasource provider to get datasources runtime. these datasource will be created runtime on require basis
73+
```ts
74+
export class CustomDatasourceProvider implements Provider<DatasourceProviderFn> {
75+
constructor(
76+
@repository(TenantRepository)
77+
private tenantRepo: TenantRepository,
78+
) {
79+
}
80+
81+
value(): DatasourceProviderFn {
82+
return async (datasourceIdentifier) => {
83+
return {
84+
pgdb: async () => {
85+
const tenantData = await this.tenantRepo.findById(datasourceIdentifier.id);
86+
return new juggler.DataSource({
87+
...tenantData.dbConfig,
88+
});
89+
}
90+
}
91+
}
92+
}
93+
}
94+
```
95+
Now bind that provider in application.ts
96+
```ts
97+
this.bind(DynamicDatasourceBindings.DATASOURCE_PROVIDER).toProvider(CustomDatasourceProvider);
98+
```
99+
Note:- connector of following datasource should be present in package.json like in this example we are using **loopback-connector-postgresql**
100+
101+
Now return of this provider is an object where you can give as many keys you want but that should return juggler.Datasource
102+
This is used as the intention of connecting multiple datasource for tenant.
103+
`pgdb` this key is custom and it can be used as per your choice but your repository must use specified key in injection
17104

18105
```ts
19-
import {Loopback4DynamicDatasourceComponent, Loopback4DynamicDatasourceComponentOptions, DEFAULT_LOOPBACK4_DYNAMIC_DATASOURCE_OPTIONS} from 'loopback4-dynamic-datasource';
20-
// ...
21-
export class MyApplication extends BootMixin(ServiceMixin(RepositoryMixin(RestApplication))) {
22-
constructor(options: ApplicationConfig = {}) {
23-
const opts: Loopback4DynamicDatasourceComponentOptions = DEFAULT_LOOPBACK4_DYNAMIC_DATASOURCE_OPTIONS;
24-
this.configure(Loopback4DynamicDatasourceComponentBindings.COMPONENT).to(opts);
25-
// Put the configuration options here
26-
});
27-
this.component(Loopback4DynamicDatasourceComponent);
28-
// ...
29-
}
30-
// ...
106+
export class UserRepository extends DefaultCrudRepository<User,
107+
typeof User.prototype.id,
108+
UserRelations> {
109+
constructor(
110+
@inject('datasources.pgdb') dataSource: JugglerDataSource,
111+
) {
112+
super(User, dataSource);
113+
}
31114
}
32115
```
116+
117+
That's all.
118+
119+
## Feedback
120+
121+
If you've noticed a bug or have a question or have a feature request, [search the issue tracker](https://github.com/sourcefuse/loopBack4-dynamic-datasource/issues) to see if someone else in the community has already created a ticket.
122+
If not, go ahead and [make one](https://github.com/sourcefuse/loopBack4-dynamic-datasource/issues/new/choose)!
123+
All feature requests are welcome. Implementation time may vary. Feel free to contribute the same, if you can.
124+
If you think this extension is useful, please [star](https://help.github.com/en/articles/about-stars) it. Appreciation really helps in keeping this project alive.
125+
126+
## Contributing
127+
128+
Please read [CONTRIBUTING.md](https://github.com/sourcefuse/loopBack4-dynamic-datasource/blob/master/.github/CONTRIBUTING.md) for details on the process for submitting pull requests to us.
129+
130+
## Code of conduct
131+
132+
Code of conduct guidelines [here](https://github.com/sourcefuse/loopBack4-dynamic-datasource/blob/master/.github/CODE_OF_CONDUCT.md).
133+
134+
## License
135+
136+
[MIT](https://github.com/sourcefuse/loopBack4-dynamic-datasource/blob/master/LICENSE)

architecture.jpg

47 KB
Loading

0 commit comments

Comments
 (0)