@ewb/mongoose-query 1.1.16 → 1.2.0
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 +0 -25
- package/lib/Query.js +3 -2
- package/lib/QueryError.d.ts +0 -25
- package/lib/QueryError.js +1 -2
- package/lib/QueryHistory.d.ts +3 -26
- package/lib/QueryHistory.js +5 -3
- package/lib/QueryModel.d.ts +25 -37
- package/lib/QueryModel.js +16 -16
- package/lib/QueryOptions.d.ts +3 -28
- package/lib/QueryOptions.js +2 -1
- package/lib/src/Query.d.ts +64 -0
- package/lib/src/Query.js +250 -0
- package/lib/src/QueryError.d.ts +16 -0
- package/lib/src/QueryError.js +52 -0
- package/lib/src/QueryHistory.d.ts +77 -0
- package/lib/src/QueryHistory.js +137 -0
- package/lib/src/QueryModel.d.ts +58 -0
- package/lib/src/QueryModel.js +250 -0
- package/lib/src/QueryOptions.d.ts +64 -0
- package/lib/src/QueryOptions.js +200 -0
- package/lib/src/index.d.ts +6 -0
- package/lib/src/index.js +6 -0
- package/lib/src/utils/Query.utils.d.ts +40 -0
- package/lib/src/utils/Query.utils.js +144 -0
- package/lib/src/utils/constants.d.ts +1 -0
- package/lib/src/utils/constants.js +1 -0
- package/lib/src/utils/index.d.ts +3 -0
- package/lib/src/utils/index.js +3 -0
- package/lib/src/utils/types.d.ts +23 -0
- package/lib/src/utils/types.js +1 -0
- package/lib/src/utils/utils.d.ts +8 -0
- package/lib/src/utils/utils.js +38 -0
- package/lib/utils/Query.utils.d.ts +4 -29
- package/lib/utils/types.d.ts +1 -26
- package/package.json +21 -29
package/lib/src/Query.js
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import mongoSanitize from 'express-mongo-sanitize';
|
|
2
|
+
import { mongooseQueryOptions, mongooseQueryWithOperation, } from './utils/Query.utils.js';
|
|
3
|
+
export class Query {
|
|
4
|
+
constructor(queries) {
|
|
5
|
+
this._root = null;
|
|
6
|
+
this._rootNext = null;
|
|
7
|
+
this._rootPopulate = [];
|
|
8
|
+
this._or = [];
|
|
9
|
+
this._model = undefined;
|
|
10
|
+
this._addToModelQuery = {};
|
|
11
|
+
this._export = false;
|
|
12
|
+
this._projection = undefined;
|
|
13
|
+
this.sort = [];
|
|
14
|
+
this._select = [];
|
|
15
|
+
this._query = {};
|
|
16
|
+
this.hasTextSearch = false;
|
|
17
|
+
this.options = {};
|
|
18
|
+
this.populate = [];
|
|
19
|
+
const query = Object.assign({}, queries);
|
|
20
|
+
const { limit, skip, sort, select, populate, page } = mongooseQueryOptions(query);
|
|
21
|
+
this._limit = limit;
|
|
22
|
+
this._skip = skip;
|
|
23
|
+
this.sort = sort;
|
|
24
|
+
this._select = select;
|
|
25
|
+
this.populate = populate;
|
|
26
|
+
this.page = page;
|
|
27
|
+
this.textQuery = this.mongooseTextSearch(query);
|
|
28
|
+
if (this.textQuery) {
|
|
29
|
+
this.hasTextSearch = true;
|
|
30
|
+
}
|
|
31
|
+
this.documentQuery = query;
|
|
32
|
+
}
|
|
33
|
+
get skip() {
|
|
34
|
+
return this._skip;
|
|
35
|
+
}
|
|
36
|
+
get limit() {
|
|
37
|
+
return this._limit;
|
|
38
|
+
}
|
|
39
|
+
get root() {
|
|
40
|
+
const modelQuery = this._addToModelQuery[this._model];
|
|
41
|
+
const root = this._root;
|
|
42
|
+
if (this._model && modelQuery && Object.keys(modelQuery).length) {
|
|
43
|
+
if (root) {
|
|
44
|
+
return { $and: [root, modelQuery] };
|
|
45
|
+
}
|
|
46
|
+
return { ...modelQuery };
|
|
47
|
+
}
|
|
48
|
+
return { ...root };
|
|
49
|
+
}
|
|
50
|
+
get model() {
|
|
51
|
+
return this._model;
|
|
52
|
+
}
|
|
53
|
+
set model(value) {
|
|
54
|
+
this._model = value;
|
|
55
|
+
}
|
|
56
|
+
get rootPopulate() {
|
|
57
|
+
return this._rootPopulate;
|
|
58
|
+
}
|
|
59
|
+
get select() {
|
|
60
|
+
return this._select;
|
|
61
|
+
}
|
|
62
|
+
get or() {
|
|
63
|
+
return this._or;
|
|
64
|
+
}
|
|
65
|
+
get query() {
|
|
66
|
+
return this.documentQuery;
|
|
67
|
+
}
|
|
68
|
+
get export() {
|
|
69
|
+
return this._export;
|
|
70
|
+
}
|
|
71
|
+
set export(value) {
|
|
72
|
+
this._export = value;
|
|
73
|
+
}
|
|
74
|
+
get projection() {
|
|
75
|
+
return this._projection;
|
|
76
|
+
}
|
|
77
|
+
set projection(value) {
|
|
78
|
+
this._projection = value;
|
|
79
|
+
}
|
|
80
|
+
setSkipAndLimit(skip, limit) {
|
|
81
|
+
this._skip = skip;
|
|
82
|
+
this._limit = limit;
|
|
83
|
+
}
|
|
84
|
+
setQuery(key, value) {
|
|
85
|
+
this._query[key] = value;
|
|
86
|
+
}
|
|
87
|
+
addOr(query) {
|
|
88
|
+
this._or.push(query);
|
|
89
|
+
}
|
|
90
|
+
defaultSelect(select) {
|
|
91
|
+
if (this._select.length !== 0)
|
|
92
|
+
return;
|
|
93
|
+
this._select = select;
|
|
94
|
+
}
|
|
95
|
+
addPopulate(populate) {
|
|
96
|
+
this._rootPopulate.push(...(Array.isArray(populate) ? populate : [populate]));
|
|
97
|
+
}
|
|
98
|
+
addRoot(query) {
|
|
99
|
+
this._root = {
|
|
100
|
+
...(this._root || {}),
|
|
101
|
+
...query,
|
|
102
|
+
};
|
|
103
|
+
return this._root;
|
|
104
|
+
}
|
|
105
|
+
removeRoot(key) {
|
|
106
|
+
if (this._root && key in this._root) {
|
|
107
|
+
delete this._root[key];
|
|
108
|
+
}
|
|
109
|
+
return this._root;
|
|
110
|
+
}
|
|
111
|
+
addRootNext(query) {
|
|
112
|
+
this._rootNext = {
|
|
113
|
+
...(this._rootNext || {}),
|
|
114
|
+
...query,
|
|
115
|
+
};
|
|
116
|
+
return this._rootNext;
|
|
117
|
+
}
|
|
118
|
+
removeQuery(key) {
|
|
119
|
+
return this.deleteKeysFromQuery(key);
|
|
120
|
+
}
|
|
121
|
+
deleteKeysFromQuery(key) {
|
|
122
|
+
if (key in this._query) {
|
|
123
|
+
delete this._query[key];
|
|
124
|
+
}
|
|
125
|
+
if (key in this.documentQuery) {
|
|
126
|
+
delete this.documentQuery[key];
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
deleteKeysFromRoot(keys) {
|
|
130
|
+
if (this._root) {
|
|
131
|
+
for (const key of keys) {
|
|
132
|
+
if (key in this._root) {
|
|
133
|
+
delete this._root[key];
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
getModelQuery(key) {
|
|
139
|
+
return this._addToModelQuery[key];
|
|
140
|
+
}
|
|
141
|
+
addToModelQuery(model, query) {
|
|
142
|
+
const modelKeys = model;
|
|
143
|
+
const keys = Array.isArray(modelKeys)
|
|
144
|
+
? modelKeys
|
|
145
|
+
: [modelKeys];
|
|
146
|
+
for (const modelKey of keys) {
|
|
147
|
+
if (this._addToModelQuery[modelKey]) {
|
|
148
|
+
this._addToModelQuery[modelKey] = {
|
|
149
|
+
...this._addToModelQuery[modelKey],
|
|
150
|
+
...query,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
this._addToModelQuery[modelKey] = { ...query };
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return this;
|
|
158
|
+
}
|
|
159
|
+
removeFromModelQuery(model, key) {
|
|
160
|
+
if (this._addToModelQuery[model]) {
|
|
161
|
+
if (key in this._addToModelQuery[model]) {
|
|
162
|
+
delete this._addToModelQuery[model][key];
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return this;
|
|
166
|
+
}
|
|
167
|
+
createQuery({ query, noRoot, } = {}) {
|
|
168
|
+
const _query = this.initQuery();
|
|
169
|
+
const queryArray = [];
|
|
170
|
+
const root = this.root;
|
|
171
|
+
if (!noRoot && root && Object.keys(root).length > 0) {
|
|
172
|
+
queryArray.push(root);
|
|
173
|
+
}
|
|
174
|
+
if (!noRoot && this._rootNext && Object.keys(this._rootNext).length > 0) {
|
|
175
|
+
queryArray.push(this._rootNext);
|
|
176
|
+
this._rootNext = null;
|
|
177
|
+
}
|
|
178
|
+
if (Object.keys(_query).length > 0) {
|
|
179
|
+
const addQuery = query
|
|
180
|
+
? query.reduce((obj, key) => {
|
|
181
|
+
if (typeof _query[key] !== 'undefined') {
|
|
182
|
+
obj[key] = _query[key];
|
|
183
|
+
}
|
|
184
|
+
return obj;
|
|
185
|
+
}, {})
|
|
186
|
+
: _query;
|
|
187
|
+
if (Object.keys(addQuery).length > 0) {
|
|
188
|
+
queryArray.push(addQuery);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
const mQuery = queryArray.length > 1 ? { $and: queryArray } : queryArray[0] || {};
|
|
192
|
+
if (this._or.length > 0) {
|
|
193
|
+
return {
|
|
194
|
+
$or: [...this._or, mQuery],
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
return mQuery || {};
|
|
198
|
+
}
|
|
199
|
+
initQuery() {
|
|
200
|
+
const query = Object.assign({}, this.documentQuery);
|
|
201
|
+
const operations = this.mongooseQueryWithOperations(query);
|
|
202
|
+
const sanitized = this.sanitizedQuery(query);
|
|
203
|
+
return { ...operations, ...sanitized, ...this.textQuery, ...this._query };
|
|
204
|
+
}
|
|
205
|
+
mongooseTextSearch(query) {
|
|
206
|
+
const _query = {};
|
|
207
|
+
if ('$text' in query) {
|
|
208
|
+
if (query.$text) {
|
|
209
|
+
_query['$text'] = { $search: query.$text };
|
|
210
|
+
this.options['score'] = { $meta: 'textScore' };
|
|
211
|
+
}
|
|
212
|
+
delete query.$text;
|
|
213
|
+
}
|
|
214
|
+
return _query;
|
|
215
|
+
}
|
|
216
|
+
mongooseQueryWithOperations(query) {
|
|
217
|
+
const operationQuery = {};
|
|
218
|
+
for (const key in query) {
|
|
219
|
+
if (query.hasOwnProperty(key)) {
|
|
220
|
+
const str = String(query[key]);
|
|
221
|
+
if (key[0] === '$') {
|
|
222
|
+
if (typeof query[key] !== 'string')
|
|
223
|
+
continue;
|
|
224
|
+
const { field, value } = mongooseQueryWithOperation(operationQuery, key, str);
|
|
225
|
+
if (field && value) {
|
|
226
|
+
operationQuery[field] = value;
|
|
227
|
+
}
|
|
228
|
+
delete query[key];
|
|
229
|
+
}
|
|
230
|
+
else if (key[0] === '!') {
|
|
231
|
+
const field = key.slice(1);
|
|
232
|
+
operationQuery[field] = { $ne: str };
|
|
233
|
+
delete query[key];
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return operationQuery;
|
|
238
|
+
}
|
|
239
|
+
sanitizedQuery(query) {
|
|
240
|
+
const cleanedQuery = {};
|
|
241
|
+
for (const key in query) {
|
|
242
|
+
if (query.hasOwnProperty(key)) {
|
|
243
|
+
if (typeof query[key] !== 'string' || query[key] !== '') {
|
|
244
|
+
cleanedQuery[key] = query[key];
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
return mongoSanitize.sanitize(cleanedQuery, { replaceWith: '_', allowDots: true });
|
|
249
|
+
}
|
|
250
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import mongoose from 'mongoose';
|
|
2
|
+
interface ErrorOpts {
|
|
3
|
+
message?: string;
|
|
4
|
+
detail?: string;
|
|
5
|
+
errors?: Omit<ErrorOpts, 'errors'>[];
|
|
6
|
+
error?: any;
|
|
7
|
+
}
|
|
8
|
+
export declare class QueryError<R extends object, C extends string> extends Error {
|
|
9
|
+
readonly status: number;
|
|
10
|
+
readonly detail?: string;
|
|
11
|
+
readonly errors?: ErrorOpts[];
|
|
12
|
+
constructor(status: number, message: string, opts?: Pick<ErrorOpts, 'detail' | 'errors' | 'error'>);
|
|
13
|
+
static catch<R extends object, C extends string>(e: Error | QueryError<R, C>): QueryError<R, C>;
|
|
14
|
+
static getData(error: mongoose.Error): [number, ErrorOpts];
|
|
15
|
+
}
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,52 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
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, {}, mongoose.DefaultSchemaOptions> & mongoose.Require_id<T> & {
|
|
68
|
+
__v: number;
|
|
69
|
+
} & mongoose.AddDefaultId<T, {}, mongoose.DefaultSchemaOptions>>>;
|
|
70
|
+
getOld(): T;
|
|
71
|
+
getUpdated(): T;
|
|
72
|
+
findUpdated(): Promise<T>;
|
|
73
|
+
private ignoreToSelect;
|
|
74
|
+
private removeIgnoredFields;
|
|
75
|
+
private getModel;
|
|
76
|
+
}
|
|
77
|
+
export {};
|
|
@@ -0,0 +1,137 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import mongoose from 'mongoose';
|
|
2
|
+
import { Query } from './Query.js';
|
|
3
|
+
import { IQueryOptionsPopulate, QueryOptions } from './QueryOptions.js';
|
|
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 QueryModel<T extends object> {
|
|
11
|
+
private _model;
|
|
12
|
+
private options;
|
|
13
|
+
constructor(_model: mongoose.Model<T>, options: QueryOptions<T>);
|
|
14
|
+
get model(): mongoose.Model<T, {}, {}, {}, mongoose.Document<unknown, {}, T, {}, mongoose.DefaultSchemaOptions> & mongoose.Require_id<T> & {
|
|
15
|
+
__v: number;
|
|
16
|
+
} & mongoose.AddDefaultId<T, {}, mongoose.DefaultSchemaOptions>, any, mongoose.Require_id<T> extends infer T_1 ? T_1 extends mongoose.Require_id<T> ? T_1 extends {
|
|
17
|
+
__v?: infer U;
|
|
18
|
+
} ? T_1 : T_1 & {
|
|
19
|
+
__v: number;
|
|
20
|
+
} : never : never>;
|
|
21
|
+
create<B extends Partial<T>>(body: B): Promise<mongoose.Document<unknown, {}, T, {}, mongoose.DefaultSchemaOptions> & mongoose.Require_id<T> & {
|
|
22
|
+
__v: number;
|
|
23
|
+
} & mongoose.AddDefaultId<T, {}, mongoose.DefaultSchemaOptions>>;
|
|
24
|
+
json<D extends mongoose.Document>(doc: D): Promise<any>;
|
|
25
|
+
search(query: Query<T>): Promise<{
|
|
26
|
+
data: T[];
|
|
27
|
+
count: number;
|
|
28
|
+
limit: number;
|
|
29
|
+
skip: number;
|
|
30
|
+
}>;
|
|
31
|
+
findOne(query: Query<T>, opts?: QueryFindOpts): Promise<mongoose.Document<unknown, {}, T, {}, mongoose.DefaultSchemaOptions> & mongoose.Require_id<T> & {
|
|
32
|
+
__v: number;
|
|
33
|
+
} & mongoose.AddDefaultId<T, {}, mongoose.DefaultSchemaOptions>>;
|
|
34
|
+
findOneAndUpdate(query: Query<T>, filter: mongoose.FilterQuery<T>, update: mongoose.UpdateQuery<T>): Promise<mongoose.Document<unknown, {}, T, {}, mongoose.DefaultSchemaOptions> & mongoose.Require_id<T> & {
|
|
35
|
+
__v: number;
|
|
36
|
+
} & mongoose.AddDefaultId<T, {}, mongoose.DefaultSchemaOptions>>;
|
|
37
|
+
deleteOne(filter: mongoose.FilterQuery<T>): Promise<mongoose.mongo.DeleteResult>;
|
|
38
|
+
populate(query: Query<T>, doc: T | T[any], type?: keyof IQueryOptionsPopulate<T, any>): Promise<void>;
|
|
39
|
+
getPopulates(type: keyof IQueryOptionsPopulate<T, any>): IQueryOptionsPopulate<T, keyof T>[];
|
|
40
|
+
updateMany(query: mongoose.FilterQuery<T>, $in: mongoose.Types.ObjectId[], body: Partial<T>): Promise<mongoose.UpdateWriteOpResult>;
|
|
41
|
+
deleteMany(query: mongoose.FilterQuery<T>, $in: mongoose.Types.ObjectId[]): Promise<mongoose.mongo.DeleteResult>;
|
|
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: mongoose.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: mongoose.FilterQuery<T[K]>): Promise<SubDocItem<T, K>>;
|
|
50
|
+
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>;
|
|
51
|
+
subDelete<K extends keyof T>(sub: K, query: Query<T>, subQuery: mongoose.FilterQuery<T[K]>): Promise<(T[K] extends [] ? T[K][0] : null) | null>;
|
|
52
|
+
subPopulate<R extends Request, SD, K extends keyof T>(query: Query<T>, subDoc: SD, sub: K, type?: keyof IQueryOptionsPopulate<T, K>): Promise<void[]>;
|
|
53
|
+
getSubPopulate<K extends keyof T>(sub: K, type: keyof IQueryOptionsPopulate<T, K>): IQueryOptionsPopulate<T[keyof T], keyof T[keyof T]>[];
|
|
54
|
+
private getSubDocument;
|
|
55
|
+
toJSON(_: unknown, ret: T): void;
|
|
56
|
+
private populateDoc;
|
|
57
|
+
}
|
|
58
|
+
export {};
|