Skip to content

Commit

Permalink
Add support for full-text search
Browse files Browse the repository at this point in the history
  • Loading branch information
fzaninotto committed Nov 7, 2024
1 parent 0d8775a commit f507185
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ export const CreateGuesserView = (
}
const inferredInputs = Object.keys(resourceDefinition.properties)
.filter((source: string) => source !== 'id')
.filter(
source =>
resourceDefinition.properties![source].format !== 'tsvector'
)
.map((source: string) =>
inferElementFromType({
name: source,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ export const EditGuesserView = (
}
const inferredInputs = Object.keys(resourceDefinition.properties)
.filter((source: string) => source !== 'id')
.filter(
source =>
resourceDefinition.properties![source].format !== 'tsvector'
)
.map((source: string) =>
inferElementFromType({
name: source,
Expand Down
62 changes: 50 additions & 12 deletions packages/ra-supabase-ui-materialui/src/guessers/ListGuesser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
InferredElement,
listFieldTypes,
editFieldTypes,
SearchInput,
} from 'react-admin';
import type { ListProps, ListViewProps } from 'react-admin';
import { capitalize, singularize } from 'inflection';
Expand Down Expand Up @@ -79,8 +80,12 @@ export const ListGuesserView = (
if (!resourceDefinition || !resourceDefinition.properties) {
return;
}
const inferredFields = Object.keys(resourceDefinition.properties).map(
(source: string) =>
const inferredFields = Object.keys(resourceDefinition.properties)
.filter(
source =>
resourceDefinition.properties![source].format !== 'tsvector'
)
.map((source: string) =>
inferElementFromType({
name: source,
types: listFieldTypes,
Expand All @@ -94,7 +99,7 @@ export const ListGuesserView = (
? resourceDefinition.properties![source].type
: 'string') as string,
})
);
);
const inferredTable = new InferredElement(
listFieldTypes.table,
null,
Expand All @@ -108,16 +113,49 @@ export const ListGuesserView = (
obj['$ref'].includes('rowFilter')
)
.map(obj => obj['$ref'].split('.').pop()) ?? [];
const inferredInputsForFilters = rowFilters.map(source => {
const field = resourceDefinition.properties![source];
return inferElementFromType({
name: source,
types: editFieldTypes,
description: field.description,
format: field.format,
type: field.type as string,
const inferredInputsForFilters = rowFilters
.filter(
source =>
resourceDefinition.properties![source].format !== 'tsvector'
)
.map(source => {
const field = resourceDefinition.properties![source];
return inferElementFromType({
name: source,
types: editFieldTypes,
description: field.description,
format: field.format,
type: field.type as string,
});
});
});
if (
rowFilters.some(
source =>
resourceDefinition.properties![source].format === 'tsvector'
)
) {
const fullTextSearchSource = rowFilters.find(
source =>
resourceDefinition.properties![source].format === 'tsvector'
);
const field = resourceDefinition.properties![fullTextSearchSource!];
inferredInputsForFilters.push(
inferElementFromType({
name: `${fullTextSearchSource!}@fts`,
types: {
string: {
component: SearchInput,
representation: props =>
`<SearchInput alwaysOn source="${props.source}" />`,
},
},
description: field.description,
format: 'tsvector',
props: { alwaysOn: true },
type: field.type as string,
})
);
}
if (inferredInputsForFilters.length > 0) {
const filterElements = inferredInputsForFilters
.map(inferredInput => inferredInput.getElement())
Expand Down
10 changes: 7 additions & 3 deletions packages/ra-supabase-ui-materialui/src/guessers/ShowGuesser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ export const ShowGuesserView = (
if (!resourceDefinition || !resourceDefinition.properties) {
return;
}
const inferredFields = Object.keys(resourceDefinition.properties).map(
(source: string) =>
const inferredFields = Object.keys(resourceDefinition.properties)
.filter(
source =>
resourceDefinition.properties![source].format !== 'tsvector'
)
.map((source: string) =>
inferElementFromType({
name: source,
types: showFieldTypes,
Expand All @@ -71,7 +75,7 @@ export const ShowGuesserView = (
? resourceDefinition.properties![source].type
: 'string') as string,
})
);
);
const inferredLayout = new InferredElement(
showFieldTypes.show,
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ export const inferElementFromType = ({
type,
requiredFields,
types,
props,
}: {
name: string;
types: InferredTypeMap;
description?: string;
format?: string;
type?: string;
requiredFields?: string[];
props?: any;
}) => {
if (name === 'id' && hasType('id', types)) {
return new InferredElement(types.id, { source: 'id' });
Expand All @@ -31,6 +33,7 @@ export const inferElementFromType = ({
return new InferredElement(types.reference, {
source: name,
reference,
...props,
});
}
if (
Expand All @@ -41,6 +44,7 @@ export const inferElementFromType = ({
return new InferredElement(types.referenceArray, {
source: name,
reference,
...props,
});
}
if (type === 'array') {
Expand All @@ -55,12 +59,14 @@ export const inferElementFromType = ({
return new InferredElement(types.email, {
source: name,
validate,
...props,
});
}
if (['url', 'website'].includes(name) && hasType('url', types)) {
return new InferredElement(types.url, {
source: name,
validate,
...props,
});
}
if (
Expand All @@ -74,23 +80,27 @@ export const inferElementFromType = ({
return new InferredElement(types.date, {
source: name,
validate,
...props,
});
}
}
if (type === 'integer' && hasType('number', types)) {
return new InferredElement(types.number, {
source: name,
validate,
...props,
});
}
if (type && hasType(type, types)) {
return new InferredElement(types[type], {
source: name,
validate,
...props,
});
}
return new InferredElement(types.string, {
source: name,
validate,
...props,
});
};

0 comments on commit f507185

Please sign in to comment.