Skip to content

Commit

Permalink
Merge pull request #346 from lenneTech/feat/find-one
Browse files Browse the repository at this point in the history
FindOne methods in CrudService integrated
  • Loading branch information
kaihaase authored Dec 17, 2023
2 parents d90d7fe + 87a38bf commit 5a5407b
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 5 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": "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",
Expand Down
2 changes: 1 addition & 1 deletion spectaql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/core/common/interceptors/check-security.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { processDeep } from '../helpers/input.helper';
export class CheckSecurityInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
// 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;
Expand Down
63 changes: 63 additions & 0 deletions src/core/common/services/crud.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>; queryOptions?: QueryOptions },
serviceOptions?: ServiceOptions,
): Promise<Model> {
// 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<any>; queryOptions?: QueryOptions; samples?: number },
serviceOptions: ServiceOptions = {},
): Promise<Model> {
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<any>; queryOptions?: QueryOptions; samples?: number },
serviceOptions: ServiceOptions = {},
): Promise<Model> {
serviceOptions = serviceOptions || {};
serviceOptions.prepareInput = null;
serviceOptions.prepareOutput = null;
return this.findOneForce(filter, serviceOptions);
}

/**
* CRUD alias for get
*/
Expand Down

0 comments on commit 5a5407b

Please sign in to comment.