Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion packages/database/README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
19 changes: 12 additions & 7 deletions packages/database/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
Expand All @@ -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",
Expand All @@ -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": "*",
Expand Down
6 changes: 5 additions & 1 deletion packages/database/scripts/build.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
20 changes: 20 additions & 0 deletions packages/database/scripts/dev.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
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);
}
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;
};
Expand Down
17 changes: 17 additions & 0 deletions packages/database/types/dbDotEnv.d.ts
Original file line number Diff line number Diff line change
@@ -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;
8 changes: 6 additions & 2 deletions turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
},
Expand All @@ -58,8 +63,7 @@
},
"deploy": {
"cache": false,
"inputs": ["$TURBO_DEFAULT$", ".env*"],
"dependsOn": ["@repo/database#build"]
"inputs": ["$TURBO_DEFAULT$", ".env*"]
},
"publish": {
"cache": false,
Expand Down