mongodb-ops 0.8.2 → 0.9.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.
Files changed (2) hide show
  1. package/lib/mongodb-ops.js +86 -9
  2. package/package.json +1 -1
@@ -114,14 +114,8 @@ class MongoDBOps {
114
114
  queryExp = queryExp || {};
115
115
  projection = { projection: projection || {} };
116
116
 
117
- let skip, limit;
118
- if (pagination && Number.isInteger(pagination.startIndex) && Number.isInteger(pagination.endIndex)) {
119
- if (pagination.endIndex < pagination.startIndex) { return Promise.resolve([]); }
120
-
121
- let startIndex = pagination.startIndex < 1 ? 1 : pagination.startIndex;
122
- skip = startIndex - 1;
123
- limit = (pagination.endIndex - startIndex + 1) < 0 ? 0 : pagination.endIndex - startIndex + 1;
124
- }
117
+ const { skip, limit } = parsePagination(pagination);
118
+ if (limit < 1) { return Promise.resolve([]); }
125
119
 
126
120
  if (isGetCount) { docs = await db.collection(collectionName).countDocuments(queryExp); }
127
121
  else if (sort && pagination) { docs = await db.collection(collectionName).find(queryExp, projection).sort(sort).skip(skip).limit(limit).toArray(); }
@@ -151,6 +145,71 @@ class MongoDBOps {
151
145
  return Promise.resolve(await MongoDBOps.getData(collectionName, queryExp, isAggregate, projection, sort, pagination, isGetCount, this.connString));
152
146
  }
153
147
 
148
+
149
+ /**
150
+ * Static method - Use altas search at MongoDB
151
+ * {@link https://www.mongodb.com/docs/atlas/atlas-search/}
152
+ *
153
+ * @param {string} connString Database connection string
154
+ * @param {string} collectionName Collection name
155
+ * @param {object} search $search object - {@link https://www.mongodb.com/docs/atlas/atlas-search/query-syntax/}
156
+ * @param {object} [obj]
157
+ * @param {object} [obj.projection] Projection {@link https://docs.mongodb.com/manual/reference/method/db.collection.find/#find-projection}
158
+ * @param {object} [obj.sort] Sort {@link https://docs.mongodb.com/manual/reference/method/cursor.sort/#cursor.sort}
159
+ * @param {object} [obj.pagination] Pagination `E.g., { startIndex: 11, endIndex: 20 }`
160
+ * @param {boolean} [isGetCount=true] Set true to get the number of total matching docs and current page number
161
+ * @returns {promise} Promise with object or object array
162
+ */
163
+ static async search(connString, collectionName, search, { projection, sort, pagination }={}, isGetCount = true) {
164
+ if ([connString, collectionName, search].includes(undefined)) { return Promise.reject("Connection string, collection name and search cannot be undefined"); }
165
+
166
+ const { skip, limit } = parsePagination(pagination);
167
+ if (limit < 1) { return Promise.resolve([]); }
168
+
169
+ const payload = [
170
+ { $search: search },
171
+ { $addFields: { score: { $meta: "searchScore" }}},
172
+ ];
173
+
174
+ if (projection) { payload.push({ $project: projection }); }
175
+ if (sort) { payload.push({ $sort: sort }); }
176
+
177
+ const skipLimit = [];
178
+ if (skip) { skipLimit.push({ $skip: skip }); }
179
+ if (limit) { skipLimit.push({ $limit: limit }); }
180
+
181
+ if (isGetCount) {
182
+ payload.push({
183
+ $facet: {
184
+ metadata: [{ $count: "total" }, { $addFields: { page: Math.ceil((skip + 1) / limit) }}],
185
+ data: skipLimit,
186
+ }
187
+ });
188
+ }
189
+ else { payload.push(...skipLimit); }
190
+
191
+ const result = await MongoDBOps.getData(collectionName, payload, true, undefined, undefined, undefined, undefined, connString);
192
+
193
+ return Promise.resolve(isGetCount ? result[0] : result);
194
+ }
195
+
196
+ /**
197
+ * Instance method - Use altas search at MongoDB
198
+ * {@link https://www.mongodb.com/docs/atlas/atlas-search/}
199
+ *
200
+ * @param {string} collectionName Collection name
201
+ * @param {object} search $search object - {@link https://www.mongodb.com/docs/atlas/atlas-search/query-syntax/}
202
+ * @param {object} [obj]
203
+ * @param {object} [obj.projection] Projection {@link https://docs.mongodb.com/manual/reference/method/db.collection.find/#find-projection}
204
+ * @param {object} [obj.sort] Sort {@link https://docs.mongodb.com/manual/reference/method/cursor.sort/#cursor.sort}
205
+ * @param {object} [obj.pagination] Pagination `E.g., { startIndex: 11, endIndex: 20 }`
206
+ * @param {boolean} [isGetCount=true] Set true to get the number of total matching docs and current page number
207
+ * @returns {promise} Promise with object or object array
208
+ */
209
+ async search(collectionName, search, { projection, sort, pagination }={}, isGetCount = true) {
210
+ return Promise.resolve(await MongoDBOps.search(this.connString, collectionName, search, { projection, sort, pagination }, isGetCount));
211
+ }
212
+
154
213
  /**
155
214
  * Static method - Write document to MongoDB
156
215
  *
@@ -289,4 +348,22 @@ class MongoDBOps {
289
348
  }
290
349
  }
291
350
 
292
- module.exports = MongoDBOps;
351
+ module.exports = MongoDBOps;
352
+
353
+ /**
354
+ * Parse the pagaintion and return skip and limit values
355
+ *
356
+ * @param {object} obj
357
+ * @param {number} [obj.startIndex] Start index
358
+ * @param {number} [obj.endIndex] Start index
359
+ * @returns {object} Object with skip and limit values
360
+ */
361
+ const parsePagination = ({ startIndex, endIndex }={})=> {
362
+ startIndex = +startIndex; endIndex = +endIndex;
363
+ startIndex = startIndex < 1 ? 1 : startIndex;
364
+
365
+ return {
366
+ skip: Number.isInteger(startIndex) ? startIndex - 1 : undefined,
367
+ limit: Number.isInteger(startIndex) && Number.isInteger(endIndex) ? ((endIndex - startIndex + 1) < 0 ? 0 : endIndex - startIndex + 1) : undefined
368
+ };
369
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mongodb-ops",
3
- "version": "0.8.2",
3
+ "version": "0.9.0",
4
4
  "description": "Read and write ops for MongoDB",
5
5
  "main": "index.js",
6
6
  "scripts": {