annotations
This commit is contained in:
+63
-21
@@ -8,10 +8,19 @@ let dbo;
|
||||
*/
|
||||
class Db {
|
||||
name = 'db';
|
||||
|
||||
/**
|
||||
* Initializes the database plugin
|
||||
* @param {App} app The application instance
|
||||
*/
|
||||
init(app){
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the database plugin
|
||||
* @param {App} app The application instance
|
||||
*/
|
||||
async start(app){
|
||||
db = await MongoClient.connect(app.config.db.url, {maxPoolSize: 256});
|
||||
try {
|
||||
@@ -74,6 +83,12 @@ class Db {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a database aggregation according to a given pipeline
|
||||
* @param {string} collection Database collection name
|
||||
* @param {Object} specs aggregation definition (the pipeline)
|
||||
* @returns {Object[]} Array of records
|
||||
*/
|
||||
async aggregate(collection, specs){
|
||||
try {
|
||||
let cursor = dbo.collection(collection);
|
||||
@@ -84,6 +99,13 @@ class Db {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the distinct values for a specified field across a single collection
|
||||
* @param {string} collection Database collection name
|
||||
* @param {Object} key The target field for the distinction
|
||||
* @param {Object} query filter to be applied
|
||||
* @returns {Object[]}
|
||||
*/
|
||||
async distinct(collection, key, query){
|
||||
try {
|
||||
return await dbo.collection(collection).distinct(key, query);
|
||||
@@ -91,6 +113,13 @@ class Db {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a record in database by given key and value
|
||||
* @param {Object} collection DB collection
|
||||
* @param {Object} key The key/query which identifies the record to be updated
|
||||
* @param {Object} value The new value for the record
|
||||
* @returns {Object} The result from the update operation
|
||||
*/
|
||||
async update(collection, key, value){
|
||||
let r;
|
||||
try {
|
||||
@@ -101,6 +130,13 @@ class Db {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs partial update on a record by given key and partial value
|
||||
* @param {Object} collection Database collection
|
||||
* @param {Object} key The key/query which identifies the record to be updated
|
||||
* @param {Object} value The partial value to be updated
|
||||
* @returns {Object} The result from the update operation
|
||||
*/
|
||||
async updateSet(collection, key, value){
|
||||
let r;
|
||||
try {
|
||||
@@ -110,6 +146,11 @@ class Db {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a record from the database by given key
|
||||
* @param {Object} collection Database collection
|
||||
* @param {Object} key The key/query which identifies the record to be updated
|
||||
*/
|
||||
async remove(collection, key){
|
||||
try {
|
||||
await dbo.collection(collection).deleteMany(key);
|
||||
@@ -117,27 +158,28 @@ class Db {
|
||||
}
|
||||
}
|
||||
|
||||
convertToObjectId(object, key, recursive, result){
|
||||
if (object && object[key]){
|
||||
if (Array.isArray(object[key])){
|
||||
object[key].forEach((v, i, a)=>{
|
||||
a[i] = this.ObjectId(v);
|
||||
result && result.push(a[i]);
|
||||
})
|
||||
}else{
|
||||
let oid = this.ObjectId(object[key])
|
||||
object[key] = oid;
|
||||
result && result.push(oid);
|
||||
}
|
||||
}
|
||||
if (recursive){
|
||||
for (var k in object){
|
||||
if (typeof(object[k]) == 'object'){
|
||||
this.convertToObjectId(object[k], key, recursive, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// convertToObjectId(object, key, recursive, result){
|
||||
// if (object && object[key]){
|
||||
// if (Array.isArray(object[key])){
|
||||
// object[key].forEach((v, i, a)=>{
|
||||
// a[i] = this.ObjectId(v);
|
||||
// result && result.push(a[i]);
|
||||
// })
|
||||
// }else{
|
||||
// let oid = this.ObjectId(object[key])
|
||||
// object[key] = oid;
|
||||
// result && result.push(oid);
|
||||
// }
|
||||
// }
|
||||
// if (recursive){
|
||||
// for (var k in object){
|
||||
// if (typeof(object[k]) == 'object'){
|
||||
// this.convertToObjectId(object[k], key, recursive, result);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
sanitizeQuery(q){
|
||||
if (!q) return;
|
||||
|
||||
Reference in New Issue
Block a user