spine-framework 0.3.31 → 0.3.32
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.
|
@@ -18,24 +18,8 @@ import { createClient } from '@supabase/supabase-js'
|
|
|
18
18
|
|
|
19
19
|
// ─── ENVIRONMENT RESOLUTION ──────────────────────────────────────────────────
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
const supabaseServiceKey: string = _env.SUPABASE_SERVICE_ROLE_KEY!
|
|
24
|
-
const supabaseAnonKey: string = _env.SUPABASE_ANON_KEY!
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Active database schema name, read from `DB_SCHEMA` env var.
|
|
28
|
-
*
|
|
29
|
-
* Defaults to `'public'` (production schema). Set to `'v2'` only in legacy
|
|
30
|
-
* environments. All new migrations target `public`.
|
|
31
|
-
*
|
|
32
|
-
* @inputSpec DB_SCHEMA: string — one of 'public' | 'v2'. Any other value is
|
|
33
|
-
* passed through as-is and will cause runtime query errors.
|
|
34
|
-
* @outputSpec string — schema name applied to both Supabase clients.
|
|
35
|
-
* @sideEffects none
|
|
36
|
-
* @calledBy adminDb, getUserDb (applied at client construction time)
|
|
37
|
-
*/
|
|
38
|
-
const dbSchema: string = _env.DB_SCHEMA || 'public'
|
|
21
|
+
// Environment variables are read lazily at call time (not at module load)
|
|
22
|
+
// so the CLI works before .env is configured (e.g. for --help and init).
|
|
39
23
|
|
|
40
24
|
// ─── CLIENTS ─────────────────────────────────────────────────────────────────
|
|
41
25
|
|
|
@@ -73,9 +57,22 @@ const dbSchema: string = _env.DB_SCHEMA || 'public'
|
|
|
73
57
|
* // Only use adminDb if your custom code runs as a system/cron principal
|
|
74
58
|
* ```
|
|
75
59
|
*/
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
60
|
+
function getAdminDb() {
|
|
61
|
+
const url = (globalThis as any).process?.env?.SUPABASE_URL
|
|
62
|
+
const key = (globalThis as any).process?.env?.SUPABASE_SERVICE_ROLE_KEY
|
|
63
|
+
const schema = (globalThis as any).process?.env?.DB_SCHEMA || 'public'
|
|
64
|
+
if (!url || !key) {
|
|
65
|
+
throw new Error(
|
|
66
|
+
'SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY must be set.\n' +
|
|
67
|
+
'Add them to your .env file and run: npx spine-framework migrate'
|
|
68
|
+
)
|
|
69
|
+
}
|
|
70
|
+
return createClient(url, key, { db: { schema } })
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export const adminDb = new Proxy({} as ReturnType<typeof getAdminDb>, {
|
|
74
|
+
get(_target, prop) {
|
|
75
|
+
return (getAdminDb() as any)[prop]
|
|
79
76
|
}
|
|
80
77
|
})
|
|
81
78
|
|
|
@@ -94,15 +91,12 @@ export const adminDb = createClient(supabaseUrl, supabaseServiceKey, {
|
|
|
94
91
|
* @tags database, supabase, rls, authentication, user-scoped
|
|
95
92
|
*/
|
|
96
93
|
export function getUserDb(jwt: string) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
Authorization: `Bearer ${jwt}`
|
|
104
|
-
}
|
|
105
|
-
}
|
|
94
|
+
const url = (globalThis as any).process?.env?.SUPABASE_URL
|
|
95
|
+
const anonKey = (globalThis as any).process?.env?.SUPABASE_ANON_KEY
|
|
96
|
+
const schema = (globalThis as any).process?.env?.DB_SCHEMA || 'public'
|
|
97
|
+
return createClient(url, anonKey, {
|
|
98
|
+
db: { schema },
|
|
99
|
+
global: { headers: { Authorization: `Bearer ${jwt}` } }
|
|
106
100
|
})
|
|
107
101
|
}
|
|
108
102
|
// ─── CHUNK_END: SHARED_DB_GET_USER_DB ────────────────────────────────────────────────
|