@pgpm/supabase 0.4.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.
Files changed (57) hide show
  1. package/LICENSE +21 -0
  2. package/Makefile +7 -0
  3. package/README.md +37 -0
  4. package/__tests__/_realtime/_realtime-extensions.test.ts +129 -0
  5. package/__tests__/_realtime/_realtime-schema-migrations.test.ts +160 -0
  6. package/__tests__/_realtime/_realtime-tenants.test.ts +163 -0
  7. package/__tests__/auth/auth.audit-log-entries.test.ts +148 -0
  8. package/__tests__/auth/auth.flow-state.test.ts +114 -0
  9. package/__tests__/auth/auth.identities.test.ts +149 -0
  10. package/__tests__/auth/auth.instances.test.ts +134 -0
  11. package/__tests__/auth/auth.mfa-amr-claims.test.ts +142 -0
  12. package/__tests__/auth/auth.mfa-challenges.test.ts +117 -0
  13. package/__tests__/auth/auth.mfa-factors.test.ts +118 -0
  14. package/__tests__/auth/auth.oauth-authorizations.test.ts +123 -0
  15. package/__tests__/auth/auth.oauth-clients.test.ts +120 -0
  16. package/__tests__/auth/auth.oauth-consents.test.ts +122 -0
  17. package/__tests__/auth/auth.one-time-tokens.test.ts +194 -0
  18. package/__tests__/auth/auth.refresh-tokens.test.ts +134 -0
  19. package/__tests__/auth/auth.saml-providers.test.ts +154 -0
  20. package/__tests__/auth/auth.saml-relay-states.test.ts +145 -0
  21. package/__tests__/auth/auth.schema-migrations.test.ts +131 -0
  22. package/__tests__/auth/auth.sessions.test.ts +140 -0
  23. package/__tests__/auth/auth.sso-domains.test.ts +137 -0
  24. package/__tests__/auth/auth.sso-providers.test.ts +136 -0
  25. package/__tests__/auth/auth.users.test.ts +314 -0
  26. package/__tests__/conditional.test.ts +229 -0
  27. package/__tests__/functions.test.ts +252 -0
  28. package/__tests__/jsonb.test.ts +237 -0
  29. package/__tests__/net/net.http-request-queue.test.ts +159 -0
  30. package/__tests__/net/net.http-response.test.ts +163 -0
  31. package/__tests__/realtime/realtime.messages-2025-10-31.test.ts +168 -0
  32. package/__tests__/realtime/realtime.messages.test.ts +162 -0
  33. package/__tests__/realtime/realtime.schema-migrations.test.ts +139 -0
  34. package/__tests__/realtime/realtime.subscription.test.ts +159 -0
  35. package/__tests__/storage/storage.buckets-analytics.test.ts +134 -0
  36. package/__tests__/storage/storage.buckets.test.ts +146 -0
  37. package/__tests__/storage/storage.iceberg-namespaces.test.ts +132 -0
  38. package/__tests__/storage/storage.iceberg-tables.test.ts +142 -0
  39. package/__tests__/storage/storage.migrations.test.ts +133 -0
  40. package/__tests__/storage/storage.objects.test.ts +160 -0
  41. package/__tests__/storage/storage.prefixes.test.ts +138 -0
  42. package/__tests__/storage/storage.s3-multipart-uploads-parts.test.ts +133 -0
  43. package/__tests__/storage/storage.s3-multipart-uploads.test.ts +128 -0
  44. package/__tests__/supabase-functions/supabase.functions-hooks.test.ts +125 -0
  45. package/__tests__/supabase-functions/supabase.functions-migrations.test.ts +213 -0
  46. package/__tests__/temporal.test.ts +225 -0
  47. package/__tests__/vault-secrets.test.ts +145 -0
  48. package/deploy/LICENSE +9 -0
  49. package/deploy/supabase.sql +2271 -0
  50. package/jest.config.js +15 -0
  51. package/launchql.plan +4 -0
  52. package/package.json +23 -0
  53. package/revert/supabase.sql +3 -0
  54. package/sql/supabase--0.0.1.sql +1368 -0
  55. package/src/index.ts +3 -0
  56. package/supabase.control +7 -0
  57. package/verify/supabase.sql +3 -0
