beanbagdb 0.5.70 → 0.5.71

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.70",
3
+ "version": "0.5.71",
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
@@ -162,7 +162,7 @@ export class BeanBagDB {
162
162
  // this works on its own but is usually called by ready automatically if required
163
163
  // check for schema_scehma : if yes, check if latest and upgrade if required, if no create a new schema doc
164
164
  try {
165
- let app_data = await this.initialize_app(sys_sch.default_app)
165
+ let app_data = await this.initialize_db(sys_sch.default_app)
166
166
  console.log(app_data)
167
167
  this.meta.beanbagdb_version_db = this._version;
168
168
  this.active = true;
@@ -175,7 +175,7 @@ export class BeanBagDB {
175
175
 
176
176
  }
177
177
 
178
- async initialize_app(app_data){
178
+ async initialize_db(app_data){
179
179
  // app_data : meta(name,description), schemas[] , default_records:[]
180
180
  // TODO check if add_data is valid
181
181
  // calculate the app_version
@@ -697,18 +697,35 @@ export class BeanBagDB {
697
697
  }
698
698
  }
699
699
 
700
+ /**
701
+ * To load a plugin in the current BeanBagDB instance.
702
+ * Plug_module has to be loaded manually first. It must export an object containing fields: `actions` and `schema`.
703
+ * `actions` is an object of methods which can be called after loading the plugin.
704
+ * `schema` is an array of JSON schemas that are required by the plugin. Every time a plugin is loaded, this list is schemas is verified. New updates are added automatically to the database and logged
705
+ * methods inside actions must be async and must have at least one parameter : `db_instance` which is assumed to be the current instance of the BeanBagDB object itself. They ideally must also return some value.
706
+ * @param {string} plugin_name
707
+ * @param {object} plugin_module
708
+ */
700
709
  async load_plugin(plugin_name, plugin_module) {
701
710
  this._check_ready_to_use();
702
711
  this.plugins[plugin_name] = {};
703
- for (let func_name in plugin_module) {
704
- if (typeof plugin_module[func_name] == "function") {
705
- this.plugins[plugin_name][func_name] = plugin_module[func_name].bind(null,this)
712
+ //console.log(plugin_module)
713
+ if(plugin_module.actions){
714
+ for (let func_name in plugin_module.actions) {
715
+ if (typeof plugin_module.actions[func_name] == "function") {
716
+ this.plugins[plugin_name][func_name] = plugin_module.actions[func_name].bind(null,this)
717
+ }
706
718
  }
707
719
  }
708
- // Check if the plugin has an on_load method and call it
709
- if (typeof this.plugins[plugin_name].on_load === "function") {
710
- await this.plugins[plugin_name].on_load();
720
+
721
+ if(plugin_module.schemas){
722
+ await this._upgrade_schema_in_bulk(plugin_module.schemas,true,`Updating ${plugin_name} plugin schemas`)
711
723
  }
724
+
725
+ // Check if the plugin has an on_load method and call it
726
+ // if (typeof this.plugins[plugin_name].on_load === "function") {
727
+ // await this.plugins[plugin_name].on_load();
728
+ // }
712
729
  }
713
730
 
714
731
  ///////////////////////////////////////////////////////////
@@ -828,6 +845,65 @@ _check_nodes_edge(node1Rule, node2Rule, schema1, schema2) {
828
845
  //////////////// Internal methods ////////////////////////
829
846
  //////////////////////////////////////////////////////////
830
847
 
848
+
849
+ async _upgrade_schema_in_bulk(schemas,log_upgrade=false,log_message="Schema Upgrade in bulk"){
850
+ // TODO add a check to now allow default system schema to be updated from this method
851
+
852
+ let steps = ["schema update started"]
853
+ let update_was_required = false
854
+ for (let index = 0; index < schemas.length; index++) {
855
+ const schema_name = schemas[index]["name"];
856
+ const schema_data = schemas[index]
857
+ steps.push(`checking.${schema_name}`)
858
+ try {
859
+ let schema1 = await this.get("schema",{name:schema_name})
860
+ if (schema1["data"]["version"] != schema_data.version) {
861
+ steps.push(`old.${schema_name}.v.${schema1["data"]["version"]}`);
862
+ let full_doc = await this.db_api.get(schema1["_id"]);
863
+ full_doc["data"] = { ...schema_data };
864
+ full_doc["meta"]["updated_on"] = this.util_get_now_unix_timestamp();
865
+ console.log(full_doc)
866
+ await this.db_api.update(full_doc);
867
+ steps.push(`new.${schema_name}.v=${schema_data.version}`);
868
+ update_was_required = update_was_required || true
869
+ }else{
870
+ steps.push(`${schema_name}.v.${schema1["data"]["version"]}=latest`)
871
+ }
872
+ } catch (error) {
873
+ // console.log(error);
874
+ if (error instanceof DocNotFoundError) {
875
+ // inserting new schema doc
876
+ let system_schema = sys_sch.default_app.schemas[0]["schema"]
877
+ let new_schema_doc = this._get_blank_schema_doc(
878
+ "schema",
879
+ system_schema,
880
+ schema_data
881
+ );
882
+ await this.db_api.insert(new_schema_doc);
883
+
884
+ steps.push(`init.${schema_name}.v=${schema_data.version}`);
885
+ update_was_required = update_was_required || true
886
+ }else{
887
+ steps.push(`${schema_name}.error.message : ${error.message} `);
888
+ }
889
+ }
890
+ }
891
+ // console.log(JSON.stringify(steps))
892
+ // console.log(update_was_required)
893
+ if (update_was_required && log_upgrade){
894
+ // log it if asked
895
+ try {
896
+ let new_log_doc = this._get_blank_doc("system_log")
897
+ new_log_doc.data = {text:log_message,data:{steps},time:this.util_get_now_unix_timestamp()}
898
+ await this.db_api.insert(new_log_doc);
899
+ } catch (error) {
900
+ console.log(error)
901
+ }
902
+
903
+ }
904
+ return {update_was_required,logs:steps}
905
+ }
906
+
831
907
  /**
832
908
  * Retrieves the current version of the system by summing up the version numbers
833
909
  * of all system-defined schemas.
@@ -1,6 +1,3 @@
1
-
2
-
3
-
4
1
  const commands = {
5
2
  new: {
6
3
  parse: async (instance,parts) => {
@@ -187,7 +184,8 @@ const parse_and_run = async(instance, text) => {
187
184
  let command_result = await run(instance,command)
188
185
  return command_result
189
186
  }
187
+ // const schemas = []
190
188
 
191
189
  export const text_command = {
192
- parse,run,parse_and_run
190
+ actions: {parse,run,parse_and_run}
193
191
  };
@@ -8,23 +8,26 @@ export const default_app = {
8
8
  name: "schema",
9
9
  description:"Meta-schema or the schema for defining other schemas",
10
10
  system_generated:true,
11
- version:0.70,
11
+ version:0.80,
12
12
  schema: {
13
13
  type: "object",
14
14
  additionalProperties: false,
15
15
  properties: {
16
16
  system_generated:{
17
+ title:"System generated schema",
17
18
  type:"boolean",
18
19
  default:false
19
20
  },
20
21
  version: {
21
22
  type: "number",
23
+ title:"Version",
22
24
  minimum: 0,
23
25
  default: 1,
24
26
  description:"This is an optional field.To be used primarily for system schemas"
25
27
  },
26
28
  name: {
27
29
  type: "string",
30
+ title:"Name",
28
31
  minLength: 4,
29
32
  maxLength: 50,
30
33
  pattern: "^[a-zA-Z][a-zA-Z0-9_]*$",
@@ -32,12 +35,14 @@ export const default_app = {
32
35
  },
33
36
  description:{
34
37
  type:"string",
38
+ title:"About",
35
39
  minLength:0,
36
40
  maxLength:1000,
37
41
  description:"A small description of what data in this schema stores."
38
42
  },
39
43
  schema: {
40
44
  type: "object",
45
+ title:"JSON Schema specification",
41
46
  additionalProperties: true,
42
47
  minProperties: 1,
43
48
  maxProperties: 50,
@@ -45,9 +50,11 @@ export const default_app = {
45
50
  },
46
51
  settings: {
47
52
  type: "object",
53
+ title:"Additional Settings",
48
54
  additionalProperties: true,
49
55
  properties: {
50
56
  primary_keys: {
57
+ title:"Primary key",
51
58
  type: "array",
52
59
  default: [],
53
60
  items: {
@@ -58,6 +65,7 @@ export const default_app = {
58
65
  },
59
66
  non_editable_fields: {
60
67
  type: "array",
68
+ title:"Non editable fields",
61
69
  default: [],
62
70
  items: {
63
71
  type: "string",
@@ -68,23 +76,35 @@ export const default_app = {
68
76
  },
69
77
  encrypted_fields: {
70
78
  type: "array",
79
+ title:"List of fields encrypted",
71
80
  default: [],
72
81
  items: {
73
82
  type: "string",
74
83
  },
75
84
  maxItems: 50,
76
85
  description:"Once set, all the data in this field will be encrypted before storing it in the database. Encryption key must be provided during the time of BeanBagDB initialization and must be managed by the user as it is NOT stored in the database"
86
+ },
87
+ display_fields: {
88
+ type: "array",
89
+ title:"List of fields to show in short view",
90
+ default: [],
91
+ items: {
92
+ type: "string",
93
+ },
94
+ maxItems: 50,
95
+ description:"These fields will be used when a record is displayed in short"
77
96
  }
78
97
  },
79
98
  required :["primary_keys","non_editable_fields","encrypted_fields"]
80
99
  },
81
100
  },
82
- required: ["name","description","schema", "settings"],
101
+ required: ["name","version","description","schema", "settings"],
83
102
  },
84
103
  settings: {
85
104
  primary_keys: ["name"],
86
105
  non_editable_fields:[],
87
- encrypted_fields:[]
106
+ encrypted_fields:[],
107
+ display_fields:["name","version","description"]
88
108
  },
89
109
  },
90
110
  {
@@ -295,7 +315,7 @@ export const default_app = {
295
315
  non_editable_fields:[],
296
316
  encrypted_fields:[]
297
317
  },
298
- },
318
+ }
299
319
  ],
300
320
  records:[]
301
321
  }