forked from supabase/wrappers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.sql
More file actions
305 lines (290 loc) · 8.6 KB
/
init.sql
File metadata and controls
305 lines (290 loc) · 8.6 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
-- OpenAPI FDW example: GitHub API
-- Requires a GitHub personal access token. Replace 'placeholder' in the server definitions
-- below with your token.
-- See: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens
-- Note: fdw_package_url uses file:// for local Docker testing. In production, use the
-- GitHub release URL: https://github.com/supabase/wrappers/releases/download/wasm_openapi_fdw_v0.2.0/openapi_fdw.wasm
-- Create supabase_admin role if it doesn't exist (required by wrappers extension)
DO $$
BEGIN
IF NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = 'supabase_admin') THEN
CREATE ROLE supabase_admin WITH SUPERUSER CREATEDB CREATEROLE LOGIN PASSWORD 'postgres';
END IF;
END
$$;
create schema if not exists extensions;
create extension if not exists wrappers with schema extensions;
set search_path to public, extensions;
create foreign data wrapper wasm_wrapper
handler wasm_fdw_handler
validator wasm_fdw_validator;
-- ============================================================
-- Server 1: github — Main GitHub API server
-- Bearer token auth via Authorization header (default behavior)
-- Custom headers for GitHub API versioning
-- ============================================================
create server github
foreign data wrapper wasm_wrapper
options (
fdw_package_url 'file:///openapi_fdw.wasm',
fdw_package_name 'supabase:openapi-fdw',
fdw_package_version '0.2.0',
base_url 'https://api.github.com',
api_key 'placeholder',
user_agent 'openapi-fdw-example/0.2.0',
accept 'application/vnd.github+json',
headers '{"X-GitHub-Api-Version": "2022-11-28"}',
page_size '30',
page_size_param 'per_page'
);
-- ============================================================
-- Server 2: github_debug — Same API with debug output
-- Emits HTTP request details as INFO messages
-- ============================================================
create server github_debug
foreign data wrapper wasm_wrapper
options (
fdw_package_url 'file:///openapi_fdw.wasm',
fdw_package_name 'supabase:openapi-fdw',
fdw_package_version '0.2.0',
base_url 'https://api.github.com',
api_key 'placeholder',
user_agent 'openapi-fdw-example/0.2.0',
accept 'application/vnd.github+json',
headers '{"X-GitHub-Api-Version": "2022-11-28"}',
page_size '30',
page_size_param 'per_page',
debug 'true'
);
-- ============================================================
-- Server 3: github_import — With spec_url for IMPORT FOREIGN SCHEMA
-- Note: The GitHub spec is large (~15 MB). The FDW's default
-- max_response_bytes (50 MiB) can handle it, but the initial
-- IMPORT may take a few seconds.
-- ============================================================
create server github_import
foreign data wrapper wasm_wrapper
options (
fdw_package_url 'file:///openapi_fdw.wasm',
fdw_package_name 'supabase:openapi-fdw',
fdw_package_version '0.2.0',
base_url 'https://api.github.com',
api_key 'placeholder',
user_agent 'openapi-fdw-example/0.2.0',
accept 'application/vnd.github+json',
headers '{"X-GitHub-Api-Version": "2022-11-28"}',
page_size '30',
page_size_param 'per_page',
spec_url 'https://raw.githubusercontent.com/github/rest-api-description/main/descriptions/api.github.com/api.github.com.json'
);
-- ============================================================
-- Table 1: my_profile
-- Authenticated user's profile — GET /user
-- Features: single object response, bearer token auth, custom headers
-- ============================================================
create foreign table my_profile (
login text,
id bigint,
name text,
email text,
bio text,
public_repos integer,
public_gists integer,
followers integer,
following integer,
created_at timestamptz,
avatar_url text,
company text,
location text,
blog text,
attrs jsonb
)
server github
options (
endpoint '/user'
);
-- ============================================================
-- Table 2: my_repos
-- Authenticated user's repositories — GET /user/repos
-- Features: page-based pagination (auto-detected), query pushdown (type, sort)
-- ============================================================
create foreign table my_repos (
id bigint,
name text,
full_name text,
description text,
private boolean,
fork boolean,
language text,
stargazers_count integer,
forks_count integer,
open_issues_count integer,
created_at timestamptz,
updated_at timestamptz,
pushed_at timestamptz,
html_url text,
default_branch text,
archived boolean,
type text,
sort text,
attrs jsonb
)
server github
options (
endpoint '/user/repos',
rowid_column 'id'
);
-- ============================================================
-- Table 3: repo_detail
-- Full repository metadata — GET /repos/{owner}/{repo}
-- Features: two path parameters, single object response
-- ============================================================
create foreign table repo_detail (
id bigint,
name text,
full_name text,
description text,
private boolean,
stargazers_count integer,
forks_count integer,
open_issues_count integer,
watchers_count integer,
language text,
default_branch text,
created_at timestamptz,
updated_at timestamptz,
license jsonb,
topics jsonb,
owner text,
repo text,
attrs jsonb
)
server github
options (
endpoint '/repos/{owner}/{repo}'
);
-- ============================================================
-- Table 4: repo_issues
-- Repository issues — GET /repos/{owner}/{repo}/issues
-- Features: two path parameters, page-based pagination,
-- query pushdown (state), timestamptz coercion
-- ============================================================
create foreign table repo_issues (
id bigint,
number integer,
title text,
state text,
body text,
created_at timestamptz,
updated_at timestamptz,
closed_at timestamptz,
comments integer,
user_col jsonb,
labels jsonb,
html_url text,
owner text,
repo text,
attrs jsonb
)
server github
options (
endpoint '/repos/{owner}/{repo}/issues',
rowid_column 'id'
);
-- ============================================================
-- Table 5: repo_pulls
-- Repository pull requests — GET /repos/{owner}/{repo}/pulls
-- Features: two path parameters, page-based pagination,
-- query pushdown (state), boolean + timestamptz coercion
-- ============================================================
create foreign table repo_pulls (
id bigint,
number integer,
title text,
state text,
draft boolean,
created_at timestamptz,
updated_at timestamptz,
merged_at timestamptz,
user_col jsonb,
head jsonb,
base jsonb,
html_url text,
owner text,
repo text,
attrs jsonb
)
server github
options (
endpoint '/repos/{owner}/{repo}/pulls',
rowid_column 'id'
);
-- ============================================================
-- Table 6: repo_releases
-- Repository releases — GET /repos/{owner}/{repo}/releases
-- Features: two path parameters, page-based pagination
-- ============================================================
create foreign table repo_releases (
id bigint,
tag_name text,
name text,
body text,
draft boolean,
prerelease boolean,
created_at timestamptz,
published_at timestamptz,
author jsonb,
html_url text,
owner text,
repo text,
attrs jsonb
)
server github
options (
endpoint '/repos/{owner}/{repo}/releases',
rowid_column 'id'
);
-- ============================================================
-- Table 7: search_repos
-- Search repositories — GET /search/repositories
-- Features: query pushdown (q), auto-detected "items" wrapper key
-- ============================================================
create foreign table search_repos (
id bigint,
name text,
full_name text,
description text,
stargazers_count integer,
forks_count integer,
language text,
open_issues_count integer,
created_at timestamptz,
html_url text,
topics jsonb,
license jsonb,
q text,
attrs jsonb
)
server github
options (
endpoint '/search/repositories',
rowid_column 'id'
);
-- ============================================================
-- Table 8: search_repos_debug
-- Same as search_repos but on the debug server
-- Features: debug output in INFO messages
-- ============================================================
create foreign table search_repos_debug (
id bigint,
name text,
full_name text,
stargazers_count integer,
q text,
attrs jsonb
)
server github_debug
options (
endpoint '/search/repositories',
rowid_column 'id'
);