mongodb-ops 0.8.1 → 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.
- package/lib/mongodb-ops.js +86 -9
- package/lib/mongodb-tool-set.js +37 -0
- package/package.json +1 -1
package/lib/mongodb-ops.js
CHANGED
|
@@ -114,14 +114,8 @@ class MongoDBOps {
|
|
|
114
114
|
queryExp = queryExp || {};
|
|
115
115
|
projection = { projection: projection || {} };
|
|
116
116
|
|
|
117
|
-
|
|
118
|
-
if (
|
|
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/lib/mongodb-tool-set.js
CHANGED
|
@@ -86,6 +86,43 @@ class MongoDBToolSet extends MongoDBOps {
|
|
|
86
86
|
return Promise.resolve(await MongoDBToolSet.getDataCount(this.collectionName, filter, this.connString));
|
|
87
87
|
}
|
|
88
88
|
|
|
89
|
+
/**
|
|
90
|
+
* Static method - List record with optional no. of data counts
|
|
91
|
+
*
|
|
92
|
+
* @param {string} collectionName Collection name
|
|
93
|
+
* @param {object} [query] Query filter {@link https://docs.mongodb.com/manual/core/document/#document-query-filter}
|
|
94
|
+
* @param {object} [projection] Projection {@link https://docs.mongodb.com/manual/reference/method/db.collection.find/#find-projection}
|
|
95
|
+
* @param {object} [sort] Sort filter {@link https://docs.mongodb.com/manual/reference/method/cursor.sort/#cursor.sort}
|
|
96
|
+
* @param {object} [pagination] Pagination `E.g., { startIndex: 11, endIndex: 20 }`
|
|
97
|
+
* @param {boolean|string} [showCount] Set true to return the data with total_count which is the record count on the data by query
|
|
98
|
+
* @param {string} connString Database connection string
|
|
99
|
+
* @returns {promise} Promise with data object or array
|
|
100
|
+
*/
|
|
101
|
+
static async list(collectionName, query, projection, sort, pagination, showCount, connString) {
|
|
102
|
+
if (["true", true].includes(showCount)) {
|
|
103
|
+
const [total_count, data] = await Promise.all([
|
|
104
|
+
MongoDBToolSet.getDataCount(collectionName, query, connString),
|
|
105
|
+
MongoDBToolSet.getDataByFilter(collectionName, query, projection, sort, pagination, connString)
|
|
106
|
+
]);
|
|
107
|
+
return Promise.resolve({ total_count, data });
|
|
108
|
+
}
|
|
109
|
+
else { return Promise.resolve(await MongoDBToolSet.getDataByFilter(collectionName, query, projection, sort, pagination, connString)); }
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Instance method - List record with optional no. of data counts
|
|
114
|
+
*
|
|
115
|
+
* @param {object} [query] Query filter {@link https://docs.mongodb.com/manual/core/document/#document-query-filter}
|
|
116
|
+
* @param {object} [projection] Projection {@link https://docs.mongodb.com/manual/reference/method/db.collection.find/#find-projection}
|
|
117
|
+
* @param {object} [sort] Sort filter {@link https://docs.mongodb.com/manual/reference/method/cursor.sort/#cursor.sort}
|
|
118
|
+
* @param {object} [pagination] Pagination `E.g., { startIndex: 11, endIndex: 20 }`
|
|
119
|
+
* @param {boolean|string} [showCount=false] Set true to return the data with total_count which is the record count on the data by query
|
|
120
|
+
* @returns {promise} Promise with data count
|
|
121
|
+
*/
|
|
122
|
+
async list(query, projection, sort, pagination, showCount) {
|
|
123
|
+
return Promise.resolve(await MongoDBToolSet.list(this.collectionName, query, projection, sort, pagination, showCount, this.connString));
|
|
124
|
+
}
|
|
125
|
+
|
|
89
126
|
/**
|
|
90
127
|
* Static method - Get all data
|
|
91
128
|
*
|