@freehour/supabase-core 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,57 @@
1
+ -- Function to perform a fuzzy search on a specified table and column.
2
+ -- It returns records where the similarity score exceeds a specified threshold (optional).
3
+ -- The results are ordered by similarity score in descending order.
4
+ CREATE OR REPLACE FUNCTION core.fuzzy_search(
5
+ relation text, -- The name of the table or view to search
6
+ column_name text,
7
+ search_term text,
8
+ schema_name text DEFAULT 'public',
9
+ min_similarity float DEFAULT 0,
10
+ limit_results int DEFAULT 64
11
+ )
12
+ RETURNS SETOF json
13
+ SET search_path = ''
14
+ AS $$
15
+ DECLARE
16
+ query text;
17
+ BEGIN
18
+ IF search_term = '' THEN
19
+ query := format(
20
+ $q$
21
+ SELECT row_to_json(t) AS result
22
+ FROM (
23
+ SELECT *
24
+ FROM %I.%I
25
+ LIMIT %s
26
+ ) t
27
+ $q$,
28
+ schema_name,
29
+ relation,
30
+ limit_results
31
+ );
32
+ ELSE
33
+ query := format(
34
+ $q$
35
+ SELECT row_to_json(t) AS result
36
+ FROM (
37
+ SELECT *
38
+ FROM (
39
+ SELECT *, extensions.similarity(lower(%I), lower(%L)) AS score
40
+ FROM %I.%I
41
+ ) s
42
+ WHERE score > %s
43
+ ORDER BY score DESC
44
+ LIMIT %s
45
+ ) t
46
+ $q$,
47
+ column_name,
48
+ search_term,
49
+ schema_name,
50
+ relation,
51
+ min_similarity,
52
+ limit_results
53
+ );
54
+ END IF;
55
+ RETURN QUERY EXECUTE query;
56
+ END;
57
+ $$ LANGUAGE plpgsql STABLE;