import * as R from 'ramda'; import { createSelector, ParametricSelector } from 'reselect'; import { TableSchema, FieldSchema, Application } from '../types'; import * as tableSelectors from './tableSelectors'; export const getTableList = (tables?: TableSchema[]) => tables || []; export const getTableById: ParametricSelector = createSelector( getTableList, (_: any, tableId: string) => tableId, (tables, tableId): any => tables && tables.find(({ id }) => id === tableId), ); export const getTableByName: ( schema: TableSchema[], tableName: string, appName?: string | null, ) => TableSchema = createSelector( getTableList, (_: any, tableName: string) => tableName, (_: any, __: any, applicationName: string) => applicationName, (tables, tableName, appName): any => tables && tables .filter(({ application }) => application && appName ? application.name.toLowerCase() === appName.toLowerCase() : !appName, ) .find(({ name }) => name.toLowerCase() === tableName.toLowerCase()), ); export const getTableApplication: ParametricSelector = createSelector( getTableById, table => table && table.application, ); export const getTableFields: ParametricSelector = createSelector( getTableById, table => (table ? table.fields : []), ); export const getTableApplicationName: ParametricSelector = createSelector( getTableApplication, table => (table ? table.name : ''), ); export const getNoSystemTables: ParametricSelector = createSelector( getTableList, tablesList => tablesList.filter(({ isSystem }) => !isSystem), ); export const getSystemTables: ParametricSelector = createSelector( getTableList, tablesList => tablesList.filter(tableSelectors.isSystemTable), ); export const hasNoSystemTables: ParametricSelector = createSelector( getNoSystemTables, tables => tables.length > 0, ); export const getUserTables: ParametricSelector = createSelector( getTableList, tablesList => tablesList.filter(tableSelectors.isUserTable), ); export const getApplicationTables: ParametricSelector = createSelector( getTableList, tablesList => tablesList.filter(({ application }) => !!application), ); export const getViewTables: ParametricSelector = createSelector( getTableList, tablesList => tablesList.filter(tableSelectors.isViewTable), ); export const getTablesByApplicationName = createSelector( getTableList, (_: any, appName: string) => appName, (tablesList, appName) => tablesList.filter(({ application }) => application && application.name === appName), ); export const getTablesByApplicationId = createSelector( getTableList, (_: any, appId: string) => appId, (tablesList, appId) => tablesList.filter(({ application }) => application && application.id === appId), ); export const hasUserTables: ParametricSelector = createSelector( getUserTables, tables => tables.length > 0, );