goby-database 2.1.17 → 2.1.19

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/index.d.ts CHANGED
@@ -81,10 +81,11 @@ export default class Project {
81
81
  value: any;
82
82
  }[]): void;
83
83
  action_make_relations(relations: [input_1: ItemRelationSide, input_2: ItemRelationSide][]): void;
84
- retrieve_class_items({ class_id, class_name, class_data }: {
84
+ retrieve_class_items({ class_id, class_name, class_data, slim }: {
85
85
  class_id: number;
86
86
  class_name?: string;
87
87
  class_data?: ClassData;
88
+ slim?: boolean;
88
89
  }): ClassRow[];
89
90
  retrieve_all_classes(): ClassData[];
90
91
  parse_sql_prop(class_id: number, sql_prop: {
@@ -108,9 +109,9 @@ export default class Project {
108
109
  id?: number;
109
110
  }): number | undefined;
110
111
  create_workspace(open: ApplicationWindow["open"], metadata: ApplicationWindow["metadata"]): number;
111
- action_create_workspace_block({ workspace_id, thing_type, block_metadata, thing_id }: {
112
+ action_create_workspace_block({ workspace_id, type, block_metadata, thing_id }: {
112
113
  workspace_id: ApplicationWindow["id"];
113
- thing_type: WorkspaceBlock["thing_type"];
114
+ type: WorkspaceBlock["type"];
114
115
  block_metadata: WorkspaceBlock["metadata"];
115
116
  thing_id: WorkspaceBlock["thing_id"];
116
117
  }): number;
@@ -118,14 +119,14 @@ export default class Project {
118
119
  workspace_id: number;
119
120
  block_id: number;
120
121
  }): void;
121
- action_create_and_add_to_workspace({ workspace_id, thing_type, block_metadata, thing_data }: {
122
+ action_create_and_add_to_workspace({ workspace_id, type, block_metadata, thing_data }: {
122
123
  workspace_id: number;
123
- thing_type: WorkspaceBlock["thing_type"];
124
+ type: WorkspaceBlock["type"];
124
125
  block_metadata: WorkspaceBlock["metadata"];
125
126
  thing_data: any;
126
127
  }): {
127
128
  thing_id: number;
128
129
  block_id: number;
129
130
  };
130
- action_remove_from_workspace_and_delete(workspace_id: number, block_id: number, thing_type: WorkspaceBlock["thing_type"], thing_id: number): void;
131
+ action_remove_from_workspace_and_delete(workspace_id: number, block_id: number, type: WorkspaceBlock["type"], thing_id: number): void;
131
132
  }
package/dist/index.js CHANGED
@@ -111,6 +111,7 @@ export default class Project {
111
111
  if (caches.includes('items')) {
112
112
  let refreshed_items = [];
113
113
  for (let class_data of this.class_cache) {
114
+ // NOTE: add pagination to this
114
115
  let items = this.retrieve_class_items({ class_id: class_data.id });
115
116
  refreshed_items.push({
116
117
  class_id: class_data.id,
@@ -149,6 +150,10 @@ export default class Project {
149
150
  const class_meta = {
150
151
  style: {
151
152
  color: '#b5ffd5'
153
+ },
154
+ label: {
155
+ // TODO: build functionality to change label property in the future
156
+ properties: [1]
152
157
  }
153
158
  };
154
159
  // create entry for class in classlist
@@ -807,7 +812,8 @@ export default class Project {
807
812
  this.refresh_caches(['items']);
808
813
  // NOTE: should this trigger a refresh to items?
809
814
  }
810
- retrieve_class_items({ class_id, class_name, class_data }) {
815
+ retrieve_class_items({ class_id, class_name, class_data, slim = false }) {
816
+ var _a, _b, _c, _d;
811
817
  if (class_name == undefined || class_data == undefined) {
812
818
  class_data = this.lookup_class(class_id);
813
819
  class_name = class_data.name;
@@ -821,48 +827,68 @@ export default class Project {
821
827
  // //joined+added between SELECT and FROM, built from relations
822
828
  const relation_selections = [];
823
829
  let relation_properties = class_data.properties.filter(a => a.type == 'relation');
824
- for (let prop of relation_properties) {
825
- const target_selects = [];
826
- let property_junction_column_name = junction_col_name(class_id, prop.id);
827
- if (prop.relation_targets.length > 0) {
828
- for (let i = 0; i < prop.relation_targets.length; i++) {
829
- // find the side that does not match both the class and prop IDs
830
- let target = prop.relation_targets[i];
831
- if (target) {
832
- let target_junction_column_name = junction_col_name(target.class_id, target.prop_id);
833
- let junction_id = target.junction_id;
834
- let target_select = `SELECT "${property_junction_column_name}", json_object('class_id',${target.class_id},'id',"${target_junction_column_name}") AS target_data FROM junction_${junction_id}`;
835
- target_selects.push(target_select);
836
- }
837
- else {
838
- throw Error('Something went wrong trying to retrieve relationship data');
830
+ if (!slim) {
831
+ for (let prop of relation_properties) {
832
+ const target_selects = [];
833
+ let property_junction_column_name = junction_col_name(class_id, prop.id);
834
+ if (prop.relation_targets.length > 0) {
835
+ for (let i = 0; i < prop.relation_targets.length; i++) {
836
+ // find the side that does not match both the class and prop IDs
837
+ let target = prop.relation_targets[i];
838
+ const target_class = this.class_cache.find((a) => a.id == (target === null || target === void 0 ? void 0 : target.class_id));
839
+ if (target && target_class) {
840
+ let target_junction_column_name = junction_col_name(target.class_id, target.prop_id);
841
+ // NOTE: as mentioned elsewhere, possibly allow multiple label props
842
+ const target_label_id = (_b = (_a = target_class === null || target_class === void 0 ? void 0 : target_class.metadata) === null || _a === void 0 ? void 0 : _a.label) === null || _b === void 0 ? void 0 : _b.properties[0];
843
+ const target_label = target_class === null || target_class === void 0 ? void 0 : target_class.properties.find((p) => p.id == target_label_id);
844
+ const label_sql_string = target_label ? `,'user_${target_label.name}',target_class."user_${target_label.name}"` : '';
845
+ let junction_id = target.junction_id;
846
+ let target_select = `
847
+ SELECT
848
+ "${property_junction_column_name}",
849
+ json_object('class_id',${target.class_id},'id',junction."${target_junction_column_name}"${label_sql_string}) AS target_data
850
+ FROM junction_${junction_id} AS junction
851
+ LEFT JOIN "class_${target_class === null || target_class === void 0 ? void 0 : target_class.name}" AS target_class ON junction."${target_junction_column_name}" = target_class.system_id
852
+ `;
853
+ target_selects.push(target_select);
854
+ }
855
+ else {
856
+ throw Error('Something went wrong trying to retrieve relationship data');
857
+ }
839
858
  }
859
+ // uses built-in aggregate json function instead of group_concat craziness
860
+ const cte = `[${prop.id}_cte] AS (
861
+ SELECT "${property_junction_column_name}", json_group_array( json(target_data) ) AS [user_${prop.name}]
862
+ FROM
863
+ (
864
+ ${target_selects.join(`
865
+ UNION
866
+ `)}
867
+ )
868
+ GROUP BY "${property_junction_column_name}"
869
+ )`;
870
+ cte_strings.push(cte);
871
+ relation_selections.push(`[${prop.id}_cte].[user_${prop.name}]`);
872
+ cte_joins.push(`LEFT JOIN [${prop.id}_cte] ON [${prop.id}_cte]."${property_junction_column_name}" = ${class_string}.system_id`);
873
+ }
874
+ else {
875
+ relation_selections.push(`'[]' AS [user_${prop.name}]`);
840
876
  }
841
- // uses built-in aggregate json function instead of group_concat craziness
842
- const cte = `[${prop.id}_cte] AS (
843
- SELECT "${property_junction_column_name}", json_group_array( json(target_data) ) AS [user_${prop.name}]
844
- FROM
845
- (
846
- ${target_selects.join(`
847
- UNION
848
- `)}
849
- )
850
- GROUP BY "${property_junction_column_name}"
851
- )`;
852
- cte_strings.push(cte);
853
- relation_selections.push(`[${prop.id}_cte].[user_${prop.name}]`);
854
- cte_joins.push(`LEFT JOIN [${prop.id}_cte] ON [${prop.id}_cte]."${property_junction_column_name}" = ${class_string}.system_id`);
855
- }
856
- else {
857
- relation_selections.push(`'[]' AS [user_${prop.name}]`);
858
877
  }
859
878
  }
860
879
  let orderby = `ORDER BY ${class_string}.system_order`;
880
+ let table_selection = `[class_${class_name}].*`;
881
+ if (slim) {
882
+ const label_prop_ids = (_d = (_c = class_data.metadata.label) === null || _c === void 0 ? void 0 : _c.properties) !== null && _d !== void 0 ? _d : [];
883
+ const label_props = class_data.properties.filter((p) => label_prop_ids.includes(p.id));
884
+ const label_prop_sql_string = label_props.map((p) => `[user_${p.name}]`).join(',');
885
+ table_selection = `system_id,system_order,${label_prop_sql_string}`;
886
+ }
861
887
  let comma_break = `,
862
888
  `;
863
889
  let query = `
864
890
  ${cte_strings.length > 0 ? "WITH " + cte_strings.join(comma_break) : ''}
865
- SELECT [class_${class_name}].* ${relation_selections.length > 0 ? ', ' + relation_selections.join(`, `) : ''}
891
+ SELECT ${table_selection} ${relation_selections.length > 0 ? ', ' + relation_selections.join(`, `) : ''}
866
892
  FROM [class_${class_name}]
867
893
  ${cte_joins.join(' ')}
868
894
  ${orderby}`;
@@ -949,7 +975,7 @@ export default class Project {
949
975
  WHERE workspace_${id}.type = 'item';
950
976
  `).all();
951
977
  // get any relevant classes
952
- const classes = this.class_cache.filter((cls) => blocks.find((block) => { block.thing_type == 'class' && block.thing_id == cls.id; }));
978
+ const classes = this.class_cache.filter((cls) => blocks.some((block) => block.type == 'class' && block.thing_id == cls.id));
953
979
  // NOTE: could possibly add class items as well in the future
954
980
  return {
955
981
  blocks,
@@ -988,11 +1014,11 @@ export default class Project {
988
1014
  ]);
989
1015
  return id;
990
1016
  }
991
- action_create_workspace_block({ workspace_id, thing_type, block_metadata, thing_id }) {
1017
+ action_create_workspace_block({ workspace_id, type, block_metadata, thing_id }) {
992
1018
  var _a;
993
1019
  // should return block id
994
1020
  this.db.prepare(`INSERT INTO workspace_${workspace_id}(type,metadata,thing_id) VALUES (@type,@metadata,@thing_id)`).run({
995
- type: thing_type,
1021
+ type: type,
996
1022
  metadata: JSON.stringify(block_metadata),
997
1023
  thing_id
998
1024
  });
@@ -1005,10 +1031,10 @@ export default class Project {
1005
1031
  this.db.prepare(`DELETE FROM workspace_${workspace_id} WHERE block_id = ${block_id}`).run();
1006
1032
  }
1007
1033
  ;
1008
- action_create_and_add_to_workspace({ workspace_id, thing_type, block_metadata, thing_data }) {
1034
+ action_create_and_add_to_workspace({ workspace_id, type, block_metadata, thing_data }) {
1009
1035
  let thing_id;
1010
1036
  // thing creation
1011
- switch (thing_type) {
1037
+ switch (type) {
1012
1038
  case 'item':
1013
1039
  let { value: item_value, type: item_type } = thing_data;
1014
1040
  thing_id = this.create_item_in_root({ type: item_type, value: item_value });
@@ -1019,7 +1045,7 @@ export default class Project {
1019
1045
  throw Error('Something went wrong saving an item from a workspace');
1020
1046
  let block_id = this.action_create_workspace_block({
1021
1047
  workspace_id,
1022
- thing_type,
1048
+ type,
1023
1049
  block_metadata,
1024
1050
  thing_id
1025
1051
  });
@@ -1029,9 +1055,9 @@ export default class Project {
1029
1055
  };
1030
1056
  // should return the block id and item id
1031
1057
  }
1032
- action_remove_from_workspace_and_delete(workspace_id, block_id, thing_type, thing_id) {
1058
+ action_remove_from_workspace_and_delete(workspace_id, block_id, type, thing_id) {
1033
1059
  this.action_remove_workspace_block({ workspace_id, block_id });
1034
- switch (thing_type) {
1060
+ switch (type) {
1035
1061
  case 'item':
1036
1062
  this.delete_item_from_root(thing_id);
1037
1063
  break;
package/dist/types.d.ts CHANGED
@@ -96,6 +96,9 @@ export type ClassMetadata = {
96
96
  style: {
97
97
  color?: string;
98
98
  };
99
+ label?: {
100
+ properties: number[];
101
+ };
99
102
  };
100
103
  export type PropertyType = 'data' | 'relation';
101
104
  export type BinaryBoolean = 0 | 1;
@@ -163,7 +166,7 @@ export type ThingType = 'item' | 'class';
163
166
  export type BaseWorkspaceBlock = {
164
167
  /** this is the ID of the display element in the workspace, generated by the workspace table */
165
168
  block_id: number;
166
- thing_type: ThingType;
169
+ type: ThingType;
167
170
  /** this is the ID of the class, item, or other “thing” represented in the data */
168
171
  thing_id: number;
169
172
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "goby-database",
3
- "version": "2.1.17",
3
+ "version": "2.1.19",
4
4
  "description": "This will hold the core better-sqlite3-powered application for creating and modifying goby databases",
5
5
  "main": "dist/index.js",
6
6
  "files": [
@@ -10,10 +10,7 @@
10
10
  "!dist/utils.js.map",
11
11
  "!dist/sandbox.js",
12
12
  "!dist/sandbox.d.ts",
13
- "!dist/sandbox.js.map",
14
- "!dist/unit-tests.js",
15
- "!dist/unit-tests.d.ts",
16
- "!dist/unit-tests.js.map"
13
+ "!dist/sandbox.js.map"
17
14
  ],
18
15
  "types": "dist/index.d.ts",
19
16
  "scripts": {