@lenne.tech/nest-server 10.2.0 → 10.2.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lenne.tech/nest-server",
3
- "version": "10.2.0",
3
+ "version": "10.2.1",
4
4
  "description": "Modern, fast, powerful Node.js web framework in TypeScript based on Nest with a GraphQL API and a connection to MongoDB (or other databases).",
5
5
  "keywords": [
6
6
  "node",
@@ -505,8 +505,13 @@ export async function popAndMap<T extends CoreModel>(
505
505
 
506
506
  // Map result
507
507
  if (Array.isArray(result)) {
508
- result = result.map(item => (modelClass as any).map(item));
509
- } else {
508
+ result = result.map((item) => {
509
+ if (item && typeof item === 'object') {
510
+ return (modelClass as any).map(item);
511
+ }
512
+ return item;
513
+ });
514
+ } else if (result && typeof result === 'object') {
510
515
  result = (modelClass as any).map(result);
511
516
  }
512
517
 
@@ -514,12 +519,19 @@ export async function popAndMap<T extends CoreModel>(
514
519
  } else {
515
520
  if (Array.isArray(queryOrDocument)) {
516
521
  await setPopulates(queryOrDocument, populateOptions, mongooseModel, { ignoreSelections });
517
- result = queryOrDocument.map(item => (modelClass as any).map(item));
522
+ result = queryOrDocument.map((item) => {
523
+ if (item && typeof item === 'object') {
524
+ return (modelClass as any).map(item);
525
+ }
526
+ return item;
527
+ });
518
528
 
519
529
  // Process document
520
530
  } else {
521
531
  await setPopulates(queryOrDocument, populateOptions, mongooseModel, { ignoreSelections });
522
- result = (modelClass as any).map(queryOrDocument);
532
+ if (queryOrDocument && typeof queryOrDocument === 'object') {
533
+ result = (modelClass as any).map(queryOrDocument);
534
+ }
523
535
  }
524
536
  }
525
537
 
@@ -262,7 +262,7 @@ export async function check(
262
262
  }
263
263
 
264
264
  // Return value if it is only a basic type
265
- if (typeof value !== 'object') {
265
+ if (!value || typeof value !== 'object') {
266
266
  return value;
267
267
  }
268
268
 
@@ -92,7 +92,7 @@ export async function prepareInput<T = any>(
92
92
  };
93
93
 
94
94
  // Check input
95
- if (typeof input !== 'object') {
95
+ if (!input || typeof input !== 'object') {
96
96
  return input;
97
97
  }
98
98
 
@@ -206,7 +206,7 @@ export async function prepareOutput<T = { [key: string]: any; map: (...args: any
206
206
  };
207
207
 
208
208
  // Check output
209
- if (typeof output !== 'object') {
209
+ if (!output || typeof output !== 'object') {
210
210
  return output;
211
211
  }
212
212
 
@@ -8,7 +8,7 @@ export class MapAndValidatePipe implements PipeTransform {
8
8
  async transform(value: any, metadata: ArgumentMetadata) {
9
9
  const { metatype } = metadata;
10
10
 
11
- if (typeof value !== 'object' || !metatype || isBasicType(metatype)) {
11
+ if (!value || typeof value !== 'object' || !metatype || isBasicType(metatype)) {
12
12
  return value;
13
13
  }
14
14
 
@@ -1,13 +1,11 @@
1
1
  import { NotFoundException } from '@nestjs/common';
2
- import { Document, FilterQuery, Model as MongooseModel, PipelineStage, Query, QueryOptions } from 'mongoose';
2
+ import { Document, FilterQuery, PipelineStage, Query, QueryOptions } from 'mongoose';
3
3
  import { FilterArgs } from '../args/filter.args';
4
- import { getStringIds, popAndMap } from '../helpers/db.helper';
4
+ import { getStringIds } from '../helpers/db.helper';
5
5
  import { convertFilterArgsToQuery } from '../helpers/filter.helper';
6
6
  import { mergePlain, prepareServiceOptionsForCreate } from '../helpers/input.helper';
7
7
  import { ServiceOptions } from '../interfaces/service-options.interface';
8
8
  import { CoreModel } from '../models/core-model.model';
9
- import { ArrayElement } from '../types/array-element.type';
10
- import { FieldSelection } from '../types/field-selection.type';
11
9
  import { PlainObject } from '../types/plain-object.type';
12
10
  import { ConfigService } from './config.service';
13
11
  import { ModuleService } from './module.service';
@@ -478,38 +476,21 @@ export abstract class CrudService<
478
476
  }
479
477
 
480
478
  /**
481
- * Populate, exec and map Mongoose query or document(s) with serviceOptions
479
+ * Execute, populate and map Mongoose query or document(s) with serviceOptions
482
480
  * Generic T is the type of the returned object(s)
483
481
  *
484
482
  * @example const user = await this.populateAndProcessQuery<User>(User.findById(id), serviceOptions);
485
- * @example const users = await this.populateAndProcessQuery<User[]>(User.find({name: {'$regex': 'ma'}}), serviceOptions);
483
+ * @example const users = await this.populateAndProcessQuery<User[]>(User.find({name: {'$regex': 'ma'}}), {...serviceOptions, populate:['contacts'], force: true});
486
484
  */
487
- async populateAndProcessQuery<T extends CoreModel | CoreModel[] = CoreModel>(
485
+ async processQueryOrDocument<T extends CoreModel | CoreModel[] = CoreModel>(
488
486
  queryOrDocument: Query<unknown, unknown> | Document | Document[],
489
- options?: {
490
- fieldSelection?: FieldSelection;
491
- ignoreSelections?: boolean;
492
- modelClass?: new (...args: any[]) => ArrayElement<T>;
493
- populate?: FieldSelection;
494
- mongooseModel?: MongooseModel<any>;
495
- },
487
+ serviceOptions?: ServiceOptions,
496
488
  ): Promise<T> {
497
- const config = {
498
- modelClass: this.mainModelConstructor,
499
- mongooseModel: this.mainDbModel,
500
- ...options,
501
- };
502
- let result: T;
503
- if (config.fieldSelection || config.populate) {
504
- result = (await popAndMap<ArrayElement<T>>(
505
- queryOrDocument,
506
- config.populate || config.fieldSelection,
507
- config.modelClass as new (...args: any[]) => ArrayElement<T>,
508
- config.mongooseModel)
509
- ) as T;
510
- } else if (queryOrDocument instanceof Query) {
511
- result = (await queryOrDocument.exec()) as T;
512
- }
513
- return result;
489
+ return this.process(() => {
490
+ if (queryOrDocument instanceof Query) {
491
+ return queryOrDocument.exec();
492
+ }
493
+ return queryOrDocument;
494
+ }, { serviceOptions });
514
495
  }
515
496
  }
@@ -77,7 +77,7 @@ export class CoreAuthController {
77
77
  // Check if cookie handling is activated
78
78
  if (this.configService.getFastButReadOnly('cookies')) {
79
79
  // Set cookies
80
- if (typeof result !== 'object') {
80
+ if (!result || typeof result !== 'object') {
81
81
  res.cookie('token', '', { httpOnly: true });
82
82
  res.cookie('refreshToken', '', { httpOnly: true });
83
83
  return result;
@@ -96,7 +96,7 @@ export class CoreAuthResolver {
96
96
  // Check if cookie handling is activated
97
97
  if (this.configService.getFastButReadOnly('cookies')) {
98
98
  // Set cookies
99
- if (typeof result !== 'object') {
99
+ if (!result || typeof result !== 'object') {
100
100
  ctx.res.cookie('token', '', { httpOnly: true });
101
101
  ctx.res.cookie('refreshToken', '', { httpOnly: true });
102
102
  return result;