goby-database 2.1.18 → 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: {
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}`;
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "goby-database",
3
- "version": "2.1.18",
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": {