MCPcopy Create free account
hub / github.com/clientdb/clientdb / createEntityStore

Function createEntityStore

core/store.ts:73–319  ·  view source on GitHub ↗
(
  definition: EntityDefinition<Data, View>,
  db: ClientDb,
  cleanup: CleanupObject
)

Source from the content-addressed store, hash-verified

71 * This allows distinguishing between 'system' and 'user' events where user is only making changes with client.
72 */
73export function createEntityStore<Data, View>(
74 definition: EntityDefinition<Data, View>,
75 db: ClientDb,
76 cleanup: CleanupObject
77): EntityStore<Data, View> {
78 type StoreEntity = Entity<Data, View>;
79
80 const { config } = definition;
81 /**
82 * Keep 2 'versions' of items list. Array and id<>item map for quick 'by id' access.
83 */
84 const items = observable.array<StoreEntity>([]);
85 const itemsMap = observable.object<Record<string, Entity<Data, View>>>({});
86
87 const getIsEntityAccessable =
88 config.rootFilter &&
89 cachedComputed(function getIsEntityAccessable(entity: StoreEntity) {
90 return config.rootFilter!(entity, db);
91 });
92
93 const getRootSource = cachedComputed(
94 function getSourceForQueryInput(): Entity<Data, View>[] {
95 let output = items as StoreEntity[];
96
97 if (config.rootFilter) {
98 output = output.filter((entity) => getIsEntityAccessable!(entity));
99 }
100
101 return output;
102 },
103 { equals: areArraysShallowEqual }
104 );
105
106 const sortItems = cachedComputed((items: StoreEntity[]) => {
107 if (!config.defaultSort) {
108 return items;
109 }
110
111 return sortBy(items, config.defaultSort);
112 });
113
114 const allItems = cachedComputedWithoutArgs(() => {
115 return sortItems(getRootSource());
116 });
117
118 const propIndexMap = new Map<
119 keyof Data | keyof View,
120 QueryIndex<Data, View, IndexableKey<Data & View>>
121 >();
122
123 function getEntityId(entity: Entity<Data, View>) {
124 const id = `${entity[config.idField!]}`;
125
126 return id;
127 }
128
129 const createOrReuseQuery = deepMemoize(
130 function createOrReuseQuery(

Callers 1

createEntityClientFunction · 0.90

Calls 5

cachedComputedFunction · 0.90
deepMemoizeFunction · 0.90
resolveSortInputFunction · 0.90
createEntityQueryFunction · 0.90

Tested by

no test coverage detected