beanbagdb 0.5.77 → 0.5.80

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "beanbagdb",
3
- "version": "0.5.77",
3
+ "version": "0.5.80",
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
@@ -451,6 +451,11 @@ export class BeanBagDB {
451
451
  // }
452
452
  // }
453
453
 
454
+ // system generated schemas cannot be edited
455
+ if(full_doc.schema=="schema"&&full_doc.data.system_generated==true){
456
+ throw new DocUpdateError("System schemas cannot be updated using this API");
457
+ }
458
+
454
459
  // update new value depending on settings.non_editable_fields (if does not exists, all fields are editable)
455
460
  let all_fields = Object.keys(schema.schema.properties);
456
461
  let unedit_fields = schema.settings["non_editable_fields"];
@@ -462,7 +467,7 @@ export class BeanBagDB {
462
467
  // todo : what if additionalField are allowed ??
463
468
  let updated_data = { ...full_doc.data, ...allowed_updates };
464
469
 
465
- this.util_validate_data(schema.schema, updated_data);
470
+ updated_data = this.util_validate_data(schema.schema, updated_data);
466
471
 
467
472
  // primary key check if multiple records can be created
468
473
  if (schema.settings["primary_keys"].length > 0) {
@@ -489,7 +494,7 @@ export class BeanBagDB {
489
494
  let m_sch = sys_sch.editable_metadata_schema;
490
495
  let editable_fields = Object.keys(m_sch["properties"]);
491
496
  let allowed_meta = this.util_filter_object(updates.meta, editable_fields);
492
- this.util_validate_data(m_sch, allowed_meta);
497
+ allowed_meta = this.util_validate_data(m_sch, allowed_meta);
493
498
  // if update has a link ,then check if it already exists
494
499
  if (allowed_meta.link){
495
500
  let search = await this.search({ selector: {"meta.link":allowed_meta.link} })
@@ -652,6 +657,7 @@ export class BeanBagDB {
652
657
  system_defined : doc.data.system_generated,
653
658
  description: doc.data.description,
654
659
  link: doc.meta.link,
660
+ title:doc.data.title,
655
661
  _id:doc._id
656
662
  })
657
663
  })
@@ -951,9 +957,9 @@ async _upgrade_schema_in_bulk(schemas,log_upgrade=false,log_message="Schema Upgr
951
957
  * @returns {Object}
952
958
  */
953
959
  _get_blank_schema_doc(schema_name, schema_object, data) {
954
- this.util_validate_data(schema_object, data);
960
+ let new_data = this.util_validate_data(schema_object, data);
955
961
  let obj = this._get_blank_doc(schema_name);
956
- obj["data"] = data;
962
+ obj["data"] = new_data;
957
963
  return obj;
958
964
  }
959
965
 
@@ -1024,11 +1030,13 @@ async _upgrade_schema_in_bulk(schemas,log_upgrade=false,log_message="Schema Upgr
1024
1030
  if (sch_search.docs.length == 0) {throw new DocCreationError(`The schema "${schema}" does not exists`)}
1025
1031
  let schemaDoc = sch_search.docs[0]["data"];
1026
1032
  // validate data
1027
- this.util_validate_data(schemaDoc.schema, data);
1033
+ if(!schemaDoc.active){throw new DocCreationError(`The schema "${schema}" is not active`)}
1034
+
1035
+ let new_data = this.util_validate_data(schemaDoc.schema, data);
1028
1036
 
1029
1037
  // validate meta
1030
- if(Object.keys.length>0){
1031
- this.util_validate_data(sys_sch.editable_metadata_schema, meta)
1038
+ if(Object.keys(meta).length>0){
1039
+ meta = this.util_validate_data(sys_sch.editable_metadata_schema, meta)
1032
1040
  }
1033
1041
 
1034
1042
 
@@ -1042,25 +1050,25 @@ async _upgrade_schema_in_bulk(schemas,log_upgrade=false,log_message="Schema Upgr
1042
1050
  // @TODO : for schema dos: settings fields must be in schema field
1043
1051
  if (schema == "schema") {
1044
1052
  //more checks are required
1045
- this.util_validate_schema_object(data);
1053
+ this.util_validate_schema_object(new_data);
1046
1054
  }
1047
1055
  // @TODO : check if single record setting is set to true
1048
1056
  //console.log(schemaDoc)
1049
1057
  // duplicate check
1050
1058
  if (schemaDoc.settings["primary_keys"].length > 0) {
1051
1059
  let primary_obj = { schema: schema };
1052
- schemaDoc.settings["primary_keys"].map((ky) => {primary_obj["data." + ky] = data[ky];});
1060
+ schemaDoc.settings["primary_keys"].map((ky) => {primary_obj["data." + ky] = new_data[ky];});
1053
1061
  let prim_search = await this.search({ selector: primary_obj });
1054
1062
  if (prim_search.docs.length > 0) {
1055
1063
  throw new DocCreationError(`Document with the given primary key (${schemaDoc.settings["primary_keys"].join(",")}) already exists in the schema "${schema}"`);
1056
1064
  }
1057
1065
  }
1058
1066
  // encrypt if required
1059
- let new_data = { ...data };
1067
+
1060
1068
  if (schemaDoc.settings["encrypted_fields"].length > 0) {
1061
1069
  // todo test if encryption is successful
1062
1070
  for (let itm of schemaDoc.settings["encrypted_fields"]) {
1063
- new_data[itm] = await this.utils.encrypt(data[itm], this.encryption_key);
1071
+ new_data[itm] = await this.utils.encrypt(new_data[itm], this.encryption_key);
1064
1072
  }
1065
1073
  }
1066
1074
 
@@ -1124,16 +1132,18 @@ async _upgrade_schema_in_bulk(schemas,log_upgrade=false,log_message="Schema Upgr
1124
1132
 
1125
1133
 
1126
1134
  /**
1127
- * Validates a data object against a provided JSON schema
1135
+ * Validates a data object against a provided JSON schema and returns a valid data object (with default value for missing field for which default values are defined in the schema )
1128
1136
  * It relies on the external API provided by the user
1129
1137
  * @param {Object} schema_obj - The JSON schema object to validate against
1130
1138
  * @param {Object} data_obj - The data object to validate
1131
1139
  * @throws {Error} If the data object does not conform to the schema
1132
1140
  */
1133
1141
  util_validate_data(schema_obj, data_obj) {
1134
- const { valid, validate } = this.utils.validate_schema(schema_obj,data_obj)
1142
+ const { valid, validate , data} = this.utils.validate_schema(schema_obj,data_obj)
1135
1143
  if (!valid) {
1136
1144
  throw new ValidationError(validate.errors);
1145
+ }else{
1146
+ return data
1137
1147
  }
1138
1148
  }
1139
1149
 
@@ -6,9 +6,10 @@ export const default_app = {
6
6
  schemas:[
7
7
  {
8
8
  name: "schema",
9
+ active:true,
9
10
  description:"Meta-schema or the schema for defining other schemas",
10
11
  system_generated:true,
11
- version:0.86,
12
+ version:0.87,
12
13
  title:"Schema document",
13
14
  schema: {
14
15
  type: "object",
@@ -19,6 +20,11 @@ export const default_app = {
19
20
  type:"boolean",
20
21
  default:false
21
22
  },
23
+ active:{
24
+ title:"This indicates where new documents can be created using this schema or not. Old documents can still be edited",
25
+ type:"boolean",
26
+ default:false
27
+ },
22
28
  version: {
23
29
  type: "number",
24
30
  title:"Version",
@@ -34,19 +40,19 @@ export const default_app = {
34
40
  pattern: "^[a-zA-Z][a-zA-Z0-9_]*$",
35
41
  description:"This is the name of the schema.It cannot be changed later"
36
42
  },
37
- description:{
43
+ title:{
38
44
  type:"string",
39
- title:"About",
45
+ title:"Title",
40
46
  minLength:0,
41
47
  maxLength:1000,
42
- description:"A small description of what data in this schema stores."
48
+ description:"A title to display with records."
43
49
  },
44
- title:{
50
+ description:{
45
51
  type:"string",
46
52
  title:"About",
47
53
  minLength:0,
48
54
  maxLength:1000,
49
- description:"A title to display with records."
55
+ description:"A small description of what data in this schema stores."
50
56
  },
51
57
  schema: {
52
58
  type: "object",
@@ -55,6 +61,7 @@ export const default_app = {
55
61
  minProperties: 1,
56
62
  maxProperties: 50,
57
63
  description:"This must be a valid JSON Schema which will be used to validate documents created with this schema.See this https://tour.json-schema.org/",
64
+ default:{}
58
65
  },
59
66
  settings: {
60
67
  type: "object",
@@ -110,17 +117,18 @@ export const default_app = {
110
117
  },
111
118
  settings: {
112
119
  primary_keys: ["name"],
113
- non_editable_fields:[],
120
+ non_editable_fields:["system_generated"],
114
121
  encrypted_fields:[],
115
- display_fields:["name","version","description","title"]
122
+ display_fields:["name","version","description","title","active"]
116
123
  },
117
124
  },
118
125
  {
119
126
  system_generated:true,
120
- version:0.62,
127
+ version:0.63,
121
128
  description:"To store user defined key. this can include anything like API tokens etc. There is a special method to fetch this. The values are encrypted",
122
129
  name: "system_key",
123
130
  title:"System key",
131
+ active:true,
124
132
  schema: {
125
133
  type: "object",
126
134
  additionalProperties: true,
@@ -153,11 +161,12 @@ export const default_app = {
153
161
  },
154
162
  },
155
163
  {
156
- version:0.66,
164
+ version:0.67,
157
165
  system_generated:true,
158
166
  description:"The system relies on these settings for proper functioning or enabling optional features.",
159
167
  name: "system_setting",
160
168
  title:"System Setting",
169
+ active:true,
161
170
  schema: {
162
171
  required:["name","value"],
163
172
  type: "object",
@@ -184,7 +193,8 @@ export const default_app = {
184
193
  name:"system_edge_constraint",
185
194
  title:"Edge constraint",
186
195
  system_generated:true,
187
- version:0.51,
196
+ active:true,
197
+ version:0.52,
188
198
  description: "To define edge constraints for simple directed graph of records.",
189
199
  schema:{
190
200
  type: "object",
@@ -236,8 +246,9 @@ export const default_app = {
236
246
  {
237
247
  name:"system_edge",
238
248
  title:"Edge in the system graph",
249
+ active:true,
239
250
  system_generated:true,
240
- version:0.51,
251
+ version:0.52,
241
252
  description: "To define edges in the simple directed graph of records.",
242
253
  schema:{
243
254
  type: "object",
@@ -264,8 +275,9 @@ export const default_app = {
264
275
  {
265
276
  name:"system_media",
266
277
  title:"Media content",
278
+ active:true,
267
279
  system_generated:true,
268
- version:0.61,
280
+ version:0.62,
269
281
  description: "To store images as Base64",
270
282
  schema:{
271
283
  type: "object",
@@ -296,7 +308,8 @@ export const default_app = {
296
308
  name:"system_log",
297
309
  system_generated:true,
298
310
  title:"System log",
299
- version:0.51,
311
+ active:true,
312
+ version:0.52,
300
313
  description: "To define edges in the simple directed graph of records.",
301
314
  schema:{
302
315
  type: "object",
@@ -563,6 +563,7 @@ describe("Doc insertion tests", async () => {
563
563
 
564
564
  const test_schema = {
565
565
  name:"book",
566
+ active:true,
566
567
  description:"Test schema 1",
567
568
  title:"Book",
568
569
  schema: {
@@ -761,6 +762,7 @@ describe("Doc insertion tests with encryption", async () => {
761
762
  const test_schema = {
762
763
  name:"book",
763
764
  title:"Book",
765
+ active:true,
764
766
  description:"Test schema 1",
765
767
  schema: {
766
768
  $schema: "http://json-schema.org/draft-07/schema#",
@@ -959,6 +961,7 @@ describe("Doc read tests", async () => {
959
961
  name:"book",
960
962
  description:"Test schema 1",
961
963
  title:"Book",
964
+ active:true,
962
965
  schema: {
963
966
  $schema: "http://json-schema.org/draft-07/schema#",
964
967
  type: "object",
@@ -1149,6 +1152,7 @@ describe("Doc update tests", async () => {
1149
1152
  name:"book",
1150
1153
  description:"Test schema 1",
1151
1154
  title:"Book",
1155
+ active:true,
1152
1156
  schema: {
1153
1157
  $schema: "http://json-schema.org/draft-07/schema#",
1154
1158
  type: "object",
@@ -1391,6 +1395,7 @@ describe("Doc delete tests", async () => {
1391
1395
 
1392
1396
  const test_schema = {
1393
1397
  name:"book",
1398
+ active:true,
1394
1399
  description:"Test schema 1",
1395
1400
  title:"Book",
1396
1401
  schema: {
@@ -1560,6 +1565,7 @@ describe("Doc search tests", async () => {
1560
1565
  const test_schema = {
1561
1566
  name:"book",
1562
1567
  title:"Book",
1568
+ active:true,
1563
1569
  description:"Test schema 1",
1564
1570
  schema: {
1565
1571
  $schema: "http://json-schema.org/draft-07/schema#",
package/test/pouchdb.js CHANGED
@@ -69,10 +69,12 @@ const pdb = new PouchDB(dbname);
69
69
  // @TODO ping the database to check connectivity when class is ready to use
70
70
  },
71
71
  validate_schema: (schema_obj, data_obj)=>{
72
- const ajv = new Ajv({code: {esm: true}}) // options can be passed, e.g. {allErrors: true}
72
+ const ajv = new Ajv({code: {esm: true},strict:false,useDefaults:true}) // options can be passed, e.g. {allErrors: true}
73
+ const data_copy = {...data_obj}
73
74
  const validate = ajv.compile(schema_obj);
74
- const valid = validate(data_obj);
75
- return {valid,validate}
75
+ const valid = validate(data_copy);
76
+
77
+ return {valid,validate,data:data_copy}
76
78
  }
77
79
  },
78
80
  }
package/test/test1.js CHANGED
@@ -5,157 +5,116 @@ import {BeanBagDB} from '../src/index.js';
5
5
  import {text_command} from "../src/plugins/text_command.js"
6
6
 
7
7
  (async()=>{
8
-
9
- // let schema_docs_invalid = [
10
- // {
11
- // name: "",
12
- // description: "",
13
- // schema: {},
14
- // settings: {},
15
- // }
16
- // ]
17
- // let doc_obj = get_pdb_doc("test_database_27","qwertyuiopaqwsde1254")
18
- // let database1 = new BeanBagDB(doc_obj);
19
- // // await database.ready()
20
- // // let a = await database.create("schema",schema_docs_invalid[0])
21
-
22
- // const test_schema = {
23
- // name:"book",
24
- // description:"Test schema 1",
25
- // schema: {
26
- // $schema: "http://json-schema.org/draft-07/schema#",
27
- // type: "object",
28
- // properties: {
29
- // title: {
30
- // type: "string",
31
- // minLength: 1,
32
- // description: "The title of the book",
33
- // },
34
- // author: {
35
- // type: "string",
36
- // minLength: 1,
37
- // description: "The author of the book",
38
- // },
39
- // isbn: {
40
- // type: "string",
41
- // pattern: "^(97(8|9))?\\d{9}(\\d|X)$",
42
- // description: "The ISBN of the book, can be 10 or 13 digits",
43
- // },
44
- // publicationYear: {
45
- // type: "integer",
46
- // minimum: 1450,
47
- // maximum: 2024,
48
- // description:
49
- // "The year the book was published (between 1450 and 2024)",
50
- // },
51
- // genre: {
52
- // type: "string",
53
- // enum: [
54
- // "Fiction",
55
- // "Non-Fiction",
56
- // "Science",
57
- // "History",
58
- // "Fantasy",
59
- // "Biography",
60
- // "Children",
61
- // "Mystery",
62
- // "Horror",
63
- // ],
64
- // description: "The genre of the book",
65
- // },
66
- // language: {
67
- // type: "string",
68
- // description: "The language of the book",
69
- // default: "English",
70
- // },
71
- // publisher: {
72
- // type: "string",
73
- // description: "The publisher of the book",
74
- // minLength: 1,
75
- // },
76
- // pages: {
77
- // type: "integer",
78
- // minimum: 1,
79
- // description: "The number of pages in the book",
80
- // },
81
- // secret: {
82
- // type: "string",
83
- // description: "Super secret related to the book",
84
- // minLength: 1,
85
- // },
86
- // },
87
- // required: ["title", "author", "isbn", "publicationYear", "genre"],
88
- // additionalProperties: false,
89
- // },
90
- // settings : {
91
- // primary_keys:['title','author'],
92
- // encrypted_fields:['secret'],
93
- // non_editable_fields:[],
94
- // single_record:false
95
- // }
96
- // };
97
-
98
-
99
- // await database1.ready(); // Ensure the database is ready before running tests
100
-
101
- // try {
102
- // //console.log(test_schema)
103
- // let a = await database1.create("schema",test_schema)
104
-
105
- // } catch (error) {
106
- // console.log("error in before")
107
- // console.log(error)
108
- // }
109
-
110
- // const book1 = {
111
- // title: "Harry Potter",
112
- // author: "J.K. Rowling",
113
- // isbn: "9780439139601",
114
- // publicationYear: 1999,
115
- // genre: "Fantasy",
116
- // publisher: "ABC DEF",
117
- // secret: "Super secret 1"
118
- // };
119
-
120
- // let d
121
- // try {
122
- // d = await database1.create("book", book1,{link:"sample1"});
123
- // console.log(d)
124
- // let rec = await database1.read({"_id":d._id});
125
- // console.log(rec)
126
-
127
- // let e = await database1.create("book", {...book1,title:"Something"},{link:"sample2"});
128
- // console.log(e)
129
-
130
- // } catch (error) {
131
- // console.log(error)
132
- // throw error
133
- // }
134
-
135
-
136
- // try {
137
- // let s = await database1.search({selector:{}})
138
- // console.log(s.docs.length)
139
- // //console.log( JSON.stringify(s,null,2))
140
- // } catch (error) {
141
- // console.log(error)
142
- // }
143
-
144
-
145
8
  let database; // this is the global db object
146
9
  let doc_obj = get_pdb_doc("test_database_40", "qwertyuiopaqwsde1254");
147
10
  database = new BeanBagDB(doc_obj);
148
11
  await database.ready(); // Ensure the database is ready before running tests
149
- await database.load_plugin("txtcmd",text_command)
150
- console.log()
151
- let command = await database.plugins["txtcmd"].parse_and_run("new/schema")
152
- console.log(command)
153
- let command2 = await database.plugins["txtcmd"].parse_and_run("new")
154
- console.log(command2)
155
- let command3 = await database.plugins["txtcmd"].parse_and_run("open/link/thunder-kangchenjunga-mango")
156
- console.log(command3)
157
- let command4 = await database.plugins["txtcmd"].parse_and_run("tool/info")
158
- console.log(command4)
12
+ // await database.load_plugin("txtcmd",text_command)
13
+ // console.log()
14
+ // let command = await database.plugins["txtcmd"].parse_and_run("new/schema")
15
+ // console.log(command)
16
+ // let command2 = await database.plugins["txtcmd"].parse_and_run("new")
17
+ // console.log(command2)
18
+ // let command3 = await database.plugins["txtcmd"].parse_and_run("open/link/thunder-kangchenjunga-mango")
19
+ // console.log(command3)
20
+ // let command4 = await database.plugins["txtcmd"].parse_and_run("tool/info")
21
+ // console.log(command4)
22
+ const test_schema = {
23
+ name:"book",
24
+ title:"Book",
25
+ description:"Test schema 1",
26
+ schema: {
27
+ $schema: "http://json-schema.org/draft-07/schema#",
28
+ type: "object",
29
+ properties: {
30
+ title: {
31
+ type: "string",
32
+ minLength: 1,
33
+ description: "The title of the book",
34
+ },
35
+ author: {
36
+ type: "string",
37
+ minLength: 1,
38
+ description: "The author of the book",
39
+ },
40
+ isbn: {
41
+ type: "string",
42
+ pattern: "^(97(8|9))?\\d{9}(\\d|X)$",
43
+ description: "The ISBN of the book, can be 10 or 13 digits",
44
+ },
45
+ publicationYear: {
46
+ type: "integer",
47
+ minimum: 1450,
48
+ maximum: 2024,
49
+ description:
50
+ "The year the book was published (between 1450 and 2024)",
51
+ },
52
+ genre: {
53
+ type: "string",
54
+ enum: [
55
+ "Fiction",
56
+ "Non-Fiction",
57
+ "Science",
58
+ "History",
59
+ "Fantasy",
60
+ "Biography",
61
+ "Children",
62
+ "Mystery",
63
+ "Horror",
64
+ ],
65
+ description: "The genre of the book",
66
+ },
67
+ language: {
68
+ type: "string",
69
+ description: "The language of the book",
70
+ default: "English",
71
+ },
72
+ publisher: {
73
+ type: "string",
74
+ description: "The publisher of the book",
75
+ minLength: 1,
76
+ },
77
+ pages: {
78
+ type: "integer",
79
+ minimum: 1,
80
+ description: "The number of pages in the book",
81
+ },
82
+ secret: {
83
+ type: "string",
84
+ description: "Super secret related to the book",
85
+ minLength: 1,
86
+ },
87
+ },
88
+ required: ["title", "author", "isbn", "publicationYear", "genre"],
89
+ additionalProperties: false,
90
+ },
91
+ settings : {
92
+ primary_keys:['title','author'],
93
+ encrypted_fields:['secret'],
94
+ non_editable_fields:[],
95
+ single_record:false
96
+ }
97
+ };
98
+
99
+ try {
100
+ //console.log(test_schema)
101
+ let a = await database.create("schema",test_schema)
102
+ console.log(a)
103
+ } catch (error) {
104
+ console.log("error in before")
105
+ console.log(error)
106
+ }
107
+
108
+
109
+ // try {
110
+ // //console.log(test_schema)
111
+ // let a = await database.update({link:"7f82b8"},{meta:{tags:["sample"]}})
112
+ // console.log(a)
113
+ // } catch (error) {
114
+ // console.log("error in before")
115
+ // console.log(error)
116
+ // }
117
+
159
118
  })()
160
119
 
161
120