beanbagdb 0.5.80 → 0.6.1

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,87 +728,16 @@ describe("Doc insertion tests", async () => {
561
728
  ],
562
729
  ];
563
730
 
564
- const test_schema = {
565
- name:"book",
566
- active:true,
567
- description:"Test schema 1",
568
- title:"Book",
569
- schema: {
570
- $schema: "http://json-schema.org/draft-07/schema#",
571
- type: "object",
572
- properties: {
573
- title: {
574
- type: "string",
575
- minLength: 1,
576
- description: "The title of the book",
577
- },
578
- author: {
579
- type: "string",
580
- minLength: 1,
581
- description: "The author of the book",
582
- },
583
- isbn: {
584
- type: "string",
585
- pattern: "^(97(8|9))?\\d{9}(\\d|X)$",
586
- description: "The ISBN of the book, can be 10 or 13 digits",
587
- },
588
- publicationYear: {
589
- type: "integer",
590
- minimum: 1450,
591
- maximum: 2024,
592
- description:
593
- "The year the book was published (between 1450 and 2024)",
594
- },
595
- genre: {
596
- type: "string",
597
- enum: [
598
- "Fiction",
599
- "Non-Fiction",
600
- "Science",
601
- "History",
602
- "Fantasy",
603
- "Biography",
604
- "Children",
605
- "Mystery",
606
- "Horror",
607
- ],
608
- description: "The genre of the book",
609
- },
610
- language: {
611
- type: "string",
612
- description: "The language of the book",
613
- default: "English",
614
- },
615
- publisher: {
616
- type: "string",
617
- description: "The publisher of the book",
618
- minLength: 1,
619
- },
620
- pages: {
621
- type: "integer",
622
- minimum: 1,
623
- description: "The number of pages in the book",
624
- },
625
- },
626
- required: ["title", "author", "isbn", "publicationYear", "genre"],
627
- additionalProperties: false,
628
- },
629
- settings : {
630
- primary_keys:['title','author'],
631
- encrypted_fields:[],
632
- non_editable_fields:[],
633
- single_record:false
634
- }
635
- }
731
+
636
732
 
