mongodb-dynamic-api 2.0.0 → 2.1.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,14 @@
1
1
  Changelog
2
2
 
3
+ ## [2.1.1](https://github.com/MikeDev75015/mongodb-dynamic-api/compare/v2.1.0...v2.1.1) (2024-03-21)
4
+
5
+ ## [2.1.0](https://github.com/MikeDev75015/mongodb-dynamic-api/compare/v2.0.0...v2.1.0) (2024-03-20)
6
+
7
+
8
+ ### schema
9
+
10
+ * **schema:** add the possibility to customize initialization ([7fd541b](https://github.com/MikeDev75015/mongodb-dynamic-api/commit/7fd541b055bb0baa60409651a54d42244d8f3042))
11
+
3
12
  ## [2.0.0](https://github.com/MikeDev75015/mongodb-dynamic-api/compare/v1.4.3...v2.0.0) (2024-03-18)
4
13
 
5
14
 
package/README.md CHANGED
@@ -63,7 +63,7 @@ npm install --save mongodb-dynamic-api
63
63
 
64
64
  <p style="text-align: justify; width: 100%;font-size: 15px;">
65
65
 
66
- In summary, DynamicApiModule is a flexible and configurable module using NestJS 10 that provides dynamic API functionality.
66
+ In summary, DynamicApiModule is a flexible and configurable module using NestJS 10 that provides dynamic API functionality for your contents.
67
67
  <br>It must be set up at the root level with global settings and then configured for individual features.
68
68
  <br>It has several optional features such as
69
69
  [Swagger UI](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README/swagger-ui.md),
@@ -113,11 +113,11 @@ export class AppModule {}
113
113
  - 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.
114
114
  - 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.
115
115
 
116
- - You must extend the `BaseEntity` | `SoftDeletableEntity` class from the `mongodb-dynamic-api` package **for all your collection models**.
117
- **See more details [here](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README/entities.md)**.
116
+ - You must extend the `BaseEntity` | `SoftDeletableEntity` class from the `mongodb-dynamic-api` package for all your **collection models**.
117
+ See more details **[here](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README/entities.md)**.
118
118
 
119
119
  - You can also add the `@DynamicAPISchemaOptions` decorator to pass schema options.
120
- **See more details [here](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README/schema-options.md)**.
120
+ See more details **[here](https://github.com/MikeDev75015/mongodb-dynamic-api/blob/develop/README/schema-options.md)**.
121
121
 
122
122
  Just create a new file `user.ts` and add the following code.
123
123
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mongodb-dynamic-api",
3
- "version": "2.0.0",
3
+ "version": "2.1.1",
4
4
  "description": "Auto generated CRUD API for MongoDB using NestJS",
5
5
  "readmeFilename": "README.md",
6
6
  "main": "index.js",
@@ -5,7 +5,7 @@ const mongoose_1 = require("@nestjs/mongoose");
5
5
  const decorators_1 = require("../decorators");
6
6
  const interfaces_1 = require("../interfaces");
7
7
  function buildSchemaFromEntity(entity) {
8
- const { indexes, hooks } = Reflect.getOwnMetadata(decorators_1.DYNAMIC_API_SCHEMA_OPTIONS_METADATA, entity) ?? {};
8
+ const { indexes, hooks, customInit } = Reflect.getOwnMetadata(decorators_1.DYNAMIC_API_SCHEMA_OPTIONS_METADATA, entity) ?? {};
9
9
  const schema = mongoose_1.SchemaFactory.createForClass(entity);
10
10
  if (Object.getOwnPropertyNames(schema.paths).includes('createdAt')) {
11
11
  schema.set('timestamps', true);
@@ -22,6 +22,9 @@ function buildSchemaFromEntity(entity) {
22
22
  schema[method](isSoftDeletable && softDeletableQuery ? softDeletableQuery : query, { document: true, query: true, ...options }, callback);
23
23
  });
24
24
  }
25
+ if (customInit) {
26
+ customInit(schema);
27
+ }
25
28
  return schema;
26
29
  }
27
30
  exports.buildSchemaFromEntity = buildSchemaFromEntity;
@@ -22,7 +22,7 @@
22
22
  /// <reference types="mongoose/types/validation" />
23
23
  /// <reference types="mongoose/types/virtuals" />
24
24
  /// <reference types="mongoose/types/inferschematype" />
25
- import { IndexDefinition, IndexOptions } from 'mongoose';
25
+ import { IndexDefinition, IndexOptions, Schema } from 'mongoose';
26
26
  import { RouteType } from './dynamic-api-route-type.type';
27
27
  type HookEvent = RouteType;
28
28
  type MongoDBQuery = 'deleteMany' | 'deleteOne' | 'find' | 'findOne' | 'findOneAndReplace' | 'findOneAndUpdate' | 'save' | 'updateMany' | 'updateOne';
@@ -45,5 +45,6 @@ interface DynamicAPISchemaOptionsInterface {
45
45
  options?: IndexOptions;
46
46
  }[];
47
47
  hooks?: SchemaHook[];
48
+ customInit?: (schema: Schema) => void;
48
49
  }
49
50
  export type { SchemaHook, DynamicAPISchemaOptionsInterface };
@@ -14,7 +14,7 @@ class BaseAuthService extends services_1.BaseService {
14
14
  }
15
15
  async validateUser(login, pass) {
16
16
  const user = (await this.model.findOne({ [this.loginField]: login }).lean().exec());
17
- if (!user || !await this.bcryptService.compare(pass, user[this.passwordField])) {
17
+ if (!user || !await this.bcryptService.comparePassword(pass, user[this.passwordField])) {
18
18
  return null;
19
19
  }
20
20
  const fieldsToBuild = [
@@ -37,7 +37,7 @@ class BaseAuthService extends services_1.BaseService {
37
37
  };
38
38
  }
39
39
  async register(userToCreate) {
40
- const hashedPassword = await this.bcryptService.hash(userToCreate[this.passwordField]);
40
+ const hashedPassword = await this.bcryptService.hashPassword(userToCreate[this.passwordField]);
41
41
  const { _id } = await this.model.create({ ...userToCreate, [this.passwordField]: hashedPassword });
42
42
  const user = await this.getUserById(_id.toString());
43
43
  return this.login(user);
@@ -52,7 +52,7 @@ class BaseAuthService extends services_1.BaseService {
52
52
  return this.buildInstance(fieldsToBuild.reduce((acc, field) => (user[field] !== undefined ? { ...acc, [field]: user[field] } : acc), {}));
53
53
  }
54
54
  async changePassword(userId, newPassword) {
55
- const hashedPassword = await this.bcryptService.hash(newPassword);
55
+ const hashedPassword = await this.bcryptService.hashPassword(newPassword);
56
56
  const { _id } = await this.model.findOneAndUpdate({ _id: userId }, { [this.passwordField]: hashedPassword }, { new: true });
57
57
  const user = await this.getUserById(_id.toString());
58
58
  return this.login(user);
@@ -1,5 +1,5 @@
1
1
  export declare class BcryptService {
2
2
  private readonly saltOrRounds;
3
- hash(password: string): Promise<string>;
4
- compare(password: string, hash: string): Promise<boolean>;
3
+ hashPassword(password: string): Promise<string>;
4
+ comparePassword(password: string, hash: string): Promise<boolean>;
5
5
  }
@@ -13,10 +13,10 @@ let BcryptService = class BcryptService {
13
13
  constructor() {
14
14
  this.saltOrRounds = 10;
15
15
  }
16
- async hash(password) {
16
+ async hashPassword(password) {
17
17
  return bcrypt.hash(password, this.saltOrRounds);
18
18
  }
19
- async compare(password, hash) {
19
+ async comparePassword(password, hash) {
20
20
  return bcrypt.compare(password, hash);
21
21
  }
22
22
  };
package/src/version.json CHANGED
@@ -1,3 +1,3 @@
1
1
  {
2
- "version": "2.0.0"
2
+ "version": "2.1.1"
3
3
  }