beanbagdb 0.5.77 → 0.6.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.
@@ -16,9 +16,176 @@ let database; // this is the global db object
16
16
  let database1
17
17
  let database2
18
18
 
19
+ let good_book_schema_1 = {
20
+ name:"book",
21
+ active:true,
22
+ description:"Test schema 1",
23
+ title:"Book",
24
+ schema: {
25
+ $schema: "http://json-schema.org/draft-07/schema#",
26
+ type: "object",
27
+ properties: {
28
+ title: {
29
+ type: "string",
30
+ minLength: 1,
31
+ description: "The title of the book",
32
+ },
33
+ author: {
34
+ type: "string",
35
+ minLength: 1,
36
+ description: "The author of the book",
37
+ },
38
+ isbn: {
39
+ type: "string",
40
+ pattern: "^(97(8|9))?\\d{9}(\\d|X)$",
41
+ description: "The ISBN of the book, can be 10 or 13 digits",
42
+ },
43
+ publicationYear: {
44
+ type: "integer",
45
+ minimum: 1450,
46
+ maximum: 2024,
47
+ description:
48
+ "The year the book was published (between 1450 and 2024)",
49
+ },
50
+ genre: {
51
+ type: "string",
52
+ enum: [
53
+ "Fiction",
54
+ "Non-Fiction",
55
+ "Science",
56
+ "History",
57
+ "Fantasy",
58
+ "Biography",
59
+ "Children",
60
+ "Mystery",
61
+ "Horror",
62
+ ],
63
+ description: "The genre of the book",
64
+ },
65
+ language: {
66
+ type: "string",
67
+ description: "The language of the book",
68
+ default: "English",
69
+ },
70
+ publisher: {
71
+ type: "string",
72
+ description: "The publisher of the book",
73
+ minLength: 1,
74
+ },
75
+ pages: {
76
+ type: "integer",
77
+ minimum: 1,
78
+ description: "The number of pages in the book",
79
+ },
80
+ },
81
+ required: ["title", "author", "isbn", "publicationYear", "genre"],
82
+ additionalProperties: false,
83
+ },
84
+ settings : {
85
+ primary_keys:['title','author'],
86
+ encrypted_fields:[],
87
+ non_editable_fields:[],
88
+ }
89
+ }
90
+
91
+ const record_good_book1 = {
92
+ title: "Harry Potter",
93
+ author: "J.K. Rowling",
94
+ isbn: "9780439139601",
95
+ publicationYear: 1999,
96
+ genre: "Fantasy",
97
+ publisher: "ABC DEF"
98
+ }
99
+
100
+ let good_book_schema_2 = {
101
+ name:"book",
102
+ title:"Book",
103
+ active:true,
104
+ description:"Test schema 1",
105
+ schema: {
106
+ $schema: "http://json-schema.org/draft-07/schema#",
107
+ type: "object",
108
+ properties: {
109
+ title: {
110
+ type: "string",
111
+ minLength: 1,
112
+ description: "The title of the book",
113
+ },
114
+ author: {
115
+ type: "string",
116
+ minLength: 1,
117
+ description: "The author of the book",
118
+ },
119
+ isbn: {
120
+ type: "string",
121
+ pattern: "^(97(8|9))?\\d{9}(\\d|X)$",
122
+ description: "The ISBN of the book, can be 10 or 13 digits",
123
+ },
124
+ publicationYear: {
125
+ type: "integer",
126
+ minimum: 1450,
127
+ maximum: 2024,
128
+ description:
129
+ "The year the book was published (between 1450 and 2024)",
130
+ },
131
+ genre: {
132
+ type: "string",
133
+ enum: [
134
+ "Fiction",
135
+ "Non-Fiction",
136
+ "Science",
137
+ "History",
138
+ "Fantasy",
139
+ "Biography",
140
+ "Children",
141
+ "Mystery",
142
+ "Horror",
143
+ ],
144
+ description: "The genre of the book",
145
+ },
146
+ language: {
147
+ type: "string",
148
+ description: "The language of the book",
149
+ default: "English",
150
+ },
151
+ publisher: {
152
+ type: "string",
153
+ description: "The publisher of the book",
154
+ minLength: 1,
155
+ },
156
+ pages: {
157
+ type: "integer",
158
+ minimum: 1,
159
+ description: "The number of pages in the book",
160
+ },
161
+ secret: {
162
+ type: "string",
163
+ description: "Super secret related to the book",
164
+ minLength: 1,
165
+ },
166
+ },
167
+ required: ["title", "author", "isbn", "publicationYear", "genre"],
168
+ additionalProperties: false,
169
+ },
170
+ settings : {
171
+ primary_keys:['title','author'],
172
+ encrypted_fields:['secret'],
173
+ non_editable_fields:[],
174
+ }
175
+ };
176
+ const record_good_book2 = {
177
+ title: "Harry Potter",
178
+ author: "J.K. Rowling",
179
+ isbn: "9780439139601",
180
+ publicationYear: 1999,
181
+ genre: "Fantasy",
182
+ publisher: "ABC DEF",
183
+ secret: "Super secret 1"
184
+ }
185
+
19
186
  describe("Successful database class init (required for further testing) ", async () => {
20
187
  it("DB init successful", () => {
21
- let doc_obj = get_pdb_doc("test_database_25", "qwertyuiopaqwsde1254");
188
+ let doc_obj = get_pdb_doc("test_database_25", "qwertyuiopaqwsde12542323");
22
189
  database = new BeanBagDB(doc_obj);
23
190
  strictEqual(
24
191
  database instanceof BeanBagDB,
@@ -399,7 +566,7 @@ describe("Schema doc insertion gives errors when", async () => {
399
566
  ];
400
567
 
401
568
  before(async () => {
402
- let doc_obj = get_pdb_doc("test_database_25", "qwertyuiopaqwsde1254");
569
+ let doc_obj = get_pdb_doc("test_database_25", "qwertyuiopaqwsde12544343");
403
570
  database = new BeanBagDB(doc_obj);
404
571
  await database.ready(); // Ensure the database is ready before running tests
405
572
  console.log("Ready for more tests...");
@@ -408,14 +575,14 @@ describe("Schema doc insertion gives errors when", async () => {
408
575
  schema_docs_invalid.forEach((element, index) => {
409
576
  it(`${element[0]}`, async () => {
410
577
  await rejects(async () => {
411
- await database.create("schema", element[1]);
578
+ await database.create({schema: "schema", data: element[1]});
412
579
  }, ValidationError);
413
580
  });
414
581
  });
415
582
  });
416
583
 
417
584
  describe("Doc insertion tests", async () => {
418
- let schema_docs_invalid = [
585
+ let book_docs_invalid = [
419
586
  [
420
587
  "error when title empty",
421
588
  {
@@ -561,86 +728,16 @@ describe("Doc insertion tests", async () => {
561
728
  ],
562
729
  ];
563
730
 
564
- const test_schema = {
565
- name:"book",
566
- description:"Test schema 1",
567
- title:"Book",
568
- schema: {
569
- $schema: "http://json-schema.org/draft-07/schema#",
570
- type: "object",
571
- properties: {
572
- title: {
573
- type: "string",
574
- minLength: 1,
575
- description: "The title of the book",
576
- },
577
- author: {
578
- type: "string",
579
- minLength: 1,
580
- description: "The author of the book",
581
- },
582
- isbn: {
583
- type: "string",
584
- pattern: "^(97(8|9))?\\d{9}(\\d|X)$",
585
- description: "The ISBN of the book, can be 10 or 13 digits",
586
- },
587
- publicationYear: {
588
- type: "integer",
589
- minimum: 1450,
590
- maximum: 2024,
591
- description:
592
- "The year the book was published (between 1450 and 2024)",
593
- },
594
- genre: {
595
- type: "string",
596
- enum: [
597
- "Fiction",
598
- "Non-Fiction",
599
- "Science",
600
- "History",
601
- "Fantasy",
602
- "Biography",
603
- "Children",
604
- "Mystery",
605
- "Horror",
606
- ],
607
- description: "The genre of the book",
608
- },
609
- language: {
610
- type: "string",
611
- description: "The language of the book",
612
- default: "English",
613
- },
614
- publisher: {
615
- type: "string",
616
- description: "The publisher of the book",
617
- minLength: 1,
618
- },
619
- pages: {
620
- type: "integer",
621
- minimum: 1,
622
- description: "The number of pages in the book",
623
- },
624
- },
625
- required: ["title", "author", "isbn", "publicationYear", "genre"],
626
- additionalProperties: false,
627
- },
628
- settings : {
629
- primary_keys:['title','author'],
630
- encrypted_fields:[],
631
- non_editable_fields:[],
632
- single_record:false
633
- }
634
- }
731
+
635
732
 
636
733
  before(async () => {
637
734
  // adding a schema
638
- let doc_obj = get_pdb_doc("test_database_26", "qwertyuiopaqwsde1254");
735
+ let doc_obj = get_pdb_doc("test_database_26", "qwertyuiopaqwsde12541234");
639
736
  database = new BeanBagDB(doc_obj);
640
737
  await database.ready(); // Ensure the database is ready before running tests
641
738
  try {
642
739
  //console.log(test_schema)
643
- let a = await database.create("schema",test_schema)
740
+ let a = await database.create({schema:"schema",data:good_book_schema_1})
644
741
  console.log("Ready for more tests...");
645
742
  } catch (error) {
646
743
  console.log("error in before")
@@ -660,7 +757,7 @@ describe("Doc insertion tests", async () => {
660
757
  it(`when inserting the book schema again, must throw error`, async () => {
661
758
  await rejects(async () => {
662
759
  try {
663
- await database.create("schema", test_schema);
760
+ await database.create({schema:"schema",data: good_book_schema_1});
664
761
  } catch (error) {
665
762
  console.log(error)
666
763
  throw error
@@ -668,23 +765,23 @@ describe("Doc insertion tests", async () => {
668
765
  }, DocCreationError);
669
766
  })
670
767
 
671
- schema_docs_invalid.forEach((element, index) => {
768
+ book_docs_invalid.forEach((element, index) => {
672
769
  it(`${element[0]}`, async () => {
673
770
  await rejects(async () => {
674
- await database.create("schema", element[1]);
771
+ await database.create({schema:"book", data:element[1]});
675
772
  }, ValidationError);
676
773
  })
677
774
  })
678
775
 
679
776
  it('successfully inserts a book doc', async () => {
680
- await expect(database.create("book", book1)).to.eventually.have.property("_id");
777
+ await expect(database.create({schema:"book", data: book1})).to.eventually.have.property("_id");
681
778
  });
682
779
 
683
780
 
684
781
  let invalid_meta = [
685
- ["invalid field",{tabs:[]}],
686
- ["invalid tags",{tags:"a,b,c"}],
687
- ["invalid link",{link:{'1':1}}],
782
+ ["invalid meta field",{tabs:[]}],
783
+ ["invalid meta.tags",{tags:"a,b,c"}],
784
+ ["invalid meta.link",{link:{'1':1}}],
688
785
  ]
689
786
 
690
787
  invalid_meta.forEach((element, index) => {
@@ -692,7 +789,7 @@ describe("Doc insertion tests", async () => {
692
789
  let bd = {...book1}
693
790
  bd.title = bd.title+" "+index
694
791
  await rejects(async () => {
695
- await database.create("schema",bd,element[1]);
792
+ await database.create({schema:"book",data:bd,meta:element[1]});
696
793
  }, ValidationError);
697
794
  })
698
795
  })
@@ -700,7 +797,7 @@ describe("Doc insertion tests", async () => {
700
797
  it('successfully inserts a book doc with a link', async () => {
701
798
  let bd = {...book1}
702
799
  bd.title = bd.title+" test1"
703
- let new_rec = await database.create("book", bd,{link:"sample1"})
800
+ let new_rec = await database.create({schema:"book", data: bd,meta:{link:"sample1"}})
704
801
  assert(new_rec.meta.link=="sample1")
705
802
  });
706
803
 
@@ -709,10 +806,10 @@ describe("Doc insertion tests", async () => {
709
806
  try {
710
807
  let bd = {...book1}
711
808
  bd.title = bd.title+" test1234"
712
- await database.create("book", bd,{link:"sample1"});
809
+ await database.create({schema:"book",data: bd,meta:{link:"sample1"}});
713
810
  } catch (error) {
714
- console.log(error)
715
- console.log("22222")
811
+ // console.log(error)
812
+ //console.log("22222")
716
813
  throw error
717
814
  }
718
815
  }, DocCreationError);
@@ -723,7 +820,7 @@ describe("Doc insertion tests", async () => {
723
820
  let bd = {...book1}
724
821
  bd.title = bd.title+" test2"
725
822
  let tags1 = ["tag1"]
726
- let new_rec = await database.create("book", bd,{tags:tags1})
823
+ let new_rec = await database.create({schema:"book", data:bd,meta:{tags:tags1}})
727
824
  assert(new_rec.meta.tags===tags1)
728
825
  });
729
826
 
@@ -732,10 +829,9 @@ describe("Doc insertion tests", async () => {
732
829
  try {
733
830
  let bd = {...book1}
734
831
  bd.title = bd.title+" test1234"
735
- await database.create("", bd,{link:"sample1"});
832
+ await database.create({data:bd,meta:{link:"sample1"}});
736
833
  } catch (error) {
737
- console.log(error)
738
- console.log("22222")
834
+ // console.log(error)
739
835
  throw error
740
836
  }
741
837
  }, DocCreationError);
@@ -746,9 +842,9 @@ describe("Doc insertion tests", async () => {
746
842
  try {
747
843
  let bd = {...book1}
748
844
  bd.title = bd.title+" test1234"
749
- await database.create("book",{},{link:"sample1"});
845
+ await database.create({schema:"book",meta:{link:"sample1"}});
750
846
  } catch (error) {
751
- console.log(error)
847
+ // console.log(error)
752
848
  throw error
753
849
  }
754
850
  }, DocCreationError);
@@ -758,95 +854,20 @@ describe("Doc insertion tests", async () => {
758
854
  })
759
855
 
760
856
  describe("Doc insertion tests with encryption", async () => {
761
- const test_schema = {
762
- name:"book",
763
- title:"Book",
764
- description:"Test schema 1",
765
- schema: {
766
- $schema: "http://json-schema.org/draft-07/schema#",
767
- type: "object",
768
- properties: {
769
- title: {
770
- type: "string",
771
- minLength: 1,
772
- description: "The title of the book",
773
- },
774
- author: {
775
- type: "string",
776
- minLength: 1,
777
- description: "The author of the book",
778
- },
779
- isbn: {
780
- type: "string",
781
- pattern: "^(97(8|9))?\\d{9}(\\d|X)$",
782
- description: "The ISBN of the book, can be 10 or 13 digits",
783
- },
784
- publicationYear: {
785
- type: "integer",
786
- minimum: 1450,
787
- maximum: 2024,
788
- description:
789
- "The year the book was published (between 1450 and 2024)",
790
- },
791
- genre: {
792
- type: "string",
793
- enum: [
794
- "Fiction",
795
- "Non-Fiction",
796
- "Science",
797
- "History",
798
- "Fantasy",
799
- "Biography",
800
- "Children",
801
- "Mystery",
802
- "Horror",
803
- ],
804
- description: "The genre of the book",
805
- },
806
- language: {
807
- type: "string",
808
- description: "The language of the book",
809
- default: "English",
810
- },
811
- publisher: {
812
- type: "string",
813
- description: "The publisher of the book",
814
- minLength: 1,
815
- },
816
- pages: {
817
- type: "integer",
818
- minimum: 1,
819
- description: "The number of pages in the book",
820
- },
821
- secret: {
822
- type: "string",
823
- description: "Super secret related to the book",
824
- minLength: 1,
825
- },
826
- },
827
- required: ["title", "author", "isbn", "publicationYear", "genre"],
828
- additionalProperties: false,
829
- },
830
- settings : {
831
- primary_keys:['title','author'],
832
- encrypted_fields:['secret'],
833
- non_editable_fields:[],
834
- single_record:false
835
- }
836
- };
857
+
837
858
 
838
859
  before(async () => {
839
860
  // adding a schema
840
- let doc_obj_orig = get_pdb_doc("test_database_27", "qwertyuiopaqwsde1254");
841
- let doc_obj_dupl = get_pdb_doc("test_database_27", "qwertyuiopaqwsde12545");
861
+ let doc_obj_orig = get_pdb_doc("test_database_27", "123456789012345612341234");
862
+ let doc_obj_dupl = get_pdb_doc("test_database_27", "098765432109876565432345");
842
863
  database1 = new BeanBagDB(doc_obj_orig);
843
864
  await database1.ready(); // Ensure the database is ready before running tests
844
865
  console.log(database1.encryption_key)
845
866
  database2 = new BeanBagDB(doc_obj_dupl);
846
867
  await database2.ready(); // Ensure the database is ready before running tests
847
868
  try {
848
- //console.log(test_schema)
849
- let a = await database1.create("schema",test_schema)
869
+ console.log(good_book_schema_2)
870
+ let a = await database1.create({schema:"schema",data:good_book_schema_2})
850
871
  } catch (error) {
851
872
  console.log("error in before")
852
873
  console.log(error)
@@ -856,9 +877,9 @@ describe("Doc insertion tests with encryption", async () => {
856
877
  it(`when inserting the book schema again in db1, must throw error`, async () => {
857
878
  await rejects(async () => {
858
879
  try {
859
- await database1.create("schema", test_schema);
880
+ await database1.create({schema:"schema",data:good_book_schema_2});
860
881
  } catch (error) {
861
- //console.log(error)
882
+ console.log(error)
862
883
  throw error
863
884
  }
864
885
  }, DocCreationError);
@@ -867,7 +888,7 @@ describe("Doc insertion tests with encryption", async () => {
867
888
  it(`when inserting the book schema again in db2 , must throw error`, async () => {
868
889
  await rejects(async () => {
869
890
  try {
870
- await database2.create("schema", test_schema);
891
+ await database2.create({schema:"schema",data:good_book_schema_2});
871
892
  } catch (error) {
872
893
  //console.log(error)
873
894
  throw error
@@ -876,31 +897,15 @@ describe("Doc insertion tests with encryption", async () => {
876
897
  });
877
898
 
878
899
  it('successfully inserts a book doc with some secret', async () => {
879
- const book1 = {
880
- title: "Harry Potter",
881
- author: "J.K. Rowling",
882
- isbn: "9780439139601",
883
- publicationYear: 1999,
884
- genre: "Fantasy",
885
- publisher: "ABC DEF",
886
- secret: "Super secret 1"
887
- }
888
- await expect(database1.create("book", book1)).to.eventually.have.property("_id");
900
+
901
+ await expect(database1.create({schema:"book",data:record_good_book2})).to.eventually.have.property("_id");
889
902
  });
890
903
 
891
904
  it('gives error when inserting the same doc again', async () => {
892
- const book1 = {
893
- title: "Harry Potter",
894
- author: "J.K. Rowling",
895
- isbn: "9780439139601",
896
- publicationYear: 1999,
897
- genre: "Fantasy",
898
- publisher: "ABC DEF",
899
- secret: "Super secret 1"
900
- };
905
+
901
906
  await rejects(async () => {
902
907
  try {
903
- await database1.create("book", book1);
908
+ await database1.create({schema:"book",data: record_good_book2});
904
909
  } catch (error) {
905
910
  //console.log(error)
906
911
  throw error
@@ -909,34 +914,16 @@ describe("Doc insertion tests with encryption", async () => {
909
914
  })
910
915
 
911
916
  it('fetches the doc and the encrypted field is returned successfully', async () => {
912
- const book1 = {
913
- title: "Harry Potter",
914
- author: "J.K. Rowling",
915
- isbn: "9780439139601",
916
- publicationYear: 1999,
917
- genre: "Fantasy",
918
- publisher: "ABC DEF",
919
- secret: "Super secret 1"
920
- };
921
- let data = await database1.read({schema:"book",data:{"title":book1.title,"author":book1.author}})
917
+
918
+ let data = await database1.read({schema:"book",data:{"title":record_good_book2.title,"author":record_good_book2.author}})
922
919
  //console.log(data)
923
- assert(data.doc.data.secret == book1.secret)
920
+ assert(data.doc.data.secret == record_good_book2.secret)
924
921
  })
925
922
 
926
923
  it('should throw encryption error when using the incorrect key', async () => {
927
- const book1 = {
928
- title: "Harry Potter",
929
- author: "J.K. Rowling",
930
- isbn: "9780439139601",
931
- publicationYear: 1999,
932
- genre: "Fantasy",
933
- publisher: "ABC DEF",
934
- secret: "Super secret 1"
935
- }
936
-
937
924
  await rejects(async () => {
938
925
  try {
939
- let d = await database2.read({schema:"book",data:{"title":book1.title,"author":book1.author}});
926
+ let d = await database2.read({schema:"book",data:{"title":record_good_book2.title,"author":record_good_book2.author}});
940
927
  } catch (error) {
941
928
  //console.log(error)
942
929
  throw error
@@ -946,7 +933,6 @@ describe("Doc insertion tests with encryption", async () => {
946
933
 
947
934
  })
948
935
 
949
-
950
936
  /**
951
937
  * read
952
938
  */
@@ -955,90 +941,6 @@ describe("Doc read tests", async () => {
955
941
  let database3
956
942
 
957
943
 
958
- const test_schema = {
959
- name:"book",
960
- description:"Test schema 1",
961
- title:"Book",
962
- schema: {
963
- $schema: "http://json-schema.org/draft-07/schema#",
964
- type: "object",
965
- properties: {
966
- title: {
967
- type: "string",
968
- minLength: 1,
969
- description: "The title of the book",
970
- },
971
- author: {
972
- type: "string",
973
- minLength: 1,
974
- description: "The author of the book",
975
- },
976
- isbn: {
977
- type: "string",
978
- pattern: "^(97(8|9))?\\d{9}(\\d|X)$",
979
- description: "The ISBN of the book, can be 10 or 13 digits",
980
- },
981
- publicationYear: {
982
- type: "integer",
983
- minimum: 1450,
984
- maximum: 2024,
985
- description:
986
- "The year the book was published (between 1450 and 2024)",
987
- },
988
- genre: {
989
- type: "string",
990
- enum: [
991
- "Fiction",
992
- "Non-Fiction",
993
- "Science",
994
- "History",
995
- "Fantasy",
996
- "Biography",
997
- "Children",
998
- "Mystery",
999
- "Horror",
1000
- ],
1001
- description: "The genre of the book",
1002
- },
1003
- language: {
1004
- type: "string",
1005
- description: "The language of the book",
1006
- default: "English",
1007
- },
1008
- publisher: {
1009
- type: "string",
1010
- description: "The publisher of the book",
1011
- minLength: 1,
1012
- },
1013
- pages: {
1014
- type: "integer",
1015
- minimum: 1,
1016
- description: "The number of pages in the book",
1017
- },
1018
- secret:{
1019
- type:"string"
1020
- }
1021
- },
1022
- required: ["title", "author"],
1023
- additionalProperties: false,
1024
- },
1025
- settings : {
1026
- primary_keys:['title','author'],
1027
- encrypted_fields:[],
1028
- non_editable_fields:["secret"],
1029
- single_record:false
1030
- }
1031
- }
1032
-
1033
- const book1 = {
1034
- title: "Harry Potter",
1035
- author: "J.K. Rowling",
1036
- isbn: "9780439139601",
1037
- publicationYear: 1999,
1038
- genre: "Fantasy",
1039
- publisher: "ABC DEF",
1040
- secret:"Super secret1"
1041
- }
1042
944
  const meta = {
1043
945
  link:"sample1"
1044
946
  }
@@ -1046,44 +948,49 @@ describe("Doc read tests", async () => {
1046
948
 
1047
949
  before(async () => {
1048
950
  // adding a schema
1049
- let doc_obj = get_pdb_doc("test_database_28", "qwertyuiopaqwsde1254");
951
+ let doc_obj = get_pdb_doc("test_database_28", "qwertyuiopaqwsde12544532");
1050
952
  database3 = new BeanBagDB(doc_obj);
1051
953
  await database3.ready(); // Ensure the database is ready before running tests
1052
954
  try {
1053
- let a = await database3.create("schema",test_schema)
1054
- doc_inserted = await database3.create("book",book1,meta)
955
+ let a = await database3.create({schema:"schema",data:good_book_schema_1})
956
+
1055
957
  console.log("Ready for more tests...");
1056
958
  } catch (error) {
1057
959
  //console.log("error in before")
1058
960
  console.log(error)
1059
961
  }
962
+ try {
963
+ doc_inserted = await database3.create({schema:"book",data:record_good_book1,meta:meta})
964
+ } catch (error) {
965
+ console.log(error)
966
+ }
1060
967
  })
1061
968
 
1062
969
  it('fetches the doc and the encrypted field is returned unencrypted successfully', async () => {
1063
- let data = await database3.read({schema:"book",data:{"title":book1.title,"author":book1.author}})
1064
- assert(data.doc.data.secret == book1.secret)
970
+ let data = await database3.read({schema:"book",data:{"title":record_good_book1.title,"author":record_good_book1.author}})
971
+ assert(data.doc.data.secret == record_good_book1.secret)
1065
972
  })
1066
973
 
1067
974
 
1068
975
  it('read doc using _id', async () => {
1069
976
  let data = await database3.read({"_id":doc_inserted._id})
1070
- assert(data.doc.data.secret == book1.secret)
977
+ assert(data.doc.data.secret == record_good_book1.secret)
1071
978
  })
1072
979
 
1073
980
  it('read doc using link', async () => {
1074
981
  let data = await database3.read({"link":meta.link})
1075
- assert(data.doc.data.secret == book1.secret)
982
+ assert(data.doc.data.secret == record_good_book1.secret)
1076
983
  })
1077
984
 
1078
985
  it('read doc using primary key', async () => {
1079
- let data = await database3.read({schema:"book",data:{title:book1.title,author:book1.author}})
1080
- assert(data.doc.data.secret == book1.secret)
986
+ let data = await database3.read({schema:"book",data:{title:record_good_book1.title,author:record_good_book1.author}})
987
+ assert(data.doc.data.secret == record_good_book1.secret)
1081
988
  })
1082
989
 
1083
990
  it('throws error when incomplete primary key given', async () => {
1084
991
  await rejects(async () => {
1085
992
  try {
1086
- let a = await database3.read({"schema":"book","data":{title:book1.title}});
993
+ let a = await database3.read({"schema":"book","data":{title:record_good_book1.title}});
1087
994
  } catch (error) {
1088
995
  //console.log(error)
1089
996
  throw error
@@ -1125,12 +1032,12 @@ describe("Doc read tests", async () => {
1125
1032
  })
1126
1033
 
1127
1034
  it('check if schema included', async () => {
1128
- let data = await database3.read({schema:"book",data:{"title":book1.title,"author":book1.author}},true)
1035
+ let data = await database3.read({schema:"book",data:{"title":record_good_book1.title,"author":record_good_book1.author},include_schema:true})
1129
1036
  assert(Object.keys(data).length==2)
1130
1037
  })
1131
1038
 
1132
1039
  it('check if schema not included', async () => {
1133
- let data = await database3.read({schema:"book",data:{"title":book1.title,"author":book1.author}},false)
1040
+ let data = await database3.read({schema:"book",data:{"title":record_good_book1.title,"author":record_good_book1.author},include_schema:false})
1134
1041
  assert(Object.keys(data).length==1)
1135
1042
  })
1136
1043
  })
@@ -1145,89 +1052,6 @@ describe("Doc read tests", async () => {
1145
1052
  describe("Doc update tests", async () => {
1146
1053
  let database3
1147
1054
 
1148
- const test_schema = {
1149
- name:"book",
1150
- description:"Test schema 1",
1151
- title:"Book",
1152
- schema: {
1153
- $schema: "http://json-schema.org/draft-07/schema#",
1154
- type: "object",
1155
- properties: {
1156
- title: {
1157
- type: "string",
1158
- minLength: 1,
1159
- description: "The title of the book",
1160
- },
1161
- author: {
1162
- type: "string",
1163
- minLength: 1,
1164
- description: "The author of the book",
1165
- },
1166
- isbn: {
1167
- type: "string",
1168
- pattern: "^(97(8|9))?\\d{9}(\\d|X)$",
1169
- description: "The ISBN of the book, can be 10 or 13 digits",
1170
- },
1171
- publicationYear: {
1172
- type: "integer",
1173
- minimum: 1450,
1174
- maximum: 2024,
1175
- description:
1176
- "The year the book was published (between 1450 and 2024)",
1177
- },
1178
- genre: {
1179
- type: "string",
1180
- enum: [
1181
- "Fiction",
1182
- "Non-Fiction",
1183
- "Science",
1184
- "History",
1185
- "Fantasy",
1186
- "Biography",
1187
- "Children",
1188
- "Mystery",
1189
- "Horror",
1190
- ],
1191
- description: "The genre of the book",
1192
- },
1193
- language: {
1194
- type: "string",
1195
- description: "The language of the book",
1196
- default: "English",
1197
- },
1198
- publisher: {
1199
- type: "string",
1200
- description: "The publisher of the book",
1201
- minLength: 1,
1202
- },
1203
- pages: {
1204
- type: "integer",
1205
- minimum: 1,
1206
- description: "The number of pages in the book",
1207
- },
1208
- secret:{
1209
- type:"string"
1210
- }
1211
- },
1212
- required: ["title", "author"],
1213
- additionalProperties: false,
1214
- },
1215
- settings : {
1216
- primary_keys:['title','author'],
1217
- non_editable_fields:['pages','genre'],
1218
- encrypted_fields:["secret"]
1219
- }
1220
- }
1221
-
1222
- const book1 = {
1223
- title: "Harry Potter",
1224
- author: "J.K. Rowling",
1225
- isbn: "9780439139601",
1226
- publicationYear: 1999,
1227
- genre: "Fantasy",
1228
- publisher: "ABC DEF",
1229
- secret:"Super secret1"
1230
- }
1231
1055
  const meta = {
1232
1056
  link:"sample1",
1233
1057
  tags:["tag1"]
@@ -1236,13 +1060,16 @@ describe("Doc update tests", async () => {
1236
1060
 
1237
1061
  before(async () => {
1238
1062
  // adding a schema
1239
- let doc_obj = get_pdb_doc("test_database_29", "qwertyuiopaqwsde1254");
1063
+ let doc_obj = get_pdb_doc("test_database_29", "qwertyuiopaqwsde12456754");
1240
1064
  database3 = new BeanBagDB(doc_obj);
1241
1065
  await database3.ready(); // Ensure the database is ready before running tests
1242
1066
  try {
1243
- let a = await database3.create("schema",test_schema)
1244
- doc_inserted = await database3.create("book",book1,meta)
1245
- let b = await database3.create("book",{...book1,title:"HP2"},{...meta,link:"sample2"})
1067
+ let test_schema = {...good_book_schema_2}
1068
+ test_schema["settings"]["non_editable_fields"] = ['pages','genre']
1069
+ test_schema["settings"]["primary_keys"] = ['title','author']
1070
+ let a = await database3.create({schema:"schema",data:test_schema})
1071
+ doc_inserted = await database3.create({schema:"book",data:record_good_book2,meta:meta})
1072
+ let b = await database3.create({schema:"book",data:{...record_good_book2,title:"HP2"},meta:{...meta,link:"sample2"}})
1246
1073
  //console.log(b)
1247
1074
  console.log("Ready for more tests...");
1248
1075
  } catch (error) {
@@ -1254,24 +1081,24 @@ describe("Doc update tests", async () => {
1254
1081
  it('error when nothing to update ', async () => {
1255
1082
  await rejects(async () => {
1256
1083
  try {
1257
- let udata = await database3.update({"_id":doc_inserted._id},{})
1084
+ let udata = await database3.update({criteria:{"_id":doc_inserted._id}})
1258
1085
  } catch (error) {
1259
- //console.log(error)
1086
+ console.log(error)
1260
1087
  throw error
1261
1088
  }
1262
1089
  }, DocUpdateError)
1263
1090
  })
1264
1091
 
1265
- it('update selected fields -1 ', async () => {
1092
+ it('update selected fields - 1 ', async () => {
1266
1093
  let updates = {publisher:"Something else"}
1267
- let udata = await database3.update({"_id":doc_inserted._id},{data:updates})
1094
+ let udata = await database3.update({criteria:{"_id":doc_inserted._id},updates:{data:updates}})
1268
1095
  let rdata= await database3.read({_id:doc_inserted._id})
1269
1096
  assert(rdata.doc.data.publisher === updates.publisher )
1270
1097
  })
1271
1098
 
1272
1099
  it('update selected fields - primary key', async () => {
1273
1100
  let updates = {title:"Something else"}
1274
- let udata = await database3.update({"_id":doc_inserted._id},{data:updates})
1101
+ let udata = await database3.update({criteria:{"_id":doc_inserted._id},updates:{data:updates}})
1275
1102
  let rdata= await database3.read({_id:doc_inserted._id})
1276
1103
  assert(rdata.doc.data.title === updates.title )
1277
1104
  })
@@ -1282,9 +1109,8 @@ describe("Doc update tests", async () => {
1282
1109
  //assert(data.doc.data.secret == book1.secret)
1283
1110
  await rejects(async () => {
1284
1111
  try {
1285
- let udata = await database3.update({"_id":doc_inserted._id},{data:updates})
1112
+ let udata = await database3.update({criteria:{"_id":doc_inserted._id},updates:{data:updates}})
1286
1113
  } catch (error) {
1287
- //console.log(error)
1288
1114
  throw error
1289
1115
  }
1290
1116
  }, DocUpdateError)
@@ -1298,14 +1124,14 @@ describe("Doc update tests", async () => {
1298
1124
 
1299
1125
  it('updating encrypted field', async () => {
1300
1126
  let updates = {secret:"Something else"}
1301
- let udata = await database3.update({"_id":doc_inserted._id},{data:updates})
1127
+ let udata = await database3.update({criteria:{"_id":doc_inserted._id},updates:{data:updates}})
1302
1128
  let rdata= await database3.read({_id:doc_inserted._id})
1303
1129
  assert(rdata.doc.data.secret === updates.secret )
1304
1130
  })
1305
1131
 
1306
1132
  it('updating meta.link', async () => {
1307
1133
  let updates = {title:"Something else"}
1308
- let udata = await database3.update({"_id":doc_inserted._id},{meta:{link:"this-is-new"}})
1134
+ let udata = await database3.update({criteria:{"_id":doc_inserted._id},updates:{meta:{link:"this-is-new"}}})
1309
1135
  //console.log(udata)
1310
1136
  let rdata= await database3.read({_id:doc_inserted._id})
1311
1137
  //console.log(rdata)
@@ -1315,7 +1141,7 @@ describe("Doc update tests", async () => {
1315
1141
  it('error updating meta.link not valid ', async () => {
1316
1142
  await rejects(async () => {
1317
1143
  try {
1318
- let udata = await database3.update({"_id":doc_inserted._id},{meta:{link:1234}})
1144
+ let udata = await database3.update({criteria:{"_id":doc_inserted._id},updates:{meta:{link:1234}}})
1319
1145
  } catch (error) {
1320
1146
  //console.log(error)
1321
1147
  throw error
@@ -1326,7 +1152,7 @@ describe("Doc update tests", async () => {
1326
1152
  it('error updating meta.link already exists ', async () => {
1327
1153
  await rejects(async () => {
1328
1154
  try {
1329
- let udata = await database3.update({"_id":doc_inserted._id},{meta:{link:"sample2"}})
1155
+ let udata = await database3.update({criteria:{"_id":doc_inserted._id},updates:{meta:{link:"sample2"}}})
1330
1156
  } catch (error) {
1331
1157
  //console.log(error)
1332
1158
  throw error
@@ -1337,14 +1163,14 @@ describe("Doc update tests", async () => {
1337
1163
 
1338
1164
  it('updating meta.tags,link at the same time ', async () => {
1339
1165
  let updates = {tags:["something","in","the","way"],link:"something-in-the-way"}
1340
- let udata = await database3.update({"_id":doc_inserted._id},{meta:updates})
1166
+ let udata = await database3.update({criteria:{"_id":doc_inserted._id},updates:{meta:updates}})
1341
1167
  let rdata= await database3.read({_id:doc_inserted._id})
1342
1168
  assert(rdata.doc.meta.tags.join(",") == updates.tags.join(",") && rdata.doc.meta.link === updates.link )
1343
1169
  })
1344
1170
 
1345
1171
  it('updating meta.tags ', async () => {
1346
1172
  let updates = {tags:["something","in","the","way","all","apologies"]}
1347
- let udata = await database3.update({"_id":doc_inserted._id},{meta:updates})
1173
+ let udata = await database3.update({criteria:{"_id":doc_inserted._id},updates:{meta:updates}})
1348
1174
  let rdata= await database3.read({_id:doc_inserted._id})
1349
1175
  assert(rdata.doc.meta.tags.join(",") == updates.tags.join(","))
1350
1176
  })
@@ -1354,7 +1180,7 @@ describe("Doc update tests", async () => {
1354
1180
  await rejects(async () => {
1355
1181
  try {
1356
1182
  let updates = {text1:"sample text 1"}
1357
- let udata = await database3.update({"_id":doc_inserted._id},{data:updates})
1183
+ let udata = await database3.update({criteria:{"_id":doc_inserted._id},updates:{data:updates}})
1358
1184
  } catch (error) {
1359
1185
  //console.log(error)
1360
1186
  throw error
@@ -1365,8 +1191,8 @@ describe("Doc update tests", async () => {
1365
1191
  it('updating only non editable fields generates error', async () => {
1366
1192
  await rejects(async () => {
1367
1193
  try {
1368
- let updates = {page:1234,genre:"Horror"}
1369
- let udata = await database3.update({"_id":doc_inserted._id},{data:updates})
1194
+ let updates1 = {page:1234,genre:"Horror"}
1195
+ let udata = await database3.update({criteria:{"_id":doc_inserted._id},"updates":{data:updates1}})
1370
1196
  } catch (error) {
1371
1197
  //console.log(error)
1372
1198
  throw error
@@ -1376,7 +1202,7 @@ describe("Doc update tests", async () => {
1376
1202
 
1377
1203
  it('updating non editable fields not allowed', async () => {
1378
1204
  let updates = {title:"HP1234",genre:"Horror"}
1379
- let udata = await database3.update({"_id":doc_inserted._id},{data:updates})
1205
+ let udata = await database3.update({criteria:{"_id":doc_inserted._id},updates:{data:updates}})
1380
1206
  let rdata= await database3.read({_id:doc_inserted._id})
1381
1207
  assert(rdata.doc.data.title == updates.title && rdata.doc.data.genre != updates.genre )
1382
1208
  })
@@ -1389,80 +1215,6 @@ describe("Doc update tests", async () => {
1389
1215
  describe("Doc delete tests", async () => {
1390
1216
  let database3
1391
1217
 
1392
- const test_schema = {
1393
- name:"book",
1394
- description:"Test schema 1",
1395
- title:"Book",
1396
- schema: {
1397
- $schema: "http://json-schema.org/draft-07/schema#",
1398
- type: "object",
1399
- properties: {
1400
- title: {
1401
- type: "string",
1402
- minLength: 1,
1403
- description: "The title of the book",
1404
- },
1405
- author: {
1406
- type: "string",
1407
- minLength: 1,
1408
- description: "The author of the book",
1409
- },
1410
- isbn: {
1411
- type: "string",
1412
- pattern: "^(97(8|9))?\\d{9}(\\d|X)$",
1413
- description: "The ISBN of the book, can be 10 or 13 digits",
1414
- },
1415
- publicationYear: {
1416
- type: "integer",
1417
- minimum: 1450,
1418
- maximum: 2024,
1419
- description:
1420
- "The year the book was published (between 1450 and 2024)",
1421
- },
1422
- genre: {
1423
- type: "string",
1424
- enum: [
1425
- "Fiction",
1426
- "Non-Fiction",
1427
- "Science",
1428
- "History",
1429
- "Fantasy",
1430
- "Biography",
1431
- "Children",
1432
- "Mystery",
1433
- "Horror",
1434
- ],
1435
- description: "The genre of the book",
1436
- },
1437
- language: {
1438
- type: "string",
1439
- description: "The language of the book",
1440
- default: "English",
1441
- },
1442
- publisher: {
1443
- type: "string",
1444
- description: "The publisher of the book",
1445
- minLength: 1,
1446
- },
1447
- pages: {
1448
- type: "integer",
1449
- minimum: 1,
1450
- description: "The number of pages in the book",
1451
- },
1452
- secret:{
1453
- type:"string"
1454
- }
1455
- },
1456
- required: ["title", "author"],
1457
- additionalProperties: false,
1458
- },
1459
- settings : {
1460
- primary_keys:['title','author'],
1461
- non_editable_fields:['pages','genre'],
1462
- encrypted_fields:["secret"]
1463
- }
1464
- }
1465
-
1466
1218
  const book1 = {
1467
1219
  title: "Harry Potter",
1468
1220
  author: "J.K. Rowling",
@@ -1479,14 +1231,17 @@ describe("Doc delete tests", async () => {
1479
1231
  let doc_inserted
1480
1232
 
1481
1233
  before(async () => {
1482
- // adding a schema
1483
- let doc_obj = get_pdb_doc("test_database_30", "qwertyuiopaqwsde1254");
1484
- database3 = new BeanBagDB(doc_obj);
1485
- await database3.ready(); // Ensure the database is ready before running tests
1486
1234
  try {
1487
- let a = await database3.create("schema",test_schema)
1488
- doc_inserted = await database3.create("book",book1,meta)
1489
- let b = await database3.create("book",{...book1,title:"HP2"},{...meta,link:"sample2"})
1235
+ let doc_obj = get_pdb_doc("test_database_30", "qwertyuiopaqwsde45451254");
1236
+ database3 = new BeanBagDB(doc_obj);
1237
+ let test_schema = {...good_book_schema_2}
1238
+ test_schema["settings"]["non_editable_fields"] = ['pages','genre']
1239
+ test_schema["settings"]["primary_keys"] = ['title','author']
1240
+
1241
+ await database3.ready(); // Ensure the database is ready before running tests
1242
+ let a = await database3.create({schema:"schema",data:test_schema})
1243
+ doc_inserted = await database3.create({schema:"book",data:book1,meta:meta})
1244
+ let b = await database3.create({schema:"book",data:{...book1,title:"HP2"},meta:{...meta,link:"sample2"}})
1490
1245
  //console.log(b)
1491
1246
  console.log("Ready for more tests...");
1492
1247
  } catch (error) {
@@ -1556,81 +1311,6 @@ describe("Doc delete tests", async () => {
1556
1311
  // search
1557
1312
  describe("Doc search tests", async () => {
1558
1313
  let database3
1559
-
1560
- const test_schema = {
1561
- name:"book",
1562
- title:"Book",
1563
- description:"Test schema 1",
1564
- schema: {
1565
- $schema: "http://json-schema.org/draft-07/schema#",
1566
- type: "object",
1567
- properties: {
1568
- title: {
1569
- type: "string",
1570
- minLength: 1,
1571
- description: "The title of the book",
1572
- },
1573
- author: {
1574
- type: "string",
1575
- minLength: 1,
1576
- description: "The author of the book",
1577
- },
1578
- isbn: {
1579
- type: "string",
1580
- pattern: "^(97(8|9))?\\d{9}(\\d|X)$",
1581
- description: "The ISBN of the book, can be 10 or 13 digits",
1582
- },
1583
- publicationYear: {
1584
- type: "integer",
1585
- minimum: 1450,
1586
- maximum: 2024,
1587
- description:
1588
- "The year the book was published (between 1450 and 2024)",
1589
- },
1590
- genre: {
1591
- type: "string",
1592
- enum: [
1593
- "Fiction",
1594
- "Non-Fiction",
1595
- "Science",
1596
- "History",
1597
- "Fantasy",
1598
- "Biography",
1599
- "Children",
1600
- "Mystery",
1601
- "Horror",
1602
- ],
1603
- description: "The genre of the book",
1604
- },
1605
- language: {
1606
- type: "string",
1607
- description: "The language of the book",
1608
- default: "English",
1609
- },
1610
- publisher: {
1611
- type: "string",
1612
- description: "The publisher of the book",
1613
- minLength: 1,
1614
- },
1615
- pages: {
1616
- type: "integer",
1617
- minimum: 1,
1618
- description: "The number of pages in the book",
1619
- },
1620
- secret:{
1621
- type:"string"
1622
- }
1623
- },
1624
- required: ["title", "author"],
1625
- additionalProperties: false,
1626
- },
1627
- settings : {
1628
- primary_keys:['title','author'],
1629
- non_editable_fields:['pages','genre'],
1630
- encrypted_fields:["secret"]
1631
- }
1632
- }
1633
-
1634
1314
  const book1 = {
1635
1315
  title: "Harry Potter",
1636
1316
  author: "J.K. Rowling",
@@ -1658,13 +1338,16 @@ describe("Doc search tests", async () => {
1658
1338
  let doc_inserted
1659
1339
  before(async () => {
1660
1340
  // adding a schema
1661
- let doc_obj = get_pdb_doc("test_database_31", "qwertyuiopaqwsde1254");
1341
+ let doc_obj = get_pdb_doc("test_database_31", "qwertyuiopaqwsde12544545");
1662
1342
  database3 = new BeanBagDB(doc_obj);
1343
+ let test_schema = {...good_book_schema_2}
1344
+ test_schema["settings"]["non_editable_fields"] = ['pages','genre']
1345
+ test_schema["settings"]["primary_keys"] = ['title','author']
1663
1346
  await database3.ready(); // Ensure the database is ready before running tests
1664
1347
  try {
1665
- let a = await database3.create("schema",test_schema)
1666
- doc_inserted = await database3.create("book",book1,meta)
1667
- let b = await database3.create("book",book2,{...meta,link:"sample2"})
1348
+ let a = await database3.create({schema:"schema",data:test_schema})
1349
+ doc_inserted = await database3.create({schema:"book",data:book1,meta:meta})
1350
+ let b = await database3.create({schema:"book",data:book2,meta:{...meta,link:"sample2"}})
1668
1351
  //console.log(b)
1669
1352
  console.log("Ready for more tests...");
1670
1353
  } catch (error) {