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

Jaybell/crud mongoose query params #509

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor(mongoose): decouple mongoose from typeORM
- create DeepPartial type in utils
- use DeepPartial and ObjectLiteral from utils instead of from typeORM
  • Loading branch information
yharaskrik committed May 17, 2020
commit 27647c8225c930755145be9faec6b152c18aac06
26 changes: 24 additions & 2 deletions packages/crud-mongoose/src/mongoose-crud.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,42 @@ import {
QuerySort,
} from '@nestjsx/crud-request';
import {
DeepPartial,
hasLength,
isArrayFull,
isNil,
isObject,
isObjectFull,
isUndefined,
ObjectLiteral,
objKeys,
} from '@nestjsx/util';
/**
* mongoose imports
*/
import { Document, DocumentQuery, Model, Schema, Types } from 'mongoose';
import { MONGOOSE_OPERATOR_MAP } from 'nest-crud-mongoose/mongoose-operator-map';
import { DeepPartial, ObjectLiteral } from 'typeorm';

const MONGOOSE_OPERATOR_MAP: { [key: string]: (value?: any) => any } = {
$eq: (value) => ({
$eq: value,
}),
$ne: (value) => ({
$ne: value,
}),
$gt: (value) => ({ $gt: value }),
$lt: (value) => ({ $lt: value }),
$gte: (value) => ({ $gte: value }),
$lte: (value) => ({ $lte: value }),
$in: (value) => ({ $in: value }),
$notin: (value) => ({ $nin: value }),
$isnull: () => ({ $eq: null }),
$notnull: () => ({ $ne: null }),
$between: (value: any[]) => ({ $gt: Math.min(...value), $lt: Math.max(...value) }),
$starts: (value) => ({ $regex: `/^${value}.*$/` }),
$end: (value) => ({ $regex: `/^.*${value}$/` }),
$cont: (value) => ({ $regex: `/^.*${value}.*$/` }),
$excl: (value) => ({ $regex: `/^((?!${value}).)*$/` }),
};

/**
* Required so that ObjectIds are serialized correctly
Expand Down
21 changes: 0 additions & 21 deletions packages/crud-mongoose/src/mongoose-operator-map.ts

This file was deleted.

10 changes: 10 additions & 0 deletions packages/util/src/types/deep-partial.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Same as Partial<T> but goes deeper and makes Partial<T> all its properties and sub-properties.
*/
export declare type DeepPartial<T> = {
[P in keyof T]?: T[P] extends Array<infer U>
? Array<DeepPartial<U>>
: T[P] extends ReadonlyArray<infer U>
? ReadonlyArray<DeepPartial<U>>
: DeepPartial<T[P]>;
};
1 change: 1 addition & 0 deletions packages/util/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './object-literal.type';
export * from './deep-partial';