goby-database 2.2.23 → 2.2.25

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
@@ -76,7 +76,10 @@ export default class Project {
76
76
  property_id: number;
77
77
  value: any;
78
78
  }[]): void;
79
- action_make_relations(relations: [input_1: ItemRelationSide, input_2: ItemRelationSide][]): void;
79
+ action_edit_relations(relations: {
80
+ change: 'add' | 'remove';
81
+ sides: [input_1: ItemRelationSide, input_2: ItemRelationSide];
82
+ }[]): void;
80
83
  retrieve_class_items({ class_id, class_name, class_data, pagination }: {
81
84
  class_id: number;
82
85
  class_name?: string;
package/dist/index.js CHANGED
@@ -604,7 +604,8 @@ export default class Project {
604
604
  // creates table
605
605
  this.create_table('junction', id, [
606
606
  `"${junction_col_name(sides[0].class_id, sides[0].prop_id)}" INTEGER`,
607
- `"${junction_col_name(sides[1].class_id, sides[1].prop_id)}" INTEGER`
607
+ `"${junction_col_name(sides[1].class_id, sides[1].prop_id)}" INTEGER`,
608
+ `date_added INTEGER`
608
609
  ]);
609
610
  return id;
610
611
  }
@@ -771,23 +772,33 @@ export default class Project {
771
772
  };
772
773
  }
773
774
  }
774
- action_make_relations(relations) {
775
+ action_edit_relations(relations) {
775
776
  // NOTE: changes to make to this in the future:
776
777
  // - for input readability, allow class_name and prop_name as input options, assuming they’re enforced as unique, and use them to look up IDs
777
778
  // - enforce max_values here
778
779
  var _a;
779
- for (let [input_1, input_2] of relations) {
780
- let column_names = {
780
+ for (let { change, sides } of relations) {
781
+ const [input_1, input_2] = sides;
782
+ const column_names = {
781
783
  input_1: junction_col_name(input_1.class_id, input_1.prop_id),
782
784
  input_2: junction_col_name(input_2.class_id, input_2.prop_id)
783
785
  };
784
- let 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;
786
+ 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;
785
787
  if (junction_id) {
786
- this.db.prepare(`
787
- INSERT INTO junction_${junction_id}
788
- ("${column_names.input_1}", "${column_names.input_2}")
789
- VALUES (${input_1.item_id},${input_2.item_id})
790
- `).run();
788
+ if (change == 'add') {
789
+ const date_added = Date.now();
790
+ this.db.prepare(`
791
+ INSERT INTO junction_${junction_id}
792
+ ("${column_names.input_1}", "${column_names.input_2}",date_added)
793
+ VALUES (${input_1.item_id},${input_2.item_id},${date_added})
794
+ `).run();
795
+ }
796
+ else if (change == 'remove') {
797
+ this.db.prepare(`
798
+ DELETE FROM junction_${junction_id}
799
+ WHERE "${column_names.input_1}" = ${input_1.item_id}
800
+ AND "${column_names.input_2}" = ${input_2.item_id}`).run();
801
+ }
791
802
  }
792
803
  else {
793
804
  throw Error('Something went wrong - junction table for relationship not found');
@@ -837,7 +848,7 @@ export default class Project {
837
848
  let target_select = `
838
849
  SELECT
839
850
  "${property_junction_column_name}",
840
- json_object('class_id',${target.class_id},'system_id',junction."${target_junction_column_name}"${label_sql_string}) AS target_data
851
+ json_object('class_id',${target.class_id},'system_id',junction."${target_junction_column_name}"${label_sql_string}) AS target_data, junction.date_added AS date_added
841
852
  FROM junction_${junction_id} AS junction
842
853
  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
843
854
  `;
@@ -855,8 +866,10 @@ export default class Project {
855
866
  ${target_selects.join(`
856
867
  UNION
857
868
  `)}
869
+ ORDER BY date_added
858
870
  )
859
871
  GROUP BY "${property_junction_column_name}"
872
+
860
873
  )`;
861
874
  cte_strings.push(cte);
862
875
  relation_selections.push(`[${prop.id}_cte].[user_${prop.name}]`);
package/dist/types.d.ts CHANGED
@@ -23,8 +23,10 @@ export type RelationTarget = {
23
23
  prop_id?: number | null;
24
24
  junction_id: number;
25
25
  };
26
- export type ItemRelationSide = RelationshipSide & {
26
+ export type ItemRelationSide = {
27
+ class_id: number;
27
28
  item_id: number;
29
+ prop_id?: number;
28
30
  };
29
31
  type RelationCreate = {
30
32
  type: 'create';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "goby-database",
3
- "version": "2.2.23",
3
+ "version": "2.2.25",
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": [