@ewb/mongoose-query 1.0.0 → 1.0.1
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/README.md +24 -35
- package/lib/Query.d.ts +60 -77
- package/lib/Query.js +227 -307
- package/lib/QueryError.d.ts +16 -50
- package/lib/QueryError.js +52 -62
- package/lib/QueryHandler.d.ts +47 -59
- package/lib/QueryHandler.js +250 -297
- package/lib/QueryHistory.d.ts +75 -76
- package/lib/QueryHistory.js +137 -154
- package/lib/QueryModel.d.ts +65 -80
- package/lib/QueryModel.js +214 -201
- package/lib/index.d.ts +9 -10
- package/lib/index.js +6 -32
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/utils/Query.utils.d.ts +49 -0
- package/lib/utils/Query.utils.js +143 -0
- package/lib/{constants.d.ts → utils/constants.d.ts} +2 -2
- package/lib/utils/constants.js +2 -0
- package/lib/utils/types.d.ts +11 -0
- package/lib/utils/types.js +1 -0
- package/lib/utils/utils.d.ts +8 -0
- package/lib/{utils.js → utils/utils.js} +38 -44
- package/package.json +4 -4
- package/lib/ExpressHandler.d.ts +0 -30
- package/lib/ExpressHandler.js +0 -215
- package/lib/ExpressHandler.js.map +0 -1
- package/lib/ModelQuery.d.ts +0 -24
- package/lib/ModelQuery.js +0 -80
- package/lib/ModelQuery.js.map +0 -1
- package/lib/Query.js.map +0 -1
- package/lib/QueryError.js.map +0 -1
- package/lib/QueryExpressHandler.d.ts +0 -50
- package/lib/QueryExpressHandler.js +0 -383
- package/lib/QueryExpressHandler.js.map +0 -1
- package/lib/QueryExpressSubHandler.d.ts +0 -20
- package/lib/QueryExpressSubHandler.js +0 -137
- package/lib/QueryExpressSubHandler.js.map +0 -1
- package/lib/QueryHandler.js.map +0 -1
- package/lib/QueryHistory.js.map +0 -1
- package/lib/QueryModel.js.map +0 -1
- package/lib/constants.js +0 -6
- package/lib/constants.js.map +0 -1
- package/lib/declarations.d.ts +0 -16
- package/lib/declarations.js +0 -3
- package/lib/declarations.js.map +0 -1
- package/lib/error/ExpressError.d.ts +0 -44
- package/lib/error/ExpressError.js +0 -39
- package/lib/error/ExpressError.js.map +0 -1
- package/lib/error/QueryError.d.ts +0 -47
- package/lib/error/QueryError.js +0 -50
- package/lib/error/QueryError.js.map +0 -1
- package/lib/error/QueryExpressError.d.ts +0 -46
- package/lib/error/QueryExpressError.js +0 -39
- package/lib/error/QueryExpressError.js.map +0 -1
- package/lib/error/V1Error.d.ts +0 -12
- package/lib/error/V1Error.js +0 -27
- package/lib/error/V1Error.js.map +0 -1
- package/lib/error/error.handler.d.ts +0 -3
- package/lib/error/error.handler.js +0 -30
- package/lib/error/error.handler.js.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/statics.d.ts +0 -17
- package/lib/statics.js +0 -35
- package/lib/statics.js.map +0 -1
- package/lib/utils.d.ts +0 -3
- package/lib/utils.js.map +0 -1
package/lib/QueryError.js
CHANGED
|
@@ -1,62 +1,52 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
(
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
if (
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
return 440;
|
|
54
|
-
case QueryErrorCode.NotImplemented:
|
|
55
|
-
return 501;
|
|
56
|
-
default:
|
|
57
|
-
return 500;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
exports.QueryError = QueryError;
|
|
62
|
-
//# sourceMappingURL=QueryError.js.map
|
|
1
|
+
import mongoose from 'mongoose';
|
|
2
|
+
import mongodb from 'mongodb';
|
|
3
|
+
import httpStatus from 'http-status';
|
|
4
|
+
export class QueryError extends Error {
|
|
5
|
+
constructor(status, message, opts) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.status = status;
|
|
8
|
+
this.detail = opts?.detail;
|
|
9
|
+
this.errors = opts?.errors;
|
|
10
|
+
}
|
|
11
|
+
static catch(e) {
|
|
12
|
+
if (e instanceof QueryError) {
|
|
13
|
+
return e;
|
|
14
|
+
}
|
|
15
|
+
const [status, { message, ...opts }] = QueryError.getData(e);
|
|
16
|
+
console.error(e);
|
|
17
|
+
return new QueryError(status, message, opts);
|
|
18
|
+
}
|
|
19
|
+
static getData(error) {
|
|
20
|
+
if (error instanceof mongodb.MongoServerError) {
|
|
21
|
+
if (error.code === 11000) {
|
|
22
|
+
return [
|
|
23
|
+
httpStatus.CONFLICT,
|
|
24
|
+
{
|
|
25
|
+
message: 'Duplicate document',
|
|
26
|
+
detail: error.message,
|
|
27
|
+
},
|
|
28
|
+
];
|
|
29
|
+
}
|
|
30
|
+
return [
|
|
31
|
+
httpStatus.INTERNAL_SERVER_ERROR,
|
|
32
|
+
{
|
|
33
|
+
message: 'Database error',
|
|
34
|
+
detail: error.message,
|
|
35
|
+
},
|
|
36
|
+
];
|
|
37
|
+
}
|
|
38
|
+
if (error instanceof mongoose.Error.ValidationError) {
|
|
39
|
+
const opts = {
|
|
40
|
+
error,
|
|
41
|
+
};
|
|
42
|
+
if (error.errors && Object.keys(error.errors).length > 0) {
|
|
43
|
+
opts.errors = Object.values(error.errors).map((x) => ({
|
|
44
|
+
title: x.message,
|
|
45
|
+
error: x,
|
|
46
|
+
}));
|
|
47
|
+
}
|
|
48
|
+
return [httpStatus.BAD_REQUEST, opts];
|
|
49
|
+
}
|
|
50
|
+
return [httpStatus.INTERNAL_SERVER_ERROR, {}];
|
|
51
|
+
}
|
|
52
|
+
}
|
package/lib/QueryHandler.d.ts
CHANGED
|
@@ -1,59 +1,47 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
declare
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}>;
|
|
49
|
-
subFindOne<K extends keyof T>(sub: K, query: Query<T>, subQuery: FilterQuery<T[K]>): Promise<SubDocItem<T, K>>;
|
|
50
|
-
subFindOneAndUpdate<K extends keyof T>(sub: K, query: Query<T>, subQuery: FilterQuery<T[K]>, body: T[K]): Promise<(T[K] extends [] ? T[K][0] : null) | null>;
|
|
51
|
-
subDelete<K extends keyof T>(sub: K, query: Query<T>, subQuery: FilterQuery<T[K]>): Promise<(T[K] extends [] ? T[K][0] : null) | null>;
|
|
52
|
-
subPopulate<R extends Request, SD, K extends keyof T>(req: R, subDoc: SD, sub: K, type?: keyof ModelSearchPopulate<T, K>): Promise<void[]>;
|
|
53
|
-
getSubPopulate<K extends keyof T>(sub: K, type: keyof ModelSearchPopulate<T, K>): ModelSearchPopulate<T[keyof T], keyof T[keyof T]>[];
|
|
54
|
-
private getSubDocument;
|
|
55
|
-
validateBody(body: Partial<LeanDocument<T>>): string[] | null;
|
|
56
|
-
toJSON(_: unknown, ret: T): void;
|
|
57
|
-
private populateDoc;
|
|
58
|
-
}
|
|
59
|
-
export {};
|
|
1
|
+
import mongoose from 'mongoose';
|
|
2
|
+
import { Query } from './Query';
|
|
3
|
+
import { ModelSearchPopulate, QueryModel } from './QueryModel';
|
|
4
|
+
export interface QueryFindOpts {
|
|
5
|
+
populate?: boolean;
|
|
6
|
+
select?: boolean;
|
|
7
|
+
}
|
|
8
|
+
type SubDocArray<T extends object, K extends keyof T> = T[K][];
|
|
9
|
+
type SubDocItem<T extends object, K extends keyof T> = (T[K] extends [] ? T[K][0] : null) | null;
|
|
10
|
+
export declare class QueryHandler<T extends object> {
|
|
11
|
+
private _model;
|
|
12
|
+
private queryModel;
|
|
13
|
+
constructor(_model: mongoose.Model<T>, queryModel: QueryModel<T>);
|
|
14
|
+
get model(): mongoose.Model<T, {}, {}, {}, mongoose.IfAny<T, any, mongoose.Document<unknown, {}, T> & Omit<mongoose.Require_id<T>, never>>, any>;
|
|
15
|
+
create<B extends Partial<T>>(body: B): Promise<mongoose.IfAny<T, any, mongoose.Document<unknown, {}, T> & Omit<mongoose.Require_id<T>, never>>>;
|
|
16
|
+
json<D extends mongoose.Document>(doc: D): Promise<any>;
|
|
17
|
+
search(query: Query<T>): Promise<{
|
|
18
|
+
data: T[];
|
|
19
|
+
count: number;
|
|
20
|
+
limit: number;
|
|
21
|
+
skip: number;
|
|
22
|
+
}>;
|
|
23
|
+
findOne(query: Query<T>, opts?: QueryFindOpts): Promise<mongoose.IfAny<T, any, mongoose.Document<unknown, {}, T> & Omit<mongoose.Require_id<T>, never>>>;
|
|
24
|
+
findOneAndUpdate(query: Query<T>, filter: mongoose.FilterQuery<T>, update: mongoose.UpdateQuery<T>): Promise<mongoose.IfAny<T, any, mongoose.Document<unknown, {}, T> & Omit<mongoose.Require_id<T>, never>>>;
|
|
25
|
+
deleteOne(filter: mongoose.FilterQuery<T>): Promise<mongoose.mongo.DeleteResult>;
|
|
26
|
+
populate(query: Query<T>, doc: T | T[any], type?: keyof ModelSearchPopulate<T, any>): Promise<void>;
|
|
27
|
+
getPopulates(type: keyof ModelSearchPopulate<T, any>): ModelSearchPopulate<T, keyof T>[];
|
|
28
|
+
updateMany(ids: mongoose.Types.ObjectId[], body: Partial<T>): Promise<mongoose.UpdateWriteOpResult>;
|
|
29
|
+
deleteMany(ids: mongoose.Types.ObjectId[]): Promise<mongoose.mongo.DeleteResult>;
|
|
30
|
+
subCreate<K extends keyof T>(sub: K, query: Query<T>, body: any): Promise<(T[K] extends [] ? T[K][0] : null) | null>;
|
|
31
|
+
subSearch<K extends keyof T>(sub: K, query: Query<T>, rootQuery: mongoose.FilterQuery<T>): Promise<{
|
|
32
|
+
count: number;
|
|
33
|
+
limit: number;
|
|
34
|
+
skip: number;
|
|
35
|
+
data: SubDocArray<T, K>;
|
|
36
|
+
}>;
|
|
37
|
+
subFindOne<K extends keyof T>(sub: K, query: Query<T>, subQuery: mongoose.FilterQuery<T[K]>): Promise<SubDocItem<T, K>>;
|
|
38
|
+
subFindOneAndUpdate<K extends keyof T>(sub: K, query: Query<T>, subQuery: mongoose.FilterQuery<T[K]>, body: T[K]): Promise<(T[K] extends [] ? T[K][0] : null) | null>;
|
|
39
|
+
subDelete<K extends keyof T>(sub: K, query: Query<T>, subQuery: mongoose.FilterQuery<T[K]>): Promise<(T[K] extends [] ? T[K][0] : null) | null>;
|
|
40
|
+
subPopulate<R extends Request, SD, K extends keyof T>(query: Query<T>, subDoc: SD, sub: K, type?: keyof ModelSearchPopulate<T, K>): Promise<void[]>;
|
|
41
|
+
getSubPopulate<K extends keyof T>(sub: K, type: keyof ModelSearchPopulate<T, K>): ModelSearchPopulate<T[keyof T], keyof T[keyof T]>[];
|
|
42
|
+
private getSubDocument;
|
|
43
|
+
validateBody(body: Partial<T>): any[];
|
|
44
|
+
toJSON(_: unknown, ret: T): void;
|
|
45
|
+
private populateDoc;
|
|
46
|
+
}
|
|
47
|
+
export {};
|