@cutos/core 3.2.1 → 3.2.3

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/dist/cutos.cjs.js CHANGED
@@ -398,7 +398,7 @@ const cutosAPI$2 = {
398
398
  var cutosApi = cutosAPI$2;
399
399
 
400
400
  var name = "@cutos/core";
401
- var version = "3.2.1";
401
+ var version = "3.2.3";
402
402
  var description = "The CUTOS (CUT Operating System) Core API is a JavaScript library that provides essential functionalities for LWA (Local Web Application) edge computing and communication in the CUTOS ecosystem. ";
403
403
  var main = "dist/cutos.cjs.js";
404
404
  var module$1 = "dist/cutos.esm.js";
@@ -522,175 +522,210 @@ function doCallback(id, body, err) {
522
522
  return true;
523
523
  }
524
524
 
525
-
526
- /*
527
- retentionTime: retention time, default 48 hours
528
- */
529
525
  class Database {
530
526
  KEY_TYPE_TEXT = "TEXT";
531
527
  KEY_TYPE_INTEGER = "INTEGER";
532
528
 
533
- constructor(db, callback) {
534
- // sub class
535
- let database = this;
536
- this.Table = class {
537
- constructor(db, name, callback, retentionTime, keyType, keyName) {
538
- this.db = db;
539
- this.name = name;
529
+ /**
530
+ * The constructor of Database
531
+ * @param {string} db - name of database, can be null
532
+ */
533
+ constructor(db) {
534
+ this.db = db;
535
+ }
540
536
 
541
- let body = {
542
- db: this.db, cmd: "db.table.create", name: name
543
- };
544
- if (retentionTime) {
545
- body.retentionTime = retentionTime;
546
- }
547
- if (keyType === 'INTEGER' || keyType === 'TEXT') {
548
- body.keyName = keyName;
549
- body.keyType = keyType;
550
- } else {
551
- throw new Error("keyType MUST be INTEGER or TEXT");
537
+ connect(callback) {
538
+ let body = {
539
+ cmd: "db.connect", db: this.db
540
+ };
541
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
542
+ }
552
543
 
553
- }
554
- return this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, callback, body);
555
- }
544
+ run(sql, callback) {
545
+ let body = {
546
+ db: this.db, cmd: "db.run", sql: sql
547
+ };
548
+ return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
549
+ }
556
550
 
557
- insert(value, callback, tid) {
558
- let body = {
559
- db: this.db, cmd: "db.table.insert", name: this.name, value: value, tid: tid
560
- };
561
- return this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, callback, body);
562
- }
551
+ setupMirror(gwIPC, lwaName, confirm = false) {
552
+ this.mirror = {gwIPC, lwaName, confirm};
553
+ }
554
+ }
563
555
 
564
- insertById(id, value, callback, tid) {
565
- let body = {
566
- db: this.db, cmd: "db.table.insertById", name: this.name, id: id, value: value, tid: tid
567
- };
568
- return this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, callback, body);
569
- }
556
+ Database.Table = class {
557
+ constructor(name, database) {
558
+ this.name = name;
559
+ this.database = database;
560
+ this.db = database.db;
570
561
 
571
- insertByKey(key, value, callback, tid) {
572
- let body = {
573
- db: this.db, cmd: "db.table.insertByKey", name: this.name, key: key, value: value, tid: tid
574
- };
575
- return this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, callback, body);
576
- }
562
+ if (!this.name || !this.database) {
563
+ throw new Error("name or database cannot be empty.");
564
+ }
565
+ }
577
566
 
578
- update(id, value, callback) {
579
- let body = {
580
- db: this.db, cmd: "db.table.update", name: this.name, id: id, value: value
581
- };
582
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
583
- }
567
+ create(opts, callback) {
568
+ if (typeof opts === "function") {
569
+ callback = opts;
570
+ opts = {};
571
+ }
572
+ if (!opts) {
573
+ opts = {};
574
+ }
575
+ opts.keyName = opts.keyName ? opts.keyName : 'id';
576
+ opts.keyType = opts.keyType ? opts.keyType : 'INTEGER';
577
+ if (opts.keyType !== 'INTEGER' && opts.keyType !== 'TEXT') {
578
+ throw new Error("keyType MUST be INTEGER or TEXT");
579
+ }
580
+ this.opts = opts;
584
581
 
585
- updateByKey(key, value, callback) {
586
- let body = {
587
- db: this.db, cmd: "db.table.updateByKey", name: this.name, key: key, value: value
588
- };
589
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
590
- }
582
+ let body = {
583
+ db: this.db, cmd: "db.table.create", name: this.name,
584
+ retentionTime: opts.retentionTime,
585
+ keyType: opts.keyType,
586
+ keyName: opts.keyName
587
+ };
591
588
 
592
- delete(id, callback) {
593
- let body = {
594
- db: this.db, cmd: "db.table.delete", name: this.name, id: id
595
- };
596
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
597
- }
589
+ this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, callback, body);
590
+ }
598
591
 
