beanbagdb 0.5.72 → 0.5.75

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.75",
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
@@ -277,7 +277,7 @@ export class BeanBagDB {
277
277
  }
278
278
 
279
279
  try {
280
- let new_log_doc = this._get_blank_doc("system_setting")
280
+ let new_log_doc = this._get_blank_doc("system_log")
281
281
  new_log_doc.data = {text,data:{steps},time:this.util_get_now_unix_timestamp(),app:app_data.meta.name}
282
282
  await this.db_api.insert(new_log_doc);
283
283
  console.log("init logged")
@@ -288,78 +288,6 @@ export class BeanBagDB {
288
288
  }
289
289
 
290
290
 
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
291
  /**
364
292
  * 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
293
  */
@@ -594,6 +522,49 @@ export class BeanBagDB {
594
522
  }
595
523
 
596
524
 
525
+ /**
526
+ * 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
527
+ * If existing value is an array, and update_mode is append "value" is appended to the current value array.
528
+ * if existing value is an object, update_mode "append" will update fields that exists in the new object,
529
+ * for both data types, new value is replaced in update_mode : "replace"
530
+ * @param {string} name The name of the setting
531
+ * @param {object} value Value to be modified
532
+ * @param {string} mode
533
+ */
534
+ async modify_setting(name,value,update_mode){
535
+ if(!name||!value||!update_mode){
536
+ throw new DocUpdateError("All 3 inputs (setting name, value and update_mode) are required")
537
+ }
538
+ let doc_search = await this.db_api.search({
539
+ selector: { schema: "system_setting", "data.name": name },
540
+ });
541
+
542
+ if (!["append", "update"].includes(update_mode)) {
543
+ throw new DocUpdateError("Invalid update_mode");
544
+ }
545
+
546
+ if (doc_search.docs.length > 0) {
547
+ // doc already exists,
548
+ let doc = { ...doc_search.docs[0] };
549
+ if (Array.isArray(value)) {
550
+ doc.data.value = update_mode === "append" ? [...value, new_data] : new_data; // "update" mode replaces the value
551
+ } else {
552
+ doc.data.value = update_mode === "append" ? { ...value, ...new_data } : new_data; // "update" mode replaces the value
553
+ }
554
+ // finally update it
555
+ doc["meta"]["updated_on"] = this.util_get_now_unix_timestamp();
556
+ // caution : db api is being used directly
557
+ await this.db_api.update(doc);
558
+ return doc;
559
+
560
+ } else {
561
+ // doc does not exists, generate a new one
562
+ let new_doc = {value, name};
563
+ let d = await this.create("system_setting",new_doc)
564
+ return d;
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
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")}
@@ -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
  },