Skip to content

Commit 81c3aeb

Browse files
Merge pull request #17 from nestjs-addons/add_docs_page
docs(platform): add docusaurus project with documentation
2 parents e64485b + a677f51 commit 81c3aeb

28 files changed

+7673
-220
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@ testem.log
3737
# System Files
3838
.DS_Store
3939
Thumbs.db
40+
41+
# Generated Docusaurus files
42+
.docusaurus/
43+
.cache-loader/

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22

33
/dist
44
/coverage
5+
.docusaurus/

nx.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
},
3636
"spectator": {
3737
"tags": []
38+
},
39+
"docs": {
40+
"tags": []
3841
}
3942
},
4043
"workspaceLayout": {

package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,15 @@
2929
},
3030
"private": true,
3131
"dependencies": {
32+
"@docusaurus/core": "^2.0.0-alpha.70",
33+
"@docusaurus/plugin-sitemap": "^2.0.0-alpha.70",
34+
"@docusaurus/preset-classic": "^2.0.0-alpha.70",
3235
"@nestjs/common": "^7.6.11",
3336
"@nestjs/core": "^7.6.11",
3437
"@nestjs/platform-express": "^7.6.11",
38+
"clsx": "^1.1.1",
39+
"react": "^16.8.4",
40+
"react-dom": "^16.8.4",
3541
"reflect-metadata": "^0.1.13",
3642
"rxjs": "~6.6.3"
3743
},
@@ -44,6 +50,7 @@
4450
"@nrwl/nest": "11.2.12",
4551
"@nrwl/node": "11.2.12",
4652
"@nrwl/tao": "11.2.12",
53+
"@nx-plus/docusaurus": "10.4.0",
4754
"@nrwl/workspace": "11.2.12",
4855
"@types/jest": "26.0.15",
4956
"@types/node": "12.12.38",
@@ -62,6 +69,9 @@
6269
"tslint": "6.1.3",
6370
"typescript": "4.0.5"
6471
},
72+
"resolutions": {
73+
"terser": "^4.0.0"
74+
},
6575
"config": {
6676
"commitizen": {
6777
"path": "./node_modules/cz-conventional-changelog"

packages/docs/babel.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
presets: [require.resolve('@docusaurus/core/lib/babel/preset')],
3+
};

packages/docs/blog/2019-05-28-hola.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
slug: hola
3+
title: Hola
4+
author: Gao Wei
5+
author_title: Docusaurus Core Team
6+
author_url: https://github.com/wgao19
7+
author_image_url: https://avatars1.githubusercontent.com/u/2055384?v=4
8+
tags: [hola, docusaurus]
9+
---
10+
11+
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque elementum dignissim ultricies. Fusce rhoncus ipsum tempor eros aliquam consequat. Lorem ipsum dolor sit amet
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
slug: hello-world
3+
title: Hello
4+
author: Endilie Yacop Sucipto
5+
author_title: Maintainer of Docusaurus
6+
author_url: https://github.com/endiliey
7+
author_image_url: https://avatars1.githubusercontent.com/u/17883920?s=460&v=4
8+
tags: [hello, docusaurus]
9+
---
10+
11+
Welcome to this blog. This blog is created with [**Docusaurus 2 alpha**](https://v2.docusaurus.io/).
12+
13+
<!--truncate-->
14+
15+
This is a test post.
16+
17+
A whole bunch of other information.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
slug: welcome
3+
title: Welcome
4+
author: Yangshun Tay
5+
author_title: Front End Engineer @ Facebook
6+
author_url: https://github.com/yangshun
7+
author_image_url: https://avatars0.githubusercontent.com/u/1315101?s=400&v=4
8+
tags: [facebook, hello, docusaurus]
9+
---
10+
11+
Blog features are powered by the blog plugin. Simply add files to the `blog` directory. It supports tags as well!
12+
13+
Delete the whole directory if you don't want the blog features. As simple as that!

packages/docs/docs/IMDB-apidoc.md

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
---
2+
id: in-memory-db-apidoc
3+
title: API Documentation
4+
---
5+
6+
## `InMemoryDBService<T extends InMemoryEntity>`
7+
8+
This is the service that provides the in-memory database. All methods interact with a `records` array and implement `generics` to provide type-safety and intellisense based on the `T extends InMemoryEntity` passed in.
9+
10+
### Public Methods
11+
12+
**`public create(record: Partial<T>, getNextId?: () => string): T`**
13+
14+
This method takes in a `Partial<T>` as we do not always know the `id` for a record when we are creating. If we leave off the `id` property the service will automatically generate an `id` for us. Upon successful creation, the method returns the record with the newly generated `id`. An optional parameter of `getNextId` can be used to pass a function that returns a `string` and will be used by the service to get the next id. By default this uses the `uuid` npm package.
15+
16+
Example Usage:
17+
18+
```typescript
19+
const newUser = this.userService.create({
20+
firstName: 'Some',
21+
lastName: 'Person',
22+
});
23+
24+
console.log({ newUser });
25+
26+
// logs out
27+
// {
28+
// newUser: {
29+
// id: 1,
30+
// firstName: 'Some',
31+
// lastName: 'Person,
32+
// }
33+
// }
34+
```
35+
36+
**`public createMany(records: Array<Partial<T>>, getNextId?: () => string): T[]`**
37+
38+
This method takes in an array of `Partial<T>` as we do not always know the `id` for records when we are creating. If we leave off the `id` properties the service will automatically generate `id`s for us. Upon successful creation, the method returns the an array of records with the newly generated `id`s. An optional parameter of `getNextId` can be used to pass a function that returns a `string` and will be used by the service to get the next id. By default this uses the `uuid` npm package.
39+
40+
Example Usage:
41+
42+
```typescript
43+
const recordsToCreate = [
44+
{
45+
firstName: 'Some',
46+
lastName: 'Person',
47+
},
48+
{
49+
firstName: 'Other',
50+
lastName: 'Person',
51+
},
52+
];
53+
54+
const newUsers = this.userService.createMany(recordsToCreate);
55+
56+
console.log({ newUsers });
57+
58+
// logs out
59+
// {
60+
// newUsers: [{
61+
// id: 1,
62+
// firstName: 'Some',
63+
// lastName: 'Person,
64+
// },
65+
// {
66+
// id: 2,
67+
// firstName: 'Other',
68+
// lastName: 'Person,
69+
// }]
70+
// }
71+
```
72+
73+
**`public update(record: T): void`**
74+
75+
This method takes in a `T` record object and updates the record in the `records` array based on the `id` in the object. This method does not return a value.
76+
77+
Example Usage:
78+
79+
```typescript
80+
this.userService.update({
81+
id: 1,
82+
firstName: 'Other',
83+
lastName: 'Person',
84+
});
85+
```
86+
87+
**`public delete(id: string): void`**
88+
89+
This method takes in a `id: string` and deletes the record from the `records` array based on the `id` in the object. This method does not return a value.
90+
91+
Example Usage:
92+
93+
```typescript
94+
this.userService.delete('1');
95+
```
96+
97+
**`public get(id: string): T`**
98+
99+
This method takes in a `id: string` and returns the record from the `records` array based on the `id` in the object.
100+
101+
Example Usage:
102+
103+
```typescript
104+
const foundUser = this.userService.get('1');
105+
106+
console.log({ foundUser });
107+
108+
// logs out
109+
// {
110+
// foundUser: {
111+
// id: '1',
112+
// firstName: 'Some',
113+
// lastName: 'Person'
114+
// }
115+
// }
116+
```
117+
118+
**`public getAll(): T[]`**
119+
120+
This method has no parameters and returns the all from the `records` array.
121+
122+
Example Usage:
123+
124+
```typescript
125+
const allUsers = this.userService.getAll();
126+
127+
console.log({ allUsers });
128+
129+
// logs out
130+
// {
131+
// allUsers: [
132+
// {
133+
// id: '1',
134+
// firstName: 'Some',
135+
// lastName: 'Person'
136+
// },
137+
// {
138+
// id: '2',
139+
// firstName: 'Other',
140+
// lastName: 'Person'
141+
// }
142+
// ];
143+
// }
144+
```
145+
146+
**`public query(predicate: (record: T) => boolean): T[]`**
147+
148+
This method has takes in a `record: T` predicate and returns all from the `records` array that meet that predicate's requirements.
149+
150+
Example Usage:
151+
152+
```typescript
153+
const foundUsers = this.userService.query(
154+
(record) => record.lastName === 'Person',
155+
);
156+
157+
console.log({ foundUsers });
158+
159+
// logs out
160+
// {
161+
// allUsers: [
162+
// {
163+
// id: '1',
164+
// firstName: 'Some',
165+
// lastName: 'Person'
166+
// },
167+
// {
168+
// id: '2',
169+
// firstName: 'Other',
170+
// lastName: 'Person'
171+
// }
172+
// ];
173+
// }
174+
```
175+
176+
### Public Properties
177+
178+
- `records: T[]` - This is the in-memory array used in all crud and read operations for the service. Please access with care.
179+
180+
## `InMemoryDBEntity`
181+
182+
This is an interface used by the `InMemoryDBService` for intellisense and type-safety. Do not use this interface directly. Rather, implement your own `interface` that `extends` this.
183+
184+
```typescript
185+
export interface InMemoryDBEntity {
186+
id: string;
187+
}
188+
```
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
id: in-memory-db-index
3+
title: Introduction
4+
---
5+
6+
`@nestjs-addons/in-memory-db` provides a ridiculously simple, no configuration needed, way to create a simple in-memory database for use in your `nestjs` applications. You simply define an `interface` that extends the `interface InMemoryDBEntity`, inject the `InMemoryDBService<T>` into your controllers and/or services, and immediately profit. The records are stored in-memory, as a singleton, for each interface, for the life of the service.
7+
8+
This provides a great way to quickly get up and running with prototypes and mock backends.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
id: in-memory-db-entitycontroller
3+
title: Entity Controller
4+
---
5+
6+
In order to prevent code duplication and boilerplate for each controller, we have created two base entity controllers `InMemoryDBEntityController` and `InMemoryDBEntityAsyncController`. This allows you to quickly provide endpoints to make requests without having to manually implement each action.
7+
8+
To use the controllers, simply create a new controller and extend it with one of the provided base controllers.
9+
10+
```typescript
11+
@Controller('api/users')
12+
class UsersController extends InMemoryDBEntityController<UserEntity> {
13+
constructor(protected dbService: InMemoryDBService<UserEntity>) {
14+
super(dbService);
15+
}
16+
}
17+
```
18+
19+
In order to have an Entity Controller use a feature-specific instance of the service, use the decorator `InjectInMemoryDBService` in the controller's provided by this library as shown below:
20+
21+
```typescript
22+
@Controller('api/users')
23+
class UsersController extends InMemoryDBEntityController<UserEntity> {
24+
constructor(
25+
@InjectInMemoryDBService('customer')
26+
protected readonly inMemoryDBService: InMemoryDBService<UserEntity>,
27+
) {
28+
super(inMemoryDBService);
29+
}
30+
}
31+
```
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
id: in-memory-db-featuremodules
3+
title: Feature Modules
4+
---
5+
6+
## Registering Multiple Instances using `forFeature`
7+
8+
Registering multiple instances for specific feature modules is super simple. Each feature module is guaranteed isolated to that feature. In order to get up and running you need to do the following:
9+
10+
### Registering a forFeature InMemoryDBService
11+
12+
For each feature module(s), do the following:
13+
14+
```typescript
15+
// feature-one.module.ts
16+
17+
import { Module } from '@nestjs/common';
18+
import { InMemoryDBModule } from '@nestjs-addons/in-memory-db';
19+
...
20+
21+
@Module({
22+
...
23+
imports: [InMemoryDBModule.forFeature('one', {})],
24+
...
25+
})
26+
export class FeatureOneModule {}
27+
```
28+
29+
As you can see we:
30+
31+
- Imported `InMemoryDBModule` from `@nestjs-addons/in-memory-db`
32+
- Added `InMemoryDBModule` to the `imports` array in the `@Module` of your choice
33+
- Added the `forFeature` method call passing `one` as the feature name
34+
35+
### Using the Feature Instance
36+
37+
If you would like to use the feature-specific instance, make use of the included `@InjectInMemoryDBService` decorator:
38+
39+
```typescript
40+
@Controller({...})
41+
export class FeatureOneController {
42+
constructor(@InjectInMemoryDBService('one') private oneService: InMemoryDBService<OneEntity>) {}
43+
...
44+
@Get()
45+
getAll(): OneEntity[] {
46+
return this.oneService.getAll();
47+
}
48+
}
49+
```
50+
51+
Using this decorator ensures that the correct instance is injected.

0 commit comments

Comments
 (0)