goby-database 2.1.19 → 2.1.21

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 }) {
816
- var _a, _b, _c, _d;
798
+ // MARKER: modify item retrieval
799
+ retrieve_class_items({ class_id, class_name, class_data, pagination = {} }) {
800
+ var _a, _b, _c, _d, _e;
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,29 @@ export default class Project {
905
896
  }
906
897
  }
907
898
  });
908
- return items;
899
+ let total = (_e = this.db.prepare(`SELECT COUNT(1) AS total FROM ${class_string}`).all()[0]) === null || _e === void 0 ? void 0 : _e.total;
900
+ return Object.assign(Object.assign({}, pagination), { loaded: items, total });
909
901
  }
910
- retrieve_all_classes() {
902
+ // MARKER: modify item retrieval
903
+ retrieve_all_classes(include = {}) {
911
904
  const classes_data = this.run.get_all_classes.all();
912
905
  return classes_data.map(({ id, name, metadata }) => {
913
- var _a;
914
- let existing_items = this.item_cache.find((itemlist) => itemlist.class_id == id);
906
+ var _a, _b, _c;
915
907
  let properties_sql = this.db.prepare(`SELECT * FROM class_${id}_properties`).all() || [];
916
908
  let properties = properties_sql.map((sql_prop) => this.parse_sql_prop(id, sql_prop));
909
+ 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;
910
+ const items = pagination ? this.retrieve_class_items({
911
+ class_id: id,
912
+ class_name: name,
913
+ pagination
914
+ }) : {
915
+ loaded: [],
916
+ total: 0
917
+ };
917
918
  return {
918
919
  id,
919
920
  name,
920
- items: (_a = existing_items === null || existing_items === void 0 ? void 0 : existing_items.items) !== null && _a !== void 0 ? _a : [],
921
+ items,
921
922
  properties,
922
923
  metadata: JSON.parse(metadata)
923
924
  };
@@ -974,9 +975,15 @@ export default class Project {
974
975
  ON system_root.id = workspace_${id}.thing_id
975
976
  WHERE workspace_${id}.type = 'item';
976
977
  `).all();
978
+ // MARKER: modify item retrieval
977
979
  // 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
980
+ const items_by_class = blocks.filter((b) => b.type == 'class').map((b) => ({
981
+ class_id: b.thing_id,
982
+ pagination: {
983
+ page_size: null
984
+ }
985
+ }));
986
+ const classes = this.retrieve_all_classes({ items_by_class });
980
987
  return {
981
988
  blocks,
982
989
  items,
package/dist/types.d.ts CHANGED
@@ -135,10 +135,20 @@ 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
+ /** a count of all the items in the class, not just those loaded */
150
+ total: number;
151
+ };
142
152
  export type JunctionSides = [RelationshipSide, RelationshipSide];
143
153
  export type JunctionTable = {
144
154
  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.21",
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": [