Skip to content

Commit

Permalink
Merge pull request #254 from lenneTech/develop
Browse files Browse the repository at this point in the history
Release 9.0.22
  • Loading branch information
kaihaase authored Dec 23, 2022
2 parents b473fe9 + 00be44f commit 1734e62
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 12 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lenne.tech/nest-server",
"version": "9.0.21",
"version": "9.0.22",
"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).",
"keywords": [
"node",
Expand Down
31 changes: 25 additions & 6 deletions src/core/common/helpers/input.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -648,11 +648,22 @@ export function instanceofArray(arr: any[], strict = false): string {

/**
* Process data via function deep
* @param data
* @param func
* @param processedObjects
*/
export function processDeep(data: any, func: (data: any) => any, processedObjects = new WeakMap()): any {
export function processDeep(
data: any,
func: (data: any) => any,
options?: {
processedObjects?: WeakMap<new () => any, boolean>;
specialClasses?: ((new (args: any[]) => any) | string)[];
}
): any {
// Set options
const { processedObjects, specialClasses } = {
processedObjects: new WeakMap(),
specialClasses: [],
...options,
};

// Prevent circular processing
if (typeof data === 'object') {
if (processedObjects.get(data)) {
Expand All @@ -663,13 +674,21 @@ export function processDeep(data: any, func: (data: any) => any, processedObject

// Process array
if (Array.isArray(data)) {
return data.map((item) => processDeep(item, func));
return data.map((item) => processDeep(item, func, { processedObjects, specialClasses }));
}

// Process object
if (typeof data === 'object') {
for (const specialClass of specialClasses) {
if (
(typeof specialClass === 'string' && specialClass === data.constructor.name) ||
(typeof specialClass !== 'string' && data instanceof specialClass)
) {
return func(data);
}
}
for (const [key, value] of Object.entries(data)) {
data[key] = processDeep(value, func, processedObjects);
data[key] = processDeep(value, func, { processedObjects, specialClasses });
}
return data;
}
Expand Down
19 changes: 18 additions & 1 deletion src/core/common/helpers/service.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { PrepareOutputOptions } from '../interfaces/prepare-output-options.inter
import { ResolveSelector } from '../interfaces/resolve-selector.interface';
import { ServiceOptions } from '../interfaces/service-options.interface';
import { ConfigService } from '../services/config.service';
import { clone } from './input.helper';
import { getStringIds } from './db.helper';
import { clone, processDeep } from './input.helper';

/**
* Helper class for services
Expand Down Expand Up @@ -66,6 +67,7 @@ export async function prepareInput<T = any>(
[key: string]: any;
checkRoles?: boolean;
circles?: boolean;
convertObjectIdsToString?: boolean;
create?: boolean;
clone?: boolean;
getNewArray?: boolean;
Expand All @@ -80,6 +82,7 @@ export async function prepareInput<T = any>(
checkRoles: false,
clone: false,
circles: false,
convertObjectIdsToString: true,
create: false,
getNewArray: false,
proto: false,
Expand Down Expand Up @@ -114,6 +117,20 @@ export async function prepareInput<T = any>(
}
}

// Convert ObjectIds to string
if (config.convertObjectIdsToString) {
input = processDeep(
input,
(property) => {
if (property instanceof Types.ObjectId) {
property = getStringIds(property);
}
return property;
},
{ specialClasses: ['ObjectId'] }
);
}

// Map input if target model exist
if (config.targetModel && !(input instanceof config.targetModel)) {
if ((config.targetModel as any)?.map) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
export interface PrepareInputOptions {
[key: string]: any;
checkRoles?: boolean;
convertObjectIdsToString?: boolean;
create?: boolean;
clone?: boolean;
getNewArray?: boolean;
Expand Down
4 changes: 2 additions & 2 deletions src/core/common/services/module.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export abstract class ModuleService<T extends CoreModel = any> {
if (!opts.targetModel && config.inputType) {
opts.targetModel = config.inputType;
}
config.input = await this.prepareInput(config.input, opts);
config.input = await this.prepareInput(config.input, config);
}

// Get DB object
Expand Down Expand Up @@ -156,7 +156,7 @@ export abstract class ModuleService<T extends CoreModel = any> {
if (config.outputPath) {
_.set(result, config.outputPath, await this.prepareOutput(_.get(result, config.outputPath), opts));
} else {
result = await this.prepareOutput(result, opts);
result = await this.prepareOutput(result, config);
}
}

Expand Down

0 comments on commit 1734e62

Please sign in to comment.