MCPcopy
hub / github.com/tinyplex/tinybase / saveTable

Function saveTable

src/persisters/common/database/commands.ts:147–358  ·  view source on GitHub ↗
(
    tableName: string,
    rowIdColumnName: string,
    content:
      | {
          [contentId: Id]:
            | {[contentSubId: Id]: CellOrUndefined | ValueOrUndefined}
            | undefined;
        }
      | undefined,
    deleteEmptyColumns: boolean,
    deleteEmptyTable: boolean,
    partial = false,
    condition: DpcTabularCondition = TRUE,
    contentSubIds?: Ids,
  )

Source from the content-addressed store, hash-verified

145 };
146
147 const saveTable = async (
148 tableName: string,
149 rowIdColumnName: string,
150 content:
151 | {
152 [contentId: Id]:
153 | {[contentSubId: Id]: CellOrUndefined | ValueOrUndefined}
154 | undefined;
155 }
156 | undefined,
157 deleteEmptyColumns: boolean,
158 deleteEmptyTable: boolean,
159 partial = false,
160 condition: DpcTabularCondition = TRUE,
161 contentSubIds?: Ids,
162 ): Promise<void> => {
163 const contentSubIdSet = isUndefined(contentSubIds)
164 ? undefined
165 : setNew(contentSubIds);
166 const includeContentSubId = (contentSubId: Id): boolean =>
167 isUndefined(contentSubIdSet) || collHas(contentSubIdSet, contentSubId);
168 const settingColumnNameSet = setNew<string>();
169 objMap(content ?? {}, (contentRow) =>
170 arrayMap(
171 arrayFilter(objIds(contentRow ?? {}), includeContentSubId),
172 (cellOrValueId) => setAdd(settingColumnNameSet, cellOrValueId),
173 ),
174 );
175 const settingColumnNames = collValues(settingColumnNameSet);
176
177 // Delete the table
178 if (
179 isUndefined(contentSubIdSet) &&
180 !partial &&
181 deleteEmptyTable &&
182 condition == TRUE &&
183 arrayIsEmpty(settingColumnNames) &&
184 collHas(schemaMap, tableName)
185 ) {
186 await databaseExecuteCommand('DROP ' + TABLE + escapeId(tableName));
187 mapSet(schemaMap, tableName);
188 return;
189 }
190
191 const currentColumnNames = mapGet(schemaMap, tableName);
192 const unaccountedColumnNames = setNew(
193 arrayFilter(
194 collValues(currentColumnNames),
195 (columnName) =>
196 columnName == rowIdColumnName || includeContentSubId(columnName),
197 ),
198 );
199 if (!arrayIsEmpty(settingColumnNames)) {
200 if (!collHas(schemaMap, tableName)) {
201 // Create the table
202 await databaseExecuteCommand(
203 CREATE_TABLE +
204 escapeId(tableName) +

Callers 3

saveTablesFunction · 0.85
saveValuesFunction · 0.85
setPersistedFunction · 0.85

Calls 15

isUndefinedFunction · 0.90
setNewFunction · 0.90
objMapFunction · 0.90
arrayMapFunction · 0.90
arrayFilterFunction · 0.90
setAddFunction · 0.90
collValuesFunction · 0.90
arrayIsEmptyFunction · 0.90
collHasFunction · 0.90
escapeIdFunction · 0.90
mapSetFunction · 0.90
mapGetFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…