diff --git a/package-lock.json b/package-lock.json index 3495fe143..42ee765c9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -28057,7 +28057,8 @@ "@repo/utils": "*", "@supabase/auth-js": "2.71.1", "@supabase/functions-js": "2.4.5", - "@supabase/supabase-js": "2.55.0" + "@supabase/supabase-js": "2.55.0", + "dotenv": "^16.6.1" }, "devDependencies": { "@cucumber/cucumber": "^11.3.0", diff --git a/packages/database/README.md b/packages/database/README.md index a7061234a..0ba28f556 100644 --- a/packages/database/README.md +++ b/packages/database/README.md @@ -1,11 +1,17 @@ This contains the database schema for vector embeddings and concepts. -There are three usage scenarios: +There are four usage scenarios: + +## Developing without the database + +Your frontend will not use the database. +Optional: Set `SUPABASE_USE_DB=none` in your console before running `turbo dev`. (This is now the default.) ## Local development setup Normal scenario: Your backend and frontend will work against a database instance within docker. It does mean you will have a fresh database with minimal data. +Set `SUPABASE_USE_DB=local` in your console before running `turbo dev`. ### Installation diff --git a/packages/database/package.json b/packages/database/package.json index 0ff7522f9..8a83e6e72 100644 --- a/packages/database/package.json +++ b/packages/database/package.json @@ -10,9 +10,10 @@ "default": "./dist/src/lib/*.js" }, "./dbDotEnv": { - "types": "./dist/src/dbDotEnv.d.ts", - "require": "./dist/src/dbDotEnv.js", - "default": "./dist/src/dbDotEnv.js" + "types": "./types/dbDotEnv.d.ts", + "import": "./src/dbDotEnv.js", + "require": "./src/dbDotEnv.js", + "default": "./src/dbDotEnv.js" }, "./dbTypes": { "types": "./dist/src/dbTypes.d.ts", @@ -30,7 +31,7 @@ "dist/src/lib/*.d.ts" ], "./dbDotEnv": [ - "dist/src/dbDotEnv.d.ts" + "src/dbDotEnv.d.ts" ], "./dbTypes": [ "dist/src/dbTypes.d.ts" @@ -42,13 +43,16 @@ }, "scripts": { "init": "supabase login", - "dev": "supabase start && tsx scripts/createEnv.mts && supabase functions serve", + "dev": "tsx scripts/dev.ts", + "compile": "tsc", + "serve": "supabase start && tsx scripts/createEnv.mts && supabase functions serve", "stop": "supabase stop", "check-types": "tsc --emitDeclarationOnly --skipLibCheck", "check-schema": "tsx scripts/lint.ts && supabase stop && npm run dbdiff", "lint": "eslint . && tsx scripts/lint.ts", "lint:fix": "tsx scripts/lint.ts -f", - "build": "tsc && tsx scripts/build.ts && npm run genenv -- local", + "build": "tsc", + "build-schema": "tsx scripts/build.ts && npm run genenv -- local", "test": "tsc && cucumber-js", "genenv": "tsx scripts/createEnv.mts", "gentypes:production": "supabase start && supabase gen types typescript --project-id \"$SUPABASE_PROJECT_ID\" --schema public > src/dbTypes.ts", @@ -61,7 +65,8 @@ "@repo/utils": "*", "@supabase/auth-js": "2.71.1", "@supabase/functions-js": "2.4.5", - "@supabase/supabase-js": "2.55.0" + "@supabase/supabase-js": "2.55.0", + "dotenv": "^16.6.1" }, "devDependencies": { "@repo/typescript-config": "*", diff --git a/packages/database/scripts/build.ts b/packages/database/scripts/build.ts index f46db498c..85f0d1c1c 100644 --- a/packages/database/scripts/build.ts +++ b/packages/database/scripts/build.ts @@ -1,13 +1,17 @@ import { execSync } from "node:child_process"; import { writeFileSync } from "fs"; import { join, dirname } from "path"; -import { fileURLToPath } from "url"; +import { getVariant } from "@repo/database/dbDotEnv"; const __dirname = dirname(__filename); const projectRoot = join(__dirname, ".."); if (process.env.HOME !== "/vercel") { try { + if (getVariant() === "none") { + console.log("Not using the database"); + process.exit(0); + } execSync("supabase start", { cwd: projectRoot, stdio: "inherit" }); execSync("supabase migrations up", { cwd: projectRoot, stdio: "inherit" }); const stdout = execSync( diff --git a/packages/database/scripts/dev.ts b/packages/database/scripts/dev.ts new file mode 100644 index 000000000..ed8fbf284 --- /dev/null +++ b/packages/database/scripts/dev.ts @@ -0,0 +1,20 @@ +import { execSync } from "node:child_process"; +import { join, dirname } from "path"; +import { getVariant } from "@repo/database/dbDotEnv"; + +const __dirname = dirname(__filename); +const projectRoot = join(__dirname, ".."); + +if (process.env.HOME !== "/vercel") { + try { + execSync("npm run compile", { cwd: projectRoot, stdio: "inherit" }); + if (getVariant() === "none") { + console.log("Not using the database"); + process.exit(0); + } + execSync("npm run serve", { cwd: projectRoot, stdio: "inherit" }); + } catch (err) { + console.error(err); + throw err; + } +} diff --git a/packages/database/src/dbDotEnv.ts b/packages/database/src/dbDotEnv.js similarity index 78% rename from packages/database/src/dbDotEnv.ts rename to packages/database/src/dbDotEnv.js index b6afc9bfc..e1e8b0ad1 100644 --- a/packages/database/src/dbDotEnv.ts +++ b/packages/database/src/dbDotEnv.js @@ -1,9 +1,8 @@ import dotenv from "dotenv"; import { readFileSync, existsSync } from "fs"; import { join, dirname, basename } from "path"; -import { fileURLToPath } from "url"; -const findRoot = (): string => { +const findRoot = () => { let dir = __filename; while (basename(dir) !== "database") { dir = dirname(dir); @@ -11,24 +10,24 @@ const findRoot = (): string => { return dir; }; -export const getVariant = (): string | null => { +export const getVariant = () => { if (process.env.HOME === "/vercel" || process.env.GITHUB_ACTIONS === "true") - return null; + return "implicit"; const useDbArgPos = process.argv.indexOf("--use-db"); const variant = (useDbArgPos > 0 ? process.argv[useDbArgPos + 1] - : process.env["SUPABASE_USE_DB"]) || "local"; + : process.env["SUPABASE_USE_DB"]) || "none"; - if (["local", "branch", "production"].indexOf(variant) === -1) { + if (["local", "branch", "production", "none"].indexOf(variant) === -1) { throw new Error("Invalid variant: " + variant); } return variant; }; export const envFilePath = () => { - const variant: string | null = getVariant(); - if (variant === null) return null; + const variant = getVariant(); + if (variant === "implicit" || variant === "none") return null; const name = join(findRoot(), `.env.${variant}`); return existsSync(name) ? name : null; }; diff --git a/packages/database/types/dbDotEnv.d.ts b/packages/database/types/dbDotEnv.d.ts new file mode 100644 index 000000000..44de383c3 --- /dev/null +++ b/packages/database/types/dbDotEnv.d.ts @@ -0,0 +1,17 @@ +export type Variant = + | "none" + | "local" + | "branch" + | "production" + | "all" + | "implicit"; +export declare const getVariant: () => Variant; +export declare const envFilePath: () => string | null; +export declare const envContents: () => + | EnvMap + | { + SUPABASE_URL: string | undefined; + SUPABASE_ANON_KEY: string | undefined; + NEXT_API_ROOT: string | undefined; + }; +export declare const config: () => void; diff --git a/turbo.json b/turbo.json index fbaefe529..f5bae9e4c 100644 --- a/turbo.json +++ b/turbo.json @@ -44,6 +44,11 @@ "inputs": ["$TURBO_DEFAULT$", ".env*"], "outputs": [".next/**", "!.next/cache/**", "dist/**", "src/dbTypes.ts"] }, + "build-schema": { + "dependsOn": ["^build-schema"], + "inputs": ["$TURBO_DEFAULT$", ".env*"], + "outputs": ["src/dbTypes.ts"] + }, "lint": { "dependsOn": ["^lint"] }, @@ -58,8 +63,7 @@ }, "deploy": { "cache": false, - "inputs": ["$TURBO_DEFAULT$", ".env*"], - "dependsOn": ["@repo/database#build"] + "inputs": ["$TURBO_DEFAULT$", ".env*"] }, "publish": { "cache": false,