beanbagdb 0.5.72 → 0.5.76

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.72",
3
+ "version": "0.5.76",
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
@@ -268,98 +268,24 @@ export class BeanBagDB {
268
268
 
269
269
  let app_doc = { ... app_data.meta, version: latest_version}
270
270
  try {
271
- await this.save_setting_doc(app_data.meta.name, {
272
- value: app_doc,
273
- });
274
- } catch (error) {
275
- console.log(error)
276
- console.log("error in storing/updating beanbagdb_version")
277
- }
278
-
279
- try {
280
- let new_log_doc = this._get_blank_doc("system_setting")
271
+ // modify the app setting doc
272
+ await this.modify_setting(app_data.meta.name,{value: app_doc,},"update")
273
+
274
+ // add a new log
275
+ let new_log_doc = this._get_blank_doc("system_log")
281
276
  new_log_doc.data = {text,data:{steps},time:this.util_get_now_unix_timestamp(),app:app_data.meta.name}
282
277
  await this.db_api.insert(new_log_doc);
283
278
  console.log("init logged")
279
+
284
280
  } catch (error) {
285
281
  console.log(error)
282
+ console.log("error in storing/updating beanbagdb_version")
283
+ throw error
286
284
  }
287
285
  return app_doc
288
286
  }
289
287
 
290
288
 
291
- /**
292
- * Updates a setting document if it already exists in the database or creates a new document
293
- * Inserts or updates a setting in the system settings schema.
294
- *
295
- * This method either:
296
- * - Updates an existing document if the setting with the given `name` already exists in the database.
297
- * - Inserts a new document if no matching setting is found.
298
- *
299
- * If the setting exists and the `value` is an array, the behavior depends on the `on_update_array` key:
300
- * - `"append"`: Appends the new value to the existing array.
301
- * - `"update"`: Replaces the current array with the new value.
302
- *
303
- * @async
304
- * @param {string} name - The name of the setting to insert or update.
305
- * @param {object} new_data - The new data to insert or update.
306
- * @param {*} new_data.value - The value to insert or update.
307
- * @param {string} [new_data.on_update_array] - Optional behavior for handling arrays, either "append" or "update".
308
- * @param {object} [schema={}] - Optional schema to validate the data against (currently not implemented).
309
- * @returns {Promise<object>} - The updated or newly inserted document.
310
- * @throws {Error} - Throws an error if `new_data` or `new_data.value` is not provided, or if `on_update_array` is invalid.
311
- */
312
- async save_setting_doc(name, new_data, schema = {}) {
313
- // TODO implement schema check
314
- if (!new_data) {
315
- throw new Error("No data provided");
316
- }
317
- if (!new_data.value) {
318
- throw new Error("No value provided");
319
- }
320
-
321
- let doc_search = await this.db_api.search({
322
- selector: { schema: "system_setting", "data.name": name },
323
- });
324
- if (doc_search.docs.length > 0) {
325
- // doc already exists, check schema and update it : if it exists then it's value already exists and can be
326
- let doc = { ...doc_search.docs[0] };
327
- if (Array.isArray(doc.data.value)) {
328
- let append_type = doc.data.on_update_array;
329
- if (append_type == "append") {
330
- doc["data"]["value"].push(new_data.value);
331
- } else if (append_type == "update") {
332
- doc["data"]["value"] = new_data.value;
333
- } else {
334
- throw new Error("Invalid on update array value");
335
- }
336
- } else {
337
- doc["data"]["value"] = new_data.value;
338
- }
339
- // finally update it
340
- doc["meta"]["updated_on"] = this.util_get_now_unix_timestamp();
341
- await this.db_api.update(doc);
342
- return doc;
343
- } else {
344
- // doc does not exists, generate a new one
345
- let new_val = { value: new_data.value };
346
-
347
- if (new_data.on_update_array) {
348
- // this indicates the provided value is initial value inside the array
349
- new_val.value = [new_data.value];
350
- new_val.on_update_array = new_data.on_update_array;
351
- }
352
- let new_doc = this._get_blank_doc("system_setting");
353
- new_doc["data"] = {
354
- name: name,
355
- ...new_val,
356
- };
357
- let d = await this.db_api.insert(new_doc);
358
- return d;
359
- }
360
- }
361
-
362
-
363
289
  /**
364
290
  * Adds indexes for all the schemas in the data base. This is important to make search faster. This must be done every time a new schema is introduced in the database
365
291
  */
