@ewb/mongoose-query 1.0.0 → 1.0.2

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.
Files changed (66) hide show
  1. package/README.md +24 -35
  2. package/lib/Query.d.ts +60 -77
  3. package/lib/Query.js +227 -307
  4. package/lib/QueryError.d.ts +16 -50
  5. package/lib/QueryError.js +52 -62
  6. package/lib/QueryHandler.d.ts +47 -59
  7. package/lib/QueryHandler.js +250 -297
  8. package/lib/QueryHistory.d.ts +75 -76
  9. package/lib/QueryHistory.js +137 -154
  10. package/lib/QueryModel.d.ts +65 -80
  11. package/lib/QueryModel.js +214 -201
  12. package/lib/index.d.ts +11 -10
  13. package/lib/index.js +8 -32
  14. package/lib/tsconfig.tsbuildinfo +1 -1
  15. package/lib/utils/Query.utils.d.ts +49 -0
  16. package/lib/utils/Query.utils.js +143 -0
  17. package/lib/{constants.d.ts → utils/constants.d.ts} +1 -2
  18. package/lib/utils/constants.js +1 -0
  19. package/lib/utils/types.d.ts +11 -0
  20. package/lib/utils/types.js +1 -0
  21. package/lib/utils/utils.d.ts +8 -0
  22. package/lib/{utils.js → utils/utils.js} +38 -44
  23. package/package.json +4 -4
  24. package/lib/ExpressHandler.d.ts +0 -30
  25. package/lib/ExpressHandler.js +0 -215
  26. package/lib/ExpressHandler.js.map +0 -1
  27. package/lib/ModelQuery.d.ts +0 -24
  28. package/lib/ModelQuery.js +0 -80
  29. package/lib/ModelQuery.js.map +0 -1
  30. package/lib/Query.js.map +0 -1
  31. package/lib/QueryError.js.map +0 -1
  32. package/lib/QueryExpressHandler.d.ts +0 -50
  33. package/lib/QueryExpressHandler.js +0 -383
  34. package/lib/QueryExpressHandler.js.map +0 -1
  35. package/lib/QueryExpressSubHandler.d.ts +0 -20
  36. package/lib/QueryExpressSubHandler.js +0 -137
  37. package/lib/QueryExpressSubHandler.js.map +0 -1
  38. package/lib/QueryHandler.js.map +0 -1
  39. package/lib/QueryHistory.js.map +0 -1
  40. package/lib/QueryModel.js.map +0 -1
  41. package/lib/constants.js +0 -6
  42. package/lib/constants.js.map +0 -1
  43. package/lib/declarations.d.ts +0 -16
  44. package/lib/declarations.js +0 -3
  45. package/lib/declarations.js.map +0 -1
  46. package/lib/error/ExpressError.d.ts +0 -44
  47. package/lib/error/ExpressError.js +0 -39
  48. package/lib/error/ExpressError.js.map +0 -1
  49. package/lib/error/QueryError.d.ts +0 -47
  50. package/lib/error/QueryError.js +0 -50
  51. package/lib/error/QueryError.js.map +0 -1
  52. package/lib/error/QueryExpressError.d.ts +0 -46
  53. package/lib/error/QueryExpressError.js +0 -39
  54. package/lib/error/QueryExpressError.js.map +0 -1
  55. package/lib/error/V1Error.d.ts +0 -12
  56. package/lib/error/V1Error.js +0 -27
  57. package/lib/error/V1Error.js.map +0 -1
  58. package/lib/error/error.handler.d.ts +0 -3
  59. package/lib/error/error.handler.js +0 -30
  60. package/lib/error/error.handler.js.map +0 -1
  61. package/lib/index.js.map +0 -1
  62. package/lib/statics.d.ts +0 -17
  63. package/lib/statics.js +0 -35
  64. package/lib/statics.js.map +0 -1
  65. package/lib/utils.d.ts +0 -3
  66. package/lib/utils.js.map +0 -1
