mirror of
https://github.com/docmost/docmost.git
synced 2026-08-28 09:17:06 +08:00
cleanup tsvector usage
This commit is contained in:
@@ -44,7 +44,7 @@ export function KanbanColumn({
|
|||||||
[viewFilter, groupByPropertyId, column.key],
|
[viewFilter, groupByPropertyId, column.key],
|
||||||
);
|
);
|
||||||
|
|
||||||
const rowsQuery = useBaseRowsQuery(pageId, filter, undefined, undefined);
|
const rowsQuery = useBaseRowsQuery(pageId, filter, undefined);
|
||||||
const createCard = useKanbanCreateCardMutation();
|
const createCard = useKanbanCreateCardMutation();
|
||||||
|
|
||||||
const rows = useMemo(() => {
|
const rows = useMemo(() => {
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ import {
|
|||||||
DeleteRowsInput,
|
DeleteRowsInput,
|
||||||
ReorderRowInput,
|
ReorderRowInput,
|
||||||
FilterNode,
|
FilterNode,
|
||||||
SearchSpec,
|
|
||||||
ViewSortConfig,
|
ViewSortConfig,
|
||||||
RowReferences,
|
RowReferences,
|
||||||
NO_VALUE_CHOICE_ID,
|
NO_VALUE_CHOICE_ID,
|
||||||
@@ -59,14 +58,12 @@ export function baseRowsQueryKey(
|
|||||||
pageId: string | undefined,
|
pageId: string | undefined,
|
||||||
filter: FilterNode | undefined,
|
filter: FilterNode | undefined,
|
||||||
sorts: ViewSortConfig[] | undefined,
|
sorts: ViewSortConfig[] | undefined,
|
||||||
search: SearchSpec | undefined,
|
|
||||||
) {
|
) {
|
||||||
return [
|
return [
|
||||||
"base-rows",
|
"base-rows",
|
||||||
pageId,
|
pageId,
|
||||||
normalizeFilter(filter),
|
normalizeFilter(filter),
|
||||||
sorts?.length ? sorts : undefined,
|
sorts?.length ? sorts : undefined,
|
||||||
search?.query ? search : undefined,
|
|
||||||
] as const;
|
] as const;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,21 +84,18 @@ export function useBaseRowsQuery(
|
|||||||
pageId: string | undefined,
|
pageId: string | undefined,
|
||||||
filter?: FilterNode,
|
filter?: FilterNode,
|
||||||
sorts?: ViewSortConfig[],
|
sorts?: ViewSortConfig[],
|
||||||
search?: SearchSpec,
|
|
||||||
) {
|
) {
|
||||||
const activeFilter = normalizeFilter(filter);
|
const activeFilter = normalizeFilter(filter);
|
||||||
const activeSorts = sorts?.length ? sorts : undefined;
|
const activeSorts = sorts?.length ? sorts : undefined;
|
||||||
const activeSearch = search?.query ? search : undefined;
|
|
||||||
|
|
||||||
const query = useInfiniteQuery({
|
const query = useInfiniteQuery({
|
||||||
queryKey: baseRowsQueryKey(pageId, filter, sorts, search),
|
queryKey: baseRowsQueryKey(pageId, filter, sorts),
|
||||||
queryFn: ({ pageParam }) =>
|
queryFn: ({ pageParam }) =>
|
||||||
listRows(pageId!, {
|
listRows(pageId!, {
|
||||||
cursor: pageParam,
|
cursor: pageParam,
|
||||||
limit: 100,
|
limit: 100,
|
||||||
filter: activeFilter,
|
filter: activeFilter,
|
||||||
sorts: activeSorts,
|
sorts: activeSorts,
|
||||||
search: activeSearch,
|
|
||||||
}),
|
}),
|
||||||
enabled: !!pageId,
|
enabled: !!pageId,
|
||||||
initialPageParam: undefined as string | undefined,
|
initialPageParam: undefined as string | undefined,
|
||||||
@@ -428,7 +422,6 @@ type KanbanMoveCardInput = {
|
|||||||
groupByPropertyId: string;
|
groupByPropertyId: string;
|
||||||
destChoiceValue: string | null;
|
destChoiceValue: string | null;
|
||||||
position: string;
|
position: string;
|
||||||
search?: SearchSpec;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
type KanbanMoveCardContext = {
|
type KanbanMoveCardContext = {
|
||||||
@@ -447,12 +440,12 @@ export function useKanbanMoveCardMutation() {
|
|||||||
requestId: newRequestId(),
|
requestId: newRequestId(),
|
||||||
}),
|
}),
|
||||||
onMutate: async (variables) => {
|
onMutate: async (variables) => {
|
||||||
const { pageId, rowId, sourceColumnFilter, destColumnFilter, columnChanged, groupByPropertyId, destChoiceValue, position, search } = variables;
|
const { pageId, rowId, sourceColumnFilter, destColumnFilter, columnChanged, groupByPropertyId, destChoiceValue, position } = variables;
|
||||||
|
|
||||||
await queryClient.cancelQueries({ queryKey: ["base-rows", pageId] });
|
await queryClient.cancelQueries({ queryKey: ["base-rows", pageId] });
|
||||||
|
|
||||||
const sourceKey = baseRowsQueryKey(pageId, sourceColumnFilter, undefined, search);
|
const sourceKey = baseRowsQueryKey(pageId, sourceColumnFilter, undefined);
|
||||||
const destKey = baseRowsQueryKey(pageId, destColumnFilter, undefined, search);
|
const destKey = baseRowsQueryKey(pageId, destColumnFilter, undefined);
|
||||||
|
|
||||||
const sourceSnapshot = queryClient.getQueryData<InfiniteData<IBaseRowsPage>>(sourceKey);
|
const sourceSnapshot = queryClient.getQueryData<InfiniteData<IBaseRowsPage>>(sourceKey);
|
||||||
const destSnapshot = queryClient.getQueryData<InfiniteData<IBaseRowsPage>>(destKey);
|
const destSnapshot = queryClient.getQueryData<InfiniteData<IBaseRowsPage>>(destKey);
|
||||||
@@ -531,7 +524,6 @@ type KanbanCreateCardInput = {
|
|||||||
groupByPropertyId: string;
|
groupByPropertyId: string;
|
||||||
columnKey: string;
|
columnKey: string;
|
||||||
position?: string;
|
position?: string;
|
||||||
search?: SearchSpec;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export function useKanbanCreateCardMutation() {
|
export function useKanbanCreateCardMutation() {
|
||||||
@@ -545,8 +537,8 @@ export function useKanbanCreateCardMutation() {
|
|||||||
requestId: newRequestId(),
|
requestId: newRequestId(),
|
||||||
}),
|
}),
|
||||||
onSuccess: (newRow, variables) => {
|
onSuccess: (newRow, variables) => {
|
||||||
const { pageId, destColumnFilter, search } = variables;
|
const { pageId, destColumnFilter } = variables;
|
||||||
const destKey = baseRowsQueryKey(pageId, destColumnFilter, undefined, search);
|
const destKey = baseRowsQueryKey(pageId, destColumnFilter, undefined);
|
||||||
queryClient.setQueryData<InfiniteData<IBaseRowsPage>>(destKey, (old) => {
|
queryClient.setQueryData<InfiniteData<IBaseRowsPage>>(destKey, (old) => {
|
||||||
if (!old) return old;
|
if (!old) return old;
|
||||||
const lastPageIndex = old.pages.length - 1;
|
const lastPageIndex = old.pages.length - 1;
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ import {
|
|||||||
DeleteViewInput,
|
DeleteViewInput,
|
||||||
UpdatePropertyResult,
|
UpdatePropertyResult,
|
||||||
FilterNode,
|
FilterNode,
|
||||||
SearchSpec,
|
|
||||||
ViewSortConfig,
|
ViewSortConfig,
|
||||||
RowReferences,
|
RowReferences,
|
||||||
} from "@/ee/base/types/base.types";
|
} from "@/ee/base/types/base.types";
|
||||||
@@ -146,7 +145,6 @@ export async function listRows(
|
|||||||
limit?: number;
|
limit?: number;
|
||||||
filter?: FilterNode;
|
filter?: FilterNode;
|
||||||
sorts?: ViewSortConfig[];
|
sorts?: ViewSortConfig[];
|
||||||
search?: SearchSpec;
|
|
||||||
},
|
},
|
||||||
): Promise<IBaseRowsPage> {
|
): Promise<IBaseRowsPage> {
|
||||||
const req = await api.post("/bases/rows", { pageId, ...params });
|
const req = await api.post("/bases/rows", { pageId, ...params });
|
||||||
|
|||||||
@@ -188,11 +188,6 @@ export type FilterGroup = {
|
|||||||
|
|
||||||
export type FilterNode = FilterCondition | FilterGroup;
|
export type FilterNode = FilterCondition | FilterGroup;
|
||||||
|
|
||||||
export type SearchSpec = {
|
|
||||||
query: string;
|
|
||||||
mode?: 'trgm' | 'fts';
|
|
||||||
};
|
|
||||||
|
|
||||||
export const NO_VALUE_CHOICE_ID = '__no_value';
|
export const NO_VALUE_CHOICE_ID = '__no_value';
|
||||||
|
|
||||||
export const KANBAN_CARD_DRAG_TYPE = "base-kanban-card";
|
export const KANBAN_CARD_DRAG_TYPE = "base-kanban-card";
|
||||||
|
|||||||
@@ -79,8 +79,6 @@ export async function up(db: Kysely<any>): Promise<void> {
|
|||||||
col.notNull().defaultTo(sql`'{}'::jsonb`),
|
col.notNull().defaultTo(sql`'{}'::jsonb`),
|
||||||
)
|
)
|
||||||
.addColumn('position', 'varchar', (col) => col.notNull())
|
.addColumn('position', 'varchar', (col) => col.notNull())
|
||||||
.addColumn('search_text', 'text')
|
|
||||||
.addColumn('search_tsv', sql`tsvector`)
|
|
||||||
.addColumn('creator_id', 'uuid', (col) =>
|
.addColumn('creator_id', 'uuid', (col) =>
|
||||||
col.references('users.id').onDelete('set null'),
|
col.references('users.id').onDelete('set null'),
|
||||||
)
|
)
|
||||||
@@ -117,12 +115,6 @@ export async function up(db: Kysely<any>): Promise<void> {
|
|||||||
WHERE deleted_at IS NULL
|
WHERE deleted_at IS NULL
|
||||||
`.execute(db);
|
`.execute(db);
|
||||||
|
|
||||||
await sql`
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_base_rows_search_tsv
|
|
||||||
ON base_rows USING gin (search_tsv)
|
|
||||||
WHERE deleted_at IS NULL
|
|
||||||
`.execute(db);
|
|
||||||
|
|
||||||
await db.schema
|
await db.schema
|
||||||
.createTable('base_views')
|
.createTable('base_views')
|
||||||
.ifNotExists()
|
.ifNotExists()
|
||||||
@@ -236,121 +228,6 @@ export async function up(db: Kysely<any>): Promise<void> {
|
|||||||
$$
|
$$
|
||||||
`.execute(db);
|
`.execute(db);
|
||||||
|
|
||||||
// Resolves select/multiSelect cells to their choice names. The property list
|
|
||||||
// per page is cached in a transaction-local setting so bulk writes look it up
|
|
||||||
// once instead of per row.
|
|
||||||
await sql`
|
|
||||||
CREATE OR REPLACE FUNCTION build_base_row_search_text(
|
|
||||||
_cells jsonb,
|
|
||||||
_page_id uuid
|
|
||||||
) RETURNS text
|
|
||||||
LANGUAGE plpgsql STABLE PARALLEL SAFE
|
|
||||||
AS $$
|
|
||||||
DECLARE
|
|
||||||
_parts text[] := ARRAY[]::text[];
|
|
||||||
_prop jsonb;
|
|
||||||
_value text;
|
|
||||||
_arr jsonb;
|
|
||||||
_elem jsonb;
|
|
||||||
_resolved text;
|
|
||||||
_cache_key text;
|
|
||||||
_cached text;
|
|
||||||
_props jsonb;
|
|
||||||
BEGIN
|
|
||||||
IF _cells IS NULL OR _cells = '{}'::jsonb OR _page_id IS NULL THEN
|
|
||||||
RETURN NULL;
|
|
||||||
END IF;
|
|
||||||
|
|
||||||
_cache_key := 'bases.prop_cache_' || replace(_page_id::text, '-', '_');
|
|
||||||
_cached := current_setting(_cache_key, true);
|
|
||||||
IF _cached IS NULL OR _cached = '' THEN
|
|
||||||
SELECT coalesce(
|
|
||||||
jsonb_agg(jsonb_build_object(
|
|
||||||
'id', id,
|
|
||||||
'type', type,
|
|
||||||
'type_options', type_options
|
|
||||||
)),
|
|
||||||
'[]'::jsonb
|
|
||||||
)
|
|
||||||
INTO _props
|
|
||||||
FROM base_properties
|
|
||||||
WHERE page_id = _page_id AND deleted_at IS NULL;
|
|
||||||
PERFORM set_config(_cache_key, _props::text, true);
|
|
||||||
ELSE
|
|
||||||
_props := _cached::jsonb;
|
|
||||||
END IF;
|
|
||||||
|
|
||||||
FOR _prop IN SELECT * FROM jsonb_array_elements(_props)
|
|
||||||
LOOP
|
|
||||||
IF (_prop->>'type') IN ('text', 'url', 'email') THEN
|
|
||||||
_value := _cells->>(_prop->>'id');
|
|
||||||
IF _value IS NOT NULL AND _value <> '' THEN
|
|
||||||
_parts := array_append(_parts, _value);
|
|
||||||
END IF;
|
|
||||||
ELSIF (_prop->>'type') IN ('select', 'status') THEN
|
|
||||||
_value := _cells->>(_prop->>'id');
|
|
||||||
IF _value IS NOT NULL AND _value <> '' THEN
|
|
||||||
SELECT c->>'name' INTO _resolved
|
|
||||||
FROM jsonb_array_elements(coalesce(_prop->'type_options'->'choices', '[]'::jsonb)) AS c
|
|
||||||
WHERE c->>'id' = _value
|
|
||||||
LIMIT 1;
|
|
||||||
IF _resolved IS NOT NULL AND _resolved <> '' THEN
|
|
||||||
_parts := array_append(_parts, _resolved);
|
|
||||||
END IF;
|
|
||||||
END IF;
|
|
||||||
ELSIF (_prop->>'type') = 'multiSelect' THEN
|
|
||||||
_arr := _cells->(_prop->>'id');
|
|
||||||
IF jsonb_typeof(_arr) = 'array' THEN
|
|
||||||
FOR _elem IN SELECT * FROM jsonb_array_elements(_arr)
|
|
||||||
LOOP
|
|
||||||
SELECT c->>'name' INTO _resolved
|
|
||||||
FROM jsonb_array_elements(coalesce(_prop->'type_options'->'choices', '[]'::jsonb)) AS c
|
|
||||||
WHERE c->>'id' = _elem#>>'{}'
|
|
||||||
LIMIT 1;
|
|
||||||
IF _resolved IS NOT NULL AND _resolved <> '' THEN
|
|
||||||
_parts := array_append(_parts, _resolved);
|
|
||||||
END IF;
|
|
||||||
END LOOP;
|
|
||||||
END IF;
|
|
||||||
END IF;
|
|
||||||
END LOOP;
|
|
||||||
|
|
||||||
IF array_length(_parts, 1) IS NULL THEN
|
|
||||||
RETURN NULL;
|
|
||||||
END IF;
|
|
||||||
|
|
||||||
RETURN f_unaccent(array_to_string(_parts, ' '));
|
|
||||||
END;
|
|
||||||
$$
|
|
||||||
`.execute(db);
|
|
||||||
|
|
||||||
await sql`
|
|
||||||
CREATE OR REPLACE FUNCTION base_rows_search_trigger() RETURNS trigger
|
|
||||||
LANGUAGE plpgsql AS $$
|
|
||||||
BEGIN
|
|
||||||
NEW.search_text := substring(build_base_row_search_text(NEW.cells, NEW.page_id), 1, 25000);
|
|
||||||
NEW.search_tsv := to_tsvector('english', coalesce(NEW.search_text, ''));
|
|
||||||
RETURN NEW;
|
|
||||||
END;
|
|
||||||
$$
|
|
||||||
`.execute(db);
|
|
||||||
|
|
||||||
await sql`
|
|
||||||
CREATE OR REPLACE TRIGGER base_rows_search_insert
|
|
||||||
BEFORE INSERT ON base_rows
|
|
||||||
FOR EACH ROW EXECUTE FUNCTION base_rows_search_trigger()
|
|
||||||
`.execute(db);
|
|
||||||
|
|
||||||
// Position-only reorders and metadata touches must not pay the
|
|
||||||
// search_text recompute; OLD is not referenceable on INSERT, hence
|
|
||||||
// the split triggers.
|
|
||||||
await sql`
|
|
||||||
CREATE OR REPLACE TRIGGER base_rows_search_update
|
|
||||||
BEFORE UPDATE ON base_rows
|
|
||||||
FOR EACH ROW
|
|
||||||
WHEN (OLD.cells IS DISTINCT FROM NEW.cells)
|
|
||||||
EXECUTE FUNCTION base_rows_search_trigger()
|
|
||||||
`.execute(db);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function down(db: Kysely<any>): Promise<void> {
|
export async function down(db: Kysely<any>): Promise<void> {
|
||||||
@@ -358,8 +235,6 @@ export async function down(db: Kysely<any>): Promise<void> {
|
|||||||
await db.schema.dropTable('base_rows').execute();
|
await db.schema.dropTable('base_rows').execute();
|
||||||
await db.schema.dropTable('base_properties').execute();
|
await db.schema.dropTable('base_properties').execute();
|
||||||
|
|
||||||
await sql`DROP FUNCTION base_rows_search_trigger()`.execute(db);
|
|
||||||
await sql`DROP FUNCTION build_base_row_search_text(jsonb, uuid)`.execute(db);
|
|
||||||
await sql`DROP FUNCTION jsonb_set_many(jsonb, jsonb)`.execute(db);
|
await sql`DROP FUNCTION jsonb_set_many(jsonb, jsonb)`.execute(db);
|
||||||
await sql`DROP FUNCTION base_cell_array(jsonb, uuid)`.execute(db);
|
await sql`DROP FUNCTION base_cell_array(jsonb, uuid)`.execute(db);
|
||||||
await sql`DROP FUNCTION base_cell_bool(jsonb, uuid)`.execute(db);
|
await sql`DROP FUNCTION base_cell_bool(jsonb, uuid)`.execute(db);
|
||||||
|
|||||||
-2
@@ -153,8 +153,6 @@ export interface BaseRows {
|
|||||||
lastUpdatedById: string | null;
|
lastUpdatedById: string | null;
|
||||||
pageId: string;
|
pageId: string;
|
||||||
position: string;
|
position: string;
|
||||||
searchText: string | null;
|
|
||||||
searchTsv: string | null;
|
|
||||||
updatedAt: Generated<Timestamp>;
|
updatedAt: Generated<Timestamp>;
|
||||||
workspaceId: string;
|
workspaceId: string;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -248,18 +248,9 @@ export type InsertableBaseProperty = Insertable<BaseProperties>;
|
|||||||
export type UpdatableBaseProperty = Updateable<Omit<BaseProperties, 'id'>>;
|
export type UpdatableBaseProperty = Updateable<Omit<BaseProperties, 'id'>>;
|
||||||
|
|
||||||
// Base Row
|
// Base Row
|
||||||
// `searchText` and `searchTsv` are internal fulltext-index columns maintained
|
export type BaseRow = Selectable<BaseRows>;
|
||||||
// by a trigger. They are omitted from the public types so they never leak into
|
export type InsertableBaseRow = Insertable<BaseRows>;
|
||||||
// HTTP responses or write payloads.
|
export type UpdatableBaseRow = Updateable<Omit<BaseRows, 'id'>>;
|
||||||
export type BaseRow = Omit<Selectable<BaseRows>, 'searchText' | 'searchTsv'>;
|
|
||||||
export type InsertableBaseRow = Omit<
|
|
||||||
Insertable<BaseRows>,
|
|
||||||
'searchText' | 'searchTsv'
|
|
||||||
>;
|
|
||||||
export type UpdatableBaseRow = Omit<
|
|
||||||
Updateable<Omit<BaseRows, 'id'>>,
|
|
||||||
'searchText' | 'searchTsv'
|
|
||||||
>;
|
|
||||||
|
|
||||||
// Base View
|
// Base View
|
||||||
export type BaseView = Selectable<BaseViews>;
|
export type BaseView = Selectable<BaseViews>;
|
||||||
|
|||||||
Reference in New Issue
Block a user