mongodb-dynamic-api 1.4.0 → 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,7 @@
1
1
  Changelog
2
2
 
3
+ ## [1.4.1](https://github.com/MikeDev75015/mongodb-dynamic-api/compare/v1.4.0...v1.4.1) (2024-03-12)
4
+
3
5
  ## [1.4.0](https://github.com/MikeDev75015/mongodb-dynamic-api/compare/v1.3.3...v1.4.0) (2024-03-11)
4
6
 
5
7
 
package/README.md CHANGED
@@ -71,12 +71,13 @@ ___
71
71
 
72
72
  ### Table of Contents
73
73
 
74
- [Swagger UI](#swagger-ui--optional-but-strongly-recommended)
75
- <br>[Validation](#validation--optional)
76
- <br>[Versioning](#versioning--optional)
77
- <br>[Caching](#caching--enabled-by-default)
78
- <br>[Authentication](#authentication--optional)
79
- <br>[Casl](#casl--only-with-authentication)
74
+ [Introduction](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README.md#how-to-enjoy-it)
75
+ - [Swagger UI](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README.md#swagger-ui-optional-but-strongly-recommended)
76
+ - [Validation](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README.md#validation-optional)
77
+ - [Versioning](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README.md#versioning-optional)
78
+ - [Caching](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README.md#caching-enabled-by-default)
79
+ - [Authentication](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README.md#authentication-optional)
80
+ - [Casl](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README.md#casl-only-with-authentication)
80
81
 
81
82
  ---
82
83
  ### HOW TO ENJOY IT
@@ -95,13 +96,13 @@ npm i -S mongodb-dynamic-api
95
96
  - Add `DynamicApiModule.forRoot` to your `app.module.ts` and pass your **MongoDB connection string** to the method.
96
97
 
97
98
  ```typescript
98
- // app.module.ts
99
+ // src/app.module.ts
99
100
  import { DynamicApiModule } from 'mongodb-dynamic-api';
100
101
 
101
102
  @Module({
102
103
  imports: [
103
104
  DynamicApiModule.forRoot(
104
- 'mongodb://127.0.0.1:27017/dynamic-api-db', // <- replace by your own connection string
105
+ 'mongodb-uri', // <- replace by your own MongoDB connection string
105
106
  ),
106
107
  // ...
107
108
  ],
@@ -113,19 +114,19 @@ export class AppModule {}
113
114
  **Basic Usage**
114
115
 
115
116
  - Ok, now let's add our first content with just 2 files. It will be a simple `User` with a `name` and an `email` field.
116
- - We use the `@Schema` and `@Prop` decorators from the `@nestjs/mongoose` package to define our MongoDB model. <br>*See <strong>nestjs</strong> <a href="https://docs.nestjs.com/techniques/mongodb#model-injection" target="_blank">documentation</a> for more details.*
117
+ - We use the `@Schema` and `@Prop` decorators from the <a href="https://docs.nestjs.com/techniques/mongodb#model-injection" target="_blank">@nestjs/mongoose</a> package to define our MongoDB model.
117
118
 
118
119
 
119
120
  - You must extend the `BaseEntity` (or `SoftDeletableEntity`) class from the `mongodb-dynamic-api` package **for all your collection models**.
120
121
  - Just create a new file `user.ts` and add the following code.
121
122
 
122
123
  ```typescript
123
- // users/user.ts
124
+ // src/users/user.ts
124
125
  import { Prop, Schema } from '@nestjs/mongoose';
125
126
  import { BaseEntity } from 'mongodb-dynamic-api';
126
127
 
127
128
  @Schema({ collection: 'users' })
128
- export class User extends BaseEntity {
129
+ export class User extends BaseEntity { // <- extends BaseEntity
129
130
  @Prop({ type: String, required: true })
130
131
  name: string;
131
132
 
@@ -134,12 +135,12 @@ export class User extends BaseEntity {
134
135
  }
135
136
  ```
136
137
 
137
- - Then we will use the `DynamicApiModule.forFeature` method to add the `User` content.
138
+ - Then we will use the `DynamicApiModule.forFeature` method to add the `User` API to our application.
138
139
  - We pass the `User` class to the `entity` property and specify the path `users` to the `controllerOptions` property.
139
140
  - Create a new file `users.module.ts` and add the following code.
140
141
 
141
142
  ```typescript
142
- // users/users.module.ts
143
+ // src/users/users.module.ts
143
144
  import { DynamicApiModule } from 'mongodb-dynamic-api';
144
145
  import { User } from './user';
145
146
 
@@ -159,7 +160,7 @@ export class UsersModule {}
159
160
  - Last step, add the `UsersModule` to the **imports** in the `app.module.ts` after the `DynamicApiModule.forRoot` method.
160
161
 
161
162
  ```typescript
162
- // app.module.ts
163
+ // src/app.module.ts
163
164
  import { DynamicApiModule } from 'mongodb-dynamic-api';
164
165
  import { UsersModule } from './users/users.module';
165
166
 
@@ -193,6 +194,15 @@ export class AppModule {}
193
194
  | **POST /users/duplicate** <br>*Duplicate many* | `{ name?: string; email?: string; }` | x | `ids: string[]` |
194
195
  | **POST /users/duplicate/:id**<br>*Duplicate one* | `{ name?: string; email?: string; }` | `id: string` | x |
195
196
 
197
+ ___
198
+
199
+ - TOC > [Validation](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README.md#validation-optional)
200
+ / [Versioning](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README.md#versioning-optional)
201
+ / [Caching](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README.md#caching-enabled-by-default)
202
+ / [Authentication](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README.md#authentication-optional)
203
+ / [Casl](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README.md#casl-only-with-authentication)
204
+
205
+ ___
196
206
 
197
207
  ### [Swagger UI](https://docs.nestjs.com/openapi/introduction#document-options) (optional but strongly recommended)
198
208
  `function enableDynamicAPISwagger(app: INestApplication, options?: DynamicAPISwaggerOptions): void`
@@ -200,7 +210,7 @@ export class AppModule {}
200
210
  **Configuration**
201
211
 
202
212
  ```typescript
203
- // main.ts
213
+ // src/main.ts
204
214
  import { enableDynamicAPISwagger } from 'mongodb-dynamic-api';
205
215
 
206
216
  async function bootstrap() {
@@ -222,7 +232,7 @@ Add the `@ApiProperty` | `@ApiPropertyOptional` decorators to your class propert
222
232
  <br>Let's add an optional company field to the `User` class.
223
233
 
224
234
  ```typescript
225
- // user.ts
235
+ // src/users/user.ts
226
236
  import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
227
237
 
228
238
  @Schema({ collection: 'users' })
@@ -248,13 +258,22 @@ go to the swagger API path (default to `/dynamic-api`) and you will see the auto
248
258
  <a href="https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README/swagger-user-api.md" target="_blank">See more User API screenshots</a>
249
259
 
250
260
  ___
261
+
262
+ - TOC > [Introduction](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README.md#how-to-enjoy-it)
263
+ / [Versioning](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README.md#versioning-optional)
264
+ / [Caching](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README.md#caching-enabled-by-default)
265
+ / [Authentication](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README.md#authentication-optional)
266
+ / [Casl](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README.md#casl-only-with-authentication)
267
+
268
+ ___
269
+
251
270
  ### [Validation](https://docs.nestjs.com/techniques/validation#using-the-built-in-validationpipe) (optional)
252
- `function enableDynamicAPIValidation(app: INestApplication, options?: ValidationPipeOptions): void`
271
+ <br>`function enableDynamicAPIValidation(app: INestApplication, options?: ValidationPipeOptions): void`
253
272
 
254
273
  **Configuration**
255
274
 
256
275
  ```typescript
257
- // main.ts
276
+ // src/main.ts
258
277
  import { enableDynamicAPIValidation } from 'mongodb-dynamic-api';
259
278
 
260
279
  async function bootstrap() {
@@ -272,7 +291,7 @@ or in each route object defined in the routes property.
272
291
  <br>*If the options are specified in 2, the options specified in the route will have priority.*
273
292
 
274
293
  ```typescript
275
- // users.module.ts
294
+ // src/users/users.module.ts
276
295
  import { DynamicApiModule } from 'mongodb-dynamic-api';
277
296
  import { User } from './user';
278
297
 
@@ -306,7 +325,7 @@ Use the `Class validator` <a href="https://github.com/typestack/class-validator?
306
325
  <br>Let's add `IsEmail` decorator to the `email` field.
307
326
 
308
327
  ```typescript
309
- // user.ts
328
+ // src/users/user.ts
310
329
  import { IsEmail } from 'class-validator';
311
330
 
312
331
  @Schema({ collection: 'users' })
@@ -330,8 +349,16 @@ Ok, now if you try to create a new user with an invalid email, you will get a `4
330
349
 
331
350
  ![User API Validation](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README/images/dynamic-api-validation.Jpeg?raw=true "User API Validation")
332
351
 
352
+ ___
353
+
354
+ - TOC > [Introduction](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README.md#how-to-enjoy-it)
355
+ / [Swagger UI](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README.md#swagger-ui-optional-but-strongly-recommended)
356
+ / [Caching](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README.md#caching-enabled-by-default)
357
+ / [Authentication](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README.md#authentication-optional)
358
+ / [Casl](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README.md#casl-only-with-authentication)
333
359
 
334
360
  ___
361
+
335
362
  ### [Versioning](https://docs.nestjs.com/techniques/versioning) (optional)
336
363
  `function enableDynamicAPIVersioning(app: INestApplication, options?: VersioningOptions): void`
337
364
 
@@ -343,7 +370,7 @@ The `enableDynamicAPIVersioning` function will automatically add versioning to t
343
370
  **Configuration**
344
371
 
345
372
  ```typescript
346
- // main.ts
373
+ // src/main.ts
347
374
  import { enableDynamicAPIVersioning } from 'mongodb-dynamic-api';
348
375
 
349
376
  async function bootstrap() {
@@ -363,7 +390,7 @@ Pass the `version` property to the `controllerOptions` object or to the `route`
363
390
  Let's add a new version to the `User` content.
364
391
 
365
392
  ```typescript
366
- // create-one-user-v2.dto.ts
393
+ // src/users/create-one-user-v2.dto.ts
367
394
  import { ApiProperty, ApiPropertyOptional, PickType } from '@nestjs/swagger';
368
395
  import { IsOptional, IsString } from 'class-validator';
369
396
  import { User } from './user';
@@ -385,7 +412,7 @@ export class CreateOneUserV2Dto extends PickType(User, ['email']) {
385
412
  ```
386
413
 
387
414
  ```typescript
388
- // user-v2.presenter.ts
415
+ // src/users/user-v2.presenter.ts
389
416
  import { ApiProperty, ApiPropertyOptional, PickType } from '@nestjs/swagger';
390
417
  import { User } from './user';
391
418
 
@@ -402,7 +429,7 @@ export class UserV2Presenter extends PickType(User, ['email']) {
402
429
  ```
403
430
 
404
431
  ```typescript
405
- // users.module.ts
432
+ // src/users/users.module.ts
406
433
  import { DynamicApiModule } from 'mongodb-dynamic-api';
407
434
  import { User } from './user';
408
435
  import { CreateOneUserV2Dto } from './create-one-user-v2.dto';
@@ -438,6 +465,13 @@ Great, now you have a versioned User API, and you can access it at the `/v1/user
438
465
 
439
466
  ![User API Versioned](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README/images/dynamic-api-versioning.Jpeg?raw=true "User API Versioned")
440
467
 
468
+ ___
469
+
470
+ - TOC > [Introduction](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README.md#how-to-enjoy-it)
471
+ / [Swagger UI](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README.md#swagger-ui-optional-but-strongly-recommended)
472
+ / [Validation](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README.md#validation-optional)
473
+ / [Authentication](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README.md#authentication-optional)
474
+ / [Casl](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README.md#casl-only-with-authentication)
441
475
 
442
476
  ___
443
477
 
@@ -449,7 +483,7 @@ By default, the caching is activated globally for all the routes. It uses the ne
449
483
  **Configuration**
450
484
 
451
485
  ```typescript
452
- // app.module.ts
486
+ // src/app.module.ts
453
487
  import { DynamicApiModule } from 'mongodb-dynamic-api';
454
488
 
455
489
  @Module({
@@ -473,7 +507,7 @@ export class AppModule {}
473
507
  **[Not recommended]** The cache can also be disabled globally with the `useGlobalCache` property set to `false` in the `DynamicApiModule.forRoot` method.
474
508
 
475
509
  ```typescript
476
- // app.module.ts
510
+ // src/app.module.ts
477
511
  import { DynamicApiModule } from 'mongodb-dynamic-api';
478
512
 
479
513
  @Module({
@@ -515,6 +549,13 @@ When you request the `/users` route with the `GET` method, the response will be
515
549
  ```
516
550
  ![Third GET request](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README/images/dynamic-api-caching-4-GET-third-request.Jpeg?raw=true "Third GET request")
517
551
 
552
+ ___
553
+
554
+ - TOC > [Introduction](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README.md#how-to-enjoy-it)
555
+ / [Swagger UI](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README.md#swagger-ui-optional-but-strongly-recommended)
556
+ / [Validation](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README.md#validation-optional)
557
+ / [Versioning](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README.md#versioning-optional)
558
+ / [Casl](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README.md#casl-only-with-authentication)
518
559
 
519
560
  ___
520
561
 
@@ -528,7 +569,7 @@ All you have to do is to pass the User object and some options to the `useAuth`
528
569
  Ok, let's update our `User` class to add a `password` field.
529
570
 
530
571
  ```typescript
531
- // user.ts
572
+ // src/users/user.ts
532
573
  import { IsEmail } from 'class-validator';
533
574
 
534
575
  @Schema({ collection: 'users' })
@@ -561,9 +602,11 @@ export class User extends BaseEntity {
561
602
  ```
562
603
 
563
604
  Now, we are going to add the `useAuth` property to the `DynamicApiModule.forRoot` method and pass the `User` object and some options.
605
+ <br>By default, the login field is `email` and the password field is `password`. Your User class must have these fields.
606
+ <br>If you want to use other fields, you can specify them in the `user` property by passing the `loginField` and / or `passwordField` properties.
564
607
 
565
608
  ```typescript
566
- // app.module.ts
609
+ // src/app.module.ts
567
610
  import { DynamicApiModule } from 'mongodb-dynamic-api';
568
611
  import { User } from './users/user';
569
612
  import { UsersModule } from './users/users.module';
@@ -575,11 +618,6 @@ import { UsersModule } from './users/users.module';
575
618
  useAuth: { // <- add this
576
619
  user: {
577
620
  entity: User, // <- put here the entity which will represent a User of your API
578
- loginField: 'email',
579
- passwordField: 'password',
580
- },
581
- jwt: {
582
- secret: 'my-secret', // <- replace by your own JWT secret in production
583
621
  },
584
622
  },
585
623
  }),
@@ -603,7 +641,7 @@ For Swagger users, you must enable the bearer Auth option by setting the `bearer
603
641
  This will add the Authorize button in the Swagger UI. This button will allow you to pass the `JWT Token` and unlock the protected routes.
604
642
 
605
643
  ```typescript
606
- // main.ts
644
+ // src/main.ts
607
645
  import { enableDynamicAPISwagger } from 'mongodb-dynamic-api';
608
646
 
609
647
  async function bootstrap() {
@@ -687,7 +725,7 @@ Great, now you have a fully functional authentication API.
687
725
  All other routes are protected and require a valid JWT token to be accessed. You can easily make it public by adding the `isPublic` property to the `controllerOptions` object or to the `route` object in the `DynamicApiModule.forFeature` method.
688
726
 
689
727
  ```typescript
690
- // users.module.ts
728
+ // src/users/users.module.ts
691
729
  import { DynamicApiModule } from 'mongodb-dynamic-api';
692
730
  import { User } from './user';
693
731
 
@@ -706,7 +744,7 @@ import { User } from './user';
706
744
  export class UsersModule {}
707
745
  ```
708
746
  ```typescript
709
- // users.module.ts
747
+ // src/users/users.module.ts
710
748
  import { DynamicApiModule } from 'mongodb-dynamic-api';
711
749
  import { User } from './user';
712
750
 
@@ -731,6 +769,14 @@ export class UsersModule {}
731
769
 
732
770
  ___
733
771
 
772
+ - TOC > [Introduction](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README.md#how-to-enjoy-it)
773
+ / [Swagger UI](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README.md#swagger-ui-optional-but-strongly-recommended)
774
+ / [Validation](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README.md#validation-optional)
775
+ / [Versioning](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README.md#versioning-optional)
776
+ / [Caching](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README.md#caching-enabled-by-default)
777
+
778
+ ___
779
+
734
780
  ### [Casl](https://docs.nestjs.com/security/authorization#integrating-casl) (only with authentication)
735
781
 
736
782
  Casl will allow you to condition the actions of your users for each protected route of your APIs.
@@ -748,7 +794,7 @@ Let's create a new Article content and set the ability predicates to the `Update
748
794
  **Configuration**
749
795
 
750
796
  ```typescript
751
- // article.ts
797
+ // src/articles/article.ts
752
798
  import { Prop } from '@nestjs/mongoose';
753
799
  import { ApiProperty } from '@nestjs/swagger';
754
800
  import { BaseEntity } from 'mongodb-dynamic-api';
@@ -765,7 +811,7 @@ export class Article extends BaseEntity {
765
811
  ```
766
812
 
767
813
  ```typescript
768
- // articles.module.ts
814
+ // src/articles/articles.module.ts
769
815
  import { Module } from '@nestjs/common';
770
816
  import { DynamicApiModule } from 'mongodb-dynamic-api';
771
817
  import { User } from '../users/user';
@@ -801,7 +847,7 @@ export class ArticlesModule {}
801
847
  ```
802
848
 
803
849
  ```typescript
804
- // app.module.ts
850
+ // src/app.module.ts
805
851
  import { Module } from '@nestjs/common';
806
852
  import { DynamicApiModule } from 'mongodb-dynamic-api';
807
853
  import { AppController } from './app.controller';
@@ -853,7 +899,7 @@ curl -X 'POST' \
853
899
 
854
900
  Then, we are going to protect the `/auth/register` route by setting the `protectRegister` property to `true` and add a **register ability predicate** in the useAuth Object of the `DynamicApiModule.forRoot` method.
855
901
  ```typescript
856
- // app.module.ts
902
+ // src/app.module.ts
857
903
  @Module({
858
904
  imports: [
859
905
  DynamicApiModule.forRoot(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mongodb-dynamic-api",
3
- "version": "1.4.0",
3
+ "version": "1.4.1",
4
4
  "description": "Auto generated CRUD API for MongoDB using NestJS",
5
5
  "readmeFilename": "README.md",
6
6
  "main": "index.js",
package/src/version.json CHANGED
@@ -1,3 +1,3 @@
1
1
  {
2
- "version": "1.4.0"
2
+ "version": "1.4.1"
3
3
  }