@yuneta/gobj-js 7.26.10 → 7.26.12

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.
@@ -5246,37 +5246,48 @@ function jdb_update(jdb, topic_name, path, kw) {
5246
5246
  log_error("jdb_update: topic not found: " + topic_name);
5247
5247
  return null;
5248
5248
  }
5249
- if (empty_string(kw["id"])) {
5250
- log_error("jdb_update: record without id: " + kw);
5251
- trace_msg(kw);
5249
+ if (!is_object(kw) || empty_string(kw["id"])) {
5250
+ log_error(`jdb_update: record without id: ${JSON.stringify(kw)}`);
5252
5251
  return null;
5253
5252
  }
5254
5253
  if (!is_string(path)) {
5255
5254
  log_error(`jdb_update: path must be a string: ${String(path)}`);
5256
5255
  return null;
5257
5256
  }
5257
+ if (path !== "" && path.split("`").includes("")) {
5258
+ log_error(`jdb_update: empty segment in path: '${path}'`);
5259
+ return null;
5260
+ }
5258
5261
  let ids = path.split("`");
5259
5262
  let id;
5260
5263
  let v = topic;
5261
5264
  while (id = ids.shift()) {
5262
5265
  if (empty_string(id) || !v) break;
5263
- if (is_object(v)) v = kw_get_dict_value(null, v, jdb.hook, [], kw_flag_t.KW_CREATE);
5266
+ if (is_object(v)) v = kw_get_dict_value(null, v, jdb.hook, null, 0);
5264
5267
  v = _jdb_get(v, null, id, false);
5265
5268
  }
5266
5269
  if (!v || ids.length !== 0) {
5267
5270
  log_error(`jdb_update: path not found in topic '${topic_name}': '${path}'`);
5268
5271
  return null;
5269
5272
  }
5270
- if (ids.length === 0 && v) {
5271
- if (is_array(v)) v.push(kw);
5272
- else if (v["id"] === kw["id"]) json_object_update(v, kw);
5273
- else {
5274
- let v_ = kw_get_dict_value(null, v, jdb.hook, [], kw_flag_t.KW_CREATE);
5275
- let v__ = _jdb_get(v_, jdb.hook, kw["id"], false);
5276
- if (v__) json_object_update(v__, kw);
5277
- else v_.push(kw);
5273
+ let list;
5274
+ if (is_array(v)) list = v;
5275
+ else if (v["id"] === kw["id"]) {
5276
+ json_object_update(v, kw);
5277
+ return topic;
5278
+ } else {
5279
+ list = kw_get_dict_value(null, v, jdb.hook, null, 0);
5280
+ if (list === null || list === void 0) {
5281
+ list = [];
5282
+ json_object_set(v, jdb.hook, list);
5283
+ } else if (!is_array(list)) {
5284
+ log_error(`jdb_update: the hook '${jdb.hook}' of '${path}' is not a list in topic '${topic_name}'`);
5285
+ return null;
5278
5286
  }
5279
5287
  }
5288
+ let found = _jdb_get(list, jdb.hook, kw["id"], false);
5289
+ if (found) json_object_update(found, kw);
5290
+ else list.push(kw);
5280
5291
  return topic;
5281
5292
  }
5282
5293
  /********************************************
@@ -5288,15 +5299,18 @@ function jdb_delete(jdb, topic_name, path, kw) {
5288
5299
  log_error("jdb_delete: topic not found: " + topic_name);
5289
5300
  return null;
5290
5301
  }
5291
- if (empty_string(kw["id"])) {
5292
- log_error("jdb_delete: record without id: " + kw);
5293
- trace_msg(kw);
5302
+ if (!is_object(kw) || empty_string(kw["id"])) {
5303
+ log_error(`jdb_delete: record without id: ${JSON.stringify(kw)}`);
5294
5304
  return null;
5295
5305
  }
5296
5306
  if (!is_string(path)) {
5297
5307
  log_error(`jdb_delete: path must be a string: ${String(path)}`);
5298
5308
  return null;
5299
5309
  }
5310
+ if (path !== "" && path.split("`").includes("")) {
5311
+ log_error(`jdb_delete: empty segment in path: '${path}'`);
5312
+ return null;
5313
+ }
5300
5314
  let ids = path.split("`");
5301
5315
  let id;
5302
5316
  let v = topic;