goby-database 1.0.4 → 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.
- package/index.js +129 -23
- 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 = ?`),
|
|
@@ -72,8 +73,12 @@ class Project{
|
|
|
72
73
|
init(){
|
|
73
74
|
|
|
74
75
|
|
|
75
|
-
//System table to contain all
|
|
76
|
-
this.create_table('system','root',[
|
|
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
|
+
]);
|
|
77
82
|
|
|
78
83
|
|
|
79
84
|
//System table to contain metadata for all classes created by user
|
|
@@ -100,7 +105,7 @@ class Project{
|
|
|
100
105
|
('home',0,'${JSON.stringify({pos:[null,null], size:[540,400]})}'),
|
|
101
106
|
('hopper',0,'${JSON.stringify({pos:[null,null], size:[300,400]})}')`).run();
|
|
102
107
|
|
|
103
|
-
// [TO ADD: special junction table for root
|
|
108
|
+
// [TO ADD: special junction table for root items to reference themselves in individual relation]
|
|
104
109
|
this.create_table('system','junction_root',[
|
|
105
110
|
'id_1 INTEGER',
|
|
106
111
|
'id_2 INTEGER',
|
|
@@ -164,7 +169,7 @@ class Project{
|
|
|
164
169
|
|
|
165
170
|
this.create_table('class',name,columns);
|
|
166
171
|
|
|
167
|
-
const
|
|
172
|
+
const class_meta={
|
|
168
173
|
properties:[
|
|
169
174
|
{
|
|
170
175
|
name:'name',
|
|
@@ -188,7 +193,7 @@ class Project{
|
|
|
188
193
|
}
|
|
189
194
|
};
|
|
190
195
|
|
|
191
|
-
this.db.prepare(`INSERT INTO system_classlist (name, metadata) VALUES ('${name}','${JSON.stringify(
|
|
196
|
+
this.db.prepare(`INSERT INTO system_classlist (name, metadata) VALUES ('${name}','${JSON.stringify(class_meta)}')`).run();
|
|
192
197
|
//get the id of newest value from system_classlist and return
|
|
193
198
|
const class_id=this.db.prepare('SELECT id FROM system_classlist ORDER BY id DESC').get().id;
|
|
194
199
|
|
|
@@ -559,8 +564,8 @@ class Project{
|
|
|
559
564
|
|
|
560
565
|
let cls=this.retrieve_class(class_id);
|
|
561
566
|
let prop_name='user_'+cls.metadata.properties.find(a=>a.id==prop_id).name;
|
|
562
|
-
for(let
|
|
563
|
-
let prop_values=
|
|
567
|
+
for(let item of cls.items){
|
|
568
|
+
let prop_values=item[prop_name];
|
|
564
569
|
console.log(prop_name,prop_values);
|
|
565
570
|
}
|
|
566
571
|
|
|
@@ -573,15 +578,26 @@ class Project{
|
|
|
573
578
|
if(this.db.inTransaction) this.run.commit.run();
|
|
574
579
|
this.db.close();
|
|
575
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
|
+
}
|
|
576
592
|
|
|
577
593
|
|
|
578
594
|
action_add_row(class_id,class_name){
|
|
579
595
|
//first add new row to root and get id
|
|
580
596
|
if(class_name==undefined) class_name=this.class_cache[class_id].name;
|
|
581
|
-
// console.log(class_name)
|
|
582
|
-
this.db.prepare('INSERT INTO system_root VALUES (null)').run();
|
|
583
|
-
const root_id=this.db.prepare('SELECT id FROM system_root ORDER BY id DESC').get().id;
|
|
584
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
|
+
|
|
585
601
|
//get the last item in class table order and use it to get the order for the new item
|
|
586
602
|
const last_order=this.db.prepare(`SELECT system_order FROM [class_${class_name}] ORDER BY system_order DESC`).get();
|
|
587
603
|
const new_order=last_order?last_order.system_order+1:1;
|
|
@@ -595,7 +611,7 @@ class Project{
|
|
|
595
611
|
/* input = {
|
|
596
612
|
class_id: INT,
|
|
597
613
|
prop_id: INT,
|
|
598
|
-
|
|
614
|
+
item_id: INT
|
|
599
615
|
}
|
|
600
616
|
*/
|
|
601
617
|
|
|
@@ -604,7 +620,7 @@ class Project{
|
|
|
604
620
|
input_2:`${input_2.class_id}.${input_2.prop_id || ''}`
|
|
605
621
|
}
|
|
606
622
|
let junction_id=this.run.match_junction.get(sides)?.id;
|
|
607
|
-
this.db.prepare(`INSERT INTO junction_${junction_id} ("${sides.input_1}", "${sides.input_2}") VALUES (${input_1.
|
|
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();
|
|
608
624
|
|
|
609
625
|
}
|
|
610
626
|
|
|
@@ -667,16 +683,16 @@ class Project{
|
|
|
667
683
|
${cte_joins.join(' ')}
|
|
668
684
|
${orderby}`;
|
|
669
685
|
|
|
670
|
-
let
|
|
686
|
+
let items=this.db.prepare(query).all();
|
|
671
687
|
|
|
672
688
|
let stringified_properties=class_meta.properties.filter(a=>a.type=='relation'||a.conditions?.max>1);
|
|
673
|
-
|
|
689
|
+
items.map(row=>{
|
|
674
690
|
for (let prop of stringified_properties){
|
|
675
691
|
row['user_'+prop.name]=JSON.parse(row['user_'+prop.name]);
|
|
676
692
|
}
|
|
677
693
|
})
|
|
678
694
|
return {
|
|
679
|
-
|
|
695
|
+
items,
|
|
680
696
|
metadata:class_meta,
|
|
681
697
|
name:class_name
|
|
682
698
|
};
|
|
@@ -699,6 +715,25 @@ class Project{
|
|
|
699
715
|
return windows;
|
|
700
716
|
}
|
|
701
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
|
+
}
|
|
736
|
+
|
|
702
737
|
action_config_window(type,open,meta={pos:[null,null], size:[1000,700]},id){
|
|
703
738
|
if(id!==undefined){
|
|
704
739
|
this.run.update_window.run({
|
|
@@ -709,22 +744,93 @@ class Project{
|
|
|
709
744
|
})
|
|
710
745
|
}else{
|
|
711
746
|
|
|
712
|
-
this.
|
|
713
|
-
|
|
714
|
-
type,
|
|
715
|
-
meta:JSON.stringify(meta)
|
|
716
|
-
})
|
|
717
|
-
let id=this.get_latest_id('system_windows')
|
|
747
|
+
let id=this.create_workspace(open,meta)
|
|
748
|
+
|
|
718
749
|
return id;
|
|
719
750
|
}
|
|
720
|
-
// {pos:[null,null], size:[1000,700]}
|
|
721
751
|
|
|
722
|
-
// this.db.prepare(`INSERT INTO system_classlist (name, metadata) VALUES ('${name}','${JSON.stringify(table_meta)}')`)
|
|
723
752
|
}
|
|
724
753
|
|
|
725
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
|
+
|
|
726
830
|
}
|
|
727
831
|
|
|
832
|
+
|
|
833
|
+
|
|
728
834
|
// export default Project;
|
|
729
835
|
module.exports=Project;
|
|
730
836
|
|