Skip to content

Commit

Permalink
Merge pull request #3832 from mathesar-foundation/target_types_fe
Browse files Browse the repository at this point in the history
Hard-code type cast map on front end
  • Loading branch information
pavish authored Sep 16, 2024
2 parents 2483faf + 9cca017 commit 4d79e08
Show file tree
Hide file tree
Showing 12 changed files with 528 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
abstractTypesMap,
getAllowedAbstractTypesForDbTypeAndItsTargetTypes,
} from '@mathesar/stores/abstract-types';
import { typeCastMap } from '@mathesar/stores/abstract-types/typeCastMap';
import type { AbstractType } from '@mathesar/stores/abstract-types/types';
import { LabeledInput, Select } from '@mathesar-component-library';
Expand All @@ -26,15 +27,7 @@
$: allowedTypeConversions = getAllowedAbstractTypesForDbTypeAndItsTargetTypes(
column.type,
// TODO_BETA
//
// We need to find another way to get the valid target types for a column
// since the RPC API no longer returns this.
// column.valid_target_types ?? [],
[],
typeCastMap[column.type] ?? [],
abstractTypesMap,
).filter((item) => !['jsonlist', 'map'].includes(item.identifier));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { DbType } from '@mathesar/AppTypes';

import { abstractTypeCategory } from './constants';
import { DB_TYPES } from './dbTypes';
import Boolean from './type-configs/boolean';
import arrayFactory from './type-configs/comboTypes/arrayFactory';
import jsonArrayFactory from './type-configs/comboTypes/jsonArrayFactory';
Expand All @@ -12,7 +13,7 @@ import Email from './type-configs/email';
import Fallback from './type-configs/fallback';
import Money from './type-configs/money';
import Number from './type-configs/number';
import Text, { DB_TYPES as textDbTypes } from './type-configs/text';
import Text from './type-configs/text';
import Time from './type-configs/time';
import Uri from './type-configs/uri';
import type {
Expand Down Expand Up @@ -50,7 +51,7 @@ const comboAbstractTypeCategories: Partial<
[abstractTypeCategory.JsonObject]: jsonObjectFactory,
};

export const defaultDbType = textDbTypes.TEXT;
export const defaultDbType = DB_TYPES.TEXT;

