beanbagdb 0.8.2 → 0.8.3

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/package.json +1 -1
  2. package/src/index.js +29 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "beanbagdb",
3
- "version": "0.8.2",
3
+ "version": "0.8.3",
4
4
  "description": "A JS library to introduce a schema layer to a No-SQL local database",
5
5
  "main": "src/index.js",
6
6
  "module": "src/index.js",
package/src/index.js CHANGED
@@ -721,10 +721,38 @@ export class BeanBagDB {
721
721
  //if (!criteria["selector"]["schema"]) {
722
722
  // throw new Error("The search criteria must contain the schema");
723
723
  //}
724
- const results = await this.db_api.search(criteria);
724
+ let results = await this.db_api.search(criteria);
725
+ let def_options = {decrypt_docs:false}
726
+ let options = { ...def_options, ...criteria?.options||{}, }
727
+ // console.log(options)
728
+ if(options["decrypt_docs"]){
729
+ if(results.docs.length>0){
730
+ results = await this._decrypt_docs(results)
731
+ }
732
+ }
733
+
725
734
  return results;
726
735
  }
727
736
 
737
+ async _decrypt_docs(search_results){
738
+ const uniqueSchemas = [...new Set(search_results.docs.map(item => item.schema))];
739
+ let schemaSearch = await this.db_api.search({
740
+ selector: { schema: "schema", "data.name":{ "$in":uniqueSchemas } },
741
+ });
742
+ //console.log(schemaSearch)
743
+ let schema = {}
744
+ schemaSearch.docs.map(itm=>{
745
+ schema[itm.data.name] = itm.data
746
+ })
747
+ //console.log(schema)
748
+ let d_results = []
749
+ for (let index = 0; index < search_results.docs.length; index++) {
750
+ const element = search_results.docs[index];
751
+ d_results.push(await this._decrypt_doc(schema[element.schema],element))
752
+ }
753
+ return {docs:d_results}
754
+ }
755
+
728
756
  /**
729
757
  * Retrieves special types of documents from the database, such as schema documents or blank documents
730
758
  * for a given schema. It handles system-related data and throws errors for invalid document types