From 87a38bf0a0ef326bd16b21c05aa0d250aa740aa5 Mon Sep 17 00:00:00 2001 From: Kai Haase Date: Sun, 17 Dec 2023 18:14:46 +0100 Subject: [PATCH] FindOne methods in CrudService integrated --- package-lock.json | 4 +- package.json | 2 +- spectaql.yml | 2 +- .../check-security.interceptor.ts | 2 +- src/core/common/services/crud.service.ts | 63 +++++++++++++++++++ 5 files changed, 68 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index f521b7e..9b50c13 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@lenne.tech/nest-server", - "version": "10.2.5", + "version": "10.2.6", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@lenne.tech/nest-server", - "version": "10.2.5", + "version": "10.2.6", "license": "MIT", "dependencies": { "@apollo/gateway": "2.5.7", diff --git a/package.json b/package.json index 1194cd8..4846b01 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@lenne.tech/nest-server", - "version": "10.2.5", + "version": "10.2.6", "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", diff --git a/spectaql.yml b/spectaql.yml index 99636a8..c49708d 100644 --- a/spectaql.yml +++ b/spectaql.yml @@ -11,7 +11,7 @@ servers: info: title: lT Nest Server 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). - version: 10.2.5 + version: 10.2.6 contact: name: lenne.Tech GmbH url: https://lenne.tech diff --git a/src/core/common/interceptors/check-security.interceptor.ts b/src/core/common/interceptors/check-security.interceptor.ts index 6ec6172..fd48d3a 100644 --- a/src/core/common/interceptors/check-security.interceptor.ts +++ b/src/core/common/interceptors/check-security.interceptor.ts @@ -12,7 +12,7 @@ import { processDeep } from '../helpers/input.helper'; export class CheckSecurityInterceptor implements NestInterceptor { intercept(context: ExecutionContext, next: CallHandler): Observable { // Get current user - const user = getContextData(context)?.currentUser; + const user = getContextData(context)?.currentUser || null; // Set force mode for sign in and sign up let force = false; diff --git a/src/core/common/services/crud.service.ts b/src/core/common/services/crud.service.ts index b55d9a0..61c2024 100644 --- a/src/core/common/services/crud.service.ts +++ b/src/core/common/services/crud.service.ts @@ -311,6 +311,69 @@ export abstract class CrudService< return this.findAndUpdateForce(filter, update, serviceOptions); } + /** + * Find one item via filter + */ + async findOne( + filter?: FilterArgs | { filterQuery?: FilterQuery; queryOptions?: QueryOptions }, + serviceOptions?: ServiceOptions, + ): Promise { + // If filter is not instance of FilterArgs a simple form with filterQuery and queryOptions is set + // and should not be processed as FilterArgs + if (!(filter instanceof FilterArgs) && serviceOptions?.inputType === FilterArgs) { + serviceOptions = Object.assign({ prepareInput: null }, serviceOptions, { inputType: null }); + } + + return this.process( + async (data) => { + + // Prepare filter query + const filterQuery = { filterQuery: data?.input?.filterQuery, queryOptions: data?.input?.queryOptions }; + if (data?.input instanceof FilterArgs) { + const converted = convertFilterArgsToQuery(data.input); + filterQuery.filterQuery = converted[0]; + filterQuery.queryOptions = converted[1]; + } + + // Find in DB + let find = this.mainDbModel.findOne(filterQuery.filterQuery, null, filterQuery.queryOptions); + const collation = serviceOptions?.collation || ConfigService.get('mongoose.collation'); + if (collation) { + find = find.collation(collation); + } + return find.exec(); + }, + { input: filter, serviceOptions }, + ); + } + + /** + * Find one item via filter without checks or restrictions + * Warning: Disables the handling of rights and restrictions! + */ + async findOneForce( + filter?: FilterArgs | { filterQuery?: FilterQuery; queryOptions?: QueryOptions; samples?: number }, + serviceOptions: ServiceOptions = {}, + ): Promise { + serviceOptions = serviceOptions || {}; + serviceOptions.force = true; + return this.findOne(filter, serviceOptions); + } + + /** + * Find one item via filter without checks, restrictions or preparations + * Warning: Disables the handling of rights and restrictions! The raw data may contain secrets (such as passwords). + */ + async findOneRaw( + filter?: FilterArgs | { filterQuery?: FilterQuery; queryOptions?: QueryOptions; samples?: number }, + serviceOptions: ServiceOptions = {}, + ): Promise { + serviceOptions = serviceOptions || {}; + serviceOptions.prepareInput = null; + serviceOptions.prepareOutput = null; + return this.findOneForce(filter, serviceOptions); + } + /** * CRUD alias for get */