@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.
- 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 +11 -10
- package/lib/index.js +8 -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} +1 -2
- package/lib/utils/constants.js +1 -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/QueryHistory.d.ts
CHANGED
|
@@ -1,76 +1,75 @@
|
|
|
1
|
-
import
|
|
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
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
private
|
|
52
|
-
private
|
|
53
|
-
private
|
|
54
|
-
private
|
|
55
|
-
private
|
|
56
|
-
private
|
|
57
|
-
private
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
private
|
|
73
|
-
private
|
|
74
|
-
|
|
75
|
-
}
|
|
76
|
-
export {};
|
|
1
|
+
import mongoose from 'mongoose';
|
|
2
|
+
export interface IQueryHistorySchema {
|
|
3
|
+
_id: mongoose.Types.ObjectId;
|
|
4
|
+
locked: boolean;
|
|
5
|
+
lastChange: Date;
|
|
6
|
+
history: {
|
|
7
|
+
_id: mongoose.Types.ObjectId;
|
|
8
|
+
referenceDate: Date;
|
|
9
|
+
diffDate: Date;
|
|
10
|
+
diff: object;
|
|
11
|
+
user: mongoose.Types.ObjectId;
|
|
12
|
+
}[];
|
|
13
|
+
}
|
|
14
|
+
export declare const queryHistorySchema: {
|
|
15
|
+
locked: {
|
|
16
|
+
type: BooleanConstructor;
|
|
17
|
+
default: boolean;
|
|
18
|
+
};
|
|
19
|
+
lastChange: {
|
|
20
|
+
type: DateConstructor;
|
|
21
|
+
default: () => Date;
|
|
22
|
+
};
|
|
23
|
+
history: {
|
|
24
|
+
type: {
|
|
25
|
+
Type: StringConstructor;
|
|
26
|
+
};
|
|
27
|
+
referenceDate: {
|
|
28
|
+
type: DateConstructor;
|
|
29
|
+
default: () => Date;
|
|
30
|
+
};
|
|
31
|
+
diffDate: {
|
|
32
|
+
type: DateConstructor;
|
|
33
|
+
default: () => Date;
|
|
34
|
+
};
|
|
35
|
+
diff: ObjectConstructor;
|
|
36
|
+
user: {
|
|
37
|
+
type: typeof mongoose.Schema.Types.ObjectId;
|
|
38
|
+
ref: string;
|
|
39
|
+
};
|
|
40
|
+
}[];
|
|
41
|
+
};
|
|
42
|
+
interface IQueryHistoryOptions<T extends IQueryHistorySchema & mongoose.Document> {
|
|
43
|
+
model?: mongoose.Model<T>;
|
|
44
|
+
old?: T;
|
|
45
|
+
updated?: T;
|
|
46
|
+
ignoreFields?: (keyof T)[];
|
|
47
|
+
type?: string;
|
|
48
|
+
}
|
|
49
|
+
export declare class QueryHistory<T extends IQueryHistorySchema & mongoose.Document> {
|
|
50
|
+
private userID;
|
|
51
|
+
private model;
|
|
52
|
+
private old;
|
|
53
|
+
private updated;
|
|
54
|
+
private ignoreFields;
|
|
55
|
+
private referenceDate;
|
|
56
|
+
private type;
|
|
57
|
+
private meta;
|
|
58
|
+
constructor(userID: mongoose.Types.ObjectId, { model, old, updated, ignoreFields, type }: IQueryHistoryOptions<T>);
|
|
59
|
+
setReferenceDate(date: Date): void;
|
|
60
|
+
setIgnoreFields(ignoreFields: (keyof T)[]): void;
|
|
61
|
+
setModel(model: mongoose.Model<T>): void;
|
|
62
|
+
setOldDoc(doc: T, force?: boolean): void;
|
|
63
|
+
setType(type: string): void;
|
|
64
|
+
setMeta(meta: object): void;
|
|
65
|
+
setUpdatedDoc(doc: T): void;
|
|
66
|
+
setDiff(_id: mongoose.Types.ObjectId, referenceDate?: Date): Promise<mongoose.UpdateWriteOpResult>;
|
|
67
|
+
revertHistory(_id: mongoose.Types.ObjectId, historyID: mongoose.Types.ObjectId): Promise<mongoose.IfAny<T, any, mongoose.Document<unknown, {}, T> & Omit<mongoose.Require_id<T>, never>>>;
|
|
68
|
+
getOld(): T;
|
|
69
|
+
getUpdated(): T;
|
|
70
|
+
findUpdated(): Promise<T>;
|
|
71
|
+
private ignoreToSelect;
|
|
72
|
+
private removeIgnoredFields;
|
|
73
|
+
private getModel;
|
|
74
|
+
}
|
|
75
|
+
export {};
|
package/lib/QueryHistory.js
CHANGED
|
@@ -1,154 +1,137 @@
|
|
|
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
|
-
this.
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
this.
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
removeIgnoredFields(doc) {
|
|
139
|
-
if (this.ignoreFields) {
|
|
140
|
-
for (const field of this.ignoreFields) {
|
|
141
|
-
delete doc[field];
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
return doc;
|
|
145
|
-
}
|
|
146
|
-
getModel() {
|
|
147
|
-
if (!this.model) {
|
|
148
|
-
throw new Error('missing model in QueryHistory');
|
|
149
|
-
}
|
|
150
|
-
return this.model;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
exports.QueryHistory = QueryHistory;
|
|
154
|
-
//# sourceMappingURL=QueryHistory.js.map
|
|
1
|
+
import mongoose from 'mongoose';
|
|
2
|
+
import jsonDiffPatch from 'jsondiffpatch';
|
|
3
|
+
const diffPatcher = jsonDiffPatch.create({
|
|
4
|
+
objectHash: (obj) => {
|
|
5
|
+
return '_id' in obj ? obj._id.toString() : obj.toString();
|
|
6
|
+
},
|
|
7
|
+
});
|
|
8
|
+
export const queryHistorySchema = {
|
|
9
|
+
locked: { type: Boolean, default: false },
|
|
10
|
+
lastChange: { type: Date, default: () => new Date() },
|
|
11
|
+
history: [
|
|
12
|
+
{
|
|
13
|
+
type: { Type: String },
|
|
14
|
+
referenceDate: { type: Date, default: () => new Date() },
|
|
15
|
+
diffDate: { type: Date, default: () => new Date() },
|
|
16
|
+
diff: Object,
|
|
17
|
+
user: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
|
|
18
|
+
},
|
|
19
|
+
],
|
|
20
|
+
};
|
|
21
|
+
export class QueryHistory {
|
|
22
|
+
constructor(userID, { model, old, updated, ignoreFields, type }) {
|
|
23
|
+
this.userID = userID;
|
|
24
|
+
this.model = model;
|
|
25
|
+
this.old = old;
|
|
26
|
+
this.updated = updated;
|
|
27
|
+
this.ignoreFields = ignoreFields;
|
|
28
|
+
this.type = type;
|
|
29
|
+
}
|
|
30
|
+
setReferenceDate(date) {
|
|
31
|
+
this.referenceDate = date;
|
|
32
|
+
}
|
|
33
|
+
setIgnoreFields(ignoreFields) {
|
|
34
|
+
this.ignoreFields = ignoreFields;
|
|
35
|
+
}
|
|
36
|
+
setModel(model) {
|
|
37
|
+
this.model = model;
|
|
38
|
+
}
|
|
39
|
+
setOldDoc(doc, force) {
|
|
40
|
+
if (!this.old || force) {
|
|
41
|
+
this.old = doc;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
setType(type) {
|
|
45
|
+
this.type = type;
|
|
46
|
+
}
|
|
47
|
+
setMeta(meta) {
|
|
48
|
+
this.meta = meta;
|
|
49
|
+
}
|
|
50
|
+
setUpdatedDoc(doc) {
|
|
51
|
+
this.updated = doc;
|
|
52
|
+
}
|
|
53
|
+
async setDiff(_id, referenceDate = this.referenceDate) {
|
|
54
|
+
const diff = diffPatcher.diff(this.getOld(), this.getUpdated());
|
|
55
|
+
if (typeof diff !== 'undefined') {
|
|
56
|
+
const diffDate = new Date();
|
|
57
|
+
return this.getModel().updateOne({ _id }, {
|
|
58
|
+
$push: {
|
|
59
|
+
history: {
|
|
60
|
+
$each: [
|
|
61
|
+
{
|
|
62
|
+
type: this.type,
|
|
63
|
+
user: this.userID,
|
|
64
|
+
diff,
|
|
65
|
+
diffDate,
|
|
66
|
+
referenceDate,
|
|
67
|
+
meta: this.meta,
|
|
68
|
+
},
|
|
69
|
+
],
|
|
70
|
+
$position: 0,
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
async revertHistory(_id, historyID) {
|
|
77
|
+
const model = this.getModel();
|
|
78
|
+
const [doc, historyDoc] = await Promise.all([
|
|
79
|
+
model.findOne({ _id }).select(this.ignoreToSelect()),
|
|
80
|
+
model.findOne({ _id, 'history._id': historyID }, { 'history.$': 1 }),
|
|
81
|
+
]);
|
|
82
|
+
if (!doc) {
|
|
83
|
+
throw new Error('Doc not found');
|
|
84
|
+
}
|
|
85
|
+
if (!historyDoc) {
|
|
86
|
+
throw new Error('History not found');
|
|
87
|
+
}
|
|
88
|
+
const history = historyDoc.history[0];
|
|
89
|
+
diffPatcher.unpatch(doc, history.diff);
|
|
90
|
+
await model.updateOne({ _id }, {
|
|
91
|
+
$set: doc,
|
|
92
|
+
$pull: { history: { _id: history._id } },
|
|
93
|
+
});
|
|
94
|
+
return doc;
|
|
95
|
+
}
|
|
96
|
+
getOld() {
|
|
97
|
+
if (!this.old) {
|
|
98
|
+
throw new Error('missing old doc in QueryHistory');
|
|
99
|
+
}
|
|
100
|
+
const obj = 'toObject' in this.old ? this.old.toObject() : this.old;
|
|
101
|
+
return this.removeIgnoredFields(obj);
|
|
102
|
+
}
|
|
103
|
+
getUpdated() {
|
|
104
|
+
if (!this.updated) {
|
|
105
|
+
throw new Error('missing updated doc in QueryHistory');
|
|
106
|
+
}
|
|
107
|
+
const obj = 'toObject' in this.updated ? this.updated.toObject() : this.updated;
|
|
108
|
+
return this.removeIgnoredFields(obj);
|
|
109
|
+
}
|
|
110
|
+
async findUpdated() {
|
|
111
|
+
const updated = await this.getModel()
|
|
112
|
+
.findOne({ _id: this.getOld()._id })
|
|
113
|
+
.select(this.ignoreToSelect());
|
|
114
|
+
if (!updated) {
|
|
115
|
+
throw new Error('Updated not found');
|
|
116
|
+
}
|
|
117
|
+
this.updated = updated;
|
|
118
|
+
return this.updated;
|
|
119
|
+
}
|
|
120
|
+
ignoreToSelect() {
|
|
121
|
+
return this.ignoreFields?.map((x) => `-${String(x)}`).join(' ');
|
|
122
|
+
}
|
|
123
|
+
removeIgnoredFields(doc) {
|
|
124
|
+
if (this.ignoreFields) {
|
|
125
|
+
for (const field of this.ignoreFields) {
|
|
126
|
+
delete doc[field];
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return doc;
|
|
130
|
+
}
|
|
131
|
+
getModel() {
|
|
132
|
+
if (!this.model) {
|
|
133
|
+
throw new Error('missing model in QueryHistory');
|
|
134
|
+
}
|
|
135
|
+
return this.model;
|
|
136
|
+
}
|
|
137
|
+
}
|
package/lib/QueryModel.d.ts
CHANGED
|
@@ -1,80 +1,65 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { Query } from './Query';
|
|
3
|
-
export interface ModelSearchPopulate<T extends
|
|
4
|
-
path: K;
|
|
5
|
-
select: (T[K] extends object ? keyof T[K] : string)[];
|
|
6
|
-
ref?: string;
|
|
7
|
-
lookupRef?: string;
|
|
8
|
-
onFindOne?: boolean;
|
|
9
|
-
onPatch?: boolean;
|
|
10
|
-
onPost?: boolean;
|
|
11
|
-
onAll?: boolean;
|
|
12
|
-
onSearch?: boolean;
|
|
13
|
-
populate?: PopulateOptions[];
|
|
14
|
-
}
|
|
15
|
-
interface ModelSearchOptsSub<T extends
|
|
16
|
-
sub: K;
|
|
17
|
-
populate: ModelSearchPopulate<T[K], keyof T[K]>[];
|
|
18
|
-
}
|
|
19
|
-
interface ModelSearchOpts<T extends
|
|
20
|
-
editFields?: (keyof
|
|
21
|
-
privateFields?: (keyof
|
|
22
|
-
query?: (keyof
|
|
23
|
-
select?: (keyof
|
|
24
|
-
defaultSelect?: (keyof
|
|
25
|
-
sort?: (keyof
|
|
26
|
-
populate?: ModelSearchPopulate<
|
|
27
|
-
limit?: number;
|
|
28
|
-
subs?: ModelSearchOptsSub<T, keyof T>[];
|
|
29
|
-
updateDateKey?: keyof T;
|
|
30
|
-
}
|
|
31
|
-
export interface IQueryPopulate {
|
|
32
|
-
path: string;
|
|
33
|
-
select?: string[];
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export declare const toValidSelectRegexp: (select: string | number | symbol) => RegExp;
|
|
37
|
-
export declare const toValidSortRegexp: (sort: string | number | symbol) => RegExp;
|
|
38
|
-
export declare const toValidTextRegexp: (str: string) => RegExp | "";
|
|
39
|
-
export declare class QueryModel<T extends
|
|
40
|
-
private opts;
|
|
41
|
-
constructor(opts?: ModelSearchOpts<T>);
|
|
42
|
-
get publicFields():
|
|
43
|
-
get privateFields():
|
|
44
|
-
get populate(): ModelSearchPopulate<
|
|
45
|
-
get subs(): ModelSearchOptsSub<T, keyof T>[]
|
|
46
|
-
get updateDateKey(): keyof T
|
|
47
|
-
get query():
|
|
48
|
-
createSearchQuery(query: Query<T>): FilterQuery<T>;
|
|
49
|
-
setPopulate(modelQuery: IModelQuery, query: Query<T>): void;
|
|
50
|
-
getValidPopulate(queryPopulate: IQueryPopulate[], sub?: keyof T): ModelSearchPopulate<any, any>[];
|
|
51
|
-
setLimitAndSkip(modelQuery: IModelQuery, query: Query<T>): void;
|
|
52
|
-
setSelect(modelQuery: IModelQuery, query: Query<T>, overrideValid?: (keyof
|
|
53
|
-
setSort(modelQuery: IModelQuery, query: Query<T>): void;
|
|
54
|
-
setProjection(modelQuery: IModelQuery, query: Query<T>): void;
|
|
55
|
-
getSubPopulateLookup<K extends keyof T>(query: Query<T>, sub: K):
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
};
|
|
62
|
-
$
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
_id: number;
|
|
67
|
-
count: number;
|
|
68
|
-
};
|
|
69
|
-
$lookup?: undefined;
|
|
70
|
-
})[];
|
|
71
|
-
getSubFilterConditionAndMatch(subQuery: FilterQuery<T>, sub: keyof T): {
|
|
72
|
-
$filter: {
|
|
73
|
-
input: string;
|
|
74
|
-
as: string;
|
|
75
|
-
cond: any;
|
|
76
|
-
};
|
|
77
|
-
$match: any;
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
export {};
|
|
1
|
+
import mongoose from 'mongoose';
|
|
2
|
+
import { Query } from './Query';
|
|
3
|
+
export interface ModelSearchPopulate<T extends any, K extends keyof T> {
|
|
4
|
+
path: K;
|
|
5
|
+
select: (T[K] extends object ? keyof T[K] : string)[];
|
|
6
|
+
ref?: string;
|
|
7
|
+
lookupRef?: string;
|
|
8
|
+
onFindOne?: boolean;
|
|
9
|
+
onPatch?: boolean;
|
|
10
|
+
onPost?: boolean;
|
|
11
|
+
onAll?: boolean;
|
|
12
|
+
onSearch?: boolean;
|
|
13
|
+
populate?: mongoose.PopulateOptions[];
|
|
14
|
+
}
|
|
15
|
+
interface ModelSearchOptsSub<T extends object, K extends keyof T> {
|
|
16
|
+
sub: K;
|
|
17
|
+
populate: ModelSearchPopulate<T[K], keyof T[K]>[];
|
|
18
|
+
}
|
|
19
|
+
interface ModelSearchOpts<T extends object> {
|
|
20
|
+
editFields?: (keyof T)[];
|
|
21
|
+
privateFields?: (keyof T)[];
|
|
22
|
+
query?: (keyof T)[];
|
|
23
|
+
select?: (keyof T)[];
|
|
24
|
+
defaultSelect?: (keyof T)[] | string[];
|
|
25
|
+
sort?: (keyof T)[];
|
|
26
|
+
populate?: ModelSearchPopulate<T, keyof T>[];
|
|
27
|
+
limit?: number;
|
|
28
|
+
subs?: ModelSearchOptsSub<T, keyof T>[];
|
|
29
|
+
updateDateKey?: keyof T;
|
|
30
|
+
}
|
|
31
|
+
export interface IQueryPopulate {
|
|
32
|
+
path: string;
|
|
33
|
+
select?: string[];
|
|
34
|
+
}
|
|
35
|
+
type IModelQuery = mongoose.QueryWithHelpers<unknown, unknown>;
|
|
36
|
+
export declare const toValidSelectRegexp: (select: string | number | symbol) => RegExp;
|
|
37
|
+
export declare const toValidSortRegexp: (sort: string | number | symbol) => RegExp;
|
|
38
|
+
export declare const toValidTextRegexp: (str: string) => RegExp | "";
|
|
39
|
+
export declare class QueryModel<T extends object> {
|
|
40
|
+
private opts;
|
|
41
|
+
constructor(opts?: ModelSearchOpts<T>);
|
|
42
|
+
get publicFields(): (keyof T)[];
|
|
43
|
+
get privateFields(): (keyof T)[];
|
|
44
|
+
get populate(): ModelSearchPopulate<T, keyof T>[];
|
|
45
|
+
get subs(): ModelSearchOptsSub<T, keyof T>[];
|
|
46
|
+
get updateDateKey(): keyof T;
|
|
47
|
+
get query(): (keyof T)[];
|
|
48
|
+
createSearchQuery(query: Query<any, T>): mongoose.FilterQuery<T>;
|
|
49
|
+
setPopulate(modelQuery: IModelQuery, query: Query<T>): void;
|
|
50
|
+
getValidPopulate(queryPopulate: IQueryPopulate[], sub?: keyof T): ModelSearchPopulate<any, any>[];
|
|
51
|
+
setLimitAndSkip(modelQuery: IModelQuery, query: Query<T>): void;
|
|
52
|
+
setSelect(modelQuery: IModelQuery, query: Query<T>, overrideValid?: (keyof T)[]): void;
|
|
53
|
+
setSort(modelQuery: IModelQuery, query: Query<T>): void;
|
|
54
|
+
setProjection(modelQuery: IModelQuery, query: Query<T>): void;
|
|
55
|
+
getSubPopulateLookup<K extends keyof T>(query: Query<T>, sub: K): any[];
|
|
56
|
+
getSubFilterConditionAndMatch(subQuery: mongoose.FilterQuery<T>, sub: keyof T): {
|
|
57
|
+
$filter: {
|
|
58
|
+
input: string;
|
|
59
|
+
as: string;
|
|
60
|
+
cond: any;
|
|
61
|
+
};
|
|
62
|
+
$match: any;
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
export {};
|