@@ -594,6 +520,51 @@ export class BeanBagDB {
594
520
  }
595
521
 
596
522
 
523
+ /**
524
+ * Check if the setting with the given name exists. New record created if not found. If found data is updated based on the updated_mode and the data type of the existing data
525
+ * If existing value is an array, and update_mode is append "value" is appended to the current value array.
526
+ * if existing value is an object, update_mode "append" will update fields that exists in the new object,
527
+ * for both data types, new value is replaced in update_mode : "replace"
528
+ * @param {string} name The name of the setting
529
+ * @param {object} value Value to be modified
530
+ * @param {string} mode
531
+ */
532
+ async modify_setting(name,value,update_mode){
533
+ if(!name||!value||!update_mode){
534
+ throw new DocUpdateError("All 3 inputs (setting name, value and update_mode) are required")
535
+ }
536
+ let doc_search = await this.db_api.search({
537
+ selector: { schema: "system_setting", "data.name": name },
538
+ });
539
+
540
+ if (!["append", "update"].includes(update_mode)) {
541
+ throw new DocUpdateError("Invalid update_mode");
542
+ }
543
+
544
+ if (doc_search.docs.length > 0) {
545
+ // doc already exists,
546
+ let doc = { ...doc_search.docs[0] };
547
+ if (Array.isArray(value)) {
548
+ doc.data.value = update_mode === "append" ? [...doc.data.value, value] : value; // "update" mode replaces the value
549
+ } else {
550
+ doc.data.value = update_mode === "append" ? { ...doc.data.value, ...value } : value; // "update" mode replaces the value
551
+ }
552
+ // finally update it
553
+ doc["meta"]["updated_on"] = this.util_get_now_unix_timestamp();
554
+ // caution : db api is being used directly
555
+ await this.db_api.update(doc);
556
+ return doc;
557
+
558
+ } else {
559
+ // doc does not exists, generate a new one
560
+ let new_log_doc = this._get_blank_doc("system_setting")
561
+ new_log_doc.data = {value, name}
562
+ await this.db_api.insert(new_log_doc);
563
+
564
+
565
+ }
566
+ }
567
+
597
568
  /**
598
569
  * Deletes a document from the database by its ID.
599
570
  *
@@ -732,6 +703,14 @@ export class BeanBagDB {
732
703
  //////////////// simple directed graph ////////////////////////
733
704
  //////////////////////////////////////////////////////////
734
705
 
706
+ /**
707
+ * To add an edge between 2 nodes in the system wide simple directed graph.
708
+ * @param {object} node1
709
+ * @param {object} node2
710
+ * @param {string} edge_name
711
+ * @param {*} edge_label
712
+ * @returns {Object}
713
+ */
735
714
  async create_edge(node1,node2,edge_name,edge_label=""){
736
715
  this._check_ready_to_use();
737
716
  if(!edge_name){throw new ValidationError("edge_name required")}
@@ -8,7 +8,7 @@ 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.80,
11
+ version:0.85,
12
12
  schema: {
13
13
  type: "object",
14
14
  additionalProperties: false,
@@ -98,7 +98,7 @@ export const default_app = {
98
98
  required :["primary_keys","non_editable_fields","encrypted_fields"]
99
99
  },
100
100
  },
101
- required: ["name","version","description","schema", "settings"],
101
+ required: ["name","description","schema", "settings"],
102
102
  },
103
103
  settings: {
104
104
  primary_keys: ["name"],
@@ -144,7 +144,7 @@ export const default_app = {
144
144
  },
145
145
  },
146
146
  {
147
- version:0.60,
147
+ version:0.65,
148
148
  system_generated:true,
149
149
  description:"The system relies on these settings for proper functioning or enabling optional features.",
150
150
  name: "system_setting",
@@ -160,13 +160,7 @@ export const default_app = {
160
160
  pattern: "^[a-zA-Z][a-zA-Z0-9_]*$",
161
161
  },
162
162
  value: {
163
- type: ["string", "number", "boolean", "array"]
164
- },
165
- on_update_array:{
166
- type:"string",
167
- default:"replace",
168
- description:"Special operation only for updating Arrays. Either replace it or append new elements to it. Cannot be edited",
169
- enum:["replace","append"],
163
+ type: ["array","object"]
170
164
  }
171
165
  },
172
166
  },