@ewb/mongoose-query 1.1.17 → 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.
@@ -0,0 +1,250 @@
1
+ import httpStatus from 'http-status';
2
+ import mongoose from 'mongoose';
3
+ import { QueryError } from './QueryError.js';
4
+ import { toValidTextRegexp, } from './QueryOptions.js';
5
+ export class QueryModel {
6
+ constructor(_model, options) {
7
+ this._model = _model;
8
+ this.options = options;
9
+ this.toJSON = this.toJSON.bind(this);
10
+ }
11
+ get model() {
12
+ return this._model;
13
+ }
14
+ async create(body) {
15
+ const newDoc = new this._model(body);
16
+ await newDoc.validate();
17
+ await newDoc.save();
18
+ return newDoc;
19
+ }
20
+ async json(doc) {
21
+ return doc.toJSON({ transform: this.toJSON });
22
+ }
23
+ async search(query) {
24
+ const findQuery = this.options.createSearchQuery(query);
25
+ const modelFind = this._model.find(findQuery, query.options);
26
+ this.options.setPopulate(modelFind, query);
27
+ this.options.setLimitAndSkip(modelFind, query);
28
+ this.options.setSort(modelFind, query);
29
+ this.options.setSelect(modelFind, query, query.select);
30
+ const { skip = query.skip, limit = query.limit } = modelFind.getOptions();
31
+ const [data, count] = await Promise.all([
32
+ modelFind.lean().exec(),
33
+ this._model.find(findQuery).countDocuments(),
34
+ ]);
35
+ return { data: data, count, limit, skip };
36
+ }
37
+ async findOne(query, opts = {}) {
38
+ const filter = query.root || query.createQuery();
39
+ const modelFindOne = this._model.findOne(filter);
40
+ if (opts.populate)
41
+ this.options.setPopulate(modelFindOne, query);
42
+ this.options.setSelect(modelFindOne, query, opts.select ? undefined : []);
43
+ this.options.setProjection(modelFindOne, query);
44
+ return modelFindOne.exec();
45
+ }
46
+ async findOneAndUpdate(query, filter, update) {
47
+ if (this.options.updateDateKey) {
48
+ if ('$set' in update) {
49
+ update['$set'][this.options.updateDateKey] = new Date();
50
+ }
51
+ else {
52
+ update[this.options.updateDateKey] = new Date();
53
+ }
54
+ }
55
+ const modelUpdateOne = this._model.findOneAndUpdate(filter, update, {
56
+ new: true,
57
+ });
58
+ this.options.setPopulate(modelUpdateOne, query);
59
+ this.options.setSelect(modelUpdateOne, query);
60
+ return modelUpdateOne.exec();
61
+ }
62
+ async deleteOne(filter) {
63
+ return this._model.deleteOne(filter);
64
+ }
65
+ async populate(query, doc, type) {
66
+ try {
67
+ const populates = type ? this.getPopulates(type) : [];
68
+ populates.push(...this.options.getValidPopulate(query.populate));
69
+ await this.populateDoc(doc, populates);
70
+ }
71
+ catch (e) {
72
+ }
73
+ }
74
+ getPopulates(type) {
75
+ const populate = this.options.populate;
76
+ if (!populate || populate.length === 0) {
77
+ return [];
78
+ }
79
+ return populate.filter((x) => x[type] || x.onAll);
80
+ }
81
+ async updateMany(query, $in, body) {
82
+ const $set = { ...body };
83
+ if (this.options.updateDateKey) {
84
+ $set[this.options.updateDateKey] = new Date();
85
+ }
86
+ return this._model.updateMany({ ...query, _id: { $in } }, { $set });
87
+ }
88
+ async deleteMany(query, $in) {
89
+ return this._model.deleteMany({ ...query, _id: { $in } });
90
+ }
91
+ async subCreate(sub, query, body) {
92
+ if (!('_id' in body)) {
93
+ body['_id'] = new mongoose.Types.ObjectId();
94
+ }
95
+ await this._model.updateOne({ ...query.root }, { $push: { [sub]: body } });
96
+ return this.subFindOne(sub, query, { _id: body._id });
97
+ }
98
+ async subSearch(sub, query, rootQuery) {
99
+ const subOptions = this.options.subs?.find((x) => x.sub === sub);
100
+ if (!subOptions) {
101
+ throw new QueryError(httpStatus.NOT_IMPLEMENTED, 'Subdocument not found', {
102
+ detail: 'Missing options for subdocument',
103
+ });
104
+ }
105
+ const subQuery = query.createQuery({
106
+ noRoot: true,
107
+ });
108
+ const populate = query.populate;
109
+ const limit = query.limit;
110
+ const skip = query.skip;
111
+ const $lookups = this.options.getSubPopulateLookup(query, subOptions);
112
+ const { $filter, $match } = this.options.getSubFilterConditionAndMatch(subQuery, subOptions);
113
+ const pipeline = [
114
+ { $match: rootQuery },
115
+ {
116
+ $project: {
117
+ [sub]: { $filter },
118
+ count: { $size: `$${String(sub)}` },
119
+ },
120
+ },
121
+ { $unwind: `$${String(sub)}` },
122
+ ...(skip ? [{ $skip: skip }] : []),
123
+ ...(limit ? [{ $limit: limit }] : []),
124
+ ...$lookups,
125
+ ];
126
+ if (Object.keys($match).length > 0) {
127
+ for (const key in $match) {
128
+ if ($match.hasOwnProperty(key)) {
129
+ const [path, pathKey] = key.split('.');
130
+ if (pathKey && populate.some((pop) => pop.path === path)) {
131
+ $match[`${path}.${pathKey}`] = toValidTextRegexp($match[key]);
132
+ }
133
+ else if (typeof $match[key] === 'string') {
134
+ $match[`${String(sub)}.${path}`] = toValidTextRegexp($match[key]);
135
+ }
136
+ else {
137
+ $match[`${String(sub)}.${path}`] = $match[key];
138
+ }
139
+ delete $match[key];
140
+ }
141
+ }
142
+ pipeline.push({ $match });
143
+ }
144
+ const arr = await this._model.aggregate(pipeline);
145
+ return arr.reduce((obj, item) => {
146
+ const data = { ...item[sub] };
147
+ for (const pop of subOptions.populate) {
148
+ if (item[pop.path] && data[pop.path]) {
149
+ data[pop.path] = pop.multiple ? item[pop.path] : item[pop.path][0];
150
+ }
151
+ }
152
+ return {
153
+ count: item.count,
154
+ data: [...obj.data, data],
155
+ skip: obj.skip,
156
+ limit: obj.limit,
157
+ };
158
+ }, { count: 0, data: [], skip, limit });
159
+ }
160
+ async subFindOne(sub, query, subQuery) {
161
+ const subDoc = await this._model.findOne({
162
+ ...query.root,
163
+ [sub]: { $elemMatch: subQuery },
164
+ }, { [`${String(sub)}.$`]: 1 });
165
+ return this.getSubDocument(sub, subDoc);
166
+ }
167
+ async subFindOneAndUpdate(sub, query, subQuery, body) {
168
+ const $set = {};
169
+ for (const key in body) {
170
+ $set[`${String(sub)}.$.${key}`] = body[key];
171
+ }
172
+ const filter = query.root;
173
+ filter[sub] = { $elemMatch: subQuery };
174
+ await this._model.updateOne(filter, { $set });
175
+ return this.subFindOne(sub, query, subQuery);
176
+ }
177
+ async subDelete(sub, query, subQuery) {
178
+ const subDoc = await this.subFindOne(sub, query, subQuery);
179
+ if (!subDoc)
180
+ return null;
181
+ await this._model.updateOne({
182
+ ...query.root,
183
+ [sub]: { $elemMatch: subQuery },
184
+ }, { $pull: { [sub]: { _id: subDoc._id } } });
185
+ return subDoc;
186
+ }
187
+ async subPopulate(query, subDoc, sub, type) {
188
+ const populates = type ? this.getSubPopulate(sub, type) : [];
189
+ populates.push(...this.options.getValidPopulate(query.populate, sub));
190
+ return this.populateDoc(subDoc, populates, sub);
191
+ }
192
+ getSubPopulate(sub, type) {
193
+ const subs = this.options.subs;
194
+ if (!subs)
195
+ return [];
196
+ const docSub = subs.find((x) => x.sub === sub);
197
+ if (!docSub)
198
+ return [];
199
+ return docSub.populate.filter((x) => x[type] || x.onAll);
200
+ }
201
+ getSubDocument(key, doc) {
202
+ if (!doc)
203
+ return null;
204
+ const array = doc[key];
205
+ if (array && Array.isArray(array)) {
206
+ return array[0] || null;
207
+ }
208
+ return null;
209
+ }
210
+ toJSON(_, ret) {
211
+ if (this.options.privateFields) {
212
+ for (const key of this.options.privateFields) {
213
+ if (key in ret) {
214
+ delete ret[key];
215
+ }
216
+ }
217
+ }
218
+ }
219
+ populateDoc(doc, populates, subPath) {
220
+ return Promise.all(populates.map(async (populate) => {
221
+ try {
222
+ let subPathDoc;
223
+ if (subPath) {
224
+ subPathDoc = this._model.schema.subpaths[`${subPath}.${populate.path}`];
225
+ }
226
+ else {
227
+ subPathDoc = this._model.schema.paths[populate.path];
228
+ }
229
+ let ref = populate.ref || subPathDoc?.options.ref;
230
+ if (!ref && Array.isArray(subPathDoc?.options.type)) {
231
+ ref = subPathDoc.options.type[0].ref;
232
+ }
233
+ if (!ref) {
234
+ throw new Error(`ref was not defined in model with path ${populate.path}`);
235
+ }
236
+ const model = mongoose.model(ref);
237
+ await model.populate(doc, {
238
+ path: populate.path,
239
+ select: populate.select.join(' '),
240
+ ...(populate.populate ? { populate: populate.populate } : {}),
241
+ });
242
+ }
243
+ catch (e) {
244
+ console.error(e);
245
+ console.error({ doc, populates, path: subPath });
246
+ return Promise.resolve();
247
+ }
248
+ }));
249
+ }
250
+ }
@@ -0,0 +1,64 @@
1
+ import mongoose from 'mongoose';
2
+ import { Query } from './Query.js';
3
+ export interface IQueryOptionsPopulate<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
+ multiple?: boolean;
9
+ onFindOne?: boolean;
10
+ onPatch?: boolean;
11
+ onPost?: boolean;
12
+ onAll?: boolean;
13
+ onSearch?: boolean;
14
+ populate?: mongoose.PopulateOptions[];
15
+ }
16
+ interface IQuerySearchOptionsSub<T extends object, K extends keyof T> {
17
+ sub: K;
18
+ populate?: IQueryOptionsPopulate<T[K], keyof T[K]>[];
19
+ }
20
+ interface IQueryOptions<T extends object> {
21
+ privateFields?: (keyof T)[];
22
+ query?: (keyof T)[];
23
+ select?: (keyof T)[];
24
+ defaultSelect?: (keyof T)[] | string[];
25
+ sort?: (keyof T)[];
26
+ populate?: IQueryOptionsPopulate<T, keyof T>[];
27
+ limit?: number;
28
+ subs?: IQuerySearchOptionsSub<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 QueryOptions<T extends object> {
40
+ private opts;
41
+ constructor(opts?: IQueryOptions<T>);
42
+ get privateFields(): (keyof T)[];
43
+ get populate(): IQueryOptionsPopulate<T, keyof T>[];
44
+ get subs(): IQuerySearchOptionsSub<T, keyof T>[];
45
+ get updateDateKey(): keyof T;
46
+ get query(): (keyof T)[];
47
+ createSearchQuery(query: Query<any, T>): mongoose.FilterQuery<T>;
48
+ setPopulate(modelQuery: IModelQuery, query: Query<T>): void;
49
+ getValidPopulate(queryPopulate: IQueryPopulate[], sub?: keyof T): IQueryOptionsPopulate<any, any>[];
50
+ setLimitAndSkip(modelQuery: IModelQuery, query: Query<T>): void;
51
+ setSelect(modelQuery: IModelQuery, query: Query<T>, overrideValid?: (keyof T)[]): void;
52
+ setSort(modelQuery: IModelQuery, query: Query<T>): void;
53
+ setProjection(modelQuery: IModelQuery, query: Query<T>): void;
54
+ getSubPopulateLookup<K extends keyof T>(query: Query<T>, subOptions: IQuerySearchOptionsSub<T, keyof T>): any[];
55
+ getSubFilterConditionAndMatch(subQuery: mongoose.FilterQuery<T>, options: IQuerySearchOptionsSub<T, keyof T>): {
56
+ $filter: {
57
+ input: string;
58
+ as: string;
59
+ cond: any;
60
+ };
61
+ $match: any;
62
+ };
63
+ }
64
+ export {};
@@ -0,0 +1,200 @@
1
+ import mongoose from 'mongoose';
2
+ import { DEFAULT_LIMIT } from './utils/constants.js';
3
+ export const toValidSelectRegexp = (select) => new RegExp(`^${String(select)}(\..|$)`);
4
+ export const toValidSortRegexp = (sort) => new RegExp(`^(\\+|-)?(${String(sort)}|text)$`);
5
+ export const toValidTextRegexp = (str) => typeof str === 'string'
6
+ ? new RegExp(str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'i')
7
+ : '';
8
+ export class QueryOptions {
9
+ constructor(opts = {}) {
10
+ this.opts = opts;
11
+ }
12
+ get privateFields() {
13
+ return this.opts.privateFields;
14
+ }
15
+ get populate() {
16
+ return this.opts.populate;
17
+ }
18
+ get subs() {
19
+ return this.opts.subs;
20
+ }
21
+ get updateDateKey() {
22
+ return this.opts.updateDateKey;
23
+ }
24
+ get query() {
25
+ return this.opts.query;
26
+ }
27
+ createSearchQuery(query) {
28
+ return query.createQuery({
29
+ noRoot: false,
30
+ query: this.opts.query,
31
+ });
32
+ }
33
+ setPopulate(modelQuery, query) {
34
+ let populate = [];
35
+ if (query.populate.length > 0 && this.opts.populate) {
36
+ populate = this.getValidPopulate(query.populate).map((x) => ({
37
+ ...x,
38
+ select: x.select.join(' '),
39
+ }));
40
+ }
41
+ if (query.rootPopulate.length > 0) {
42
+ populate.push(...query.rootPopulate);
43
+ }
44
+ if (populate.length > 0) {
45
+ modelQuery.populate(populate);
46
+ }
47
+ }
48
+ getValidPopulate(queryPopulate, sub) {
49
+ let checkPop = this.opts.populate;
50
+ if (this.opts.subs && sub) {
51
+ const found = this.opts.subs.find((x) => x.sub === sub);
52
+ if (!found || !found.populate || found.populate.length === 0)
53
+ return [];
54
+ checkPop = found.populate;
55
+ }
56
+ if (!checkPop)
57
+ return [];
58
+ return queryPopulate
59
+ .map((pop) => {
60
+ const found = checkPop.find((x) => x.path === pop.path);
61
+ if (!found)
62
+ return null;
63
+ const select = pop.select?.filter((x) => found.select.includes(x)) || [];
64
+ return {
65
+ path: pop.path,
66
+ select: select.length > 0 ? select : found.select,
67
+ ...(found.populate ? { populate: found.populate } : {}),
68
+ };
69
+ })
70
+ .filter(Boolean);
71
+ }
72
+ setLimitAndSkip(modelQuery, query) {
73
+ if (query.export)
74
+ return;
75
+ modelQuery.skip(query.skip ?? 0);
76
+ modelQuery.limit(query.limit ?? this.opts.limit ?? DEFAULT_LIMIT);
77
+ }
78
+ setSelect(modelQuery, query, overrideValid) {
79
+ const valids = overrideValid || this.opts.select;
80
+ let selects = valids || [];
81
+ if (valids && valids.length > 0 && query.select.length > 0) {
82
+ selects = query.select.filter((select) => valids.some((valid) => String(select).match(toValidSelectRegexp(valid))));
83
+ }
84
+ else if (this.opts.defaultSelect) {
85
+ selects = this.opts.defaultSelect;
86
+ }
87
+ if (selects.length > 0) {
88
+ modelQuery.select(selects.join(' '));
89
+ }
90
+ }
91
+ setSort(modelQuery, query) {
92
+ if (query.sort.length > 0) {
93
+ if (this.opts.sort) {
94
+ const validSorts = query.sort.filter((x) => this.opts.sort.some((y) => String(x).match(toValidSortRegexp(y))));
95
+ modelQuery.sort(sortArrayToSortObject(validSorts, query.hasTextSearch));
96
+ }
97
+ else {
98
+ modelQuery.sort(sortArrayToSortObject(query.sort, query.hasTextSearch));
99
+ }
100
+ }
101
+ }
102
+ setProjection(modelQuery, query) {
103
+ if (query.projection) {
104
+ modelQuery.projection(query.projection);
105
+ query.projection = undefined;
106
+ }
107
+ }
108
+ getSubPopulateLookup(query, subOptions) {
109
+ const populate = query.populate;
110
+ const $lookups = [];
111
+ if (populate.length > 0) {
112
+ for (const pop of populate) {
113
+ const found = subOptions.populate.find((x) => x.path === pop.path);
114
+ if (!found)
115
+ continue;
116
+ const select = pop.select
117
+ ? found.select.filter((x) => pop.select.includes(String(x)))
118
+ : found.select;
119
+ $lookups.push(...[
120
+ {
121
+ $lookup: {
122
+ from: found.lookupRef || `${pop.path}s`,
123
+ localField: `${String(subOptions.sub)}.${pop.path}`,
124
+ foreignField: '_id',
125
+ as: pop.path,
126
+ pipeline: [{
127
+ $project: select.reduce((obj, item) => ({ ...obj, [item]: 1 }), {})
128
+ }]
129
+ },
130
+ },
131
+ ]);
132
+ }
133
+ }
134
+ return $lookups;
135
+ }
136
+ getSubFilterConditionAndMatch(subQuery, options) {
137
+ const cond = {};
138
+ let $match = {};
139
+ for (const key in subQuery) {
140
+ const v = subQuery[key];
141
+ if (key !== '$and' && !Array.isArray(v)) {
142
+ const $and = [];
143
+ const [newItem, toMatch] = queryToSubFilter(key, subQuery[key]);
144
+ if (newItem.length > 0) {
145
+ $and.push(...newItem);
146
+ }
147
+ $match = { ...$match, ...toMatch };
148
+ cond['$and'] = $and;
149
+ }
150
+ }
151
+ const $filter = { input: `$${String(options.sub)}`, as: 'subdoc', cond };
152
+ return { $filter, $match };
153
+ }
154
+ }
155
+ function queryToSubFilter(key, value) {
156
+ if (!value)
157
+ return [[], {}];
158
+ if (typeof value === 'object') {
159
+ if ('$in' in value && value.$in) {
160
+ return [
161
+ [{ $in: [`$$subdoc.${String(key)}`, value.$in.map(toCorrectIdValue)] }],
162
+ {},
163
+ ];
164
+ }
165
+ if ('$nin' in value && value.$nin) {
166
+ return [
167
+ value.$nin.map((x) => ({
168
+ $ne: [`$$subdoc.${String(key)}`, toCorrectIdValue(x)],
169
+ })),
170
+ {},
171
+ ];
172
+ }
173
+ }
174
+ return [[], { [key]: toCorrectIdValue(value) }];
175
+ }
176
+ function toCorrectIdValue(v) {
177
+ if (!v)
178
+ return v;
179
+ if (mongoose.isValidObjectId(v))
180
+ return new mongoose.Types.ObjectId(v);
181
+ return v;
182
+ }
183
+ function sortArrayToSortObject(arr, hasText) {
184
+ const sortObj = {};
185
+ for (const item of arr) {
186
+ if (item === 'text' && hasText) {
187
+ sortObj['score'] = { $meta: 'textScore' };
188
+ }
189
+ else if (item[0] === '-') {
190
+ sortObj[item.substr(1)] = -1;
191
+ }
192
+ else if (item[0] === '+') {
193
+ sortObj[item.substr(1)] = 1;
194
+ }
195
+ else {
196
+ sortObj[item] = 1;
197
+ }
198
+ }
199
+ return sortObj;
200
+ }
@@ -0,0 +1,6 @@
1
+ export * from './utils/index.js';
2
+ export * from './QueryError.js';
3
+ export * from './Query.js';
4
+ export * from './QueryModel.js';
5
+ export * from './QueryHistory.js';
6
+ export * from './QueryOptions.js';
@@ -0,0 +1,6 @@
1
+ export * from './utils/index.js';
2
+ export * from './QueryError.js';
3
+ export * from './Query.js';
4
+ export * from './QueryModel.js';
5
+ export * from './QueryHistory.js';
6
+ export * from './QueryOptions.js';
@@ -0,0 +1,40 @@
1
+ import mongoose from 'mongoose';
2
+ import { IMongooseQueryOptions } from './types.js';
3
+ export declare const toValidNumber: (v: string | number | undefined, defaultV: number) => number;
4
+ export declare function mongooseQueryOptions(query: IMongooseQueryOptions): {
5
+ limit: number;
6
+ skip: number;
7
+ sort: any[];
8
+ select: any[];
9
+ populate: any[];
10
+ page: number;
11
+ };
12
+ export declare function mongooseQuerySkipLimit(query: IMongooseQueryOptions): {
13
+ skip: number;
14
+ limit: number;
15
+ page: number;
16
+ } | {
17
+ page?: undefined;
18
+ skip: number;
19
+ limit: number;
20
+ };
21
+ export declare function mongooseQuerySortSelect(query: IMongooseQueryOptions): {
22
+ sort: any[];
23
+ select: any[];
24
+ };
25
+ export declare function mongooseQueryPopulate(query: IMongooseQueryOptions): any[];
26
+ export declare function mongooseQueryWithOperation<T extends object, Q extends mongoose.FilterQuery<T>, K extends keyof Q>(query: Q, queryKey: K, queryValue: string): {
27
+ field: K;
28
+ value: Q[K];
29
+ } | {
30
+ field: string;
31
+ value: string;
32
+ };
33
+ export declare function greaterLessThanValue<T extends object, Q extends mongoose.FilterQuery<T>, K extends keyof Q>(query: Q, operation: string, field: K, queryValue: string): {
34
+ field: K;
35
+ value: Q[K];
36
+ };
37
+ export declare function toMongooseOperation<T extends object, Q extends mongoose.FilterQuery<T>, K extends keyof Q>(query: Q, operation: string, field: K, queryValue: string): {
38
+ field: K;
39
+ value: Q[K];
40
+ };