package/lib/QueryError.js CHANGED
@@ -1,62 +1,52 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
- }) : (function(o, m, k, k2) {
6
- if (k2 === undefined) k2 = k;
7
- o[k2] = m[k];
8
- }));
9
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
- };
12
- Object.defineProperty(exports, "__esModule", { value: true });
13
- exports.QueryError = exports.QueryErrorCode = void 0;
14
- __exportStar(require("./error/error.handler"), exports);
15
- var QueryErrorCode;
16
- (function (QueryErrorCode) {
17
- QueryErrorCode["Default"] = "default";
18
- QueryErrorCode["ServerError"] = "server-error";
19
- QueryErrorCode["NotFound"] = "not-found";
20
- QueryErrorCode["BadRequest"] = "bad-request";
21
- QueryErrorCode["Unauthorized"] = "unauthorized";
22
- QueryErrorCode["Forbidden"] = "forbidden";
23
- QueryErrorCode["NotImplemented"] = "not-implemented";
24
- QueryErrorCode["SessionExpired"] = "session-expired";
25
- })(QueryErrorCode = exports.QueryErrorCode || (exports.QueryErrorCode = {}));
26
- class QueryError extends Error {
27
- constructor(req, code, optional = {}) {
28
- super(code);
29
- this.req = req;
30
- this.code = code;
31
- this.optional = optional;
32
- this.status = QueryError.getStatusFromCode(code);
33
- }
34
- toJSON() {
35
- return Object.assign({ id: this.req.id, status: String(this.status), code: this.code }, this.optional);
36
- }
37
- static catch(req, e) {
38
- if (e instanceof QueryError) {
39
- return e;
40
- }
41
- console.error(e);
42
- return new QueryError(req, QueryErrorCode.ServerError, {
43
- detail: e.message,
44
- });
45
- }
46
- static getStatusFromCode(code) {
47
- switch (code) {
48
- case QueryErrorCode.NotFound:
49
- return 404;
50
- case QueryErrorCode.BadRequest:
51
- return 400;
52
- case QueryErrorCode.SessionExpired:
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
+ }
@@ -1,59 +1,47 @@
1
- import { Request } from 'express';
2
- import { Document, FilterQuery, LeanDocument, Model, Types, UpdateQuery } from 'mongoose';
3
- import { Query } from './Query';
4
- import { ModelSearchPopulate, QueryModel } from './QueryModel';
5
- import * as mongoose from 'mongoose';
6
- export interface QueryFindOpts {
7
- populate?: boolean;
8
- select?: boolean;
9
- }
10
- declare type SubDocArray<T extends Document, K extends keyof T> = T[K][];
11
- declare type SubDocItem<T extends Document, K extends keyof T> = (T[K] extends [] ? T[K][0] : null) | null;
12
- export declare class QueryHandler<T extends Document> {
13
- private _model;
14
- private queryModel;
15
- constructor(_model: Model<T>, queryModel: QueryModel<T>);
16
- get model(): Model<T, {}, {}>;
17
- create<B extends Partial<LeanDocument<T>>>(body: B): Promise<mongoose.EnforceDocument<T, {}>>;
18
- json<D extends Document>(doc: D): Promise<any>;
19
- search(query: Query<T>): Promise<{
20
- data: LeanDocument<T>[];
21
- count: number;
22
- limit: number;
23
- skip: number;
24
- }>;
25
- findOne(query: Query<T>, opts?: QueryFindOpts): Promise<mongoose.EnforceDocument<T, {}> | null>;
26
- findOneAndUpdate(query: Query<T>, filter: FilterQuery<T>, update: UpdateQuery<T>): Promise<mongoose.EnforceDocument<T, {}> | null>;
27
- deleteOne(filter: FilterQuery<T>): Promise<{
28
- ok?: number | undefined;
29
- n?: number | undefined;
30
- } & {
31
- deletedCount?: number | undefined;
32
- }>;
33
- populate(req: Request, doc: T | T[any], type?: keyof ModelSearchPopulate<T, any>): Promise<void>;
34
- getPopulates(type: keyof ModelSearchPopulate<T, any>): ModelSearchPopulate<LeanDocument<T>, Exclude<keyof T, "$getAllSubdocs" | "$ignore" | "$isDefault" | "$isDeleted" | "$getPopulatedDocs" | "$isEmpty" | "$isValid" | "$locals" | "$markValid" | "$op" | "$session" | "$set" | "$where" | "baseModelName" | "collection" | "db" | "delete" | "deleteOne" | "depopulate" | "directModifiedPaths" | "equals" | "errors" | "execPopulate" | "get" | "getChanges" | "increment" | "init" | "invalidate" | "isDirectModified" | "isDirectSelected" | "isInit" | "isModified" | "isNew" | "isSelected" | "markModified" | "modifiedPaths" | "model" | "modelName" | "overwrite" | "$parent" | "populate" | "populated" | "remove" | "replaceOne" | "save" | "schema" | "set" | "toJSON" | "toObject" | "unmarkModified" | "update" | "updateOne" | "validate" | "validateSync" | "$isSingleNested">>[];
35
- updateMany(ids: Types.ObjectId[], body: Partial<T>): Promise<mongoose.UpdateWriteOpResult>;
36
- deleteMany(ids: Types.ObjectId[]): Promise<{
37
- ok?: number | undefined;
38
- n?: number | undefined;
39
- } & {
40
- deletedCount?: number | undefined;
41
- }>;
42
- subCreate<K extends keyof T>(sub: K, query: Query<T>, body: any): Promise<(T[K] extends [] ? T[K][0] : null) | null>;
43
- subSearch<K extends keyof T>(sub: K, query: Query<T>, rootQuery: FilterQuery<T>): Promise<{
44
- count: number;
45
- limit: number;
46
- skip: number;
47
- data: SubDocArray<T, K>;
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(query: mongoose.FilterQuery<T>, $in: mongoose.Types.ObjectId[], body: Partial<T>): Promise<mongoose.UpdateWriteOpResult>;
29
+ deleteMany(query: mongoose.FilterQuery<T>, $in: 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 {};