Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FilterMerge helper #330

Merged
merged 1 commit into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 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.0.11",
"version": "10.1.0",
"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
1 change: 1 addition & 0 deletions src/config.env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const config: { [env: string]: IServerOptions } = {
},
},
},
hostname: 'localhost',
ignoreSelectionsForPopulate: true,
jwt: {
// Each secret should be unique and not reused in other environments,
Expand Down
39 changes: 34 additions & 5 deletions src/core/common/helpers/filter.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,11 @@ export function generateFilterQuery<T = any>(
case ComparisonOperatorEnum.REGEX:
result[field] = not
? {
$not: {
$regex: new RegExp(value),
$options: options || '',
},
}
$not: {
$regex: new RegExp(value),
$options: options || '',
},
}
: { $regex: new RegExp(value), $options: options || '' };
break;
}
Expand Down Expand Up @@ -265,3 +265,32 @@ export function generateFindOptions(

return queryOptions;
}

/**
* Merge FilterArgs and FilterQueries
*/
export function filterMerge<T = unknown>(
filterArgs: Partial<FilterArgs>,
filterQuery: Partial<FilterQuery<T>>,
options?: {
operator?: '$and' | '$or' | '$nor';
queryOptions?: Partial<QueryOptions>;
samples?: number;
},
): { filterQuery: FilterQuery<T>; queryOptions?: QueryOptions; samples?: number } {
const config = {
operator: '$and',
...options,
};
const converted = convertFilterArgsToQuery(filterArgs);
return {
filterQuery: (converted[0]
? { [config.operator]: [converted[0], filterQuery || {}] }
: filterQuery
) as FilterQuery<T>,
queryOptions: converted[1] || config.queryOptions
? { ...converted[1], ...config.queryOptions }
: undefined,
samples: config.samples,
};
}
6 changes: 6 additions & 0 deletions src/core/common/interfaces/server-options.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,12 @@ export interface IServerOptions {
};
};

/**
* Hostname of the server
* default: localhost
*/
hostname?: string;

/**
* Ignore selections in fieldSelection
* [ConfigService must be integrated in ModuleService]
Expand Down
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ async function bootstrap() {
server.enableCors();

// Start server on configured port
await server.listen(envConfig.port);
await server.listen(envConfig.port, envConfig.hostname);
console.debug(`Server startet at ${await server.getUrl()}`);

// Run command after server init
if (envConfig.execAfterInit) {
Expand Down
Loading