···1+create table "public"."entities" (
2+ "id" uuid not null default gen_random_uuid(),
3+ "created_at" timestamp with time zone not null default now()
4+);
5+6+7+alter table "public"."entities" enable row level security;
8+9+create table "public"."facts" (
10+ "id" uuid not null default gen_random_uuid(),
11+ "entity" uuid not null,
12+ "attribute" text not null,
13+ "data" jsonb not null,
14+ "created_at" timestamp without time zone not null default now(),
15+ "updated_at" timestamp without time zone,
16+ "version" bigint not null default '0'::bigint
17+);
18+19+20+alter table "public"."facts" enable row level security;
21+22+create table "public"."replicache_clients" (
23+ "client_id" text not null,
24+ "client_group" text not null,
25+ "last_mutation" bigint not null
26+);
27+28+29+alter table "public"."replicache_clients" enable row level security;
30+31+CREATE UNIQUE INDEX client_pkey ON public.replicache_clients USING btree (client_id);
32+33+CREATE UNIQUE INDEX entities_pkey ON public.entities USING btree (id);
34+35+CREATE INDEX facts_expr_idx ON public.facts USING btree (((data ->> 'value'::text))) WHERE ((data ->> 'type'::text) = 'reference'::text);
36+37+CREATE UNIQUE INDEX facts_pkey ON public.facts USING btree (id);
38+39+alter table "public"."entities" add constraint "entities_pkey" PRIMARY KEY using index "entities_pkey";
40+41+alter table "public"."facts" add constraint "facts_pkey" PRIMARY KEY using index "facts_pkey";
42+43+alter table "public"."replicache_clients" add constraint "client_pkey" PRIMARY KEY using index "client_pkey";
44+45+alter table "public"."facts" add constraint "facts_entity_fkey" FOREIGN KEY (entity) REFERENCES entities(id) ON UPDATE RESTRICT ON DELETE CASCADE not valid;
46+47+alter table "public"."facts" validate constraint "facts_entity_fkey";
48+49+set check_function_bodies = off;
50+51+CREATE OR REPLACE FUNCTION public.get_facts(root uuid)
52+ RETURNS SETOF facts
53+ LANGUAGE sql
54+AS $function$
55+ WITH RECURSIVE all_facts as (
56+ select
57+ *
58+ from
59+ facts
60+ where
61+ entity = root
62+ union
63+ select
64+ f.*
65+ from
66+ facts f
67+ inner join all_facts f1 on (
68+ uuid(f1.data ->> 'value') = f.entity
69+ ) where f1.data ->> 'type' = 'reference'
70+ )
71+select
72+ *
73+from
74+ all_facts;
75+ $function$
76+;
77+78+grant delete on table "public"."entities" to "anon";
79+80+grant insert on table "public"."entities" to "anon";
81+82+grant references on table "public"."entities" to "anon";
83+84+grant select on table "public"."entities" to "anon";
85+86+grant trigger on table "public"."entities" to "anon";
87+88+grant truncate on table "public"."entities" to "anon";
89+90+grant update on table "public"."entities" to "anon";
91+92+grant delete on table "public"."entities" to "authenticated";
93+94+grant insert on table "public"."entities" to "authenticated";
95+96+grant references on table "public"."entities" to "authenticated";
97+98+grant select on table "public"."entities" to "authenticated";
99+100+grant trigger on table "public"."entities" to "authenticated";
101+102+grant truncate on table "public"."entities" to "authenticated";
103+104+grant update on table "public"."entities" to "authenticated";
105+106+grant delete on table "public"."entities" to "service_role";
107+108+grant insert on table "public"."entities" to "service_role";
109+110+grant references on table "public"."entities" to "service_role";
111+112+grant select on table "public"."entities" to "service_role";
113+114+grant trigger on table "public"."entities" to "service_role";
115+116+grant truncate on table "public"."entities" to "service_role";
117+118+grant update on table "public"."entities" to "service_role";
119+120+grant delete on table "public"."facts" to "anon";
121+122+grant insert on table "public"."facts" to "anon";
123+124+grant references on table "public"."facts" to "anon";
125+126+grant select on table "public"."facts" to "anon";
127+128+grant trigger on table "public"."facts" to "anon";
129+130+grant truncate on table "public"."facts" to "anon";
131+132+grant update on table "public"."facts" to "anon";
133+134+grant delete on table "public"."facts" to "authenticated";
135+136+grant insert on table "public"."facts" to "authenticated";
137+138+grant references on table "public"."facts" to "authenticated";
139+140+grant select on table "public"."facts" to "authenticated";
141+142+grant trigger on table "public"."facts" to "authenticated";
143+144+grant truncate on table "public"."facts" to "authenticated";
145+146+grant update on table "public"."facts" to "authenticated";
147+148+grant delete on table "public"."facts" to "service_role";
149+150+grant insert on table "public"."facts" to "service_role";
151+152+grant references on table "public"."facts" to "service_role";
153+154+grant select on table "public"."facts" to "service_role";
155+156+grant trigger on table "public"."facts" to "service_role";
157+158+grant truncate on table "public"."facts" to "service_role";
159+160+grant update on table "public"."facts" to "service_role";
161+162+grant delete on table "public"."replicache_clients" to "anon";
163+164+grant insert on table "public"."replicache_clients" to "anon";
165+166+grant references on table "public"."replicache_clients" to "anon";
167+168+grant select on table "public"."replicache_clients" to "anon";
169+170+grant trigger on table "public"."replicache_clients" to "anon";
171+172+grant truncate on table "public"."replicache_clients" to "anon";
173+174+grant update on table "public"."replicache_clients" to "anon";
175+176+grant delete on table "public"."replicache_clients" to "authenticated";
177+178+grant insert on table "public"."replicache_clients" to "authenticated";
179+180+grant references on table "public"."replicache_clients" to "authenticated";
181+182+grant select on table "public"."replicache_clients" to "authenticated";
183+184+grant trigger on table "public"."replicache_clients" to "authenticated";
185+186+grant truncate on table "public"."replicache_clients" to "authenticated";
187+188+grant update on table "public"."replicache_clients" to "authenticated";
189+190+grant delete on table "public"."replicache_clients" to "service_role";
191+192+grant insert on table "public"."replicache_clients" to "service_role";
193+194+grant references on table "public"."replicache_clients" to "service_role";
195+196+grant select on table "public"."replicache_clients" to "service_role";
197+198+grant trigger on table "public"."replicache_clients" to "service_role";
199+200+grant truncate on table "public"."replicache_clients" to "service_role";
201+202+grant update on table "public"."replicache_clients" to "service_role";