vitess.io/vitess@v0.16.2/web/vtadmin/src/util/tableDefinitions.ts (about) 1 /** 2 * Copyright 2021 The Vitess Authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 import { vtadmin as pb } from '../proto/vtadmin'; 17 18 export interface TableDefinition { 19 cluster?: pb.Schema['cluster']; 20 keyspace?: pb.Schema['keyspace']; 21 // The [0] index is a typescript quirk to infer the type of 22 // an entry in an array, and therefore the type of ALL entries 23 // in the array (not just the first one). 24 tableDefinition?: pb.Schema['table_definitions'][0]; 25 tableSize?: pb.Schema['table_sizes'][0]; 26 } 27 28 /** 29 * getTableDefinitions is a helper function for transforming an array of Schemas 30 * into a flat array of table definitions. 31 */ 32 export const getTableDefinitions = (schemas: pb.Schema[] | null | undefined): TableDefinition[] => { 33 return (schemas || []).reduce((acc: TableDefinition[], schema: pb.Schema) => { 34 // Index table definitions in this Schema by name, since we necessarily loop twice 35 const sts: { [tableName: string]: TableDefinition } = {}; 36 37 (schema.table_definitions || []).forEach((td) => { 38 if (!td.name) return; 39 sts[td.name] = { 40 cluster: schema.cluster, 41 keyspace: schema.keyspace, 42 tableDefinition: td, 43 }; 44 }); 45 46 Object.entries(schema.table_sizes || {}).forEach(([tableName, tableSize]) => { 47 // Include tables that have size/rows defined but do not have a table definition. 48 if (!(tableName in sts)) { 49 sts[tableName] = { 50 cluster: schema.cluster, 51 keyspace: schema.keyspace, 52 tableDefinition: { name: tableName }, 53 }; 54 } 55 56 sts[tableName].tableSize = tableSize; 57 }); 58 59 return acc.concat(Object.values(sts)); 60 }, []); 61 };