599
- query(id, callback, count = 100) {
600
- let body = {
601
- db: this.db, cmd: "db.table.query", name: this.name, id: id
602
- };
603
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
604
- }
592
+ insert(value, opts, callback) {
593
+ if (typeof opts === "function") {
594
+ callback = opts;
595
+ opts = {};
596
+ }
605
597
 
606
- queryByKey(key, callback, count = 100) {
607
- let body = {
608
- db: this.db, cmd: "db.table.queryByKey", name: this.name, key: key
609
- };
610
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
611
- }
598
+ let body = {
599
+ db: this.db, cmd: "db.table.insert", name: this.name, value: value, tid: opts?.tid
600
+ };
601
+ this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, callback, body);
602
+ }
612
603
 
613
- queryByTid(tid, callback, limit = 100) {
614
- let body = {
615
- db: this.db, cmd: "db.table.queryByTid", name: this.name, limit: limit, tid: tid
616
- };
617
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
618
- }
604
+ insertById(id, value, opts, callback) {
605
+ if (typeof opts === "function") {
606
+ callback = opts;
607
+ opts = {};
608
+ }
619
609
 
620
- queryUnsynced(callback, limit = 100) {
621
- let body = {
622
- db: this.db, cmd: "db.table.queryUnsynced", name: this.name, limit: limit
623
- };
624
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
625
- }
610
+ let body = {
611
+ db: this.db, cmd: "db.table.insertById", name: this.name, id: id, value: value, tid: opts?.tid
612
+ };
613
+ this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, callback, body);
614
+ }
626
615
 
627
- queryAll(callback) {
628
- let body = {
629
- db: this.db, cmd: "db.table.queryAll", name: this.name
630
- };
631
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
632
- }
616
+ insertByKey(key, value, opts, callback) {
617
+ if (typeof opts === "function") {
618
+ callback = opts;
619
+ opts = {};
620
+ }
633
621
 
634
- sync(id, callback) {
635
- let body = {
636
- db: this.db, cmd: "db.table.sync", name: this.name, id: id
637
- };
638
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
639
- }
622
+ let body = {
623
+ db: this.db, cmd: "db.table.insertByKey", name: this.name, key: key, value: value, tid: opts?.tid
624
+ };
625
+ this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, callback, body);
626
+ }
640
627
 
641
- publishRequestMirror(topic, callback, body) {
642
- let mirror = database.mirror;
643
- let cb = callback;
644
- if (mirror && mirror.gwIPC) { // mirror enabled
645
- cb = (dbResult) => {
646
- let confirmCallback = !mirror.confirm ? null : (gwResult) => {
647
- if (gwResult.status === "success") {
648
- this.sync(dbResult.row);
649
- }
650
- };
651
- if (dbResult.status === true) {
652
- switch (body.cmd) {
653
- case "db.table.create":
654
- break;
655
- case "db.table.insert":
656
- body.id = dbResult.msg.row;
657
- body.timestamp = dbResult.msg.timestamp;
658
- break;
659
- case "db.table.insertById":
660
- case "db.table.insertByKey":
661
- body.timestamp = dbResult.msg.timestamp;
662
- break;
663
- }
664
- mirror.gwIPC.mirror(body, confirmCallback, {lwaName: mirror.lwaName});
665
- }
666
- callback && callback(dbResult);
667
- };
668
- }
669
- return publishRequest(topic, cb, body);
670
- }
628
+ update(id, value, callback) {
629
+ let body = {
630
+ db: this.db, cmd: "db.table.update", name: this.name, id: id, value: value
671
631
  };
672
- this.db = db;
632
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
633
+ }
634
+
635
+ updateByKey(key, value, callback) {
673
636
  let body = {
674
- cmd: "db.connect", db: this.db
637
+ db: this.db, cmd: "db.table.updateByKey", name: this.name, key: key, value: value
675
638
  };
676
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
639
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
677
640
  }
