goby-database 1.0.3 → 1.0.5

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.
Files changed (2) hide show
  1. package/index.js +136 -24
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -35,6 +35,7 @@ class Project{
35
35
  begin:this.db.prepare('BEGIN IMMEDIATE'),
36
36
  commit:this.db.prepare('COMMIT'),
37
37
  rollback:this.db.prepare('ROLLBACK'),
38
+ create_item:this.db.prepare('INSERT INTO system_root(type,value) VALUES (@type, @value)'),
38
39
  get_junctionlist:this.db.prepare('SELECT id, junction_obj(side_a, side_b) AS sides, metadata FROM system_junctionlist'),
39
40
  get_class:this.db.prepare(`SELECT name, metadata FROM system_classlist WHERE id = ?`),
40
41
  get_class_id:this.db.prepare(`SELECT id FROM system_classlist WHERE name = ?`),
@@ -42,6 +43,7 @@ class Project{
42
43
  save_class_meta:this.db.prepare(`UPDATE system_classlist set metadata = ? WHERE id = ?`),
43
44
  update_window:this.db.prepare(`UPDATE system_windows set open = @open, type=@type, metadata = @meta WHERE id = @id`),
44
45
  create_window: this.db.prepare(`INSERT INTO system_windows (type,open, metadata) VALUES (@type, @open, @meta)`),
46
+ get_windows:this.db.prepare(`SELECT id, type, open, metadata FROM system_windows`),
45
47
  match_junction:this.db.prepare(`SELECT id, side_a, side_b, metadata FROM system_junctionlist WHERE (side_a = @input_1 AND side_b = @input_2 ) OR ( side_a = @input_2 AND side_b = @input_1 )`),
46
48
  fuzzy_match_junction:this.db.prepare(`SELECT id, side_a, side_b, metadata FROM system_junctionlist WHERE (side_a LIKE @input_1 AND side_b LIKE @input_2 ) OR ( side_a LIKE @input_2 AND side_b LIKE @input_1 )`)
47
49
  }
@@ -71,8 +73,12 @@ class Project{
71
73
  init(){
72
74
 
73
75
 
74
- //System table to contain all objects in the project.
75
- this.create_table('system','root',['id INTEGER NOT NULL PRIMARY KEY']);
76
+ //System table to contain all items in the project.
77
+ this.create_table('system','root',[
78
+ 'id INTEGER NOT NULL PRIMARY KEY',
79
+ 'type TEXT',
80
+ 'value TEXT'
81
+ ]);
76
82
 
77
83
 
78
84
  //System table to contain metadata for all classes created by user
@@ -85,7 +91,7 @@ class Project{
85
91
  this.create_table('system','windows',[
86
92
  'id INTEGER NOT NULL PRIMARY KEY',
87
93
  'type TEXT',
88
- 'open INT',
94
+ 'open INTEGER',
89
95
  'metadata TEXT'
90
96
  ]);
91
97
 
@@ -99,7 +105,7 @@ class Project{
99
105
  ('home',0,'${JSON.stringify({pos:[null,null], size:[540,400]})}'),
100
106
  ('hopper',0,'${JSON.stringify({pos:[null,null], size:[300,400]})}')`).run();
101
107
 
102
- // [TO ADD: special junction table for root objects to reference themselves in individual relation]
108
+ // [TO ADD: special junction table for root items to reference themselves in individual relation]
103
109
  this.create_table('system','junction_root',[
104
110
  'id_1 INTEGER',
105
111
  'id_2 INTEGER',
@@ -163,7 +169,7 @@ class Project{
163
169
 
164
170
  this.create_table('class',name,columns);
165
171
 
166
- const table_meta={
172
+ const class_meta={
167
173
  properties:[
168
174
  {
169
175
  name:'name',
@@ -187,7 +193,7 @@ class Project{
187
193
  }
188
194
  };
189
195
 
190
- this.db.prepare(`INSERT INTO system_classlist (name, metadata) VALUES ('${name}','${JSON.stringify(table_meta)}')`).run();
196
+ this.db.prepare(`INSERT INTO system_classlist (name, metadata) VALUES ('${name}','${JSON.stringify(class_meta)}')`).run();
191
197
  //get the id of newest value from system_classlist and return
192
198
  const class_id=this.db.prepare('SELECT id FROM system_classlist ORDER BY id DESC').get().id;
193
199
 
@@ -558,8 +564,8 @@ class Project{
558
564
 
559
565
  let cls=this.retrieve_class(class_id);
560
566
  let prop_name='user_'+cls.metadata.properties.find(a=>a.id==prop_id).name;
561
- for(let object of cls.objects){
562
- let prop_values=object[prop_name];
567
+ for(let item of cls.items){
568
+ let prop_values=item[prop_name];
563
569
  console.log(prop_name,prop_values);
564
570
  }
565
571
 
@@ -572,15 +578,26 @@ class Project{
572
578
  if(this.db.inTransaction) this.run.commit.run();
573
579
  this.db.close();
574
580
  }
581
+
582
+ action_create_item_in_root({type=null,value=''}){
583
+ // this.db.prepare('INSERT INTO system_root VALUES (null)').run();
584
+ this.run.create_item.run({type,value});
585
+ let id=this.db.prepare('SELECT id FROM system_root ORDER BY id DESC').get().id;
586
+ return id;
587
+ }
588
+
589
+ action_delete_item_from_root(id){
590
+ this.db.prepare(`DELETE FROM system_root WHERE id = ${id}`).run();
591
+ }
575
592
 
576
593
 
577
594
  action_add_row(class_id,class_name){
578
595
  //first add new row to root and get id
579
596
  if(class_name==undefined) class_name=this.class_cache[class_id].name;
580
- // console.log(class_name)
581
- this.db.prepare('INSERT INTO system_root VALUES (null)').run();
582
- const root_id=this.db.prepare('SELECT id FROM system_root ORDER BY id DESC').get().id;
583
597
 
598
+ // note for future: instead of letting class_id be undefined, locate it
599
+ const root_id=this.action_create_item_in_root({type:class_id!==undefined?'class_'+class_id:null});
600
+
584
601
  //get the last item in class table order and use it to get the order for the new item
585
602
  const last_order=this.db.prepare(`SELECT system_order FROM [class_${class_name}] ORDER BY system_order DESC`).get();
586
603
  const new_order=last_order?last_order.system_order+1:1;
@@ -594,7 +611,7 @@ class Project{
594
611
  /* input = {
595
612
  class_id: INT,
596
613
  prop_id: INT,
597
- object_id: INT
614
+ item_id: INT
598
615
  }
599
616
  */
600
617
 
@@ -603,7 +620,7 @@ class Project{
603
620
  input_2:`${input_2.class_id}.${input_2.prop_id || ''}`
604
621
  }
605
622
  let junction_id=this.run.match_junction.get(sides)?.id;
606
- this.db.prepare(`INSERT INTO junction_${junction_id} ("${sides.input_1}", "${sides.input_2}") VALUES (${input_1.object_id},${input_2.object_id})`).run();
623
+ this.db.prepare(`INSERT INTO junction_${junction_id} ("${sides.input_1}", "${sides.input_2}") VALUES (${input_1.item_id},${input_2.item_id})`).run();
607
624
 
608
625
  }
609
626
 
@@ -666,16 +683,16 @@ class Project{
666
683
  ${cte_joins.join(' ')}
667
684
  ${orderby}`;
668
685
 
669
- let objects=this.db.prepare(query).all();
686
+ let items=this.db.prepare(query).all();
670
687
 
671
688
  let stringified_properties=class_meta.properties.filter(a=>a.type=='relation'||a.conditions?.max>1);
672
- objects.map(row=>{
689
+ items.map(row=>{
673
690
  for (let prop of stringified_properties){
674
691
  row['user_'+prop.name]=JSON.parse(row['user_'+prop.name]);
675
692
  }
676
693
  })
677
694
  return {
678
- objects,
695
+ items,
679
696
  metadata:class_meta,
680
697
  name:class_name
681
698
  };
@@ -692,6 +709,30 @@ class Project{
692
709
  return classes;
693
710
  }
694
711
 
712
+ retrieve_windows(){
713
+ let windows=this.run.get_windows.all();
714
+ windows.map(a=>a.metadata=JSON.parse(a.metadata));
715
+ return windows;
716
+ }
717
+
718
+ retrieve_workspace_contents(id){
719
+ // get the workspace table
720
+ let blocks=this.db.prepare(`SELECT * FROM workspace_${id}`).all();
721
+ for(let block of blocks) block.properties=JSON.parse(block.properties);
722
+ // get any relevant root items
723
+ let items=this.db.prepare(`SELECT system_root.*
724
+ FROM system_root
725
+ LEFT JOIN workspace_${id}
726
+ ON system_root.id = workspace_1.concept_id
727
+ WHERE workspace_${id}.type = 'item';
728
+ `).all();
729
+ // get any relevant classes (going to hold off from this for now)
730
+
731
+ return {
732
+ blocks,
733
+ items
734
+ }
735
+ }
695
736
 
696
737
  action_config_window(type,open,meta={pos:[null,null], size:[1000,700]},id){
697
738
  if(id!==undefined){
@@ -703,22 +744,93 @@ class Project{
703
744
  })
704
745
  }else{
705
746
 
706
- this.run.create_window.run({
707
- open,
708
- type,
709
- meta:JSON.stringify(meta)
710
- })
711
- let id=this.get_latest_id('system_windows')
747
+ let id=this.create_workspace(open,meta)
748
+
712
749
  return id;
713
750
  }
714
- // {pos:[null,null], size:[1000,700]}
715
751
 
716
- // this.db.prepare(`INSERT INTO system_classlist (name, metadata) VALUES ('${name}','${JSON.stringify(table_meta)}')`)
717
752
  }
718
753
 
719
754
 
755
+ create_workspace(open,meta){
756
+
757
+ this.run.create_window.run({
758
+ type:'workspace',
759
+ open,
760
+ meta:JSON.stringify(meta)
761
+ })
762
+
763
+ let id=this.get_latest_id('system_windows')
764
+
765
+ this.create_table('workspace',id,[
766
+ 'block_id INTEGER NOT NULL PRIMARY KEY',
767
+ 'type TEXT',
768
+ 'properties TEXT',
769
+ 'concept_id INTEGER'
770
+ ]);
771
+
772
+
773
+
774
+ return id;
775
+ }
776
+
777
+ action_create_workspace_block({workspace_id,type,properties,concept_id}){
778
+ // should return block id
779
+ this.db.prepare(`INSERT INTO workspace_${workspace_id}(type,properties,concept_id) VALUES (@type,@properties,@concept_id)`).run({
780
+ type,
781
+ properties:JSON.stringify(properties),
782
+ concept_id
783
+ });
784
+ let block_id=this.db.prepare(`SELECT block_id FROM workspace_${workspace_id} ORDER BY block_id DESC`).get().id;
785
+ return block_id;
786
+
787
+ }
788
+
789
+ action_remove_workspace_block({workspace_id,block_id}){
790
+ this.db.prepare(`DELETE FROM workspace_${workspace_id} WHERE block_id = ${block_id}`).run();
791
+ }
792
+
793
+ action_create_and_add_to_workspace(workspace_id,blocktype,block_properties,concept_data){
794
+ let concept_id;
795
+ // concept creation
796
+ switch(blocktype){
797
+ case 'item':
798
+ let {
799
+ value:item_value,
800
+ type:item_type
801
+ } = concept_data;
802
+ concept_id=this.action_create_item_in_root({type:item_type,value:item_value});
803
+ break;
804
+ // add cases for class and anything else in the future
805
+ }
806
+
807
+ let block_id=this.action_create_workspace_block({
808
+ workspace_id,
809
+ type:blocktype,
810
+ properties:block_properties,
811
+ concept_id
812
+ })
813
+
814
+ return {
815
+ concept_id,
816
+ block_id
817
+ }
818
+ // should return the block id and item id
819
+ }
820
+
821
+ action_remove_from_workspace_and_delete(workspace_id,block_id,blocktype,concept_id){
822
+ action_remove_workspace_block({workspace_id,block_id});
823
+ switch(blocktype){
824
+ case 'item':
825
+ this.action_delete_item_from_root(concept_id);
826
+ break;
827
+ }
828
+ }
829
+
720
830
  }
721
831
 
832
+
833
+
722
834
  // export default Project;
723
835
  module.exports=Project;
724
836
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "goby-database",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "This will hold the core better-sqlite3-powered application for creating and modifying goby databases",
5
5
  "main": "index.js",
6
6
  "scripts": {