MCPcopy
hub / github.com/chartdb/chartdb / cloneTable

Function cloneTable

src/lib/clone.ts:64–131  ·  view source on GitHub ↗
(
    table: DBTable,
    options: {
        generateId: () => string;
        idsMap: Map<string, string>;
    } = {
        generateId: defaultGenerateId,
        idsMap: new Map<string, string>(),
    }
)

Source from the content-addressed store, hash-verified

62};
63
64export const cloneTable = (
65 table: DBTable,
66 options: {
67 generateId: () => string;
68 idsMap: Map<string, string>;
69 } = {
70 generateId: defaultGenerateId,
71 idsMap: new Map<string, string>(),
72 }
73): DBTable => {
74 const { generateId } = options;
75
76 const idsMap = new Map([
77 ...generateIdsMapFromTable(table, generateId),
78 ...options.idsMap,
79 ]);
80
81 const getNewId = (id: string): string | null => {
82 const newId = idsMap.get(id);
83 if (!newId) {
84 return null;
85 }
86 return newId;
87 };
88
89 const tableId = getNewId(table.id);
90 if (!tableId) {
91 throw new Error('Table id not found');
92 }
93
94 const newTable: DBTable = { ...table, id: tableId };
95 newTable.fields = table.fields
96 .map((field): DBField | null => {
97 const id = getNewId(field.id);
98
99 if (!id) {
100 return null;
101 }
102
103 return {
104 ...field,
105 id,
106 };
107 })
108 .filter((field): field is DBField => field !== null);
109 newTable.indexes = table.indexes
110 .map((index): DBIndex | null => {
111 const id = getNewId(index.id);
112
113 if (!id) {
114 return null;
115 }
116
117 return {
118 ...index,
119 fieldIds: index.fieldIds
120 .map((id) => getNewId(id))
121 .filter((fieldId): fieldId is string => fieldId !== null),

Callers 3

TableListItemHeaderFunction · 0.90
TableNodeContextMenuFunction · 0.90
cloneDiagramFunction · 0.85

Calls 2

generateIdsMapFromTableFunction · 0.85
getNewIdFunction · 0.85

Tested by

no test coverage detected