goby-database 2.1.19 → 2.1.20

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, ClassRow, ClassEdit, RelationEdit, PropertyEdit, MaxValues, PropertyType, RelationProperty, DataProperty, RelationEditValidSides } from './types.js';
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';
3
3
  export default class Project {
4
4
  db: DatabaseType;
5
5
  run: {
@@ -19,10 +19,6 @@ export default class Project {
19
19
  }>;
20
20
  };
21
21
  class_cache: ClassList;
22
- item_cache: {
23
- class_id: number;
24
- items: ClassRow[];
25
- }[];
26
22
  junction_cache: JunctionList;
27
23
  constructor(source: string);
28
24
  get_latest_table_row_id(table_name: string): number | null;
@@ -81,13 +77,19 @@ export default class Project {
81
77
  value: any;
82
78
  }[]): void;
83
79
  action_make_relations(relations: [input_1: ItemRelationSide, input_2: ItemRelationSide][]): void;
84
- retrieve_class_items({ class_id, class_name, class_data, slim }: {
80
+ retrieve_class_items({ class_id, class_name, class_data, pagination }: {
85
81
  class_id: number;
86
82
  class_name?: string;
87
83
  class_data?: ClassData;
88
- slim?: boolean;
89
- }): ClassRow[];
90
- retrieve_all_classes(): ClassData[];
84
+ pagination?: ItemPagination;
85
+ }): PaginatedItems;
86
+ retrieve_all_classes(include?: {
87
+ all_items?: ItemPagination;
88
+ items_by_class?: {
89
+ class_id: number;
90
+ pagination: ItemPagination;
91
+ }[];
92
+ }): ClassData[];
91
93
  parse_sql_prop(class_id: number, sql_prop: {
92
94
  id: number;
93
95
  type: PropertyType;
package/dist/index.js CHANGED
@@ -6,7 +6,6 @@ const real_data_types = ['number'];
6
6
  export default class Project {
7
7
  constructor(source) {
8
8
  this.class_cache = [];
9
- this.item_cache = [];
10
9
  this.junction_cache = [];
11
10
  this.db = new Database(source);
12
11
  //checks if goby has been initialized, initializes if not
@@ -52,7 +51,7 @@ export default class Project {
52
51
  create_window: this.db.prepare(`INSERT INTO system_windows (type,open, metadata) VALUES (@type, @open, @meta)`),
53
52
  get_windows: this.db.prepare(`SELECT id, type, open, metadata FROM system_windows`)
54
53
  };
55
- this.refresh_caches(['classlist', 'items', 'junctions']);
54
+ this.refresh_caches(['classlist', 'junctions']);
56
55
  // commenting this out until I figure out my transaction / one-step-undo functionality
57
56
  //if I understand transactions correctly, a new one will begin with every user action while committing the one before, meaning I'll need to have the first begin here
58
57
  // this.run.begin.run();
@@ -108,19 +107,6 @@ export default class Project {
108
107
  if (caches.includes('classlist')) {
109
108
  this.class_cache = this.retrieve_all_classes();
110
109
  }
111
- if (caches.includes('items')) {
112
- let refreshed_items = [];
113
- for (let class_data of this.class_cache) {
114
- // NOTE: add pagination to this
115
- let items = this.retrieve_class_items({ class_id: class_data.id });
116
- refreshed_items.push({
117
- class_id: class_data.id,
118
- items
119
- });
120
- class_data.items = items;
121
- }
122
- this.item_cache = refreshed_items;
123
- }
124
110
  if (caches.includes('junctions')) {
125
111
  this.junction_cache = this.get_junctions();
126
112
  }
@@ -710,7 +696,6 @@ export default class Project {
710
696
  //get the last item in class table order and use it to get the order for the new item
711
697
  const new_order = this.get_next_order(`[class_${class_name}]`);
712
698
  this.db.prepare(`INSERT INTO [class_${class_name}] (system_id, system_order) VALUES (${root_id},${new_order})`).run();
713
- this.refresh_caches(['items']);
714
699
  return root_id;
715
700
  }
716
701
  get_next_order(table_name) {
@@ -745,7 +730,6 @@ export default class Project {
745
730
  const set_statements = sql_column_inserts.map((p) => `${p.column_name} = ?`).join(',');
746
731
  const insert_statement = `UPDATE [class_${class_data.name}] SET ${set_statements} WHERE system_id=${item_id}`;
747
732
  this.db.prepare(insert_statement).run(params);
748
- this.refresh_caches(['items']);
749
733
  function validate(input, data_type, max_values) {
750
734
  const multiple = max_values == null || max_values > 1;
751
735
  const values = multiple ? input : [input];
@@ -809,23 +793,30 @@ export default class Project {
809
793
  throw Error('Something went wrong - junction table for relationship not found');
810
794
  }
811
795
  }
812
- this.refresh_caches(['items']);
813
796
  // NOTE: should this trigger a refresh to items?
814
797
  }
815
- retrieve_class_items({ class_id, class_name, class_data, slim = false }) {
798
+ // MARKER: modify item retrieval
799
+ retrieve_class_items({ class_id, class_name, class_data, pagination = {} }) {
816
800
  var _a, _b, _c, _d;
801
+ const pagination_defaults = {
802
+ page_size: null,
803
+ property_range: 'all'
804
+ };
805
+ pagination = Object.assign(Object.assign({}, pagination_defaults), pagination);
806
+ const slim = pagination.property_range == 'slim';
817
807
  if (class_name == undefined || class_data == undefined) {
818
808
  class_data = this.lookup_class(class_id);
819
809
  class_name = class_data.name;
820
810
  }
821
811
  ;
822
812
  const class_string = `[class_${class_name}]`;
823
- // //joined+added at beginning of the query, built from relations
813
+ // joined+added at beginning of the query, built from relations
824
814
  const cte_strings = [];
825
- // //joined+added near the end of the query, built from relations
815
+ // joined+added near the end of the query, built from relations
826
816
  const cte_joins = [];
827
- // //joined+added between SELECT and FROM, built from relations
817
+ // joined+added between SELECT and FROM, built from relations
828
818
  const relation_selections = [];
819
+ // NOTE: in the future, if a property_range is defined, first filter class_data.properties by those IDs
829
820
  let relation_properties = class_data.properties.filter(a => a.type == 'relation');
830
821
  if (!slim) {
831
822
  for (let prop of relation_properties) {
@@ -905,19 +896,31 @@ export default class Project {
905
896
  }
906
897
  }
907
898
  });
908
- return items;
899
+ return Object.assign(Object.assign({}, pagination), { loaded: items });
909
900
  }
910
- retrieve_all_classes() {
901
+ // include:{
902
+ // class_id:number;
903
+ // pagination:ItemPagination
904
+ // }[] = []
905
+ // MARKER: modify item retrieval
906
+ retrieve_all_classes(include = {}) {
911
907
  const classes_data = this.run.get_all_classes.all();
912
908
  return classes_data.map(({ id, name, metadata }) => {
913
- var _a;
914
- let existing_items = this.item_cache.find((itemlist) => itemlist.class_id == id);
909
+ var _a, _b, _c;
915
910
  let properties_sql = this.db.prepare(`SELECT * FROM class_${id}_properties`).all() || [];
916
911
  let properties = properties_sql.map((sql_prop) => this.parse_sql_prop(id, sql_prop));
912
+ const pagination = (_a = include.all_items) !== null && _a !== void 0 ? _a : (_c = (_b = include.items_by_class) === null || _b === void 0 ? void 0 : _b.find(((a) => a.class_id == id))) === null || _c === void 0 ? void 0 : _c.pagination;
913
+ const items = pagination ? this.retrieve_class_items({
914
+ class_id: id,
915
+ class_name: name,
916
+ pagination
917
+ }) : {
918
+ loaded: []
919
+ };
917
920
  return {
918
921
  id,
919
922
  name,
920
- items: (_a = existing_items === null || existing_items === void 0 ? void 0 : existing_items.items) !== null && _a !== void 0 ? _a : [],
923
+ items,
921
924
  properties,
922
925
  metadata: JSON.parse(metadata)
923
926
  };
@@ -974,9 +977,15 @@ export default class Project {
974
977
  ON system_root.id = workspace_${id}.thing_id
975
978
  WHERE workspace_${id}.type = 'item';
976
979
  `).all();
980
+ // MARKER: modify item retrieval
977
981
  // get any relevant classes
978
- const classes = this.class_cache.filter((cls) => blocks.some((block) => block.type == 'class' && block.thing_id == cls.id));
979
- // NOTE: could possibly add class items as well in the future
982
+ const items_by_class = blocks.filter((b) => b.type == 'class').map((b) => ({
983
+ class_id: b.thing_id,
984
+ pagination: {
985
+ page_size: null
986
+ }
987
+ }));
988
+ const classes = this.retrieve_all_classes({ items_by_class });
980
989
  return {
981
990
  blocks,
982
991
  items,
package/dist/types.d.ts CHANGED
@@ -135,10 +135,18 @@ export type ClassData = {
135
135
  id: number;
136
136
  name: string;
137
137
  metadata: ClassMetadata;
138
- items: ClassRow[];
138
+ items: PaginatedItems;
139
139
  properties: Property[];
140
140
  };
141
141
  export type ClassList = ClassData[];
142
+ export type ItemPagination = {
143
+ page_size?: number | null;
144
+ page_range?: [start: number, end?: number];
145
+ property_range?: number[] | 'slim' | 'all';
146
+ };
147
+ export type PaginatedItems = ItemPagination & {
148
+ loaded: ClassRow[];
149
+ };
142
150
  export type JunctionSides = [RelationshipSide, RelationshipSide];
143
151
  export type JunctionTable = {
144
152
  id: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "goby-database",
3
- "version": "2.1.19",
3
+ "version": "2.1.20",
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": [