@ewb/mongoose-query 1.1.1 → 1.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/Query.d.ts +2 -3
- package/lib/Query.js +7 -5
- package/lib/QueryModel.d.ts +0 -1
- package/lib/QueryModel.js +0 -14
- package/lib/QueryOptions.d.ts +0 -6
- package/lib/QueryOptions.js +0 -9
- package/lib/utils/types.d.ts +0 -2
- package/package.json +1 -1
package/lib/Query.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import mongoose from 'mongoose';
|
|
2
2
|
import { IQueryPopulate } from './QueryOptions.js';
|
|
3
|
-
import { IMongooseQueryOptions, IQueryOptions, MongooseSortSelectValue, QueryType
|
|
3
|
+
import { IMongooseQueryOptions, IQueryOptions, MongooseSortSelectValue, QueryType } from './utils/types.js';
|
|
4
4
|
export declare class Query<R extends object, T extends object = object, Q extends IMongooseQueryOptions = IMongooseQueryOptions> {
|
|
5
5
|
private _root;
|
|
6
6
|
private _rootNext;
|
|
@@ -48,7 +48,7 @@ export declare class Query<R extends object, T extends object = object, Q extend
|
|
|
48
48
|
addToModelQuery<TT extends {
|
|
49
49
|
[key: string]: object;
|
|
50
50
|
}, K extends keyof TT = keyof TT>(key: K | K[], query: QueryType<TT[K]>): void;
|
|
51
|
-
createQuery({ param, query, noRoot,
|
|
51
|
+
createQuery({ param, query, noRoot, }?: ICreateQueryOptions<R, T>): QueryType<T>;
|
|
52
52
|
private initQuery;
|
|
53
53
|
private mongooseTextSearch;
|
|
54
54
|
private mongooseQueryWithOperations;
|
|
@@ -58,6 +58,5 @@ interface ICreateQueryOptions<R, T extends object> {
|
|
|
58
58
|
param?: keyof R;
|
|
59
59
|
query?: (keyof T)[];
|
|
60
60
|
noRoot?: boolean;
|
|
61
|
-
validate?: QueryValidateFn;
|
|
62
61
|
}
|
|
63
62
|
export {};
|
package/lib/Query.js
CHANGED
|
@@ -147,11 +147,11 @@ export class Query {
|
|
|
147
147
|
}
|
|
148
148
|
}
|
|
149
149
|
}
|
|
150
|
-
createQuery({ param, query, noRoot,
|
|
150
|
+
createQuery({ param, query, noRoot, } = {}) {
|
|
151
151
|
if (param) {
|
|
152
152
|
this.model = param;
|
|
153
153
|
}
|
|
154
|
-
const _query = this.initQuery(
|
|
154
|
+
const _query = this.initQuery();
|
|
155
155
|
const queryArray = [];
|
|
156
156
|
const root = this.root;
|
|
157
157
|
if (!noRoot && root && Object.keys(root).length > 0) {
|
|
@@ -182,8 +182,8 @@ export class Query {
|
|
|
182
182
|
}
|
|
183
183
|
return mQuery || {};
|
|
184
184
|
}
|
|
185
|
-
initQuery(
|
|
186
|
-
const query =
|
|
185
|
+
initQuery() {
|
|
186
|
+
const query = Object.assign({}, this.documentQuery);
|
|
187
187
|
const operations = this.mongooseQueryWithOperations(query);
|
|
188
188
|
const sanitized = this.sanitizedQuery(query);
|
|
189
189
|
return { ...operations, ...sanitized, ...this.textQuery, ...this._query };
|
|
@@ -204,11 +204,13 @@ export class Query {
|
|
|
204
204
|
for (const key in query) {
|
|
205
205
|
if (query.hasOwnProperty(key)) {
|
|
206
206
|
const str = String(query[key]);
|
|
207
|
+
if (!str)
|
|
208
|
+
continue;
|
|
207
209
|
if (key[0] === '$') {
|
|
208
210
|
if (typeof query[key] !== 'string')
|
|
209
211
|
continue;
|
|
210
212
|
const { field, value } = mongooseQueryWithOperation(operationQuery, key, str);
|
|
211
|
-
if (field) {
|
|
213
|
+
if (field && value) {
|
|
212
214
|
operationQuery[field] = value;
|
|
213
215
|
}
|
|
214
216
|
delete query[key];
|
package/lib/QueryModel.d.ts
CHANGED
|
@@ -40,7 +40,6 @@ export declare class QueryModel<T extends object> {
|
|
|
40
40
|
subPopulate<R extends Request, SD, K extends keyof T>(query: Query<T>, subDoc: SD, sub: K, type?: keyof IQueryOptionsPopulate<T, K>): Promise<void[]>;
|
|
41
41
|
getSubPopulate<K extends keyof T>(sub: K, type: keyof IQueryOptionsPopulate<T, K>): IQueryOptionsPopulate<T[keyof T], keyof T[keyof T]>[];
|
|
42
42
|
private getSubDocument;
|
|
43
|
-
validateBody(body: Partial<T>): any[];
|
|
44
43
|
toJSON(_: unknown, ret: T): void;
|
|
45
44
|
private populateDoc;
|
|
46
45
|
}
|
package/lib/QueryModel.js
CHANGED
|
@@ -100,7 +100,6 @@ export class QueryModel {
|
|
|
100
100
|
}
|
|
101
101
|
const subQuery = query.createQuery({
|
|
102
102
|
noRoot: true,
|
|
103
|
-
validate: subOptions.validateQuery,
|
|
104
103
|
});
|
|
105
104
|
const populate = query.populate;
|
|
106
105
|
const limit = query.limit;
|
|
@@ -205,19 +204,6 @@ export class QueryModel {
|
|
|
205
204
|
}
|
|
206
205
|
return null;
|
|
207
206
|
}
|
|
208
|
-
validateBody(body) {
|
|
209
|
-
const invalidFields = [];
|
|
210
|
-
if (this.options.publicFields) {
|
|
211
|
-
for (const key in body) {
|
|
212
|
-
if (body.hasOwnProperty(key)) {
|
|
213
|
-
if (!this.options.publicFields.includes(key)) {
|
|
214
|
-
invalidFields.push(key);
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
return invalidFields.length > 0 ? invalidFields : null;
|
|
220
|
-
}
|
|
221
207
|
toJSON(_, ret) {
|
|
222
208
|
if (this.options.privateFields) {
|
|
223
209
|
for (const key of this.options.privateFields) {
|
package/lib/QueryOptions.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import mongoose from 'mongoose';
|
|
2
2
|
import { Query } from './Query.js';
|
|
3
|
-
import { IMongooseQueryOptions, QueryValidateFn } from './utils/index.js';
|
|
4
3
|
export interface IQueryOptionsPopulate<T extends any, K extends keyof T> {
|
|
5
4
|
path: K;
|
|
6
5
|
select: (T[K] extends object ? keyof T[K] : string)[];
|
|
@@ -16,10 +15,8 @@ export interface IQueryOptionsPopulate<T extends any, K extends keyof T> {
|
|
|
16
15
|
interface IQuerySearchOptionsSub<T extends object, K extends keyof T> {
|
|
17
16
|
sub: K;
|
|
18
17
|
populate?: IQueryOptionsPopulate<T[K], keyof T[K]>[];
|
|
19
|
-
validateQuery?: QueryValidateFn;
|
|
20
18
|
}
|
|
21
19
|
interface IQueryOptions<T extends object> {
|
|
22
|
-
editFields?: (keyof T)[];
|
|
23
20
|
privateFields?: (keyof T)[];
|
|
24
21
|
query?: (keyof T)[];
|
|
25
22
|
select?: (keyof T)[];
|
|
@@ -29,7 +26,6 @@ interface IQueryOptions<T extends object> {
|
|
|
29
26
|
limit?: number;
|
|
30
27
|
subs?: IQuerySearchOptionsSub<T, keyof T>[];
|
|
31
28
|
updateDateKey?: keyof T;
|
|
32
|
-
validateQuery?: QueryValidateFn;
|
|
33
29
|
}
|
|
34
30
|
export interface IQueryPopulate {
|
|
35
31
|
path: string;
|
|
@@ -42,14 +38,12 @@ export declare const toValidTextRegexp: (str: string) => RegExp | "";
|
|
|
42
38
|
export declare class QueryOptions<T extends object> {
|
|
43
39
|
private opts;
|
|
44
40
|
constructor(opts?: IQueryOptions<T>);
|
|
45
|
-
get publicFields(): (keyof T)[];
|
|
46
41
|
get privateFields(): (keyof T)[];
|
|
47
42
|
get populate(): IQueryOptionsPopulate<T, keyof T>[];
|
|
48
43
|
get subs(): IQuerySearchOptionsSub<T, keyof T>[];
|
|
49
44
|
get updateDateKey(): keyof T;
|
|
50
45
|
get query(): (keyof T)[];
|
|
51
46
|
createSearchQuery(query: Query<any, T>): mongoose.FilterQuery<T>;
|
|
52
|
-
validateQuery(query: IMongooseQueryOptions): any;
|
|
53
47
|
setPopulate(modelQuery: IModelQuery, query: Query<T>): void;
|
|
54
48
|
getValidPopulate(queryPopulate: IQueryPopulate[], sub?: keyof T): IQueryOptionsPopulate<any, any>[];
|
|
55
49
|
setLimitAndSkip(modelQuery: IModelQuery, query: Query<T>): void;
|
package/lib/QueryOptions.js
CHANGED
|
@@ -9,9 +9,6 @@ export class QueryOptions {
|
|
|
9
9
|
constructor(opts = {}) {
|
|
10
10
|
this.opts = opts;
|
|
11
11
|
}
|
|
12
|
-
get publicFields() {
|
|
13
|
-
return this.opts.editFields;
|
|
14
|
-
}
|
|
15
12
|
get privateFields() {
|
|
16
13
|
return this.opts.privateFields;
|
|
17
14
|
}
|
|
@@ -31,14 +28,8 @@ export class QueryOptions {
|
|
|
31
28
|
return query.createQuery({
|
|
32
29
|
noRoot: false,
|
|
33
30
|
query: this.opts.query,
|
|
34
|
-
validate: this.validateQuery,
|
|
35
31
|
});
|
|
36
32
|
}
|
|
37
|
-
validateQuery(query) {
|
|
38
|
-
if (this.opts.validateQuery)
|
|
39
|
-
return this.opts.validateQuery(query);
|
|
40
|
-
return true;
|
|
41
|
-
}
|
|
42
33
|
setPopulate(modelQuery, query) {
|
|
43
34
|
let populate = [];
|
|
44
35
|
if (query.populate.length > 0 && this.opts.populate) {
|
package/lib/utils/types.d.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import mongoose from 'mongoose';
|
|
2
2
|
export type MongooseSortSelectValue<T extends object, K extends keyof T = keyof T> = K extends string ? `-${K}` | K : keyof T;
|
|
3
3
|
export type QueryType<T extends object> = mongoose.FilterQuery<T>;
|
|
4
|
-
export type QueryActions = 'create' | 'query' | 'update' | 'updateMany';
|
|
5
|
-
export type QueryValidateFn = (query: any) => any;
|
|
6
4
|
export type IQueryAddToModelQuery<R extends object, T extends object> = {
|
|
7
5
|
[key in keyof R]?: QueryType<T>;
|
|
8
6
|
};
|
package/package.json
CHANGED