Skip to content

Commit 6bcad0d

Browse files
authored
ENG-801 Compile database before dev (#386)
* Compile database before running dev * In @repo/database, separate tsc from building the schema. Make build depend on building the schema, but not check-types. (Still thinking about the pros and cons here) * Precompile dbDotEnv so database does not have to be built Introduce SUPABASE_USE_DB=none. (Now the default value.) dev and build in database will not activate supabase if that is set. * coderabbit suggestions
1 parent 35a2d55 commit 6bcad0d

File tree

8 files changed

+76
-20
lines changed

8 files changed

+76
-20
lines changed

package-lock.json

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/database/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
This contains the database schema for vector embeddings and concepts.
22

3-
There are three usage scenarios:
3+
There are four usage scenarios:
4+
5+
## Developing without the database
6+
7+
Your frontend will not use the database.
8+
Optional: Set `SUPABASE_USE_DB=none` in your console before running `turbo dev`. (This is now the default.)
49

510
## Local development setup
611

712
Normal scenario: Your backend and frontend will work against a database instance within docker.
813
It does mean you will have a fresh database with minimal data.
14+
Set `SUPABASE_USE_DB=local` in your console before running `turbo dev`.
915

1016
### Installation
1117

packages/database/package.json

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
"default": "./dist/src/lib/*.js"
1111
},
1212
"./dbDotEnv": {
13-
"types": "./dist/src/dbDotEnv.d.ts",
14-
"require": "./dist/src/dbDotEnv.js",
15-
"default": "./dist/src/dbDotEnv.js"
13+
"types": "./types/dbDotEnv.d.ts",
14+
"import": "./src/dbDotEnv.js",
15+
"require": "./src/dbDotEnv.js",
16+
"default": "./src/dbDotEnv.js"
1617
},
1718
"./dbTypes": {
1819
"types": "./dist/src/dbTypes.d.ts",
@@ -30,7 +31,7 @@
3031
"dist/src/lib/*.d.ts"
3132
],
3233
"./dbDotEnv": [
33-
"dist/src/dbDotEnv.d.ts"
34+
"src/dbDotEnv.d.ts"
3435
],
3536
"./dbTypes": [
3637
"dist/src/dbTypes.d.ts"
@@ -42,13 +43,16 @@
4243
},
4344
"scripts": {
4445
"init": "supabase login",
45-
"dev": "supabase start && tsx scripts/createEnv.mts && supabase functions serve",
46+
"dev": "tsx scripts/dev.ts",
47+
"compile": "tsc",
48+
"serve": "supabase start && tsx scripts/createEnv.mts && supabase functions serve",
4649
"stop": "supabase stop",
4750
"check-types": "tsc --emitDeclarationOnly --skipLibCheck",
4851
"check-schema": "tsx scripts/lint.ts && supabase stop && npm run dbdiff",
4952
"lint": "eslint . && tsx scripts/lint.ts",
5053
"lint:fix": "tsx scripts/lint.ts -f",
51-
"build": "tsc && tsx scripts/build.ts && npm run genenv -- local",
54+
"build": "tsc",
55+
"build-schema": "tsx scripts/build.ts && npm run genenv -- local",
5256
"test": "tsc && cucumber-js",
5357
"genenv": "tsx scripts/createEnv.mts",
5458
"gentypes:production": "supabase start && supabase gen types typescript --project-id \"$SUPABASE_PROJECT_ID\" --schema public > src/dbTypes.ts",
@@ -61,7 +65,8 @@
6165
"@repo/utils": "*",
6266
"@supabase/auth-js": "2.71.1",
6367
"@supabase/functions-js": "2.4.5",
64-
"@supabase/supabase-js": "2.55.0"
68+
"@supabase/supabase-js": "2.55.0",
69+
"dotenv": "^16.6.1"
6570
},
6671
"devDependencies": {
6772
"@repo/typescript-config": "*",

packages/database/scripts/build.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import { execSync } from "node:child_process";
22
import { writeFileSync } from "fs";
33
import { join, dirname } from "path";
4-
import { fileURLToPath } from "url";
4+
import { getVariant } from "@repo/database/dbDotEnv";
55

66
const __dirname = dirname(__filename);
77
const projectRoot = join(__dirname, "..");
88

99
if (process.env.HOME !== "/vercel") {
1010
try {
11+
if (getVariant() === "none") {
12+
console.log("Not using the database");
13+
process.exit(0);
14+
}
1115
execSync("supabase start", { cwd: projectRoot, stdio: "inherit" });
1216
execSync("supabase migrations up", { cwd: projectRoot, stdio: "inherit" });
1317
const stdout = execSync(

packages/database/scripts/dev.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { execSync } from "node:child_process";
2+
import { join, dirname } from "path";
3+
import { getVariant } from "@repo/database/dbDotEnv";
4+
5+
const __dirname = dirname(__filename);
6+
const projectRoot = join(__dirname, "..");
7+
8+
if (process.env.HOME !== "/vercel") {
9+
try {
10+
execSync("npm run compile", { cwd: projectRoot, stdio: "inherit" });
11+
if (getVariant() === "none") {
12+
console.log("Not using the database");
13+
process.exit(0);
14+
}
15+
execSync("npm run serve", { cwd: projectRoot, stdio: "inherit" });
16+
} catch (err) {
17+
console.error(err);
18+
throw err;
19+
}
20+
}

packages/database/src/dbDotEnv.ts renamed to packages/database/src/dbDotEnv.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
11
import dotenv from "dotenv";
22
import { readFileSync, existsSync } from "fs";
33
import { join, dirname, basename } from "path";
4-
import { fileURLToPath } from "url";
54

6-
const findRoot = (): string => {
5+
const findRoot = () => {
76
let dir = __filename;
87
while (basename(dir) !== "database") {
98
dir = dirname(dir);
109
}
1110
return dir;
1211
};
1312

14-
export const getVariant = (): string | null => {
13+
export const getVariant = () => {
1514
if (process.env.HOME === "/vercel" || process.env.GITHUB_ACTIONS === "true")
16-
return null;
15+
return "implicit";
1716
const useDbArgPos = process.argv.indexOf("--use-db");
1817
const variant =
1918
(useDbArgPos > 0
2019
? process.argv[useDbArgPos + 1]
21-
: process.env["SUPABASE_USE_DB"]) || "local";
20+
: process.env["SUPABASE_USE_DB"]) || "none";
2221

23-
if (["local", "branch", "production"].indexOf(variant) === -1) {
22+
if (["local", "branch", "production", "none"].indexOf(variant) === -1) {
2423
throw new Error("Invalid variant: " + variant);
2524
}
2625
return variant;
2726
};
2827

2928
export const envFilePath = () => {
30-
const variant: string | null = getVariant();
31-
if (variant === null) return null;
29+
const variant = getVariant();
30+
if (variant === "implicit" || variant === "none") return null;
3231
const name = join(findRoot(), `.env.${variant}`);
3332
return existsSync(name) ? name : null;
3433
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export type Variant =
2+
| "none"
3+
| "local"
4+
| "branch"
5+
| "production"
6+
| "all"
7+
| "implicit";
8+
export declare const getVariant: () => Variant;
9+
export declare const envFilePath: () => string | null;
10+
export declare const envContents: () =>
11+
| EnvMap
12+
| {
13+
SUPABASE_URL: string | undefined;
14+
SUPABASE_ANON_KEY: string | undefined;
15+
NEXT_API_ROOT: string | undefined;
16+
};
17+
export declare const config: () => void;

turbo.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@
4444
"inputs": ["$TURBO_DEFAULT$", ".env*"],
4545
"outputs": [".next/**", "!.next/cache/**", "dist/**", "src/dbTypes.ts"]
4646
},
47+
"build-schema": {
48+
"dependsOn": ["^build-schema"],
49+
"inputs": ["$TURBO_DEFAULT$", ".env*"],
50+
"outputs": ["src/dbTypes.ts"]
51+
},
4752
"lint": {
4853
"dependsOn": ["^lint"]
4954
},
@@ -58,8 +63,7 @@
5863
},
5964
"deploy": {
6065
"cache": false,
61-
"inputs": ["$TURBO_DEFAULT$", ".env*"],
62-
"dependsOn": ["@repo/database#build"]
66+
"inputs": ["$TURBO_DEFAULT$", ".env*"]
6367
},
6468
"publish": {
6569
"cache": false,

0 commit comments

Comments
 (0)