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