@@ -0,0 +1,229 @@
1
+ import { getConnections, PgTestClient } from 'supabase-test';
2
+
3
+ let pg: PgTestClient;
4
+ let db: PgTestClient;
5
+ let teardown: () => Promise<void>;
6
+
7
+ beforeAll(async () => {
8
+ ({ pg, db, teardown } = await getConnections());
9
+
10
+ // verify auth schema exists
11
+ const authSchemaExists = await pg.any(
12
+ `SELECT EXISTS (
13
+ SELECT FROM information_schema.schemata
14
+ WHERE schema_name = 'auth'
15
+ ) as exists`
16
+ );
17
+ expect(authSchemaExists[0].exists).toBe(true);
18
+
19
+ // minimal grants for tests
20
+ await pg.any(
21
+ `GRANT USAGE ON SCHEMA auth TO public;
22
+ GRANT USAGE ON SCHEMA storage TO public;
23
+ GRANT SELECT ON TABLE auth.users TO service_role;
24
+ GRANT SELECT ON TABLE storage.buckets TO service_role;
25
+ GRANT EXECUTE ON FUNCTION auth.uid() TO public;
26
+ GRANT EXECUTE ON FUNCTION auth.role() TO public;`,
27
+ []
28
+ );
29
+ });
30
+
31
+ afterAll(async () => {
32
+ await teardown();
33
+ });
34
+
35
+ beforeEach(async () => {
36
+ await db.beforeEach();
37
+ });
38
+
39
+ afterEach(async () => {
40
+ await db.afterEach();
41
+ });
42
+
43
+ describe('tutorial: rls with conditional policies', () => {
44
+ it('should verify rls policies can use case statements', async () => {
45
+ db.setContext({ role: 'service_role' });
46
+
47
+ // insert into auth.users using admin connection
48
+ const user1 = await pg.one(
49
+ `INSERT INTO auth.users (id, email)
50
+ VALUES (gen_random_uuid(), $1)
51
+ RETURNING id`,
52
+ ['case1@example.com']
53
+ );
54
+ // ensure auth.role() sees service_role via jwt claim
55
+ db.setContext({
56
+ role: 'service_role',
57
+ 'request.jwt.claim.role': 'service_role'
58
+ });
59
+ // case over email and auth.role()
60
+ const rows = await db.any(
61
+ `SELECT
62
+ CASE WHEN email LIKE '%@example.com' THEN 'example' ELSE 'other' END AS grp,
63
+ CASE WHEN auth.role() = 'service_role' THEN 'visible' ELSE 'hidden' END AS visibility
64
+ FROM auth.users
65
+ WHERE id = $1`,
66
+ [user1.id]
67
+ );
68
+ expect(Array.isArray(rows)).toBe(true);
69
+ expect(rows.length).toBe(1);
70
+ expect(rows[0].grp).toBe('example');
71
+ expect(rows[0].visibility).toBe('visible');
72
+ });
73
+
74
+ it('should verify rls policies work with multiple conditions', async () => {
75
+ db.setContext({ role: 'service_role' });
76
+
77
+ // insert into auth.users using admin connection
78
+ const user = await pg.one(
79
+ `INSERT INTO auth.users (id, email)
80
+ VALUES (gen_random_uuid(), $1)
81
+ RETURNING id`,
82
+ ['multi1@example.com']
83
+ );
84
+ // ensure auth.role() sees service_role via jwt claim
85
+ db.setContext({
86
+ role: 'service_role',
87
+ 'request.jwt.claim.role': 'service_role'
88
+ });
89
+ // and/or combinations with auth.role()
90
+ const rows = await db.any(
91
+ `SELECT id
92
+ FROM auth.users
93
+ WHERE (email = $1 AND (auth.role() = 'service_role' OR auth.role() = 'authenticated'))
94
+ AND id = $2`,
95
+ ['multi1@example.com', user.id]
96
+ );
97
+ expect(Array.isArray(rows)).toBe(true);
98
+ expect(rows.length).toBe(1);
99
+ expect(rows[0].id).toBe(user.id);
100
+ });
101
+
102
+ it('should verify rls policies work with or conditions', async () => {
103
+ db.setContext({ role: 'service_role' });
104
+
105
+ // insert into auth.users using admin connection
106
+ const user = await pg.one(
107
+ `INSERT INTO auth.users (id, email)
108
+ VALUES (gen_random_uuid(), $1)
109
+ RETURNING id`,
110
+ ['or1@example.com']
111
+ );
112
+ const user2 = await pg.one(
113
+ `INSERT INTO auth.users (id, email)
114
+ VALUES (gen_random_uuid(), $1)
115
+ RETURNING id`,
116
+ ['or2@example.com']
117
+ );
118
+
119
+ const rows = await db.any(
120
+ `SELECT COUNT(*)::integer AS count
121
+ FROM auth.users
122
+ WHERE email = $1 OR email = $2`,
123
+ ['or1@example.com', 'or2@example.com']
124
+ );
125
+ expect(Array.isArray(rows)).toBe(true);
126
+ expect(Number(rows[0].count)).toBe(2);
127
+ });
128
+
129
+ it('should verify rls policies work with subqueries', async () => {
130
+ db.setContext({ role: 'service_role' });
131
+
132
+ // insert into auth.users using admin connection
133
+ const user1 = await pg.one(
134
+ `INSERT INTO auth.users (id, email)
135
+ VALUES (gen_random_uuid(), $1)
136
+ RETURNING id`,
137
+ ['sub1@example.com']
138
+ );
139
+
140
+
141
+ const user2 = await pg.one(
142
+ `INSERT INTO auth.users (id, email)
143
+ VALUES (gen_random_uuid(), $1)
144
+ RETURNING id`,
145
+ ['sub2@example.com']
146
+ );
147
+ // use a subquery to resolve ids by email
148
+ const viaSub = await db.any(
149
+ `SELECT u.id
150
+ FROM auth.users u
151
+ WHERE u.id IN (SELECT id FROM auth.users WHERE email = $1)`,
152
+ ['sub1@example.com']
153
+ );
154
+ expect(viaSub.length).toBe(1);
155
+ expect(viaSub[0].id).toBe(user1.id);
156
+
157
+ // correlated exists
158
+ const existsRows = await db.any(
159
+ `SELECT u.id
160
+ FROM auth.users u
161
+ WHERE EXISTS (
162
+ SELECT 1 FROM auth.users i WHERE i.id = u.id AND i.email = $1
163
+ )`,
164
+ ['sub2@example.com']
165
+ );
166
+ expect(existsRows.length).toBeGreaterThan(0);
167
+ const found = existsRows.find((r: any) => r.id === user2.id);
168
+ expect(Boolean(found)).toBe(true);
169
+ });
170
+
171
+ it('should verify rls policies work with related table checks', async () => {
172
+ db.setContext({ role: 'service_role' });
173
+
174
+ // insert into auth.users using admin connection
175
+ const user = await pg.one(
176
+ `INSERT INTO auth.users (id, email)
177
+ VALUES (gen_random_uuid(), $1)
178
+ RETURNING id`,
179
+ ['rel1@example.com']
180
+ );
181
+ // left join storage.buckets by fk owner -> auth.users(id)
182
+ const rows = await db.any(
183
+ `SELECT u.id, b.id AS bucket_id
184
+ FROM auth.users u
185
+ LEFT JOIN storage.buckets b ON b.owner = u.id
186
+ WHERE u.id = $1`,
187
+ [user.id]
188
+ );
189
+ expect(rows.length).toBe(1);
190
+ // no bucket yet, so join should be null
191
+ expect(rows[0].bucket_id === null || rows[0].bucket_id === undefined).toBe(true);
192
+ });
193
+
194
+ it('should verify rls policies work with null checks', async () => {
195
+ db.setContext({ role: 'service_role' });
196
+
197
+ // insert into auth.users using admin connection
198
+ const user = await pg.one(
199
+ `INSERT INTO auth.users (id, email)
200
+ VALUES (gen_random_uuid(), $1)
201
+ RETURNING id, email`,
202
+ [null]
203
+ );
204
+ const rows = await db.any(
205
+ `SELECT id FROM auth.users WHERE id = $1 AND email IS NULL`,
206
+ [user.id]
207
+ );
208
+ expect(rows.length).toBe(1);
209
+ });
210
+
211
+ it('should verify rls policies work with coalesce functions', async () => {
212
+ db.setContext({ role: 'service_role' });
213
+
214
+ // insert into auth.users using admin connection
215
+ const user = await pg.one(
216
+ `INSERT INTO auth.users (id, email)
217
+ VALUES (gen_random_uuid(), $1)
218
+ RETURNING id`,
219
+ [null]
220
+ );
221
+ const rows = await db.any(
222
+ `SELECT COALESCE(email, '') AS safe_email FROM auth.users WHERE id = $1`,
223
+ [user.id]
224
+ );
225
+ expect(rows.length).toBe(1);
226
+ expect(rows[0].safe_email).toBe('');
227
+ });
228
+ });
229
+
@@ -0,0 +1,252 @@
1
+ import { getConnections, PgTestClient } from 'supabase-test';
2
+
3
+ let pg: PgTestClient;
4
+ let db: PgTestClient;
5
+ let teardown: () => Promise<void>;
6
+
7
+ beforeAll(async () => {
8
+ ({ pg, db, teardown } = await getConnections());
9
+
10
+ // verify auth schema exists
11
+ const authSchemaExists = await pg.any(
12
+ `SELECT EXISTS (
13
+ SELECT FROM information_schema.schemata
14
+ WHERE schema_name = 'auth'
15
+ ) as exists`
16
+ );
17
+ expect(authSchemaExists[0].exists).toBe(true);
18
+
19
+ // grant access to auth and storage schemas for testing
20
+ // grant INSERT on auth.users to service_role so we can create test users
21
+ await pg.any(
22
+ `GRANT USAGE ON SCHEMA auth TO public;
23
+ GRANT USAGE ON SCHEMA storage TO public;
24
+ GRANT INSERT ON TABLE auth.users TO service_role;
25
+ GRANT EXECUTE ON FUNCTION auth.uid() TO public;
26
+ GRANT EXECUTE ON FUNCTION auth.role() TO public;
27
+ GRANT EXECUTE ON FUNCTION auth.email() TO public;`,
28
+ []
29
+ );
30
+ });
31
+
32
+ afterAll(async () => {
33
+ await teardown();
34
+ });
35
+
36
+ beforeEach(async () => {
37
+ await db.beforeEach();
38
+ });
39
+
40
+ afterEach(async () => {
41
+ await db.afterEach();
42
+ });
43
+
44
+ describe('tutorial: rls with supabase functions', () => {
45
+ it('should verify auth.uid() function exists and works', async () => {
46
+ // verify function exists first
47
+ db.setContext({ role: 'service_role' });
48
+ const funcExists = await db.any(
49
+ `SELECT EXISTS (
50
+ SELECT FROM pg_proc p
51
+ JOIN pg_namespace n ON n.oid = p.pronamespace
52
+ WHERE n.nspname = 'auth' AND p.proname = 'uid'
53
+ ) as exists`
54
+ );
55
+ expect(funcExists[0].exists).toBe(true);
56
+
57
+ // using auth.users (real supabase table) instead of rls_test.user_profiles (fake test table)
58
+ // set service_role context to insert into auth.users (requires INSERT permission)
59
+ const user = await pg.one(
60
+ `INSERT INTO auth.users (id, email)
61
+ VALUES (gen_random_uuid(), $1)
62
+ RETURNING id`,
63
+ ['func1@example.com']
64
+ );
65
+
66
+ db.setContext({
67
+ role: 'authenticated',
68
+ 'request.jwt.claim.sub': user.id
69
+ });
70
+
71
+ // verify auth.uid() returns correct user id
72
+ const uidResult = await db.one(`SELECT auth.uid() as uid`);
73
+ expect(uidResult.uid).toBe(user.id);
74
+ });
75
+
76
+ it('should verify auth.role() function exists and works', async () => {
77
+ // verify function exists first
78
+ db.setContext({ role: 'service_role' });
79
+ const funcExists = await db.any(
80
+ `SELECT EXISTS (
81
+ SELECT FROM pg_proc p
82
+ JOIN pg_namespace n ON n.oid = p.pronamespace
83
+ WHERE n.nspname = 'auth' AND p.proname = 'role'
84
+ ) as exists`
85
+ );
86
+ expect(funcExists[0].exists).toBe(true);
87
+
88
+ db.setContext({
89
+ role: 'authenticated',
90
+ 'request.jwt.claim.role': 'authenticated'
91
+ });
92
+
93
+ const roleResult = await db.one(`SELECT auth.role() as role`);
94
+ expect(roleResult.role).toBe('authenticated');
95
+ });
96
+
97
+ it('should verify auth.email() function exists and works', async () => {
98
+ // verify function exists first
99
+ db.setContext({ role: 'service_role' });
100
+ const funcExists = await db.any(
101
+ `SELECT EXISTS (
102
+ SELECT FROM pg_proc p
103
+ JOIN pg_namespace n ON n.oid = p.pronamespace
104
+ WHERE n.nspname = 'auth' AND p.proname = 'email'
105
+ ) as exists`
106
+ );
107
+ expect(funcExists[0].exists).toBe(true);
108
+
109
+ // using auth.users (real supabase table) instead of rls_test.user_profiles (fake test table)
110
+ // use pg for auth.users insert since it requires superuser privileges
111
+ const user = await pg.one(
112
+ `INSERT INTO auth.users (id, email)
113
+ VALUES (gen_random_uuid(), $1)
114
+ RETURNING id, email`,
115
+ ['emailfunc@example.com']
116
+ );
117
+
118
+
119
+
120
+ db.setContext({
121
+ role: 'authenticated',
122
+ 'request.jwt.claim.sub': user.id,
123
+ 'request.jwt.claim.email': user.email
124
+ });
125
+
126
+ const emailResult = await db.one(`SELECT auth.email() as email`);
127
+ expect(emailResult.email).toBe(user.email);
128
+ });
129
+
130
+ it('should verify functions work in rls policy context', async () => {
131
+ // verify function exists first
132
+ db.setContext({ role: 'service_role' });
133
+ const funcExists = await db.any(
134
+ `SELECT EXISTS (
135
+ SELECT FROM pg_proc p
136
+ JOIN pg_namespace n ON n.oid = p.pronamespace
137
+ WHERE n.nspname = 'auth' AND p.proname = 'uid'
138
+ ) as exists`
139
+ );
140
+ expect(funcExists[0].exists).toBe(true);
141
+
142
+ // using auth.users (real supabase table) instead of rls_test.user_profiles (fake test table)
143
+ // use pg for auth.users insert since it requires superuser privileges
144
+ const user = await pg.one(
145
+ `INSERT INTO auth.users (id, email)
146
+ VALUES (gen_random_uuid(), $1)
147
+ RETURNING id`,
148
+ ['policyfunc@example.com']
149
+ );
150
+
151
+
152
+ });
153
+
154
+ it('should verify functions return null for anon users', async () => {
155
+ // verify function exists first
156
+ db.setContext({ role: 'service_role' });
157
+ const funcExists = await db.any(
158
+ `SELECT EXISTS (
159
+ SELECT FROM pg_proc p
160
+ JOIN pg_namespace n ON n.oid = p.pronamespace
161
+ WHERE n.nspname = 'auth' AND p.proname = 'uid'
162
+ ) as exists`
163
+ );
164
+ expect(funcExists[0].exists).toBe(true);
165
+
166
+ db.clearContext();
167
+
168
+ const uidResult = await db.any(`SELECT auth.uid() as uid`);
169
+ expect(uidResult.length).toBe(1);
170
+ // auth.uid() should return null for anon
171
+ expect(uidResult[0].uid).toBeNull();
172
+ });
173
+
174
+ it('should verify storage functions exist and are callable', async () => {
175
+ db.setContext({ role: 'service_role' });
176
+
177
+ // check if storage.search function exists
178
+ const funcExists = await db.any(
179
+ `SELECT EXISTS (
180
+ SELECT FROM pg_proc p
181
+ JOIN pg_namespace n ON n.oid = p.pronamespace
182
+ WHERE n.nspname = 'storage' AND p.proname = 'search'
183
+ ) as exists`
184
+ );
185
+
186
+ expect(Array.isArray(funcExists)).toBe(true);
187
+ expect(funcExists.length).toBe(1);
188
+
189
+ // if function exists, verify we can check its signature
190
+ if (funcExists[0].exists) {
191
+ const funcInfo = await db.any(
192
+ `SELECT p.proname, n.nspname
193
+ FROM pg_proc p
194
+ JOIN pg_namespace n ON n.oid = p.pronamespace
195
+ WHERE n.nspname = 'storage' AND p.proname = 'search'
196
+ LIMIT 1`
197
+ );
198
+ expect(funcInfo.length).toBe(1);
199
+ expect(funcInfo[0].proname).toBe('search');
200
+ expect(funcInfo[0].nspname).toBe('storage');
201
+ }
202
+ });
203
+
204
+ it('should verify functions can be used in where clauses with rls', async () => {
205
+ // verify function exists first
206
+ db.setContext({ role: 'service_role' });
207
+ const funcExists = await db.any(
208
+ `SELECT EXISTS (
209
+ SELECT FROM pg_proc p
210
+ JOIN pg_namespace n ON n.oid = p.pronamespace
211
+ WHERE n.nspname = 'auth' AND p.proname = 'uid'
212
+ ) as exists`
213
+ );
214
+ expect(funcExists[0].exists).toBe(true);
215
+
216
+ // using auth.users (real supabase table) instead of rls_test.user_profiles (fake test table)
217
+ // use pg for auth.users insert since it requires superuser privileges
218
+ const user = await pg.one(
219
+ `INSERT INTO auth.users (id, email)
220
+ VALUES (gen_random_uuid(), $1)
221
+ RETURNING id`,
222
+ ['wherefunc@example.com']
223
+ );
224
+
225
+
226
+ });
227
+
228
+ it('should verify functions work in subqueries with rls', async () => {
229
+ // verify function exists first
230
+ db.setContext({ role: 'service_role' });
231
+ const funcExists = await db.any(
232
+ `SELECT EXISTS (
233
+ SELECT FROM pg_proc p
234
+ JOIN pg_namespace n ON n.oid = p.pronamespace
235
+ WHERE n.nspname = 'auth' AND p.proname = 'uid'
236
+ ) as exists`
237
+ );
238
+ expect(funcExists[0].exists).toBe(true);
239
+
240
+ // using auth.users (real supabase table) instead of rls_test.user_profiles (fake test table)
241
+ // use pg for auth.users insert since it requires superuser privileges
242
+ const user = await pg.one(
243
+ `INSERT INTO auth.users (id, email)
244
+ VALUES (gen_random_uuid(), $1)
245
+ RETURNING id`,
246
+ ['subfunc@example.com']
247
+ );
248
+
249
+
250
+ });
251
+ });
252
+