637
733
  before(async () => {
638
734
  // adding a schema
639
- let doc_obj = get_pdb_doc("test_database_26", "qwertyuiopaqwsde1254");
735
+ let doc_obj = get_pdb_doc("test_database_26", "qwertyuiopaqwsde12541234");
640
736
  database = new BeanBagDB(doc_obj);
641
737
  await database.ready(); // Ensure the database is ready before running tests
642
738
  try {
643
739
  //console.log(test_schema)
644
- let a = await database.create("schema",test_schema)
740
+ let a = await database.create({schema:"schema",data:good_book_schema_1})
645
741
  console.log("Ready for more tests...");
646
742
  } catch (error) {
647
743
  console.log("error in before")
@@ -661,7 +757,7 @@ describe("Doc insertion tests", async () => {
661
757
  it(`when inserting the book schema again, must throw error`, async () => {
662
758
  await rejects(async () => {
663
759
  try {
664
- await database.create("schema", test_schema);
760
+ await database.create({schema:"schema",data: good_book_schema_1});
665
761
  } catch (error) {
666
762
  console.log(error)
667
763
  throw error
@@ -669,23 +765,23 @@ describe("Doc insertion tests", async () => {
669
765
  }, DocCreationError);
670
766
  })
671
767
 
672
- schema_docs_invalid.forEach((element, index) => {
768
+ book_docs_invalid.forEach((element, index) => {
673
769
  it(`${element[0]}`, async () => {
674
770
  await rejects(async () => {
675
- await database.create("schema", element[1]);
771
+ await database.create({schema:"book", data:element[1]});
676
772
  }, ValidationError);
677
773
  })
678
774
  })
679
775
 
680
776
  it('successfully inserts a book doc', async () => {
681
- 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");
682
778
  });
683
779
 
684
780
 
685
781
  let invalid_meta = [
686
- ["invalid field",{tabs:[]}],
687
- ["invalid tags",{tags:"a,b,c"}],
688
- ["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}}],
689
785
  ]
690
786
 
691
787
  invalid_meta.forEach((element, index) => {
@@ -693,7 +789,7 @@ describe("Doc insertion tests", async () => {
693
789
  let bd = {...book1}
694
790
  bd.title = bd.title+" "+index
695
791
  await rejects(async () => {
696
- await database.create("schema",bd,element[1]);
792
+ await database.create({schema:"book",data:bd,meta:element[1]});
697
793
  }, ValidationError);
698
794
  })
699
795
  })
@@ -701,7 +797,7 @@ describe("Doc insertion tests", async () => {
701
797
  it('successfully inserts a book doc with a link', async () => {
702
798
  let bd = {...book1}
703
799
  bd.title = bd.title+" test1"
704
- let new_rec = await database.create("book", bd,{link:"sample1"})
800
+ let new_rec = await database.create({schema:"book", data: bd,meta:{link:"sample1"}})
705
801
  assert(new_rec.meta.link=="sample1")
706
802
  });
707
803
 
@@ -710,10 +806,10 @@ describe("Doc insertion tests", async () => {
710
806
  try {
711
807
  let bd = {...book1}
712
808
  bd.title = bd.title+" test1234"
713
- await database.create("book", bd,{link:"sample1"});
809
+ await database.create({schema:"book",data: bd,meta:{link:"sample1"}});
714
810
  } catch (error) {
715
- console.log(error)
716
- console.log("22222")
811
+ // console.log(error)
812
+ //console.log("22222")
717
813
  throw error
718
814
  }
719
815
  }, DocCreationError);
@@ -724,7 +820,7 @@ describe("Doc insertion tests", async () => {
724
820
  let bd = {...book1}
725
821
  bd.title = bd.title+" test2"
726
822
  let tags1 = ["tag1"]
727
- let new_rec = await database.create("book", bd,{tags:tags1})
823
+ let new_rec = await database.create({schema:"book", data:bd,meta:{tags:tags1}})
728
824
  assert(new_rec.meta.tags===tags1)
729
825
  });
730
826
 
@@ -733,10 +829,9 @@ describe("Doc insertion tests", async () => {
733
829
  try {
734
830
  let bd = {...book1}
735
831
  bd.title = bd.title+" test1234"
736
- await database.create("", bd,{link:"sample1"});
832
+ await database.create({data:bd,meta:{link:"sample1"}});
737
833
  } catch (error) {
738
- console.log(error)
739
- console.log("22222")
834
+ // console.log(error)
740
835
  throw error
741
836
  }
742
837
  }, DocCreationError);
@@ -747,9 +842,9 @@ describe("Doc insertion tests", async () => {
747
842
  try {
748
843
  let bd = {...book1}
749
844
  bd.title = bd.title+" test1234"
750
- await database.create("book",{},{link:"sample1"});
845
+ await database.create({schema:"book",meta:{link:"sample1"}});
751
846
  } catch (error) {
752
- console.log(error)
847
+ // console.log(error)
753
848
  throw error
754
849
  }
755
850
  }, DocCreationError);
@@ -759,96 +854,20 @@ describe("Doc insertion tests", async () => {
759
854
  })
760
855
 
761
856
  describe("Doc insertion tests with encryption", async () => {
762
- const test_schema = {
763
- name:"book",
764
- title:"Book",
765
- active:true,
766
- description:"Test schema 1",
767
- schema: {
768
- $schema: "http://json-schema.org/draft-07/schema#",
769
- type: "object",
770
- properties: {
771
- title: {
772
- type: "string",
773
- minLength: 1,
774
- description: "The title of the book",
775
- },
776
- author: {
777
- type: "string",
778
- minLength: 1,
779
- description: "The author of the book",
780
- },
781
- isbn: {
782
- type: "string",
783
- pattern: "^(97(8|9))?\\d{9}(\\d|X)$",
784
- description: "The ISBN of the book, can be 10 or 13 digits",
785
- },
786
- publicationYear: {
787
- type: "integer",
788
- minimum: 1450,
789
- maximum: 2024,
790
- description:
791
- "The year the book was published (between 1450 and 2024)",
792
- },
793
- genre: {
794
- type: "string",
795
- enum: [
796
- "Fiction",
797
- "Non-Fiction",
798
- "Science",
799
- "History",
800
- "Fantasy",
801
- "Biography",
802
- "Children",
803
- "Mystery",
804
- "Horror",
805
- ],
806
- description: "The genre of the book",
807
- },
808
- language: {
809
- type: "string",
810
- description: "The language of the book",
811
- default: "English",
812
- },
813
- publisher: {
814
- type: "string",
815
- description: "The publisher of the book",
816
- minLength: 1,
817
- },
818
- pages: {
819
- type: "integer",
820
- minimum: 1,
821
- description: "The number of pages in the book",
822
- },
823
- secret: {
824
- type: "string",
825
- description: "Super secret related to the book",
826
- minLength: 1,
827
- },
828
- },
829
- required: ["title", "author", "isbn", "publicationYear", "genre"],
830
- additionalProperties: false,
831
- },
832
- settings : {
833
- primary_keys:['title','author'],
834
- encrypted_fields:['secret'],
835
- non_editable_fields:[],
836
- single_record:false
837
- }
838
- };
857
+
839
858
 
840
859
  before(async () => {
841
860
  // adding a schema
842
- let doc_obj_orig = get_pdb_doc("test_database_27", "qwertyuiopaqwsde1254");
843
- 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");
844
863
  database1 = new BeanBagDB(doc_obj_orig);
845
864
  await database1.ready(); // Ensure the database is ready before running tests
846
865
  console.log(database1.encryption_key)
847
866
  database2 = new BeanBagDB(doc_obj_dupl);
848
867
  await database2.ready(); // Ensure the database is ready before running tests
849
868
  try {
850
- //console.log(test_schema)
851
- 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})
852
871
  } catch (error) {
853
872
  console.log("error in before")
854
873
  console.log(error)
@@ -858,9 +877,9 @@ describe("Doc insertion tests with encryption", async () => {
858
877
  it(`when inserting the book schema again in db1, must throw error`, async () => {
859
878
  await rejects(async () => {
860
879
  try {
861
- await database1.create("schema", test_schema);
880
+ await database1.create({schema:"schema",data:good_book_schema_2});
862
881
  } catch (error) {
863
- //console.log(error)
882
+ console.log(error)
864
883
  throw error
865
884
  }
866
885
  }, DocCreationError);
@@ -869,7 +888,7 @@ describe("Doc insertion tests with encryption", async () => {
869
888
  it(`when inserting the book schema again in db2 , must throw error`, async () => {
870
889
  await rejects(async () => {
871
890
  try {
872
- await database2.create("schema", test_schema);
891
+ await database2.create({schema:"schema",data:good_book_schema_2});
873
892
  } catch (error) {
874
893
  //console.log(error)
875
894
  throw error
@@ -878,31 +897,15 @@ describe("Doc insertion tests with encryption", async () => {
878
897
  });
879
898
 
880
899
  it('successfully inserts a book doc with some secret', async () => {
881
- const book1 = {
882
- title: "Harry Potter",
883
- author: "J.K. Rowling",
884
- isbn: "9780439139601",
885
- publicationYear: 1999,
886
- genre: "Fantasy",
887
- publisher: "ABC DEF",
888
- secret: "Super secret 1"
889
- }
890
- 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");
891
902
  });
892
903
 
893
904
  it('gives error when inserting the same doc again', async () => {
894
- const book1 = {
895
- title: "Harry Potter",
896
- author: "J.K. Rowling",
897
- isbn: "9780439139601",
898
- publicationYear: 1999,
899
- genre: "Fantasy",
900
- publisher: "ABC DEF",
901
- secret: "Super secret 1"
902
- };
905
+
903
906
  await rejects(async () => {
904
907
  try {
905
- await database1.create("book", book1);
908
+ await database1.create({schema:"book",data: record_good_book2});
906
909
  } catch (error) {
907
910
  //console.log(error)
908
911
  throw error
@@ -911,34 +914,16 @@ describe("Doc insertion tests with encryption", async () => {
911
914
  })
912
915
 
913
916
  it('fetches the doc and the encrypted field is returned successfully', async () => {
914
- const book1 = {
915
- title: "Harry Potter",
916
- author: "J.K. Rowling",
917
- isbn: "9780439139601",
918
- publicationYear: 1999,
919
- genre: "Fantasy",
920
- publisher: "ABC DEF",
921
- secret: "Super secret 1"
922
- };
923
- 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}})
924
919
  //console.log(data)
925
- assert(data.doc.data.secret == book1.secret)
920
+ assert(data.doc.data.secret == record_good_book2.secret)
926
921
  })
927
922
 
928
923
  it('should throw encryption error when using the incorrect key', async () => {
929
- const book1 = {
930
- title: "Harry Potter",
931
- author: "J.K. Rowling",
932
- isbn: "9780439139601",
933
- publicationYear: 1999,
934
- genre: "Fantasy",
935
- publisher: "ABC DEF",
936
- secret: "Super secret 1"
937
- }
938
-
939
924
  await rejects(async () => {
940
925
  try {
941
- 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}});
942
927
  } catch (error) {
943
928
  //console.log(error)
944
929
  throw error
@@ -948,7 +933,6 @@ describe("Doc insertion tests with encryption", async () => {
948
933
 
949
934
  })
950
935
 
951
-
952
936
  /**
953
937
  * read
954
938
  */
@@ -957,91 +941,6 @@ describe("Doc read tests", async () => {
957
941
  let database3
958
942
 
959
943
 
960
- const test_schema = {
961
- name:"book",
962
- description:"Test schema 1",
963
- title:"Book",
964
- active:true,
965
- schema: {
966
- $schema: "http://json-schema.org/draft-07/schema#",
967
- type: "object",
968
- properties: {
969
- title: {
970
- type: "string",
971
- minLength: 1,
972
- description: "The title of the book",
973
- },
974
- author: {
975
- type: "string",
976
- minLength: 1,
977
- description: "The author of the book",
978
- },
979
- isbn: {
980
- type: "string",
981
- pattern: "^(97(8|9))?\\d{9}(\\d|X)$",
982
- description: "The ISBN of the book, can be 10 or 13 digits",
983
- },
984
- publicationYear: {
985
- type: "integer",
986
- minimum: 1450,
987
- maximum: 2024,
988
- description:
989
- "The year the book was published (between 1450 and 2024)",
990
- },
991
- genre: {
992
- type: "string",
993
- enum: [
994
- "Fiction",
995
- "Non-Fiction",
996
- "Science",
997
- "History",
998
- "Fantasy",
999
- "Biography",
1000
- "Children",
1001
- "Mystery",
1002
- "Horror",
1003
- ],
1004
- description: "The genre of the book",
1005
- },
1006
- language: {
1007
- type: "string",
1008
- description: "The language of the book",
1009
- default: "English",
1010
- },
1011
- publisher: {
1012
- type: "string",
1013
- description: "The publisher of the book",
1014
- minLength: 1,
1015
- },
1016
- pages: {
1017
- type: "integer",
1018
- minimum: 1,
1019
- description: "The number of pages in the book",
1020
- },
1021
- secret:{
1022
- type:"string"
1023
- }
1024
- },
1025
- required: ["title", "author"],
1026
- additionalProperties: false,
1027
- },
1028
- settings : {
1029
- primary_keys:['title','author'],
1030
- encrypted_fields:[],
1031
- non_editable_fields:["secret"],
1032
- single_record:false
1033
- }
1034
- }
1035
-
1036
- const book1 = {
1037
- title: "Harry Potter",
1038
- author: "J.K. Rowling",
1039
- isbn: "9780439139601",
1040
- publicationYear: 1999,
1041
- genre: "Fantasy",
1042
- publisher: "ABC DEF",
1043
- secret:"Super secret1"
1044
- }
1045
944
  const meta = {
1046
945
  link:"sample1"
1047
946
  }
@@ -1049,44 +948,49 @@ describe("Doc read tests", async () => {
1049
948
 
1050
949
  before(async () => {
1051
950
  // adding a schema
1052
- let doc_obj = get_pdb_doc("test_database_28", "qwertyuiopaqwsde1254");
951
+ let doc_obj = get_pdb_doc("test_database_28", "qwertyuiopaqwsde12544532");
1053
952
  database3 = new BeanBagDB(doc_obj);
1054
953
  await database3.ready(); // Ensure the database is ready before running tests
1055
954
  try {
1056
- let a = await database3.create("schema",test_schema)
1057
- doc_inserted = await database3.create("book",book1,meta)
955
+ let a = await database3.create({schema:"schema",data:good_book_schema_1})
956
+
1058
957
  console.log("Ready for more tests...");
1059
958
  } catch (error) {
1060
959
  //console.log("error in before")
1061
960
  console.log(error)
1062
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
+ }
1063
967
  })
1064
968
 
1065
969
  it('fetches the doc and the encrypted field is returned unencrypted successfully', async () => {
1066
- let data = await database3.read({schema:"book",data:{"title":book1.title,"author":book1.author}})
1067
- 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)
1068
972
  })
1069
973
 
1070
974
 
1071
975
  it('read doc using _id', async () => {
1072
976
  let data = await database3.read({"_id":doc_inserted._id})
1073
- assert(data.doc.data.secret == book1.secret)
977
+ assert(data.doc.data.secret == record_good_book1.secret)
1074
978
  })
1075
979
 
1076
980
  it('read doc using link', async () => {
1077
981
  let data = await database3.read({"link":meta.link})
1078
- assert(data.doc.data.secret == book1.secret)
982
+ assert(data.doc.data.secret == record_good_book1.secret)
1079
983
  })
1080
984
 
1081
985
  it('read doc using primary key', async () => {
1082
- let data = await database3.read({schema:"book",data:{title:book1.title,author:book1.author}})
1083
- 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)
1084
988
  })
1085
989
 
1086
990
  it('throws error when incomplete primary key given', async () => {
1087
991
  await rejects(async () => {
1088
992
  try {
1089
- 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}});
1090
994
  } catch (error) {
1091
995
  //console.log(error)
1092
996
  throw error
@@ -1128,12 +1032,12 @@ describe("Doc read tests", async () => {
1128
1032
  })
1129
1033
 
1130
1034
  it('check if schema included', async () => {
1131
- 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})
1132
1036
  assert(Object.keys(data).length==2)
1133
1037
  })
1134
1038
 
1135
1039
  it('check if schema not included', async () => {
1136
- 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})
1137
1041
  assert(Object.keys(data).length==1)
1138
1042
  })
1139
1043
  })
@@ -1148,90 +1052,6 @@ describe("Doc read tests", async () => {
1148
1052
  describe("Doc update tests", async () => {
1149
1053
  let database3
1150
1054
 
1151
- const test_schema = {
1152
- name:"book",
1153
- description:"Test schema 1",
1154
- title:"Book",
1155
- active:true,
1156
- schema: {
1157
- $schema: "http://json-schema.org/draft-07/schema#",
1158
- type: "object",
1159
- properties: {
1160
- title: {
1161
- type: "string",
1162
- minLength: 1,
1163
- description: "The title of the book",
1164
- },
1165
- author: {
1166
- type: "string",
1167
- minLength: 1,
1168
- description: "The author of the book",
1169
- },
1170
- isbn: {
1171
- type: "string",
1172
- pattern: "^(97(8|9))?\\d{9}(\\d|X)$",
1173
- description: "The ISBN of the book, can be 10 or 13 digits",
1174
- },
1175
- publicationYear: {
1176
- type: "integer",
1177
- minimum: 1450,
1178
- maximum: 2024,
1179
- description:
1180
- "The year the book was published (between 1450 and 2024)",
1181
- },
1182
- genre: {
1183
- type: "string",
1184
- enum: [
1185
- "Fiction",
1186
- "Non-Fiction",
1187
- "Science",
1188
- "History",
1189
- "Fantasy",
1190
- "Biography",
1191
- "Children",
1192
- "Mystery",
1193
- "Horror",
1194
- ],
1195
- description: "The genre of the book",
1196
- },
1197
- language: {
1198
- type: "string",
1199
- description: "The language of the book",
1200
- default: "English",
1201
- },
1202
- publisher: {
1203
- type: "string",
1204
- description: "The publisher of the book",
1205
- minLength: 1,
1206
- },
1207
- pages: {
1208
- type: "integer",
1209
- minimum: 1,
1210
- description: "The number of pages in the book",
1211
- },
1212
- secret:{
1213
- type:"string"
1214
- }
1215
- },
1216
- required: ["title", "author"],
1217
- additionalProperties: false,
1218
- },
1219
- settings : {
1220
- primary_keys:['title','author'],
1221
- non_editable_fields:['pages','genre'],
1222
- encrypted_fields:["secret"]
1223
- }
1224
- }
1225
-
1226
- const book1 = {
1227
- title: "Harry Potter",
1228
- author: "J.K. Rowling",
1229
- isbn: "9780439139601",
1230
- publicationYear: 1999,
1231
- genre: "Fantasy",
1232
- publisher: "ABC DEF",
1233
- secret:"Super secret1"
1234
- }
1235
1055
  const meta = {
1236
1056
  link:"sample1",
1237
1057
  tags:["tag1"]
@@ -1240,13 +1060,16 @@ describe("Doc update tests", async () => {
1240
1060
 
1241
1061
  before(async () => {
1242
1062
  // adding a schema
1243
- let doc_obj = get_pdb_doc("test_database_29", "qwertyuiopaqwsde1254");
1063
+ let doc_obj = get_pdb_doc("test_database_29", "qwertyuiopaqwsde12456754");
1244
1064
  database3 = new BeanBagDB(doc_obj);
1245
1065
  await database3.ready(); // Ensure the database is ready before running tests
1246
1066
  try {
1247
- let a = await database3.create("schema",test_schema)
1248
- doc_inserted = await database3.create("book",book1,meta)
1249
- 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"}})
1250
1073
  //console.log(b)
1251
1074
  console.log("Ready for more tests...");
1252
1075
  } catch (error) {
@@ -1258,24 +1081,24 @@ describe("Doc update tests", async () => {
1258
1081
  it('error when nothing to update ', async () => {
1259
1082
  await rejects(async () => {
1260
1083
  try {
1261
- let udata = await database3.update({"_id":doc_inserted._id},{})
1084
+ let udata = await database3.update({criteria:{"_id":doc_inserted._id}})
1262
1085
  } catch (error) {
1263
- //console.log(error)
1086
+ console.log(error)
1264
1087
  throw error
1265
1088
  }
1266
1089
  }, DocUpdateError)
1267
1090
  })
1268
1091
 
1269
- it('update selected fields -1 ', async () => {
1092
+ it('update selected fields - 1 ', async () => {
1270
1093
  let updates = {publisher:"Something else"}
1271
- 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}})
1272
1095
  let rdata= await database3.read({_id:doc_inserted._id})
1273
1096
  assert(rdata.doc.data.publisher === updates.publisher )
1274
1097
  })
1275
1098
 
1276
1099
  it('update selected fields - primary key', async () => {
1277
1100
  let updates = {title:"Something else"}
1278
- 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}})
1279
1102
  let rdata= await database3.read({_id:doc_inserted._id})
1280
1103
  assert(rdata.doc.data.title === updates.title )
1281
1104
  })
@@ -1286,9 +1109,8 @@ describe("Doc update tests", async () => {
1286
1109
  //assert(data.doc.data.secret == book1.secret)
1287
1110
  await rejects(async () => {
1288
1111
  try {
1289
- 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}})
1290
1113
  } catch (error) {
1291
- //console.log(error)
1292
1114
  throw error
1293
1115
  }
1294
1116
  }, DocUpdateError)
@@ -1302,14 +1124,14 @@ describe("Doc update tests", async () => {
1302
1124
 
1303
1125
  it('updating encrypted field', async () => {
1304
1126
  let updates = {secret:"Something else"}
1305
- 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}})
1306
1128
  let rdata= await database3.read({_id:doc_inserted._id})
1307
1129
  assert(rdata.doc.data.secret === updates.secret )
1308
1130
  })
1309
1131
 
1310
1132
  it('updating meta.link', async () => {
1311
1133
  let updates = {title:"Something else"}
1312
- 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"}}})
1313
1135
  //console.log(udata)
1314
1136
  let rdata= await database3.read({_id:doc_inserted._id})
1315
1137
  //console.log(rdata)
@@ -1319,7 +1141,7 @@ describe("Doc update tests", async () => {
1319
1141
  it('error updating meta.link not valid ', async () => {
1320
1142
  await rejects(async () => {
1321
1143
  try {
1322
- 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}}})
1323
1145
  } catch (error) {
1324
1146
  //console.log(error)
1325
1147
  throw error
@@ -1330,7 +1152,7 @@ describe("Doc update tests", async () => {
1330
1152
  it('error updating meta.link already exists ', async () => {
1331
1153
  await rejects(async () => {
1332
1154
  try {
1333
- 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"}}})
1334
1156
  } catch (error) {
1335
1157
  //console.log(error)
1336
1158
  throw error
@@ -1341,14 +1163,14 @@ describe("Doc update tests", async () => {
1341
1163
 
1342
1164
  it('updating meta.tags,link at the same time ', async () => {
1343
1165
  let updates = {tags:["something","in","the","way"],link:"something-in-the-way"}
1344
- 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}})
1345
1167
  let rdata= await database3.read({_id:doc_inserted._id})
1346
1168
  assert(rdata.doc.meta.tags.join(",") == updates.tags.join(",") && rdata.doc.meta.link === updates.link )
1347
1169
  })
1348
1170
 
1349
1171
  it('updating meta.tags ', async () => {
1350
1172
  let updates = {tags:["something","in","the","way","all","apologies"]}
1351
- 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}})
1352
1174
  let rdata= await database3.read({_id:doc_inserted._id})
1353
1175
  assert(rdata.doc.meta.tags.join(",") == updates.tags.join(","))
1354
1176
  })
@@ -1358,7 +1180,7 @@ describe("Doc update tests", async () => {
1358
1180
  await rejects(async () => {
1359
1181
  try {
1360
1182
  let updates = {text1:"sample text 1"}
1361
- 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}})
1362
1184
  } catch (error) {
1363
1185
  //console.log(error)
1364
1186
  throw error
@@ -1369,8 +1191,8 @@ describe("Doc update tests", async () => {
1369
1191
  it('updating only non editable fields generates error', async () => {
1370
1192
  await rejects(async () => {
1371
1193
  try {
1372
- let updates = {page:1234,genre:"Horror"}
1373
- 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}})
1374
1196
  } catch (error) {
1375
1197
  //console.log(error)
1376
1198
  throw error
@@ -1380,7 +1202,7 @@ describe("Doc update tests", async () => {
1380
1202
 
1381
1203
  it('updating non editable fields not allowed', async () => {
1382
1204
  let updates = {title:"HP1234",genre:"Horror"}
1383
- 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}})
1384
1206
  let rdata= await database3.read({_id:doc_inserted._id})
1385
1207
  assert(rdata.doc.data.title == updates.title && rdata.doc.data.genre != updates.genre )
1386
1208
  })
@@ -1393,81 +1215,6 @@ describe("Doc update tests", async () => {
1393
1215
  describe("Doc delete tests", async () => {
1394
1216
  let database3
1395
1217
 
1396
- const test_schema = {
1397
- name:"book",
1398
- active:true,
1399
- description:"Test schema 1",
1400
- title:"Book",
1401
- schema: {
1402
- $schema: "http://json-schema.org/draft-07/schema#",
1403
- type: "object",
1404
- properties: {
1405
- title: {
1406
- type: "string",
1407
- minLength: 1,
1408
- description: "The title of the book",
1409
- },
1410
- author: {
1411
- type: "string",
1412
- minLength: 1,
1413
- description: "The author of the book",
1414
- },
1415
- isbn: {
1416
- type: "string",
1417
- pattern: "^(97(8|9))?\\d{9}(\\d|X)$",
1418
- description: "The ISBN of the book, can be 10 or 13 digits",
1419
- },
1420
- publicationYear: {
1421
- type: "integer",
1422
- minimum: 1450,
1423
- maximum: 2024,
1424
- description:
1425
- "The year the book was published (between 1450 and 2024)",
1426
- },
1427
- genre: {
1428
- type: "string",
1429
- enum: [
1430
- "Fiction",
1431
- "Non-Fiction",
1432
- "Science",
1433
- "History",
1434
- "Fantasy",
1435
- "Biography",
1436
- "Children",
1437
- "Mystery",
1438
- "Horror",
1439
- ],
1440
- description: "The genre of the book",
1441
- },
1442
- language: {
1443
- type: "string",
1444
- description: "The language of the book",
1445
- default: "English",
1446
- },
1447
- publisher: {
1448
- type: "string",
1449
- description: "The publisher of the book",
1450
- minLength: 1,
1451
- },
1452
- pages: {
1453
- type: "integer",
1454
- minimum: 1,
1455
- description: "The number of pages in the book",
1456
- },
1457
- secret:{
1458
- type:"string"
1459
- }
1460
- },
1461
- required: ["title", "author"],
1462
- additionalProperties: false,
1463
- },
1464
- settings : {
1465
- primary_keys:['title','author'],
1466
- non_editable_fields:['pages','genre'],
1467
- encrypted_fields:["secret"]
1468
- }
1469
- }
1470
-
1471
1218
  const book1 = {
1472
1219
  title: "Harry Potter",
1473
1220
  author: "J.K. Rowling",
@@ -1484,14 +1231,17 @@ describe("Doc delete tests", async () => {
1484
1231
  let doc_inserted
1485
1232
 
1486
1233
  before(async () => {
1487
- // adding a schema
1488
- let doc_obj = get_pdb_doc("test_database_30", "qwertyuiopaqwsde1254");
1489
- database3 = new BeanBagDB(doc_obj);
1490
- await database3.ready(); // Ensure the database is ready before running tests
1491
1234
  try {
1492
- let a = await database3.create("schema",test_schema)
1493
- doc_inserted = await database3.create("book",book1,meta)
1494
- 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"}})
1495
1245
  //console.log(b)
1496
1246
  console.log("Ready for more tests...");
1497
1247
  } catch (error) {
@@ -1561,82 +1311,6 @@ describe("Doc delete tests", async () => {
1561
1311
  // search
1562
1312
  describe("Doc search tests", async () => {
1563
1313
  let database3
1564
-
1565
- const test_schema = {
1566
- name:"book",
1567
- title:"Book",
1568
- active:true,
1569
- description:"Test schema 1",
1570
- schema: {
1571
- $schema: "http://json-schema.org/draft-07/schema#",
1572
- type: "object",
1573
- properties: {
1574
- title: {
1575
- type: "string",
1576
- minLength: 1,
1577
- description: "The title of the book",
1578
- },
1579
- author: {
1580
- type: "string",
1581
- minLength: 1,
1582
- description: "The author of the book",
1583
- },
1584
- isbn: {
1585
- type: "string",
1586
- pattern: "^(97(8|9))?\\d{9}(\\d|X)$",
1587
- description: "The ISBN of the book, can be 10 or 13 digits",
1588
- },
1589
- publicationYear: {
1590
- type: "integer",
1591
- minimum: 1450,
1592
- maximum: 2024,
1593
- description:
1594
- "The year the book was published (between 1450 and 2024)",
1595
- },
1596
- genre: {
1597
- type: "string",
1598
- enum: [
1599
- "Fiction",
1600
- "Non-Fiction",
1601
- "Science",
1602
- "History",
1603
- "Fantasy",
1604
- "Biography",
1605
- "Children",
1606
- "Mystery",
1607
- "Horror",
1608
- ],
1609
- description: "The genre of the book",
1610
- },
1611
- language: {
1612
- type: "string",
1613
- description: "The language of the book",
1614
- default: "English",
1615
- },
1616
- publisher: {
1617
- type: "string",
1618
- description: "The publisher of the book",
1619
- minLength: 1,
1620
- },
1621
- pages: {
1622
- type: "integer",
1623
- minimum: 1,
1624
- description: "The number of pages in the book",
1625
- },
1626
- secret:{
1627
- type:"string"
1628
- }
1629
- },
1630
- required: ["title", "author"],
1631
- additionalProperties: false,
1632
- },
1633
- settings : {
1634
- primary_keys:['title','author'],
1635
- non_editable_fields:['pages','genre'],
1636
- encrypted_fields:["secret"]
1637
- }
1638
- }
1639
-
1640
1314
  const book1 = {
1641
1315
  title: "Harry Potter",
1642
1316
  author: "J.K. Rowling",
@@ -1664,13 +1338,16 @@ describe("Doc search tests", async () => {
1664
1338
  let doc_inserted
1665
1339
  before(async () => {
1666
1340
  // adding a schema
1667
- let doc_obj = get_pdb_doc("test_database_31", "qwertyuiopaqwsde1254");
1341
+ let doc_obj = get_pdb_doc("test_database_31", "qwertyuiopaqwsde12544545");
1668
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']
1669
1346
  await database3.ready(); // Ensure the database is ready before running tests
1670
1347
  try {
1671
- let a = await database3.create("schema",test_schema)
1672
- doc_inserted = await database3.create("book",book1,meta)
1673
- 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"}})
1674
1351
  //console.log(b)
1675
1352
  console.log("Ready for more tests...");
1676
1353
  } catch (error) {
@@ -1694,7 +1371,7 @@ describe("Doc search tests", async () => {
1694
1371
  it('all docs', async () => {
1695
1372
  try {
1696
1373
  let udata = await database3.search({selector:{}})
1697
- assert(udata.docs.length==12)
1374
+ assert(udata.docs.length==13)
1698
1375
  } catch (error) {
1699
1376
  //console.log(error)
1700
1377
  throw error
@@ -1714,7 +1391,7 @@ describe("Doc search tests", async () => {
1714
1391
  it('read docs 2', async () => {
1715
1392
  try {
1716
1393
  let udata = await database3.search({selector:{"schema":"schema"}})
1717
- assert(udata.docs.length==8) // schema,book,setting,key,edge,edge_constraints
1394
+ assert(udata.docs.length==9) // schema,book,setting,key,edge,edge_constraints
1718
1395
  } catch (error) {
1719
1396
  //console.log(error)
1720
1397
  throw error