mongodb-dynamic-api 1.2.1 → 1.3.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 +11 -0
- package/README.md +90 -11
- package/package.json +3 -1
- package/src/builders/route-decorators.builder.d.ts +1 -0
- package/src/builders/route-decorators.builder.js +13 -2
- package/src/dtos/delete-many-entity.query.d.ts +3 -0
- package/src/dtos/delete-many-entity.query.js +24 -0
- package/src/dtos/index.d.ts +1 -0
- package/src/dtos/index.js +1 -0
- package/src/dynamic-api.module.d.ts +4 -3
- package/src/dynamic-api.module.js +35 -4
- package/src/helpers/format.helper.d.ts +1 -0
- package/src/helpers/format.helper.js +5 -1
- package/src/helpers/route-description.helper.js +6 -0
- package/src/helpers/versioning-config.helper.d.ts +2 -1
- package/src/helpers/versioning-config.helper.js +5 -1
- package/src/interfaces/controller-options.interface.d.ts +8 -0
- package/src/interfaces/dynamic-api-cache-options.interface.d.ts +8 -0
- package/src/interfaces/dynamic-api-options.interface.d.ts +10 -10
- package/src/interfaces/{route-config.interface.d.ts → dynamic-api-route-config.interface.d.ts} +3 -3
- package/src/interfaces/dynamic-api-route-config.interface.js +2 -0
- package/src/interfaces/{service-provider.interface.d.ts → dynamic-api-service-provider.interface.d.ts} +2 -2
- package/src/interfaces/dynamic-api-service-provider.interface.js +2 -0
- package/src/interfaces/index.d.ts +4 -3
- package/src/interfaces/index.js +4 -3
- package/src/modules/create-many/create-many-controller.mixin.js +4 -4
- package/src/modules/create-many/create-many.helper.d.ts +2 -2
- package/src/modules/create-many/create-many.helper.js +3 -2
- package/src/modules/create-many/create-many.module.d.ts +2 -2
- package/src/modules/create-one/create-one-controller.mixin.js +3 -3
- package/src/modules/create-one/create-one.helper.d.ts +2 -2
- package/src/modules/create-one/create-one.helper.js +3 -2
- package/src/modules/create-one/create-one.module.d.ts +2 -2
- package/src/modules/delete-many/base-delete-many.service.d.ts +34 -0
- package/src/modules/delete-many/base-delete-many.service.js +29 -0
- package/src/modules/delete-many/delete-many-controller.interface.d.ts +8 -0
- package/src/modules/delete-many/delete-many-controller.interface.js +2 -0
- package/src/modules/delete-many/delete-many-controller.mixin.d.ts +6 -0
- package/src/modules/delete-many/delete-many-controller.mixin.js +54 -0
- package/src/modules/delete-many/delete-many-service.interface.d.ts +6 -0
- package/src/modules/delete-many/delete-many-service.interface.js +2 -0
- package/src/modules/delete-many/delete-many.helper.d.ts +7 -0
- package/src/modules/delete-many/delete-many.helper.js +69 -0
- package/src/modules/delete-many/delete-many.module.d.ts +6 -0
- package/src/modules/delete-many/delete-many.module.js +28 -0
- package/src/modules/delete-many/delete-many.presenter.d.ts +3 -0
- package/src/modules/delete-many/delete-many.presenter.js +20 -0
- package/src/modules/delete-many/index.d.ts +7 -0
- package/src/modules/delete-many/index.js +23 -0
- package/src/modules/delete-one/delete-one-controller.mixin.js +3 -3
- package/src/modules/delete-one/delete-one.helper.d.ts +2 -2
- package/src/modules/delete-one/delete-one.helper.js +3 -2
- package/src/modules/delete-one/delete-one.module.d.ts +2 -2
- package/src/modules/duplicate-many/base-duplicate-many.service.d.ts +33 -0
- package/src/modules/duplicate-many/base-duplicate-many.service.js +43 -0
- package/src/modules/duplicate-many/duplicate-many-controller.interface.d.ts +7 -0
- package/src/modules/duplicate-many/duplicate-many-controller.interface.js +2 -0
- package/src/modules/duplicate-many/duplicate-many-controller.mixin.d.ts +6 -0
- package/src/modules/duplicate-many/duplicate-many-controller.mixin.js +63 -0
- package/src/modules/duplicate-many/duplicate-many-service.interface.d.ts +5 -0
- package/src/modules/duplicate-many/duplicate-many-service.interface.js +2 -0
- package/src/modules/duplicate-many/duplicate-many.helper.d.ts +7 -0
- package/src/modules/duplicate-many/duplicate-many.helper.js +69 -0
- package/src/modules/duplicate-many/duplicate-many.module.d.ts +6 -0
- package/src/modules/duplicate-many/duplicate-many.module.js +28 -0
- package/src/modules/duplicate-many/index.d.ts +6 -0
- package/src/modules/duplicate-many/index.js +22 -0
- package/src/modules/duplicate-one/duplicate-one-controller.mixin.js +4 -4
- package/src/modules/duplicate-one/duplicate-one.helper.d.ts +2 -2
- package/src/modules/duplicate-one/duplicate-one.helper.js +3 -2
- package/src/modules/duplicate-one/duplicate-one.module.d.ts +2 -2
- package/src/modules/get-many/get-many-controller.mixin.js +3 -3
- package/src/modules/get-many/get-many.helper.d.ts +2 -2
- package/src/modules/get-many/get-many.helper.js +3 -2
- package/src/modules/get-many/get-many.module.d.ts +2 -2
- package/src/modules/get-one/get-one-controller.mixin.js +4 -4
- package/src/modules/get-one/get-one.helper.d.ts +2 -2
- package/src/modules/get-one/get-one.helper.js +3 -2
- package/src/modules/get-one/get-one.module.d.ts +2 -2
- package/src/modules/replace-one/replace-one-controller.mixin.js +4 -4
- package/src/modules/replace-one/replace-one.helper.d.ts +2 -2
- package/src/modules/replace-one/replace-one.helper.js +3 -2
- package/src/modules/replace-one/replace-one.module.d.ts +2 -2
- package/src/modules/update-many/base-update-many.service.d.ts +33 -0
- package/src/modules/update-many/base-update-many.service.js +32 -0
- package/src/modules/update-many/index.d.ts +6 -0
- package/src/modules/update-many/index.js +22 -0
- package/src/modules/update-many/update-many-controller.interface.d.ts +7 -0
- package/src/modules/update-many/update-many-controller.interface.js +2 -0
- package/src/modules/update-many/update-many-controller.mixin.d.ts +6 -0
- package/src/modules/update-many/update-many-controller.mixin.js +63 -0
- package/src/modules/update-many/update-many-service.interface.d.ts +5 -0
- package/src/modules/update-many/update-many-service.interface.js +2 -0
- package/src/modules/update-many/update-many.helper.d.ts +7 -0
- package/src/modules/update-many/update-many.helper.js +69 -0
- package/src/modules/update-many/update-many.module.d.ts +6 -0
- package/src/modules/update-many/update-many.module.js +28 -0
- package/src/modules/update-one/update-one-controller.mixin.js +4 -4
- package/src/modules/update-one/update-one.helper.d.ts +2 -2
- package/src/modules/update-one/update-one.helper.js +3 -2
- package/src/modules/update-one/update-one.module.d.ts +2 -2
- package/src/version.json +1 -1
- package/tsconfig.tsbuildinfo +1 -1
- /package/src/interfaces/{entity-mappers.interface.js → controller-options.interface.js} +0 -0
- /package/src/interfaces/{route-config.interface.js → dynamic-api-cache-options.interface.js} +0 -0
- /package/src/interfaces/{entity-mappers.interface.d.ts → dynamic-api-entity-mappers.interface.d.ts} +0 -0
- /package/src/interfaces/{service-provider.interface.js → dynamic-api-entity-mappers.interface.js} +0 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
Changelog
|
|
2
2
|
|
|
3
|
+
## [1.3.1](https://github.com/MikeDev75015/mongodb-dynamic-api/compare/v1.3.0...v1.3.1) (2024-03-05)
|
|
4
|
+
|
|
5
|
+
## [1.3.0](https://github.com/MikeDev75015/mongodb-dynamic-api/compare/v1.2.1...v1.3.0) (2024-03-04)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### api
|
|
9
|
+
|
|
10
|
+
* **api:** add delete many route ([43a89cb](https://github.com/MikeDev75015/mongodb-dynamic-api/commit/43a89cb4c087c468743559950fa232ff6e10c140))
|
|
11
|
+
* **api:** add duplicate many route ([be33d1b](https://github.com/MikeDev75015/mongodb-dynamic-api/commit/be33d1b221d81c5ff6649cd2eccc331cb58459b9))
|
|
12
|
+
* **api:** add update many route ([7543eb9](https://github.com/MikeDev75015/mongodb-dynamic-api/commit/7543eb92d7ba538f6d46b69ec194a6c44daff5b2))
|
|
13
|
+
|
|
3
14
|
## [1.2.1](https://github.com/MikeDev75015/mongodb-dynamic-api/compare/v1.2.0...v1.2.1) (2024-03-04)
|
|
4
15
|
|
|
5
16
|
|
package/README.md
CHANGED
|
@@ -100,7 +100,7 @@ import { DynamicApiModule } from 'mongodb-dynamic-api';
|
|
|
100
100
|
})
|
|
101
101
|
export class AppModule {}
|
|
102
102
|
```
|
|
103
|
-
**Basic
|
|
103
|
+
**Basic Usage**
|
|
104
104
|
|
|
105
105
|
- 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.
|
|
106
106
|
- We use the `@Schema` and `@Prop` decorators from the `@nestjs/mongoose` package to define our MongoDB model.
|
|
@@ -241,8 +241,7 @@ The `enableDynamicAPIValidation` function allow to configure the pipe validation
|
|
|
241
241
|
|
|
242
242
|
You can also define the pipe validation options in the `DynamicApiModule.forFeature` method, either in the controller options,
|
|
243
243
|
or in each route object defined in the routes property.
|
|
244
|
-
|
|
245
|
-
*If the options are specified in 2, the options specified in the route will have priority.*
|
|
244
|
+
<br>*If the options are specified in 2, the options specified in the route will have priority.*
|
|
246
245
|
|
|
247
246
|
```typescript
|
|
248
247
|
// users.module.ts
|
|
@@ -308,6 +307,11 @@ ___
|
|
|
308
307
|
### [Versioning](https://docs.nestjs.com/techniques/versioning) (optional)
|
|
309
308
|
`function enableDynamicAPIVersioning(app: INestApplication, options?: VersioningOptions): void`
|
|
310
309
|
|
|
310
|
+
The `enableDynamicAPIVersioning` function will automatically add versioning to the API.
|
|
311
|
+
<br>By default, it will use the <strong>URI versioning type</strong>.
|
|
312
|
+
<br>This method can be called with a second <strong>optional parameter</strong> to specify custom options.
|
|
313
|
+
<br>*See <strong>nestjs</strong> <a href="https://docs.nestjs.com/techniques/versioning" target="_blank">documentation</a> for more details.*
|
|
314
|
+
|
|
311
315
|
**Configuration**
|
|
312
316
|
|
|
313
317
|
```typescript
|
|
@@ -323,16 +327,12 @@ async function bootstrap() {
|
|
|
323
327
|
}
|
|
324
328
|
```
|
|
325
329
|
|
|
326
|
-
The `enableDynamicAPIVersioning` function will automatically add versioning to the API.
|
|
327
|
-
<br>By default, it will use the <strong>URI versioning type</strong>.
|
|
328
|
-
<br>This method can be called with a second <strong>optional parameter</strong> to specify custom options.
|
|
329
|
-
<br>*See <strong>nestjs</strong> <a href="https://docs.nestjs.com/techniques/versioning" target="_blank">documentation</a> for more details.*
|
|
330
|
-
|
|
331
330
|
**Usage**
|
|
332
331
|
|
|
333
332
|
Pass the `version` property to the `controllerOptions` object or to the `route` object in the `DynamicApiModule.forFeature` method.
|
|
333
|
+
<br>*If the version is specified in 2, the version specified in the route will have priority.*
|
|
334
334
|
|
|
335
|
-
|
|
335
|
+
Let's add a new version to the `User` content.
|
|
336
336
|
|
|
337
337
|
```typescript
|
|
338
338
|
// create-one-user-v2.dto.ts
|
|
@@ -406,12 +406,91 @@ import { UserV2Presenter } from './user-v2.presenter';
|
|
|
406
406
|
export class UsersModule {}
|
|
407
407
|
```
|
|
408
408
|
|
|
409
|
-
Great, now you have a versioned User API, and you can access it at the `/v1/users` and `/v2/users`
|
|
409
|
+
Great, now you have a versioned User API, and you can access it at the `/v1/users` and `/v2/users` paths.
|
|
410
410
|
|
|
411
411
|

|
|
412
412
|
|
|
413
413
|
|
|
414
414
|
___
|
|
415
415
|
|
|
416
|
-
|
|
416
|
+
### [Caching](https://docs.nestjs.com/techniques/caching#in-memory-cache) (enabled by default)
|
|
417
|
+
|
|
418
|
+
By default, the caching is activated globally for all the routes. It uses the nestjs built-in in-memory data store with the default options.
|
|
419
|
+
<br>You can configure the cache options by passing the `cacheOptions` in the second parameter of the `DynamicApiModule.forRoot` method.
|
|
420
|
+
|
|
421
|
+
**Configuration**
|
|
422
|
+
|
|
423
|
+
```typescript
|
|
424
|
+
// app.module.ts
|
|
425
|
+
import { DynamicApiModule } from 'mongodb-dynamic-api';
|
|
426
|
+
|
|
427
|
+
@Module({
|
|
428
|
+
imports: [
|
|
429
|
+
DynamicApiModule.forRoot('...', {
|
|
430
|
+
cacheOptions: {
|
|
431
|
+
ttl: 60, // <- The time to live in milliseconds. This is the maximum amount of time that an item can be in the cache before it is removed.
|
|
432
|
+
max: 100, // <- The maximum number of items that can be stored in the cache.
|
|
433
|
+
},
|
|
434
|
+
}),
|
|
435
|
+
// ...
|
|
436
|
+
],
|
|
437
|
+
controllers: [AppController],
|
|
438
|
+
providers: [AppService],
|
|
439
|
+
})
|
|
440
|
+
export class AppModule {}
|
|
441
|
+
```
|
|
442
|
+
|
|
443
|
+
*See <strong>nestjs</strong> <a href="https://docs.nestjs.com/techniques/caching" target="_blank">documentation</a> for more details.*
|
|
444
|
+
|
|
445
|
+
**[Not recommended]** The cache can also be disabled globally with the `useGlobalCache` property set to `false` in the `DynamicApiModule.forRoot` method.
|
|
446
|
+
|
|
447
|
+
```typescript
|
|
448
|
+
// app.module.ts
|
|
449
|
+
import { DynamicApiModule } from 'mongodb-dynamic-api';
|
|
450
|
+
|
|
451
|
+
@Module({
|
|
452
|
+
imports: [
|
|
453
|
+
DynamicApiModule.forRoot('...', {
|
|
454
|
+
useGlobalCache: false, // <- add this line
|
|
455
|
+
}),
|
|
456
|
+
// ...
|
|
457
|
+
],
|
|
458
|
+
controllers: [AppController],
|
|
459
|
+
providers: [AppService],
|
|
460
|
+
})
|
|
461
|
+
export class AppModule {}
|
|
462
|
+
```
|
|
463
|
+
|
|
464
|
+
**Usage**
|
|
465
|
+
|
|
466
|
+
> When you request the `/users` route with the `GET` method, the response will be cached until the cache expires or until the maximum number of items is reached or until a request like `POST`, `PUT`, `PATCH`, or `DELETE` is made on the same route.
|
|
467
|
+
<br><br>Let's inspect the behavior in the network tab of our browser
|
|
468
|
+
<br>We expected to see a code `200` for the first GET request, a code `304`* for the second GET request, and again a code `200` for the third GET request made after the POST request.
|
|
469
|
+
|
|
470
|
+
**The 304 Not Modified redirect response code indicates that there is no need to retransmit the requested resources. This is an implicit redirection to a cached resource.*
|
|
471
|
+
|
|
472
|
+
```text
|
|
473
|
+
1. GET /users
|
|
474
|
+
```
|
|
475
|
+

|
|
476
|
+
|
|
477
|
+
```text
|
|
478
|
+
2. GET /users
|
|
479
|
+
```
|
|
480
|
+

|
|
481
|
+
```text
|
|
482
|
+
3. POST /users
|
|
483
|
+
```
|
|
484
|
+

|
|
485
|
+
```text
|
|
486
|
+
4. GET /users
|
|
487
|
+
```
|
|
488
|
+

|
|
489
|
+
|
|
490
|
+
___
|
|
491
|
+
|
|
492
|
+
More coming soon...
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
|
|
417
496
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mongodb-dynamic-api",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.1",
|
|
4
4
|
"description": "Auto generated CRUD API for MongoDB using NestJS",
|
|
5
5
|
"readmeFilename": "README.md",
|
|
6
6
|
"main": "index.js",
|
|
@@ -38,6 +38,7 @@
|
|
|
38
38
|
},
|
|
39
39
|
"homepage": "https://mikedev75015.github.io",
|
|
40
40
|
"dependencies": {
|
|
41
|
+
"@nestjs/cache-manager": "^2.2.1",
|
|
41
42
|
"@nestjs/common": "^10.3.2",
|
|
42
43
|
"@nestjs/core": "^10.3.2",
|
|
43
44
|
"@nestjs/mongoose": "^10.0.4",
|
|
@@ -45,6 +46,7 @@
|
|
|
45
46
|
"@nestjs/swagger": "^7.3.0",
|
|
46
47
|
"@types/lodash": "^4.14.202",
|
|
47
48
|
"builder-pattern": "^2.2.0",
|
|
49
|
+
"cache-manager": "^5.4.0",
|
|
48
50
|
"class-transformer": "^0.5.1",
|
|
49
51
|
"class-validator": "^0.14.1",
|
|
50
52
|
"lodash": "^4.17.21",
|
|
@@ -11,6 +11,7 @@ declare class RouteDecoratorsBuilder<Entity extends BaseEntity> {
|
|
|
11
11
|
private readonly body?;
|
|
12
12
|
private readonly presenter?;
|
|
13
13
|
private readonly responseRouteTypeIsArray;
|
|
14
|
+
private readonly bodyRouteTypeIsOptional;
|
|
14
15
|
constructor(routeType: RouteType, entity: Type<Entity>, version?: string, description?: string, param?: Type, query?: Type, body?: Type, presenter?: Type);
|
|
15
16
|
build(): MethodDecorator[];
|
|
16
17
|
private getRouteDecorators;
|
|
@@ -17,6 +17,11 @@ class RouteDecoratorsBuilder {
|
|
|
17
17
|
this.responseRouteTypeIsArray = [
|
|
18
18
|
'GetMany',
|
|
19
19
|
'CreateMany',
|
|
20
|
+
'DuplicateMany',
|
|
21
|
+
];
|
|
22
|
+
this.bodyRouteTypeIsOptional = [
|
|
23
|
+
'DuplicateOne',
|
|
24
|
+
'DuplicateMany',
|
|
20
25
|
];
|
|
21
26
|
}
|
|
22
27
|
build() {
|
|
@@ -36,12 +41,18 @@ class RouteDecoratorsBuilder {
|
|
|
36
41
|
return [(0, common_1.Post)('many')];
|
|
37
42
|
case 'CreateOne':
|
|
38
43
|
return [(0, common_1.Post)()];
|
|
44
|
+
case 'UpdateMany':
|
|
45
|
+
return [(0, common_1.Patch)()];
|
|
39
46
|
case 'UpdateOne':
|
|
40
47
|
return [(0, common_1.Patch)(`:${paramKey}`)];
|
|
41
48
|
case 'ReplaceOne':
|
|
42
49
|
return [(0, common_1.Put)(`:${paramKey}`)];
|
|
50
|
+
case 'DuplicateMany':
|
|
51
|
+
return [(0, common_1.Post)(`duplicate`)];
|
|
43
52
|
case 'DuplicateOne':
|
|
44
|
-
return [(0, common_1.Post)(
|
|
53
|
+
return [(0, common_1.Post)(`duplicate/:${paramKey}`)];
|
|
54
|
+
case 'DeleteMany':
|
|
55
|
+
return [(0, common_1.Delete)()];
|
|
45
56
|
case 'DeleteOne':
|
|
46
57
|
return [(0, common_1.Delete)(`:${paramKey}`)];
|
|
47
58
|
default:
|
|
@@ -61,7 +72,7 @@ class RouteDecoratorsBuilder {
|
|
|
61
72
|
}),
|
|
62
73
|
...(this.body ? [(0, swagger_1.ApiBody)({
|
|
63
74
|
type: this.body,
|
|
64
|
-
|
|
75
|
+
required: !this.bodyRouteTypeIsOptional.includes(this.routeType),
|
|
65
76
|
})] : []),
|
|
66
77
|
...(this.param && paramKey
|
|
67
78
|
? [
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.DeleteManyEntityQuery = void 0;
|
|
13
|
+
const swagger_1 = require("@nestjs/swagger");
|
|
14
|
+
const class_validator_1 = require("class-validator");
|
|
15
|
+
class DeleteManyEntityQuery {
|
|
16
|
+
}
|
|
17
|
+
exports.DeleteManyEntityQuery = DeleteManyEntityQuery;
|
|
18
|
+
__decorate([
|
|
19
|
+
(0, swagger_1.ApiProperty)({ type: [String], minItems: 1 }),
|
|
20
|
+
(0, class_validator_1.IsNotEmpty)({ each: true }),
|
|
21
|
+
(0, class_validator_1.IsString)({ each: true }),
|
|
22
|
+
(0, class_validator_1.ArrayMinSize)(1),
|
|
23
|
+
__metadata("design:type", Array)
|
|
24
|
+
], DeleteManyEntityQuery.prototype, "ids", void 0);
|
package/src/dtos/index.d.ts
CHANGED
package/src/dtos/index.js
CHANGED
|
@@ -14,5 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./delete-many-entity.query"), exports);
|
|
17
18
|
__exportStar(require("./entity.param"), exports);
|
|
18
19
|
__exportStar(require("./entity.query"), exports);
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { DynamicModule } from '@nestjs/common';
|
|
2
|
-
import {
|
|
2
|
+
import { DynamicApiForFeatureOptions, DynamicApiForRootOptions } from './interfaces';
|
|
3
3
|
import { BaseEntity } from './models';
|
|
4
4
|
export declare class DynamicApiModule {
|
|
5
5
|
static readonly connectionName = "dynamic-api-connection";
|
|
6
|
-
static
|
|
7
|
-
static
|
|
6
|
+
static isGlobalCacheEnabled: boolean;
|
|
7
|
+
static forRoot(uri: string, { useGlobalCache, cacheOptions }?: DynamicApiForRootOptions): DynamicModule;
|
|
8
|
+
static forFeature<Entity extends BaseEntity>({ entity, controllerOptions: { path, apiTag, version: controllerVersion, validationPipeOptions: controllerValidationPipeOptions, }, routes, }: DynamicApiForFeatureOptions<Entity>): DynamicModule;
|
|
8
9
|
}
|
|
@@ -8,19 +8,28 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
8
8
|
var DynamicApiModule_1;
|
|
9
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
10
|
exports.DynamicApiModule = void 0;
|
|
11
|
+
const cache_manager_1 = require("@nestjs/cache-manager");
|
|
11
12
|
const common_1 = require("@nestjs/common");
|
|
13
|
+
const core_1 = require("@nestjs/core");
|
|
12
14
|
const mongoose_1 = require("@nestjs/mongoose");
|
|
13
15
|
const decorators_1 = require("./decorators");
|
|
14
16
|
const helpers_1 = require("./helpers");
|
|
15
17
|
const modules_1 = require("./modules");
|
|
18
|
+
const delete_many_1 = require("./modules/delete-many");
|
|
19
|
+
const duplicate_many_1 = require("./modules/duplicate-many");
|
|
20
|
+
const update_many_1 = require("./modules/update-many");
|
|
16
21
|
let DynamicApiModule = DynamicApiModule_1 = class DynamicApiModule {
|
|
17
|
-
static forRoot(uri) {
|
|
22
|
+
static forRoot(uri, { useGlobalCache = true, cacheOptions = {} } = {}) {
|
|
18
23
|
if (!uri) {
|
|
19
24
|
throw new Error('You must provide a valid mongodb uri in the forRoot method to use MongoDB Dynamic API');
|
|
20
25
|
}
|
|
26
|
+
if (!useGlobalCache) {
|
|
27
|
+
DynamicApiModule_1.isGlobalCacheEnabled = false;
|
|
28
|
+
}
|
|
21
29
|
return {
|
|
22
30
|
module: DynamicApiModule_1,
|
|
23
31
|
imports: [
|
|
32
|
+
...(useGlobalCache ? [cache_manager_1.CacheModule.register({ isGlobal: true, ...cacheOptions })] : []),
|
|
24
33
|
mongoose_1.MongooseModule.forRoot(uri, { connectionName: DynamicApiModule_1.connectionName }),
|
|
25
34
|
],
|
|
26
35
|
};
|
|
@@ -46,9 +55,12 @@ let DynamicApiModule = DynamicApiModule_1 = class DynamicApiModule {
|
|
|
46
55
|
{ type: 'GetOne' },
|
|
47
56
|
{ type: 'CreateMany' },
|
|
48
57
|
{ type: 'CreateOne' },
|
|
49
|
-
{ type: '
|
|
58
|
+
{ type: 'UpdateMany' },
|
|
50
59
|
{ type: 'UpdateOne' },
|
|
60
|
+
{ type: 'ReplaceOne' },
|
|
61
|
+
{ type: 'DuplicateMany' },
|
|
51
62
|
{ type: 'DuplicateOne' },
|
|
63
|
+
{ type: 'DeleteMany' },
|
|
52
64
|
{ type: 'DeleteOne' },
|
|
53
65
|
];
|
|
54
66
|
}
|
|
@@ -65,9 +77,15 @@ let DynamicApiModule = DynamicApiModule_1 = class DynamicApiModule {
|
|
|
65
77
|
case 'CreateOne':
|
|
66
78
|
module = modules_1.CreateOneModule;
|
|
67
79
|
break;
|
|
80
|
+
case 'DeleteMany':
|
|
81
|
+
module = delete_many_1.DeleteManyModule;
|
|
82
|
+
break;
|
|
68
83
|
case 'DeleteOne':
|
|
69
84
|
module = modules_1.DeleteOneModule;
|
|
70
85
|
break;
|
|
86
|
+
case 'DuplicateMany':
|
|
87
|
+
module = duplicate_many_1.DuplicateManyModule;
|
|
88
|
+
break;
|
|
71
89
|
case 'DuplicateOne':
|
|
72
90
|
module = modules_1.DuplicateOneModule;
|
|
73
91
|
break;
|
|
@@ -80,6 +98,9 @@ let DynamicApiModule = DynamicApiModule_1 = class DynamicApiModule {
|
|
|
80
98
|
case 'ReplaceOne':
|
|
81
99
|
module = modules_1.ReplaceOneModule;
|
|
82
100
|
break;
|
|
101
|
+
case 'UpdateMany':
|
|
102
|
+
module = update_many_1.UpdateManyModule;
|
|
103
|
+
break;
|
|
83
104
|
case 'UpdateOne':
|
|
84
105
|
module = modules_1.UpdateOneModule;
|
|
85
106
|
break;
|
|
@@ -88,18 +109,28 @@ let DynamicApiModule = DynamicApiModule_1 = class DynamicApiModule {
|
|
|
88
109
|
}
|
|
89
110
|
const description = routeDescription ?? (0, helpers_1.getDefaultRouteDescription)(type, entity.name);
|
|
90
111
|
const version = routeVersion ?? controllerVersion;
|
|
91
|
-
if (version
|
|
112
|
+
if (version && !(0, helpers_1.isValidVersion)(version)) {
|
|
92
113
|
throw new Error(`Invalid version ${version} for ${type} route. Version must be a string that matches numeric format, e.g. 1, 2, 3, ..., 99.`);
|
|
93
114
|
}
|
|
94
|
-
|
|
115
|
+
const validationPipeOptions = routeValidationPipeOptions ?? controllerValidationPipeOptions;
|
|
116
|
+
return module.forFeature(databaseModule, entity, { path, apiTag }, { description, dTOs }, version, validationPipeOptions);
|
|
95
117
|
})
|
|
96
118
|
.filter((module) => module),
|
|
97
119
|
],
|
|
120
|
+
providers: [
|
|
121
|
+
...(DynamicApiModule_1.isGlobalCacheEnabled ? [
|
|
122
|
+
{
|
|
123
|
+
provide: core_1.APP_INTERCEPTOR,
|
|
124
|
+
useClass: cache_manager_1.CacheInterceptor,
|
|
125
|
+
},
|
|
126
|
+
] : []),
|
|
127
|
+
],
|
|
98
128
|
};
|
|
99
129
|
}
|
|
100
130
|
};
|
|
101
131
|
exports.DynamicApiModule = DynamicApiModule;
|
|
102
132
|
DynamicApiModule.connectionName = 'dynamic-api-connection';
|
|
133
|
+
DynamicApiModule.isGlobalCacheEnabled = true;
|
|
103
134
|
exports.DynamicApiModule = DynamicApiModule = DynamicApiModule_1 = __decorate([
|
|
104
135
|
(0, common_1.Module)({})
|
|
105
136
|
], DynamicApiModule);
|
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.pascalCase = void 0;
|
|
3
|
+
exports.isValidVersion = exports.pascalCase = void 0;
|
|
4
4
|
const lodash_1 = require("lodash");
|
|
5
5
|
function pascalCase(str) {
|
|
6
6
|
return str ? (0, lodash_1.upperFirst)((0, lodash_1.camelCase)(str)) : undefined;
|
|
7
7
|
}
|
|
8
8
|
exports.pascalCase = pascalCase;
|
|
9
|
+
function isValidVersion(version) {
|
|
10
|
+
return /^\d+$/.test(version);
|
|
11
|
+
}
|
|
12
|
+
exports.isValidVersion = isValidVersion;
|
|
@@ -9,8 +9,12 @@ function getDefaultRouteDescription(routeType, entityName) {
|
|
|
9
9
|
return `Create many ${contentName}`;
|
|
10
10
|
case 'CreateOne':
|
|
11
11
|
return `Create one ${contentName}`;
|
|
12
|
+
case 'DeleteMany':
|
|
13
|
+
return `Delete many ${contentName}`;
|
|
12
14
|
case 'DeleteOne':
|
|
13
15
|
return `Delete one ${contentName}`;
|
|
16
|
+
case 'DuplicateMany':
|
|
17
|
+
return `Duplicate many ${contentName}`;
|
|
14
18
|
case 'DuplicateOne':
|
|
15
19
|
return `Duplicate one ${contentName}`;
|
|
16
20
|
case 'GetMany':
|
|
@@ -19,6 +23,8 @@ function getDefaultRouteDescription(routeType, entityName) {
|
|
|
19
23
|
return `Get one ${contentName} by id`;
|
|
20
24
|
case 'ReplaceOne':
|
|
21
25
|
return `Replace one ${contentName}`;
|
|
26
|
+
case 'UpdateMany':
|
|
27
|
+
return `Update many ${contentName}`;
|
|
22
28
|
case 'UpdateOne':
|
|
23
29
|
return `Update one ${contentName}`;
|
|
24
30
|
default:
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import { INestApplication, VersioningOptions } from '@nestjs/common';
|
|
2
2
|
declare function enableDynamicAPIVersioning(app: INestApplication, options?: VersioningOptions): void;
|
|
3
|
-
|
|
3
|
+
declare function addVersionSuffix(version?: string): string;
|
|
4
|
+
export { addVersionSuffix, enableDynamicAPIVersioning };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.enableDynamicAPIVersioning = void 0;
|
|
3
|
+
exports.enableDynamicAPIVersioning = exports.addVersionSuffix = void 0;
|
|
4
4
|
const common_1 = require("@nestjs/common");
|
|
5
5
|
function enableDynamicAPIVersioning(app, options) {
|
|
6
6
|
app.enableVersioning({
|
|
@@ -9,3 +9,7 @@ function enableDynamicAPIVersioning(app, options) {
|
|
|
9
9
|
});
|
|
10
10
|
}
|
|
11
11
|
exports.enableDynamicAPIVersioning = enableDynamicAPIVersioning;
|
|
12
|
+
function addVersionSuffix(version) {
|
|
13
|
+
return version ? `V${version}` : '';
|
|
14
|
+
}
|
|
15
|
+
exports.addVersionSuffix = addVersionSuffix;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { CacheStore, CacheStoreFactory } from '@nestjs/cache-manager/dist/interfaces/cache-manager.interface';
|
|
2
|
+
interface DynamicApiCacheOptions {
|
|
3
|
+
max?: number;
|
|
4
|
+
ttl?: number;
|
|
5
|
+
store?: string | CacheStoreFactory | CacheStore;
|
|
6
|
+
isCacheableValue?: (value: any) => boolean;
|
|
7
|
+
}
|
|
8
|
+
export { DynamicApiCacheOptions };
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { Type
|
|
1
|
+
import { Type } from '@nestjs/common';
|
|
2
2
|
import { BaseEntity } from '../models';
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+
import { ControllerOptions } from './controller-options.interface';
|
|
4
|
+
import { DynamicApiCacheOptions } from './dynamic-api-cache-options.interface';
|
|
5
|
+
import { DynamicAPIRouteConfig } from './dynamic-api-route-config.interface';
|
|
6
|
+
interface DynamicApiForRootOptions {
|
|
7
|
+
useGlobalCache?: boolean;
|
|
8
|
+
cacheOptions?: DynamicApiCacheOptions;
|
|
9
9
|
}
|
|
10
|
-
interface
|
|
10
|
+
interface DynamicApiForFeatureOptions<Entity extends BaseEntity> {
|
|
11
11
|
entity: Type<Entity>;
|
|
12
12
|
controllerOptions: ControllerOptions;
|
|
13
|
-
routes?:
|
|
13
|
+
routes?: DynamicAPIRouteConfig<Entity>[];
|
|
14
14
|
}
|
|
15
|
-
export {
|
|
15
|
+
export { DynamicApiForFeatureOptions, DynamicApiForRootOptions };
|
package/src/interfaces/{route-config.interface.d.ts → dynamic-api-route-config.interface.d.ts}
RENAMED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
import { Type, ValidationPipeOptions } from '@nestjs/common';
|
|
2
2
|
import { BaseEntity } from '../models';
|
|
3
|
-
type RouteType = '
|
|
3
|
+
type RouteType = 'CreateMany' | 'CreateOne' | 'DeleteMany' | 'DeleteOne' | 'DuplicateMany' | 'DuplicateOne' | 'GetMany' | 'GetOne' | 'ReplaceOne' | 'UpdateMany' | 'UpdateOne';
|
|
4
4
|
type DTOsBundle = {
|
|
5
5
|
query?: Type;
|
|
6
6
|
param?: Type;
|
|
7
7
|
body?: Type;
|
|
8
8
|
presenter?: Type;
|
|
9
9
|
};
|
|
10
|
-
interface
|
|
10
|
+
interface DynamicAPIRouteConfig<Entity extends BaseEntity> {
|
|
11
11
|
type: RouteType;
|
|
12
12
|
description?: string;
|
|
13
13
|
version?: string;
|
|
14
14
|
dTOs?: DTOsBundle;
|
|
15
15
|
validationPipeOptions?: ValidationPipeOptions;
|
|
16
16
|
}
|
|
17
|
-
export { DTOsBundle, RouteType,
|
|
17
|
+
export { DTOsBundle, RouteType, DynamicAPIRouteConfig };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
export * from './
|
|
1
|
+
export * from './controller-options.interface';
|
|
2
|
+
export * from './dynamic-api-entity-mappers.interface';
|
|
2
3
|
export * from './dynamic-api-options.interface';
|
|
3
4
|
export * from './dynamic-api-schema-options.interface';
|
|
4
5
|
export * from './dynamic-api-swagger-options.type';
|
|
5
|
-
export * from './route-config.interface';
|
|
6
|
-
export * from './service-provider.interface';
|
|
6
|
+
export * from './dynamic-api-route-config.interface';
|
|
7
|
+
export * from './dynamic-api-service-provider.interface';
|
package/src/interfaces/index.js
CHANGED
|
@@ -14,9 +14,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./
|
|
17
|
+
__exportStar(require("./controller-options.interface"), exports);
|
|
18
|
+
__exportStar(require("./dynamic-api-entity-mappers.interface"), exports);
|
|
18
19
|
__exportStar(require("./dynamic-api-options.interface"), exports);
|
|
19
20
|
__exportStar(require("./dynamic-api-schema-options.interface"), exports);
|
|
20
21
|
__exportStar(require("./dynamic-api-swagger-options.type"), exports);
|
|
21
|
-
__exportStar(require("./route-config.interface"), exports);
|
|
22
|
-
__exportStar(require("./service-provider.interface"), exports);
|
|
22
|
+
__exportStar(require("./dynamic-api-route-config.interface"), exports);
|
|
23
|
+
__exportStar(require("./dynamic-api-service-provider.interface"), exports);
|
|
@@ -26,7 +26,7 @@ function CreateManyControllerMixin(entity, path, apiTag, version, description, D
|
|
|
26
26
|
class DtoBody extends (0, mixins_1.EntityBodyMixin)(entity) {
|
|
27
27
|
}
|
|
28
28
|
Object.defineProperty(DtoBody, 'name', {
|
|
29
|
-
value: `${displayedName}${
|
|
29
|
+
value: `${displayedName}${(0, helpers_1.addVersionSuffix)(version)}Dto`,
|
|
30
30
|
writable: false,
|
|
31
31
|
});
|
|
32
32
|
class CreateManyBody {
|
|
@@ -43,7 +43,7 @@ function CreateManyControllerMixin(entity, path, apiTag, version, description, D
|
|
|
43
43
|
}
|
|
44
44
|
if (!CustomBody) {
|
|
45
45
|
Object.defineProperty(RouteBody, 'name', {
|
|
46
|
-
value: `CreateMany${displayedName}${
|
|
46
|
+
value: `CreateMany${displayedName}${(0, helpers_1.addVersionSuffix)(version)}Dto`,
|
|
47
47
|
writable: false,
|
|
48
48
|
});
|
|
49
49
|
}
|
|
@@ -51,7 +51,7 @@ function CreateManyControllerMixin(entity, path, apiTag, version, description, D
|
|
|
51
51
|
}
|
|
52
52
|
if (!CustomPresenter) {
|
|
53
53
|
Object.defineProperty(RoutePresenter, 'name', {
|
|
54
|
-
value: `${displayedName}${
|
|
54
|
+
value: `${displayedName}${(0, helpers_1.addVersionSuffix)(version)}Presenter`,
|
|
55
55
|
writable: false,
|
|
56
56
|
});
|
|
57
57
|
}
|
|
@@ -73,7 +73,7 @@ function CreateManyControllerMixin(entity, path, apiTag, version, description, D
|
|
|
73
73
|
__metadata("design:returntype", Promise)
|
|
74
74
|
], BaseCreateManyController.prototype, "createMany", null);
|
|
75
75
|
Object.defineProperty(BaseCreateManyController, 'name', {
|
|
76
|
-
value: `BaseCreateMany${entity.name}${
|
|
76
|
+
value: `BaseCreateMany${entity.name}${(0, helpers_1.addVersionSuffix)(version)}Controller`,
|
|
77
77
|
writable: false,
|
|
78
78
|
});
|
|
79
79
|
return BaseCreateManyController;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Type, ValidationPipeOptions } from '@nestjs/common';
|
|
2
|
-
import { DTOsBundle,
|
|
2
|
+
import { DTOsBundle, DynamicAPIServiceProvider } from '../../interfaces';
|
|
3
3
|
import { BaseEntity } from '../../models';
|
|
4
4
|
import { CreateManyControllerConstructor } from './create-many-controller.interface';
|
|
5
|
-
declare function createCreateManyServiceProvider<Entity extends BaseEntity>(entity: Type<Entity>, version: string | undefined):
|
|
5
|
+
declare function createCreateManyServiceProvider<Entity extends BaseEntity>(entity: Type<Entity>, version: string | undefined): DynamicAPIServiceProvider;
|
|
6
6
|
declare function createCreateManyController<Entity extends BaseEntity>(entity: Type<Entity>, path: string, apiTag?: string, version?: string, description?: string, DTOs?: DTOsBundle, validationPipeOptions?: ValidationPipeOptions): CreateManyControllerConstructor<Entity>;
|
|
7
7
|
export { createCreateManyController, createCreateManyServiceProvider };
|
|
@@ -18,10 +18,11 @@ const mongoose_1 = require("@nestjs/mongoose");
|
|
|
18
18
|
const swagger_1 = require("@nestjs/swagger");
|
|
19
19
|
const mongoose_2 = require("mongoose");
|
|
20
20
|
const dynamic_api_module_1 = require("../../dynamic-api.module");
|
|
21
|
+
const helpers_1 = require("../../helpers");
|
|
21
22
|
const base_create_many_service_1 = require("./base-create-many.service");
|
|
22
23
|
const create_many_controller_mixin_1 = require("./create-many-controller.mixin");
|
|
23
24
|
function provideServiceName(entityName, version) {
|
|
24
|
-
return `CreateMany${entityName}${
|
|
25
|
+
return `CreateMany${entityName}${(0, helpers_1.addVersionSuffix)(version)}Service`;
|
|
25
26
|
}
|
|
26
27
|
function createCreateManyServiceProvider(entity, version) {
|
|
27
28
|
let CreateManyService = class CreateManyService extends base_create_many_service_1.BaseCreateManyService {
|
|
@@ -60,7 +61,7 @@ function createCreateManyController(entity, path, apiTag, version, description,
|
|
|
60
61
|
__metadata("design:paramtypes", [Object])
|
|
61
62
|
], CreateManyController);
|
|
62
63
|
Object.defineProperty(CreateManyController, 'name', {
|
|
63
|
-
value: `CreateMany${entity.name}${
|
|
64
|
+
value: `CreateMany${entity.name}${(0, helpers_1.addVersionSuffix)(version)}Controller`,
|
|
64
65
|
writable: false,
|
|
65
66
|
});
|
|
66
67
|
return CreateManyController;
|