Skip to content

Commit 2d836c3

Browse files
authored
repairs to make routes work after some local testing (#184)
1 parent 7c90190 commit 2d836c3

7 files changed

Lines changed: 41 additions & 13 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ yarn-error.log*
3636
# Misc
3737
.DS_Store
3838
*.pem
39+
*.ipynb

apps/website/app/utils/supabase/apiUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const createApiResponse = <T>(
3030
{ status },
3131
);
3232
} else if (data !== undefined && data !== null) {
33-
response = NextResponse.json(data, { status });
33+
response = NextResponse.json<T | T[]>(data, { status });
3434
} else {
3535
// Fallback for unexpected state (e.g. no error, but no data for a success status)
3636
console.error(

apps/website/app/utils/supabase/dbUtils.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ export const getOrCreateEntity = async <
100100
ignoreDuplicates: false,
101101
count: "estimated",
102102
})
103-
.single()
104-
.overrideTypes<Tables<TableName>>();
105-
const { error: insertError } = result;
106-
if (insertError) {
103+
.select()
104+
.single();
105+
if (result.error) {
106+
const { error: insertError } = result;
107107
if (insertError.code === "23505") {
108108
// Handle race condition: unique constraint violation (PostgreSQL error code '23505')
109109
const dup_key_data = UNIQUE_KEY_RE.exec(insertError.hint);
@@ -115,9 +115,14 @@ export const getOrCreateEntity = async <
115115
console.warn(`Attempting to re-fetch using ${uniqueOn.join(", ")}`);
116116
let reFetchQueryBuilder = supabase.from(tableName).select();
117117
for (let i = 0; i < uniqueOn.length; i++) {
118-
const key: keyof TablesInsert<TableName> = uniqueOn[i]!;
118+
const key = uniqueOn[i];
119+
if (!key) {
120+
console.error("Empty key in uniqueOn");
121+
continue;
122+
}
123+
const keyS = String(key);
119124
reFetchQueryBuilder = reFetchQueryBuilder.eq(
120-
key as string,
125+
keyS,
121126
insertData[key] as any, // TS gets confused here?
122127
);
123128
}

apps/website/app/utils/supabase/server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { Database } from "@repo/database/types.gen.ts";
44

55
export const createClient = async () => {
66
const cookieStore = await cookies();
7-
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL;
8-
const supabaseKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY;
7+
const supabaseUrl = process.env.SUPABASE_URL;
8+
const supabaseKey = process.env.SUPABASE_SERVICE_ROLE_KEY;
99

1010
if (!supabaseUrl || !supabaseKey) {
1111
throw new Error("Missing required Supabase environment variables");
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
DROP INDEX IF EXISTS "public"."document_space_and_local_id_idx";
2+
CREATE UNIQUE INDEX document_space_and_local_id_idx ON public."Document" USING btree (space_id, source_local_id) NULLS DISTINCT;
3+
4+
DROP INDEX IF EXISTS "public"."content_space_and_local_id_idx";
5+
CREATE UNIQUE INDEX content_space_and_local_id_idx ON public."Content" USING btree (space_id, source_local_id) NULLS DISTINCT;

packages/database/supabase/schemas/content.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ ADD CONSTRAINT "Document_space_id_fkey" FOREIGN KEY (
4343
) ON UPDATE CASCADE ON DELETE CASCADE;
4444

4545
CREATE UNIQUE INDEX document_space_and_local_id_idx ON public."Document" USING btree (space_id, source_local_id)
46-
NULLS DISTINCT WHERE space_id IS NOT NULL;
46+
NULLS DISTINCT;
4747

4848
CREATE UNIQUE INDEX document_url_idx ON public."Document" USING btree (url);
4949

@@ -121,7 +121,7 @@ CREATE INDEX "Content_space" ON public."Content" USING btree (space_id);
121121

122122
CREATE UNIQUE INDEX content_space_and_local_id_idx ON public."Content" USING btree (
123123
space_id, source_local_id
124-
) NULLS DISTINCT WHERE (space_id IS NOT NULL);
124+
) NULLS DISTINCT;
125125

126126
CREATE INDEX "Content_text" ON public."Content" USING pgroonga (text);
127127

turbo.json

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,17 @@
2222
"dependsOn": ["^check-types"]
2323
},
2424
"dev": {
25-
"passThroughEnv": ["OBSIDIAN_PLUGIN_PATH", "NODE_ENV"],
25+
"passThroughEnv": [
26+
"OBSIDIAN_PLUGIN_PATH",
27+
"NODE_ENV",
28+
"SUPABASE_URL",
29+
"SUPABASE_ANON_KEY",
30+
"SUPABASE_SERVICE_ROLE_KEY",
31+
"POSTGRES_URL",
32+
"OPENAI_API_KEY",
33+
"ANTHROPIC_API_KEY",
34+
"GEMINI_API_KEY"
35+
],
2636
"cache": false,
2737
"persistent": true,
2838
"inputs": ["$TURBO_DEFAULT$", ".env*"]
@@ -37,7 +47,14 @@
3747
"NODE_ENV",
3848
"SUPABASE_PROJECT_ID",
3949
"SUPABASE_DB_PASSWORD",
40-
"SUPABASE_ACCESS_TOKEN"
50+
"SUPABASE_ACCESS_TOKEN",
51+
"SUPABASE_URL",
52+
"SUPABASE_ANON_KEY",
53+
"SUPABASE_SERVICE_ROLE_KEY",
54+
"POSTGRES_URL",
55+
"OPENAI_API_KEY",
56+
"ANTHROPIC_API_KEY",
57+
"GEMINI_API_KEY"
4158
]
4259
},
4360
"publish": {

0 commit comments

Comments
 (0)