export function constructAbstractTypeMapFromResponse(
abstractTypesResponse: AbstractTypeResponse[],
Expand Down
47 changes: 27 additions & 20 deletions mathesar_ui/src/stores/abstract-types/abstractTypesMap.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { constructAbstractTypeMapFromResponse } from './abstractTypeCategories';
import { DB_TYPES } from './dbTypes';
import type { AbstractTypeResponse } from './types';

/**
Expand All @@ -10,78 +11,84 @@ const typesResponse: AbstractTypeResponse[] = [
{
identifier: 'boolean',
name: 'Boolean',
db_types: ['boolean'],
db_types: [DB_TYPES.BOOLEAN],
},
{
identifier: 'date',
name: 'Date',
db_types: ['date'],
db_types: [DB_TYPES.DATE],
},
{
identifier: 'time',
name: 'Time',
db_types: ['time with time zone', 'time without time zone'],
db_types: [DB_TYPES.TIME_WITH_TZ, DB_TYPES.TIME_WITHOUT_TZ],
},
{
identifier: 'datetime',
name: 'Date & Time',
db_types: ['timestamp with time zone', 'timestamp without time zone'],
db_types: [DB_TYPES.TIMESTAMP_WITH_TZ, DB_TYPES.TIMESTAMP_WITHOUT_TZ],
},
{
identifier: 'duration',
name: 'Duration',
db_types: ['interval'],
db_types: [DB_TYPES.INTERVAL],
},
{
identifier: 'email',
name: 'Email',
db_types: ['mathesar_types.email'],
db_types: [DB_TYPES.MSAR__EMAIL],
},
{
identifier: 'money',
name: 'Money',
db_types: [
'mathesar_types.multicurrency_money',
'mathesar_types.mathesar_money',
'money',
DB_TYPES.MONEY,
DB_TYPES.MSAR__MATHESAR_MONEY,
DB_TYPES.MSAR__MULTICURRENCY_MONEY,
],
},
{
identifier: 'number',
name: 'Number',
db_types: [
'double precision',
'real',
'smallint',
'bigint',
'integer',
'numeric',
DB_TYPES.DOUBLE_PRECISION,
DB_TYPES.REAL,
DB_TYPES.SMALLINT,
DB_TYPES.BIGINT,
DB_TYPES.INTEGER,
DB_TYPES.NUMERIC,
],
},
{
identifier: 'text',
name: 'Text',
db_types: ['text', 'character', '"char"', 'name', 'character varying'],
db_types: [
DB_TYPES.CHAR,
DB_TYPES.CHARACTER_VARYING,
DB_TYPES.CHARACTER,
DB_TYPES.NAME,
DB_TYPES.TEXT,
],
},
{
identifier: 'uri',
name: 'URI',
db_types: ['mathesar_types.uri'],
db_types: [DB_TYPES.MSAR__URI],
},
{
identifier: 'jsonlist',
name: 'JSON List',
db_types: ['mathesar_types.mathesar_json_array'],
db_types: [DB_TYPES.MSAR__MATHESAR_JSON_ARRAY],
},
{
identifier: 'map',
name: 'Map',
db_types: ['mathesar_types.mathesar_json_object'],
db_types: [DB_TYPES.MSAR__MATHESAR_JSON_OBJECT],
},
{
identifier: 'array',
name: 'Array',
db_types: ['_array'],
db_types: [DB_TYPES.ARRAY],
},
];

Expand Down
47 changes: 47 additions & 0 deletions mathesar_ui/src/stores/abstract-types/dbTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
export const DB_TYPES = {
ARRAY: '_array',
BIGINT: 'bigint',
BIT: 'bit',
BOOLEAN: 'boolean',
BYTEA: 'bytea',
CHAR: '"char"',
CHARACTER_VARYING: 'character varying',
CHARACTER: 'character',
CIDR: 'cidr',
DATE: 'date',
DATERANGE: 'daterange',
DECIMAL: 'decimal',
DOUBLE_PRECISION: 'double precision',
INET: 'inet',
INT4RANGE: 'int4range',
INT8RANGE: 'int8range',
INTEGER: 'integer',
INTERVAL: 'interval',
JSON: 'json',
JSONB: 'jsonb',
LINE: 'line',
MACADDR: 'macaddr',
MONEY: 'money',
MSAR__EMAIL: 'mathesar_types.email',
MSAR__MATHESAR_JSON_ARRAY: 'mathesar_types.mathesar_json_array',
MSAR__MATHESAR_JSON_OBJECT: 'mathesar_types.mathesar_json_object',
MSAR__MATHESAR_MONEY: 'mathesar_types.mathesar_money',
MSAR__MULTICURRENCY_MONEY: 'mathesar_types.multicurrency_money',
MSAR__URI: 'mathesar_types.uri',
NAME: 'name',
NUMERIC: 'numeric',
NUMRANGE: 'numrange',
OID: 'oid',
REAL: 'real',
REGCLASS: 'regclass',
SMALLINT: 'smallint',
TEXT: 'text',
TIME_WITH_TZ: 'time with time zone',
TIME_WITHOUT_TZ: 'time without time zone',
TIMESTAMP_WITH_TZ: 'timestamp with time zone',
TIMESTAMP_WITHOUT_TZ: 'timestamp without time zone',
TSRANGE: 'tsrange',
TSTZRANGE: 'tstzrange',
TSVECTOR: 'tsvector',
UUID: 'uuid',
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import { iconUiTypeDateTime } from '@mathesar/icons';
import type { FormValues } from '@mathesar-component-library/types';

import { DB_TYPES } from '../dbTypes';
import type {
AbstractTypeConfigForm,
AbstractTypeConfiguration,
Expand All @@ -15,11 +16,6 @@ import type {

import { getDateFormatOptions, getTimeFormatOptions } from './utils';

const DB_TYPES = {
TIMESTAMP_WITH_TZ: 'timestamp with time zone',
TIMESTAMP_WITHOUT_TZ: 'timestamp without time zone',
};

const dbForm: AbstractTypeConfigForm = {
variables: {
supportTimeZones: {
Expand Down
3 changes: 2 additions & 1 deletion mathesar_ui/src/stores/abstract-types/type-configs/email.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { iconUiTypeEmail } from '@mathesar/icons';

import { DB_TYPES } from '../dbTypes';
import type { AbstractTypeConfiguration } from '../types';

const emailType: AbstractTypeConfiguration = {
getIcon: () => ({ ...iconUiTypeEmail, label: 'Email' }),
defaultDbType: 'mathesar_types.email',
defaultDbType: DB_TYPES.MSAR__EMAIL,
cellInfo: {
type: 'string',
},
Expand Down
8 changes: 2 additions & 6 deletions mathesar_ui/src/stores/abstract-types/type-configs/money.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,14 @@ import {
import { iconUiTypeMoney } from '@mathesar/icons';
import type { FormValues } from '@mathesar-component-library/types';

import { DB_TYPES } from '../dbTypes';
import type {
AbstractTypeConfigForm,
AbstractTypeConfiguration,
} from '../types';

import { getDecimalPlaces } from './number';

const DB_TYPES = {
MONEY: 'MONEY',
MATHESAR_TYPES__MATHESAR_MONEY: 'MATHESAR_TYPES.MATHESAR_MONEY',
};

const displayForm: AbstractTypeConfigForm = {
variables: {
currencySymbol: {
Expand Down Expand Up @@ -141,7 +137,7 @@ const moneyType: AbstractTypeConfiguration = {
cellInfo: {
type: 'money',
},
defaultDbType: DB_TYPES.MATHESAR_TYPES__MATHESAR_MONEY,
defaultDbType: DB_TYPES.MSAR__MATHESAR_MONEY,
getDisplayConfig: () => ({
form: displayForm,
determineDisplayOptions,
Expand Down
11 changes: 1 addition & 10 deletions mathesar_ui/src/stores/abstract-types/type-configs/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,13 @@ import type { DbType } from '@mathesar/AppTypes';
import { iconUiTypeNumber } from '@mathesar/icons';
import type { FormValues } from '@mathesar-component-library/types';

import { DB_TYPES } from '../dbTypes';
import type {
AbstractTypeConfigForm,
AbstractTypeConfiguration,
AbstractTypeDbConfig,
} from '../types';

const DB_TYPES = {
DECIMAL: 'decimal',
NUMERIC: 'numeric',
INTEGER: 'integer',
SMALLINT: 'smallint',
BIGINT: 'bigint',
REAL: 'real',
DOUBLE_PRECISION: 'double precision',
};

const dbForm: AbstractTypeConfigForm = {
variables: {
numberType: {
Expand Down
23 changes: 10 additions & 13 deletions mathesar_ui/src/stores/abstract-types/type-configs/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@ import type { DbType } from '@mathesar/AppTypes';
import { iconUiTypeText } from '@mathesar/icons';
import type { FormValues } from '@mathesar-component-library/types';

import { DB_TYPES } from '../dbTypes';
import type {
AbstractTypeConfigForm,
AbstractTypeConfiguration,
AbstractTypeDbConfig,
} from '../types';

export const DB_TYPES = {
VARCHAR: 'character varying',
CHAR: 'character',
TEXT: 'text',
};

const dbForm: AbstractTypeConfigForm = {
variables: {
restrictFieldSize: {
Expand Down Expand Up @@ -61,10 +56,12 @@ function determineDbType(dbFormValues: FormValues, columnType: DbType): DbType {
const integerValueOfLength =
typeof length === 'string' ? parseInt(length, 10) : length;
if (integerValueOfLength > 255) {
return DB_TYPES.VARCHAR;
return DB_TYPES.CHARACTER_VARYING;
}
}
return columnType === DB_TYPES.CHAR ? DB_TYPES.CHAR : DB_TYPES.VARCHAR;
return columnType === DB_TYPES.CHARACTER
? DB_TYPES.CHARACTER
: DB_TYPES.CHARACTER_VARYING;
}
return DB_TYPES.TEXT;
}
Expand All @@ -75,7 +72,7 @@ function determineDbTypeAndOptions(
): ReturnType<AbstractTypeDbConfig['determineDbTypeAndOptions']> {
const dbType = determineDbType(dbFormValues, columnType);
const typeOptions: Column['type_options'] = {};
if (dbType === DB_TYPES.CHAR || dbType === DB_TYPES.VARCHAR) {
if (dbType === DB_TYPES.CHARACTER || dbType === DB_TYPES.CHARACTER_VARYING) {
typeOptions.length = Number(dbFormValues.length);
}
return {
Expand All @@ -89,8 +86,8 @@ function constructDbFormValuesFromTypeOptions(
typeOptions: Column['type_options'],
): FormValues {
switch (columnType) {
case DB_TYPES.CHAR:
case DB_TYPES.VARCHAR:
case DB_TYPES.CHARACTER:
case DB_TYPES.CHARACTER_VARYING:
return {
length: (typeOptions?.length as number) ?? null,
restrictFieldSize: true,
Expand All @@ -104,14 +101,14 @@ function constructDbFormValuesFromTypeOptions(

const textType: AbstractTypeConfiguration = {
getIcon: () => ({ ...iconUiTypeText, label: 'Text' }),
defaultDbType: DB_TYPES.VARCHAR,
defaultDbType: DB_TYPES.CHARACTER_VARYING,
cellInfo: {
type: 'string',
config: {
multiLine: true,
},
conditionalConfig: {
[DB_TYPES.CHAR]: {
[DB_TYPES.CHARACTER]: {
multiLine: false,
},
},
Expand Down
6 changes: 1 addition & 5 deletions mathesar_ui/src/stores/abstract-types/type-configs/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
import { iconUiTypeTime } from '@mathesar/icons';
import type { FormValues } from '@mathesar-component-library/types';

import { DB_TYPES } from '../dbTypes';
import type {
AbstractTypeConfigForm,
AbstractTypeConfiguration,
Expand All @@ -14,11 +15,6 @@ import type {

import { getTimeFormatOptions } from './utils';

const DB_TYPES = {
TIME_WITH_TZ: 'time with time zone',
TIME_WITHOUT_TZ: 'time without time zone',
};

const dbForm: AbstractTypeConfigForm = {
variables: {
supportTimeZones: {
Expand Down
Loading

0 comments on commit 4d79e08

Please sign in to comment.