goby-database 2.2.32 → 2.2.33

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
@@ -1,5 +1,5 @@
1
1
  import type { Database as DatabaseType, Statement } from 'better-sqlite3';
2
- import type { SQLTableType, SQLClassListRow, SQLJunctonListRow, JunctionSides, JunctionList, ClassList, Property, DataType, ItemRelationSide, SQLApplicationWindow, ApplicationWindow, WorkspaceBlock, ClassData, ClassEdit, RelationEdit, PropertyEdit, MaxValues, PropertyType, RelationProperty, DataProperty, RelationEditValidSides, ItemPagination, PaginatedItems } from './types.js';
2
+ import type { SQLTableType, SQLClassListRow, SQLJunctonListRow, JunctionSides, JunctionList, ClassList, Property, DataType, SQLApplicationWindow, ApplicationWindow, WorkspaceBlock, ClassData, ClassEdit, RelationEdit, PropertyEdit, MaxValues, PropertyType, RelationProperty, DataProperty, RelationEditValidSides, ItemPagination, PaginatedItems, ItemRelationSideInput } from './types.js';
3
3
  export default class Project {
4
4
  db: DatabaseType;
5
5
  run: {
@@ -92,16 +92,13 @@ export default class Project {
92
92
  value: any;
93
93
  }[]): void;
94
94
  /**
95
- * Adds/removes relations between items/item properties
95
+ * Adds/removes relations between items/item properties.
96
+ * Can create new items in a class if a label is specified instead of an item id.
96
97
  * @param relations - list of pairs of items for which relations should be added or removed between specified properties
97
98
  */
98
99
  action_edit_relations(relations: {
99
100
  change: 'add' | 'remove';
100
- sides: [input_1: ItemRelationSide & {
101
- order?: number;
102
- }, input_2: ItemRelationSide & {
103
- order?: number;
104
- }];
101
+ sides: [input_1: ItemRelationSideInput, input_2: ItemRelationSideInput];
105
102
  }[]): void;
106
103
  retrieve_class_items({ class_id, class_name, class_data, pagination }: {
107
104
  class_id: number;
package/dist/index.js CHANGED
@@ -773,7 +773,8 @@ export default class Project {
773
773
  this.db.prepare(insert_statement).run(params);
774
774
  }
775
775
  /**
776
- * Adds/removes relations between items/item properties
776
+ * Adds/removes relations between items/item properties.
777
+ * Can create new items in a class if a label is specified instead of an item id.
777
778
  * @param relations - list of pairs of items for which relations should be added or removed between specified properties
778
779
  */
779
780
  action_edit_relations(relations) {
@@ -782,16 +783,45 @@ export default class Project {
782
783
  // - enforce max_values here
783
784
  var _a;
784
785
  for (let { change, sides } of relations) {
785
- const [input_1, input_2] = sides;
786
+ let [input_1, input_2] = sides;
786
787
  const column_names = {
787
788
  input_1: junction_col_name(input_1.class_id, input_1.prop_id),
788
789
  input_2: junction_col_name(input_2.class_id, input_2.prop_id)
789
790
  };
790
- const junction_id = (_a = this.junction_cache.find(j => full_relation_match(j.sides, [input_1, input_2]))) === null || _a === void 0 ? void 0 : _a.id;
791
+ const junction_id = (_a = this.junction_cache.find(j => full_relation_match(j.sides, sides))) === null || _a === void 0 ? void 0 : _a.id;
791
792
  if (!defined(junction_id)) {
792
793
  throw Error('Something went wrong - junction table for relationship not found');
793
794
  }
794
795
  if (change == 'add') {
796
+ // if items referenced by the input don’t yet have associated IDs, create them
797
+ const sides_registered = sides.map((input) => {
798
+ var _a;
799
+ if ("item_id" in input) {
800
+ return input;
801
+ }
802
+ else {
803
+ const { class_id, prop_id, order, label } = input;
804
+ const class_data = this.lookup_class(class_id);
805
+ const label_prop_id = (_a = class_data.metadata.label) === null || _a === void 0 ? void 0 : _a.properties[0];
806
+ const property_values = [];
807
+ if (label_prop_id) {
808
+ property_values.push({
809
+ property_id: label_prop_id,
810
+ value: label
811
+ });
812
+ }
813
+ const item_id = this.action_add_row(input.class_id, property_values);
814
+ return {
815
+ registered: true,
816
+ class_id,
817
+ prop_id,
818
+ item_id,
819
+ order
820
+ };
821
+ }
822
+ });
823
+ input_1 = sides_registered[0];
824
+ input_2 = sides_registered[1];
795
825
  const column_value_set = [
796
826
  {
797
827
  column: `"${column_names.input_1}"`,
@@ -802,7 +832,7 @@ export default class Project {
802
832
  value: input_2.item_id
803
833
  }
804
834
  ];
805
- sides.forEach((side, s) => {
835
+ sides_registered.forEach((side, s) => {
806
836
  var _a, _b, _c;
807
837
  // for each side, check if it has a prop id
808
838
  if (defined(side.prop_id)) {
@@ -847,10 +877,15 @@ export default class Project {
847
877
  `).run();
848
878
  }
849
879
  else if (change == 'remove') {
850
- this.db.prepare(`
851
- DELETE FROM junction_${junction_id}
852
- WHERE "${column_names.input_1}" = ${input_1.item_id}
853
- AND "${column_names.input_2}" = ${input_2.item_id}`).run();
880
+ if (("item_id" in input_1) && ("item_id" in input_2)) {
881
+ this.db.prepare(`
882
+ DELETE FROM junction_${junction_id}
883
+ WHERE "${column_names.input_1}" = ${input_1.item_id}
884
+ AND "${column_names.input_2}" = ${input_2.item_id}`).run();
885
+ }
886
+ else {
887
+ console.log('skipped relation delete (either item input is missing an ID)');
888
+ }
854
889
  }
855
890
  }
856
891
  // NOTE: should this trigger a refresh to items?
@@ -938,6 +973,7 @@ export default class Project {
938
973
  const target_label_id = (_d = (_c = target_class === null || target_class === void 0 ? void 0 : target_class.metadata) === null || _c === void 0 ? void 0 : _c.label) === null || _d === void 0 ? void 0 : _d.properties[0];
939
974
  const target_label = target_class === null || target_class === void 0 ? void 0 : target_class.properties.find((p) => p.id == target_label_id);
940
975
  const label_sql_string = target_label ? `,'user_${target_label.name}',target_class."user_${target_label.name}"` : '';
976
+ const target_prop_string = defined(target.prop_id) ? `,'prop_id',${target.prop_id}` : '';
941
977
  // NOTE: as mentioned elsewhere, possibly allow multiple label props
942
978
  let junction_id = target.junction_id;
943
979
  let target_select = `
@@ -947,6 +983,7 @@ export default class Project {
947
983
  'class_id',${target.class_id},
948
984
  'system_id',junction."${target_junction_column_name}",
949
985
  'system_order',junction."${property_junction_column_name}_order"
986
+ ${target_prop_string}
950
987
  ${label_sql_string}
951
988
  ) AS target_data, junction."${property_junction_column_name}_order" AS relation_order
952
989
  FROM junction_${junction_id} AS junction
package/dist/types.d.ts CHANGED
@@ -28,6 +28,16 @@ export type ItemRelationSide = {
28
28
  item_id: number;
29
29
  prop_id?: number;
30
30
  };
31
+ export type RegisteredItemRelationSideInput = ItemRelationSide & {
32
+ order?: number;
33
+ };
34
+ export type UnregisteredItemRelationSideInput = {
35
+ class_id: number;
36
+ prop_id?: number;
37
+ label: string;
38
+ order?: number;
39
+ };
40
+ export type ItemRelationSideInput = RegisteredItemRelationSideInput | UnregisteredItemRelationSideInput;
31
41
  type RelationCreate = {
32
42
  type: 'create';
33
43
  sides: [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "goby-database",
3
- "version": "2.2.32",
3
+ "version": "2.2.33",
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
  "repository":{