-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtables.sql
89 lines (83 loc) · 2.79 KB
/
tables.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
-- Supabase tables and functions
CREATE TABLE IF NOT EXISTS brands (
id bigint generated by default as identity,
name text not null,
description text null,
domain text null,
industry jsonb not null,
size text not null,
keywords text not null,
social_media jsonb null,
user_id text not null,
org_id text null default ''::text,
created_at timestamp with time zone not null default now(),
slug text null,
updated_at timestamp with time zone null default (now() at time zone 'utc'::text),
constraint brands_pkey primary key (id),
constraint brands_description_check check ((length(description) >= 30)),
constraint brands_domain_check check (
(
domain ~* 'https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,255}\.[a-z]{2,9}\y([-a-zA-Z0-9@:%_\+.~#?&//=]*)$'::text
)
),
constraint brands_name_check check ((length(name) <= 100)),
constraint brands_slug_check check ((length(slug) > 3))
);
CREATE TABLE IF NOT EXISTS knowledge_base (
id bigint generated by default as identity,
name text not null,
type text not null,
status text not null default 'loading'::text,
bucket_url text null,
brand_id bigint not null,
created_at timestamp with time zone not null default (now() at time zone 'utc'::text),
user_id text not null,
org_id text null,
updated_at timestamp with time zone null default (now() at time zone 'utc'::text),
constraint knowledgeBase_pkey primary key (id),
constraint knowledge_base_brand_id_fkey foreign key (brand_id) references brands (id) on delete cascade
);
CREATE TABLE IF NOT EXISTS brand_documents (
id bigserial,
content text null,
metadata jsonb null,
embedding extensions.vector null,
created_at timestamp with time zone null default (now() at time zone 'utc'::text),
updated_at timestamp with time zone null default (now() at time zone 'utc'::text),
constraint brand_documents_pkey primary key (id)
);
CREATE FUNCTION match_brand_documents(
query_embedding vector (1536),
filter jsonb default '{}'
) returns table (
id uuid,
content text,
metadata jsonb,
similarity float
) language plpgsql as $$
#variable_conflict use_column
begin
return query
select
id,
content,
metadata,
1 - (brand_documents.embedding <=> query_embedding) as similarity
from brand_documents
where metadata @> filter
order by brand_documents.embedding <=> query_embedding
limit match_count;
end;
$$;
create or replace function requesting_user_id()
returns text
language sql stable
as $$
select nullif(current_setting('request.jwt.claims', true)::json->>'sub', '')::text;
$$;
create or replace function requesting_org_id()
returns text
language sql stable
as $$
select nullif(current_setting('request.jwt.claims', true)::json->>'orgID', '')::text;
$$;