678
641
 
679
- run(sql, callback) {
642
+ delete(id, callback) {
680
643
  let body = {
681
- db: this.db, cmd: "db.run", sql: sql
644
+ db: this.db, cmd: "db.table.delete", name: this.name, id: id
682
645
  };
683
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
646
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
684
647
  }
685
648
 
686
- createTable(name, callback, retentionTime, keyType = 'INTEGER', keyName = 'id') {
687
- return new this.Table(null, name, callback, retentionTime, keyType, keyName);
649
+ query(id, callback) {
650
+ let body = {
651
+ db: this.db, cmd: "db.table.query", name: this.name, id: id
652
+ };
653
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
688
654
  }
689
655
 
690
- setupMirror(gwIPC, lwaName, confirm = false) {
691
- this.mirror = {gwIPC, lwaName, confirm};
656
+ queryByKey(key, callback) {
657
+ let body = {
658
+ db: this.db, cmd: "db.table.queryByKey", name: this.name, key: key
659
+ };
660
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
692
661
  }
693
- }
662
+
663
+ queryByTid(tid, callback) {
664
+ let body = {
665
+ db: this.db, cmd: "db.table.queryByTid", name: this.name, tid: tid
666
+ };
667
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
668
+ }
669
+
670
+ queryUnsynced(opts, callback) {
671
+ if (typeof opts === "function") {
672
+ callback = opts;
673
+ opts = {limit: 100};
674
+ }
675
+ if (!opts) opts = {};
676
+ if (opts.limit <= 0) opts.limit = 100;
677
+
678
+ let body = {
679
+ db: this.db, cmd: "db.table.queryUnsynced", name: this.name, limit: opts.limit
680
+ };
681
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
682
+ }
683
+
684
+ queryAll(callback) {
685
+ let body = {
686
+ db: this.db, cmd: "db.table.queryAll", name: this.name
687
+ };
688
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
689
+ }
690
+
691
+ sync(id, callback) {
692
+ let body = {
693
+ db: this.db, cmd: "db.table.sync", name: this.name, id: id
694
+ };
695
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
696
+ }
697
+
698
+ publishRequestMirror(topic, callback, body) {
699
+ let mirror = this.database.mirror;
700
+ let cb = callback;
701
+ if (mirror && mirror.gwIPC) { // mirror enabled
702
+ cb = (dbResult) => {
703
+ let confirmCallback = !mirror.confirm ? null : (gwResult) => {
704
+ if (gwResult.status === "success") {
705
+ this.sync(dbResult.row);
706
+ }
707
+ };
708
+ if (dbResult.status === true) {
709
+ switch (body.cmd) {
710
+ case "db.table.create":
711
+ break;
712
+ case "db.table.insert":
713
+ body.id = dbResult.msg.row;
714
+ body.timestamp = dbResult.msg.timestamp;
715
+ break;
716
+ case "db.table.insertById":
717
+ case "db.table.insertByKey":
718
+ body.timestamp = dbResult.msg.timestamp;
719
+ break;
720
+ }
721
+ mirror.gwIPC.mirror(body, confirmCallback, {lwaName: mirror.lwaName});
722
+ }
723
+ callback && callback(dbResult);
724
+ };
725
+ }
726
+ return publishRequest(topic, cb, body);
727
+ }
728
+ };
694
729
 
695
730
  class Logger {
696
731
  constructor() {
@@ -807,10 +842,8 @@ class Device {
807
842
  *
808
843
  * @param name
809
844
  * @param type
810
- * @param callback(result, error)
811
- * @param opts
812
845
  */
813
- constructor(name = 'default-device-name', type = cutosAPI$1.DEVICE.DEFAULT, callback, opts) {
846
+ constructor(name = 'default-device-name', type = cutosAPI$1.DEVICE.DEFAULT) {
814
847
  this.name = name;
815
848
  this.type = type;
816
849
  this.topicData = cutosAPI$1.DEVICE.CHANNEL + '-' + type + '-data';
@@ -837,26 +870,35 @@ class Device {
837
870
  };
838
871
  listenerMap.set(cutosAPI$1.CORE_APP.HEARTBEAT_LISTENER, Device.heartbeatListener);
839
872
  }
840
- // subscribe callback
873
+ // subscribe command callback
841
874
  client.subscribe(this.topicCommand + '-callback');
842
- this.init(name, type, callback, opts);
843
875
  }
844
876
 
845
- init(name, type, callback, opts) {
877
+ /**
878
+ *
879
+ * @param callback(result, error)
880
+ * @param opts
881
+ */
882
+ init(opts, callback) {
883
+ if (typeof opts === "function") {
884
+ callback = opts;
885
+ opts = null;
886
+ }
887
+
846
888
  let command = {};
847
889
  command.cmd = "init";
848
- command.args = {name, type, opts};
890
+ command.args = {name: this.name, type: this.type, opts};
849
891
  command.topicResponse = this.topicResponse;
850
892
  let defaultTopic = cutosAPI$1.DEVICE.CHANNEL + '-' + cutosAPI$1.DEVICE.DEFAULT + '-command';
851
893
  publishRequest(defaultTopic, null, command);
852
894
 
853
- // response callback
895
+ // subscribe response callback, currently only used for init
854
896
  client.subscribe(this.topicResponse); // receive response from device
855
- listenerMap.set(this.topicResponse, ({status, msg}) => {
856
- if (status) {
897
+ listenerMap.set(this.topicResponse, (result) => {
898
+ if (result.status) {
857
899
  callback && callback(`Driver ${this.type} loaded`);
858
900
  } else {
859
- callback && callback(null, msg);
901
+ callback && callback(null, result.msg);
860
902
  }
861
903
  });
862
904
  }
@@ -871,8 +913,8 @@ class Device {
871
913
 
872
914
  /**
873
915
  *
874
- * @param command
875
- * @param callback(result)
916
+ * @param command - command description object {cmd: 'command', args: (''|{}) }
917
+ * @param callback - (result) => {}, the result is normal make up of {status: true|false, msg: (''| {})}
876
918
  */
877
919
  sendCommand(command, callback) {
878
920
  // exam: command = {cmd: "connect", args: ""}
@@ -894,11 +936,11 @@ class Device {
894
936
  return this.data;
895
937
  }
896
938
 
897
- readDeviceInfo(context) {
939
+ readDeviceInfo(callback, context) {
898
940
  let msg = {};
899
941
  msg.cmd = "read-device-info";
900
942
  msg.context = context;
901
- this.sendCommand(msg);
943
+ this.sendCommand(msg, callback);
902
944
  }
903
945
  }
904
946
 
@@ -1036,6 +1078,7 @@ const CoreAPI$2 = {
1036
1078
  console.error("CUTOS CORE error:", err.message);
1037
1079
  });
1038
1080
  },
1081
+
1039
1082
  connected() {
1040
1083
  return !!client && client.connected;
1041
1084
  },
@@ -1090,10 +1133,6 @@ const CoreAPI$2 = {
1090
1133
  return publishRequest(cutosAPI$1.CORE.SHELL, callback, command);
1091
1134
  },
1092
1135
 
1093
- getDatabase(db, callback) {
1094
- return new Database(db, callback);
1095
- },
1096
-
1097
1136
  getVolume(callback) {
1098
1137
  return publishRequest(cutosAPI$1.CORE.GET_VOLUME, callback);
1099
1138
  },
@@ -1112,7 +1151,7 @@ cutos.CoreDefine = {
1112
1151
  DEVICE: cutosAPI$1.DEVICE, HEARTBEAT_STATUS: cutosAPI$1.HEARTBEAT_STATUS
1113
1152
  };
1114
1153
  cutos.CoreClass = {
1115
- Device, Driver
1154
+ Device, Driver, Database
1116
1155
  };
1117
1156
 
1118
1157
  cutos.cutosAPI = cutosAPI$1;
package/dist/cutos.esm.js CHANGED
@@ -690,7 +690,7 @@ const cutosAPI$2 = {
690
690
  var cutosApi = cutosAPI$2;
691
691
 
692
692
  var name = "@cutos/core";
693
- var version = "3.2.1";
693
+ var version = "3.2.3";
694
694
  var description = "The CUTOS (CUT Operating System) Core API is a JavaScript library that provides essential functionalities for LWA (Local Web Application) edge computing and communication in the CUTOS ecosystem. ";
695
695
  var main = "dist/cutos.cjs.js";
696
696
  var module = "dist/cutos.esm.js";
@@ -814,175 +814,210 @@ function doCallback(id, body, err) {
814
814
  return true;
815
815
  }
816
816
 
817
-
818
- /*
819
- retentionTime: retention time, default 48 hours
820
- */
821
817
  class Database {
822
818
  KEY_TYPE_TEXT = "TEXT";
823
819
  KEY_TYPE_INTEGER = "INTEGER";
824
820
 
825
- constructor(db, callback) {
826
- // sub class
827
- let database = this;
828
- this.Table = class {
829
- constructor(db, name, callback, retentionTime, keyType, keyName) {
830
- this.db = db;
831
- this.name = name;
821
+ /**
822
+ * The constructor of Database
823
+ * @param {string} db - name of database, can be null
824
+ */
825
+ constructor(db) {
826
+ this.db = db;
827
+ }
832
828
 
833
- let body = {
834
- db: this.db, cmd: "db.table.create", name: name
835
- };
836
- if (retentionTime) {
837
- body.retentionTime = retentionTime;
838
- }
839
- if (keyType === 'INTEGER' || keyType === 'TEXT') {
840
- body.keyName = keyName;
841
- body.keyType = keyType;
842
- } else {
843
- throw new Error("keyType MUST be INTEGER or TEXT");
829
+ connect(callback) {
830
+ let body = {
831
+ cmd: "db.connect", db: this.db
832
+ };
833
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
834
+ }
844
835
 
845
- }
846
- return this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, callback, body);
847
- }
836
+ run(sql, callback) {
837
+ let body = {
838
+ db: this.db, cmd: "db.run", sql: sql
839
+ };
840
+ return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
841
+ }
848
842
 
849
- insert(value, callback, tid) {
850
- let body = {
851
- db: this.db, cmd: "db.table.insert", name: this.name, value: value, tid: tid
852
- };
853
- return this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, callback, body);
854
- }
843
+ setupMirror(gwIPC, lwaName, confirm = false) {
844
+ this.mirror = {gwIPC, lwaName, confirm};
845
+ }
846
+ }
855
847
 
856
- insertById(id, value, callback, tid) {
857
- let body = {
858
- db: this.db, cmd: "db.table.insertById", name: this.name, id: id, value: value, tid: tid
859
- };
860
- return this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, callback, body);
861
- }
848
+ Database.Table = class {
849
+ constructor(name, database) {
850
+ this.name = name;
851
+ this.database = database;
852
+ this.db = database.db;
862
853
 
863
- insertByKey(key, value, callback, tid) {
864
- let body = {
865
- db: this.db, cmd: "db.table.insertByKey", name: this.name, key: key, value: value, tid: tid
866
- };
867
- return this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, callback, body);
868
- }
854
+ if (!this.name || !this.database) {
855
+ throw new Error("name or database cannot be empty.");
856
+ }
857
+ }
869
858
 
870
- update(id, value, callback) {
871
- let body = {
872
- db: this.db, cmd: "db.table.update", name: this.name, id: id, value: value
873
- };
874
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
875
- }
859
+ create(opts, callback) {
860
+ if (typeof opts === "function") {
861
+ callback = opts;
862
+ opts = {};
863
+ }
864
+ if (!opts) {
865
+ opts = {};
866
+ }
867
+ opts.keyName = opts.keyName ? opts.keyName : 'id';
868
+ opts.keyType = opts.keyType ? opts.keyType : 'INTEGER';
869
+ if (opts.keyType !== 'INTEGER' && opts.keyType !== 'TEXT') {
870
+ throw new Error("keyType MUST be INTEGER or TEXT");
871
+ }
872
+ this.opts = opts;
876
873
 
877
- updateByKey(key, value, callback) {
878
- let body = {
879
- db: this.db, cmd: "db.table.updateByKey", name: this.name, key: key, value: value
880
- };
881
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
882
- }
874
+ let body = {
875
+ db: this.db, cmd: "db.table.create", name: this.name,
876
+ retentionTime: opts.retentionTime,
877
+ keyType: opts.keyType,
878
+ keyName: opts.keyName
879
+ };
883
880
 
884
- delete(id, callback) {
885
- let body = {
886
- db: this.db, cmd: "db.table.delete", name: this.name, id: id
887
- };
888
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
889
- }
881
+ this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, callback, body);
882
+ }
890
883
 
891
- query(id, callback, count = 100) {
892
- let body = {
893
- db: this.db, cmd: "db.table.query", name: this.name, id: id
894
- };
895
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
896
- }
884
+ insert(value, opts, callback) {
885
+ if (typeof opts === "function") {
886
+ callback = opts;
887
+ opts = {};
888
+ }
897
889
 
898
- queryByKey(key, callback, count = 100) {
899
- let body = {
900
- db: this.db, cmd: "db.table.queryByKey", name: this.name, key: key
901
- };
902
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
903
- }
890
+ let body = {
891
+ db: this.db, cmd: "db.table.insert", name: this.name, value: value, tid: opts?.tid
892
+ };
893
+ this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, callback, body);
894
+ }
904
895
 
905
- queryByTid(tid, callback, limit = 100) {
906
- let body = {
907
- db: this.db, cmd: "db.table.queryByTid", name: this.name, limit: limit, tid: tid
908
- };
909
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
910
- }
896
+ insertById(id, value, opts, callback) {
897
+ if (typeof opts === "function") {
898
+ callback = opts;
899
+ opts = {};
900
+ }
911
901
 
912
- queryUnsynced(callback, limit = 100) {
913
- let body = {
914
- db: this.db, cmd: "db.table.queryUnsynced", name: this.name, limit: limit
915
- };
916
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
917
- }
902
+ let body = {
903
+ db: this.db, cmd: "db.table.insertById", name: this.name, id: id, value: value, tid: opts?.tid
904
+ };
905
+ this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, callback, body);
906
+ }
918
907
 
919
- queryAll(callback) {
920
- let body = {
921
- db: this.db, cmd: "db.table.queryAll", name: this.name
922
- };
923
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
924
- }
908
+ insertByKey(key, value, opts, callback) {
909
+ if (typeof opts === "function") {
910
+ callback = opts;
911
+ opts = {};
912
+ }
925
913
 
926
- sync(id, callback) {
927
- let body = {
928
- db: this.db, cmd: "db.table.sync", name: this.name, id: id
929
- };
930
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
931
- }
914
+ let body = {
915
+ db: this.db, cmd: "db.table.insertByKey", name: this.name, key: key, value: value, tid: opts?.tid
916
+ };
917
+ this.publishRequestMirror(cutosAPI$1.CORE.DATABASE, callback, body);
918
+ }
932
919
 
933
- publishRequestMirror(topic, callback, body) {
934
- let mirror = database.mirror;
935
- let cb = callback;
936
- if (mirror && mirror.gwIPC) { // mirror enabled
937
- cb = (dbResult) => {
938
- let confirmCallback = !mirror.confirm ? null : (gwResult) => {
939
- if (gwResult.status === "success") {
940
- this.sync(dbResult.row);
941
- }
942
- };
943
- if (dbResult.status === true) {
944
- switch (body.cmd) {
945
- case "db.table.create":
946
- break;
947
- case "db.table.insert":
948
- body.id = dbResult.msg.row;
949
- body.timestamp = dbResult.msg.timestamp;
950
- break;
951
- case "db.table.insertById":
952
- case "db.table.insertByKey":
953
- body.timestamp = dbResult.msg.timestamp;
954
- break;
955
- }
956
- mirror.gwIPC.mirror(body, confirmCallback, {lwaName: mirror.lwaName});
957
- }
958
- callback && callback(dbResult);
959
- };
960
- }
961
- return publishRequest(topic, cb, body);
962
- }
920
+ update(id, value, callback) {
921
+ let body = {
922
+ db: this.db, cmd: "db.table.update", name: this.name, id: id, value: value
963
923
  };
964
- this.db = db;
924
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
925
+ }
926
+
927
+ updateByKey(key, value, callback) {
965
928
  let body = {
966
- cmd: "db.connect", db: this.db
929
+ db: this.db, cmd: "db.table.updateByKey", name: this.name, key: key, value: value
967
930
  };
968
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
931
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
969
932
  }
970
933
 
971
- run(sql, callback) {
934
+ delete(id, callback) {
972
935
  let body = {
973
- db: this.db, cmd: "db.run", sql: sql
936
+ db: this.db, cmd: "db.table.delete", name: this.name, id: id
974
937
  };
975
- return publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
938
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
976
939
  }
977
940
 
978
- createTable(name, callback, retentionTime, keyType = 'INTEGER', keyName = 'id') {
979
- return new this.Table(null, name, callback, retentionTime, keyType, keyName);
941
+ query(id, callback) {
942
+ let body = {
943
+ db: this.db, cmd: "db.table.query", name: this.name, id: id
944
+ };
945
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
980
946
  }
981
947
 
982
- setupMirror(gwIPC, lwaName, confirm = false) {
983
- this.mirror = {gwIPC, lwaName, confirm};
948
+ queryByKey(key, callback) {
949
+ let body = {
950
+ db: this.db, cmd: "db.table.queryByKey", name: this.name, key: key
951
+ };
952
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
984
953
  }
985
- }
954
+
955
+ queryByTid(tid, callback) {
956
+ let body = {
957
+ db: this.db, cmd: "db.table.queryByTid", name: this.name, tid: tid
958
+ };
959
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
960
+ }
961
+
962
+ queryUnsynced(opts, callback) {
963
+ if (typeof opts === "function") {
964
+ callback = opts;
965
+ opts = {limit: 100};
966
+ }
967
+ if (!opts) opts = {};
968
+ if (opts.limit <= 0) opts.limit = 100;
969
+
970
+ let body = {
971
+ db: this.db, cmd: "db.table.queryUnsynced", name: this.name, limit: opts.limit
972
+ };
973
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
974
+ }
975
+
976
+ queryAll(callback) {
977
+ let body = {
978
+ db: this.db, cmd: "db.table.queryAll", name: this.name
979
+ };
980
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
981
+ }
982
+
983
+ sync(id, callback) {
984
+ let body = {
985
+ db: this.db, cmd: "db.table.sync", name: this.name, id: id
986
+ };
987
+ publishRequest(cutosAPI$1.CORE.DATABASE, callback, body);
988
+ }
989
+
990
+ publishRequestMirror(topic, callback, body) {
991
+ let mirror = this.database.mirror;
992
+ let cb = callback;
993
+ if (mirror && mirror.gwIPC) { // mirror enabled
994
+ cb = (dbResult) => {
995
+ let confirmCallback = !mirror.confirm ? null : (gwResult) => {
996
+ if (gwResult.status === "success") {
997
+ this.sync(dbResult.row);
998
+ }
999
+ };
1000
+ if (dbResult.status === true) {
1001
+ switch (body.cmd) {
1002
+ case "db.table.create":
1003
+ break;
1004
+ case "db.table.insert":
1005
+ body.id = dbResult.msg.row;
1006
+ body.timestamp = dbResult.msg.timestamp;
1007
+ break;
1008
+ case "db.table.insertById":
1009
+ case "db.table.insertByKey":
1010
+ body.timestamp = dbResult.msg.timestamp;
1011
+ break;
1012
+ }
1013
+ mirror.gwIPC.mirror(body, confirmCallback, {lwaName: mirror.lwaName});
1014
+ }
1015
+ callback && callback(dbResult);
1016
+ };
1017
+ }
1018
+ return publishRequest(topic, cb, body);
1019
+ }
1020
+ };
986
1021
 
987
1022
  class Logger {
988
1023
  constructor() {
@@ -1099,10 +1134,8 @@ class Device {
1099
1134
  *
1100
1135
  * @param name
1101
1136
  * @param type
1102
- * @param callback(result, error)
1103
- * @param opts
1104
1137
  */
1105
- constructor(name = 'default-device-name', type = cutosAPI$1.DEVICE.DEFAULT, callback, opts) {
1138
+ constructor(name = 'default-device-name', type = cutosAPI$1.DEVICE.DEFAULT) {
1106
1139
  this.name = name;
1107
1140
  this.type = type;
1108
1141
  this.topicData = cutosAPI$1.DEVICE.CHANNEL + '-' + type + '-data';
@@ -1129,26 +1162,35 @@ class Device {
1129
1162
  };
1130
1163
  listenerMap.set(cutosAPI$1.CORE_APP.HEARTBEAT_LISTENER, Device.heartbeatListener);
1131
1164
  }
1132
- // subscribe callback
1165
+ // subscribe command callback
1133
1166
  client.subscribe(this.topicCommand + '-callback');
1134
- this.init(name, type, callback, opts);
1135
1167
  }
1136
1168
 
1137
- init(name, type, callback, opts) {
1169
+ /**
1170
+ *
1171
+ * @param callback(result, error)
1172
+ * @param opts
1173
+ */
1174
+ init(opts, callback) {
1175
+ if (typeof opts === "function") {
1176
+ callback = opts;
1177
+ opts = null;
1178
+ }
1179
+
1138
1180
  let command = {};
1139
1181
  command.cmd = "init";
1140
- command.args = {name, type, opts};
1182
+ command.args = {name: this.name, type: this.type, opts};
1141
1183
  command.topicResponse = this.topicResponse;
1142
1184
  let defaultTopic = cutosAPI$1.DEVICE.CHANNEL + '-' + cutosAPI$1.DEVICE.DEFAULT + '-command';
1143
1185
  publishRequest(defaultTopic, null, command);
1144
1186
 
1145
- // response callback
1187
+ // subscribe response callback, currently only used for init
1146
1188
  client.subscribe(this.topicResponse); // receive response from device
1147
- listenerMap.set(this.topicResponse, ({status, msg}) => {
1148
- if (status) {
1189
+ listenerMap.set(this.topicResponse, (result) => {
1190
+ if (result.status) {
1149
1191
  callback && callback(`Driver ${this.type} loaded`);
1150
1192
  } else {
1151
- callback && callback(null, msg);
1193
+ callback && callback(null, result.msg);
1152
1194
  }
1153
1195
  });
1154
1196
  }
@@ -1163,8 +1205,8 @@ class Device {
1163
1205
 
1164
1206
  /**
1165
1207
  *
1166
- * @param command
1167
- * @param callback(result)
1208
+ * @param command - command description object {cmd: 'command', args: (''|{}) }
1209
+ * @param callback - (result) => {}, the result is normal make up of {status: true|false, msg: (''| {})}
1168
1210
  */
1169
1211
  sendCommand(command, callback) {
1170
1212
  // exam: command = {cmd: "connect", args: ""}
@@ -1186,11 +1228,11 @@ class Device {
1186
1228
  return this.data;
1187
1229
  }
1188
1230
 
1189
- readDeviceInfo(context) {
1231
+ readDeviceInfo(callback, context) {
1190
1232
  let msg = {};
1191
1233
  msg.cmd = "read-device-info";
1192
1234
  msg.context = context;
1193
- this.sendCommand(msg);
1235
+ this.sendCommand(msg, callback);
1194
1236
  }
1195
1237
  }
1196
1238
 
@@ -1328,6 +1370,7 @@ const CoreAPI$2 = {
1328
1370
  console.error("CUTOS CORE error:", err.message);
1329
1371
  });
1330
1372
  },
1373
+
1331
1374
  connected() {
1332
1375
  return !!client && client.connected;
1333
1376
  },
@@ -1382,10 +1425,6 @@ const CoreAPI$2 = {
1382
1425
  return publishRequest(cutosAPI$1.CORE.SHELL, callback, command);
1383
1426
  },
1384
1427
 
1385
- getDatabase(db, callback) {
1386
- return new Database(db, callback);
1387
- },
1388
-
1389
1428
  getVolume(callback) {
1390
1429
  return publishRequest(cutosAPI$1.CORE.GET_VOLUME, callback);
1391
1430
  },
@@ -1404,7 +1443,7 @@ cutos.CoreDefine = {
1404
1443
  DEVICE: cutosAPI$1.DEVICE, HEARTBEAT_STATUS: cutosAPI$1.HEARTBEAT_STATUS
1405
1444
  };
1406
1445
  cutos.CoreClass = {
1407
- Device, Driver
1446
+ Device, Driver, Database
1408
1447
  };
1409
1448
 
1410
1449
  cutos.cutosAPI = cutosAPI$1;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cutos/core",
3
- "version": "3.2.1",
3
+ "version": "3.2.3",
4
4
  "description": "The CUTOS (CUT Operating System) Core API is a JavaScript library that provides essential functionalities for LWA (Local Web Application) edge computing and communication in the CUTOS ecosystem. ",
5
5
  "main": "dist/cutos.cjs.js",
6
6
  "module": "dist/cutos.esm.js",