mongodb-ops 0.9.3 → 0.10.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.
@@ -92,28 +92,29 @@ class MongoDBOps {
92
92
  * @param {object} [pagination] Pagination `E.g., { startIndex: 11, endIndex: 20 }`
93
93
  * @param {boolean} [isGetCount=false] Set true to get the number of doc count based on the queryExp
94
94
  * @param {string} connString Database connection string
95
+ * @param {object} [collation] Collation {@link https://www.mongodb.com/docs/manual/reference/collation/#std-label-collation-document-fields}
96
+ * @param {object} [options] Aggregate options {@link https://www.mongodb.com/docs/manual/reference/method/db.collection.aggregate/}
95
97
  * @returns {promise} Promise with object array
96
98
  */
97
- static async getData(collectionName, queryExp, isAggregate = false, projection, sort, pagination, isGetCount = false, connString) {
99
+ static async getData(collectionName, queryExp, isAggregate = false, projection, sort, pagination, isGetCount = false, connString, collation, options) {
98
100
  const db = (await MongoDBOps.getDbClient(connString)).db();
101
+
102
+ if (isAggregate) { return Promise.resolve(await db.collection(collectionName).aggregate(queryExp, options).toArray()); }
99
103
 
100
- let docs;
101
- if (isAggregate) { docs = await db.collection(collectionName).aggregate(queryExp).toArray(); }
102
- else {
103
- queryExp = queryExp || {};
104
- projection = { projection: projection || {} };
104
+ queryExp = queryExp || {};
105
+ if (isGetCount) { return Promise.resolve(await db.collection(collectionName).countDocuments(queryExp)); }
105
106
 
106
- const { skip, limit } = parsePagination(pagination);
107
- if (limit < 1) { return Promise.resolve([]); }
107
+ const { skip, limit } = parsePagination(pagination);
108
+ if (limit < 1) { return Promise.resolve([]); }
108
109
 
109
- if (isGetCount) { docs = await db.collection(collectionName).countDocuments(queryExp); }
110
- else if (sort && pagination) { docs = await db.collection(collectionName).find(queryExp, projection).sort(sort).skip(skip).limit(limit).toArray(); }
111
- else if (!sort && pagination) { docs = await db.collection(collectionName).find(queryExp, projection).skip(skip).limit(limit).toArray(); }
112
- else if (sort && !pagination) { docs = await db.collection(collectionName).find(queryExp, projection).sort(sort).toArray(); }
113
- else { docs = await db.collection(collectionName).find(queryExp, projection).toArray(); }
114
- }
110
+ projection = { projection: projection || {} };
111
+
112
+ let data = db.collection(collectionName).find(queryExp, projection);
113
+ if (sort) { data = data.sort(sort); }
114
+ if (pagination) { data = data.skip(skip).limit(limit); }
115
+ if (collation) { data = data.collation(collation); }
115
116
 
116
- return Promise.resolve(docs);
117
+ return Promise.resolve(await data.toArray());
117
118
  }
118
119
 
119
120
  /**
@@ -128,13 +129,14 @@ class MongoDBOps {
128
129
  * @param {object} [sort] Sort {@link https://docs.mongodb.com/manual/reference/method/cursor.sort/#cursor.sort}
129
130
  * @param {object} [pagination] Pagination `E.g., { startIndex: 11, endIndex: 20 }`
130
131
  * @param {boolean} [isGetCount=false] Set true to get the number of doc count based on the queryExp
132
+ * @param {object} [collation] Collation {@link https://www.mongodb.com/docs/manual/reference/collation/#std-label-collation-document-fields}
133
+ * @param {object} [options] Aggregate options {@link https://www.mongodb.com/docs/manual/reference/method/db.collection.aggregate/}
131
134
  * @returns {promise} Promise with object array
132
135
  */
133
- async getData(collectionName, queryExp, isAggregate, projection, sort, pagination, isGetCount) {
134
- return Promise.resolve(await MongoDBOps.getData(collectionName, queryExp, isAggregate, projection, sort, pagination, isGetCount, this.connString));
136
+ async getData(collectionName, queryExp, isAggregate, projection, sort, pagination, isGetCount, collation, options) {
137
+ return Promise.resolve(await MongoDBOps.getData(collectionName, queryExp, isAggregate, projection, sort, pagination, isGetCount, this.connString, collation, options));
135
138
  }
136
139
 
137
-
138
140
  /**
139
141
  * Static method - Use altas search at MongoDB
140
142
  * {@link https://www.mongodb.com/docs/atlas/atlas-search/}
@@ -46,11 +46,12 @@ class MongoDBToolSet extends MongoDBOps {
46
46
  * @param {object} [projection] Projection {@link https://docs.mongodb.com/manual/reference/method/db.collection.find/#find-projection}
47
47
  * @param {object} [sort] Sort filter {@link https://docs.mongodb.com/manual/reference/method/cursor.sort/#cursor.sort}
48
48
  * @param {object} [pagination] Pagination `E.g., { startIndex: 11, endIndex: 20 }`
49
- * @param {string} connString Database connection string
49
+ * @param {string} connString Database connection string
50
+ * @param {object} [collation] Collation {@link https://www.mongodb.com/docs/manual/reference/collation/#std-label-collation-document-fields}
50
51
  * @returns {promise} Promise with object array
51
52
  */
52
- static async getDataByFilter(collectionName, filter, projection, sort, pagination, connString) {
53
- return Promise.resolve(await MongoDBOps.getData(collectionName, filter, false, projection, sort, pagination, undefined, connString));
53
+ static async getDataByFilter(collectionName, filter, projection, sort, pagination, connString, collation) {
54
+ return Promise.resolve(await MongoDBOps.getData(collectionName, filter, false, projection, sort, pagination, undefined, connString, collation));
54
55
  }
55
56
 
56
57
  /**
@@ -60,9 +61,32 @@ class MongoDBToolSet extends MongoDBOps {
60
61
  * @param {object} [projection] Projection {@link https://docs.mongodb.com/manual/reference/method/db.collection.find/#find-projection}
61
62
  * @param {object} [sort] Sort filter {@link https://docs.mongodb.com/manual/reference/method/cursor.sort/#cursor.sort}
62
63
  * @param {object} [pagination] Pagination `E.g., { startIndex: 11, endIndex: 20 }`
64
+ * @param {object} [collation] Collation {@link https://www.mongodb.com/docs/manual/reference/collation/#std-label-collation-document-fields}
65
+ * @returns {promise} Promise with object array
66
+ */
67
+ async getDataByFilter(filter, projection, sort, pagination, collation) { return Promise.resolve(await MongoDBToolSet.getDataByFilter(this.collectionName, filter, projection, sort, pagination, this.connString, collation)); }
68
+
69
+ /**
70
+ * Static method - Get data by aggregate
71
+ *
72
+ * @param {string} collectionName Collection name
73
+ * @param {array} pipeline Aggregate pipeline {@link https://www.mongodb.com/docs/manual/reference/operator/aggregation-pipeline/}
74
+ * @param {string} connString Database connection string
75
+ * @param {object} [options] Aggregate options {@link https://www.mongodb.com/docs/manual/reference/method/db.collection.aggregate/}
76
+ * @returns {promise} Promise with object array
77
+ */
78
+ static async getDataByAggregate(collectionName, pipeline, connString, options) {
79
+ return Promise.resolve(await MongoDBOps.getData(collectionName, pipeline, true, undefined, undefined, undefined, undefined, connString, undefined, options));
80
+ }
81
+
82
+ /**
83
+ * Instance method - Get data by aggregate
84
+ *
85
+ * @param {array} pipeline Aggregate pipeline {@link https://www.mongodb.com/docs/manual/reference/operator/aggregation-pipeline/}
86
+ * @param {object} [options] Aggregate options {@link https://www.mongodb.com/docs/manual/reference/method/db.collection.aggregate/}
63
87
  * @returns {promise} Promise with object array
64
88
  */
65
- async getDataByFilter(filter, projection, sort, pagination,) { return Promise.resolve(await MongoDBToolSet.getDataByFilter(this.collectionName, filter, projection, sort, pagination, this.connString)); }
89
+ async getDataByAggregate(pipeline, options) { return Promise.resolve(await MongoDBToolSet.getDataByAggregate(this.collectionName, pipeline, this.connString, options)); }
66
90
 
67
91
  /**
68
92
  * Static method - Get data count by query
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mongodb-ops",
3
- "version": "0.9.3",
3
+ "version": "0.10.0",
4
4
  "description": "Read and write ops for MongoDB",
5
5
  "main": "index.js",
6
6
  "scripts": {