diff --git a/backend/package.json b/backend/package.json index 81ca6ba02d..e4c81aeb0c 100644 --- a/backend/package.json +++ b/backend/package.json @@ -14,9 +14,7 @@ "dev": "nodemon --exec bun run -r tsconfig-paths/register src/index.ts", "test": "bun test", "test:manifold": "bun test src/__tests__/manifold-project.test.ts", - "test:watch": "bun test --watch", - "generate": "drizzle-kit generate", - "migrate": "drizzle-kit migrate" + "test:watch": "bun test --watch" }, "dependencies": { "@anthropic-ai/sdk": "0.27.1", diff --git a/backend/src/db/db.ts b/backend/src/db/db.ts deleted file mode 100644 index e8fc7a70d0..0000000000 --- a/backend/src/db/db.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { drizzle } from 'drizzle-orm/postgres-js' -import postgres from 'postgres' -import * as schema from './schema' - -const queryClient = postgres(process.env.DATABASE_URL!) - -export const db = drizzle(queryClient, { schema }) diff --git a/backend/src/db/drizzle/0000_last_night_thrasher.sql b/backend/src/db/drizzle/0000_last_night_thrasher.sql deleted file mode 100644 index e5b9d81b57..0000000000 --- a/backend/src/db/drizzle/0000_last_night_thrasher.sql +++ /dev/null @@ -1,6 +0,0 @@ -CREATE TABLE IF NOT EXISTS "users" ( - "id" serial PRIMARY KEY NOT NULL, - "email" varchar(255) NOT NULL, - "created_at" timestamp DEFAULT now() NOT NULL, - CONSTRAINT "users_email_unique" UNIQUE("email") -); diff --git a/backend/src/db/drizzle/meta/0000_snapshot.json b/backend/src/db/drizzle/meta/0000_snapshot.json deleted file mode 100644 index f454eea6a1..0000000000 --- a/backend/src/db/drizzle/meta/0000_snapshot.json +++ /dev/null @@ -1,53 +0,0 @@ -{ - "id": "8705a464-18c9-4c35-8ab2-e055791b8cc9", - "prevId": "00000000-0000-0000-0000-000000000000", - "version": "7", - "dialect": "postgresql", - "tables": { - "public.users": { - "name": "users", - "schema": "", - "columns": { - "id": { - "name": "id", - "type": "serial", - "primaryKey": true, - "notNull": true - }, - "email": { - "name": "email", - "type": "varchar(255)", - "primaryKey": false, - "notNull": true - }, - "created_at": { - "name": "created_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - } - }, - "indexes": {}, - "foreignKeys": {}, - "compositePrimaryKeys": {}, - "uniqueConstraints": { - "users_email_unique": { - "name": "users_email_unique", - "nullsNotDistinct": false, - "columns": [ - "email" - ] - } - } - } - }, - "enums": {}, - "schemas": {}, - "sequences": {}, - "_meta": { - "columns": {}, - "schemas": {}, - "tables": {} - } -} \ No newline at end of file diff --git a/backend/src/db/drizzle/meta/_journal.json b/backend/src/db/drizzle/meta/_journal.json deleted file mode 100644 index cd87216465..0000000000 --- a/backend/src/db/drizzle/meta/_journal.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "version": "7", - "dialect": "postgresql", - "entries": [ - { - "idx": 0, - "version": "7", - "when": 1726684376947, - "tag": "0000_last_night_thrasher", - "breakpoints": true - } - ] -} \ No newline at end of file diff --git a/backend/src/db/mac-cloud-sql-proxy b/backend/src/db/mac-cloud-sql-proxy deleted file mode 100755 index eb166c7008..0000000000 Binary files a/backend/src/db/mac-cloud-sql-proxy and /dev/null differ diff --git a/backend/src/db/migrate.ts b/backend/src/db/migrate.ts deleted file mode 100644 index 5fa473405b..0000000000 --- a/backend/src/db/migrate.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { migrate } from 'drizzle-orm/postgres-js/migrator' -import { drizzle } from 'drizzle-orm/postgres-js' -import postgres from 'postgres' -import * as schema from './schema' - -async function main() { - const connectionString = process.env.DATABASE_URL! - const client = postgres(connectionString) - const db = drizzle(client, { schema }) - - console.log('Running migrations...') - - await migrate(db, { migrationsFolder: './drizzle' }) - - console.log('Migrations complete!') - - await client.end() -} - -main().catch((err) => { - console.error('Migration failed!', err) - process.exit(1) -}) diff --git a/backend/src/db/schema.ts b/backend/src/db/schema.ts deleted file mode 100644 index afd30c5d74..0000000000 --- a/backend/src/db/schema.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { pgTable, serial, varchar, timestamp } from 'drizzle-orm/pg-core' - -export const users = pgTable('users', { - id: serial('id').primaryKey(), - email: varchar('email', { length: 255 }).notNull().unique(), - createdAt: timestamp('created_at').notNull().defaultNow(), -}) diff --git a/bun.lockb b/bun.lockb index cc3166c2eb..b91c2bd392 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/common/package.json b/common/package.json index 9ef0f7372d..d4ab2ca0ab 100644 --- a/common/package.json +++ b/common/package.json @@ -6,7 +6,10 @@ "license": "UNLICENSED", "scripts": { "build": "tsc && cp -R src/codemap/tree-sitter-queries dist/codemap/", - "clean": "rm -rf dist && rm tsconfig.tsbuildinfo" + "clean": "rm -rf dist && rm tsconfig.tsbuildinfo", + "db:generate": "drizzle-kit generate --config=./src/db/drizzle.config.ts", + "db:migrate": "drizzle-kit push --config=./src/db/drizzle.config.ts", + "db:start": "docker compose -f ./src/db/docker-compose.yml up -d --wait && bun run db:generate && bun run db:migrate" }, "dependencies": { "ignore": "5.3.2", @@ -23,9 +26,17 @@ "tree-sitter-ruby": "0.23.0", "tree-sitter-rust": "0.23.0", "tree-sitter-typescript": "0.23.0", - "zod": "3.23.8" + "zod": "3.23.8", + "@auth/drizzle-adapter": "^1.5.0", + "@types/pg": "^8.11.10", + "dotenv": "^16.4.5", + "drizzle-orm": "^0.33.0", + "next": "14.2.11", + "next-auth": "^4.24.7", + "pg": "^8.13.0" }, "peerDependencies": { + "drizzle-kit": "^0.24.2", "@types/node": "*", "@types/lodash": "*", "typescript": "*" diff --git a/common/src/db/docker-compose.yml b/common/src/db/docker-compose.yml new file mode 100644 index 0000000000..8eea0a5abb --- /dev/null +++ b/common/src/db/docker-compose.yml @@ -0,0 +1,16 @@ +name: manicode +services: + db: + image: postgres:16 + restart: always + ports: + - 5432:5432 + environment: + POSTGRES_USER: manicode_user_local + POSTGRES_PASSWORD: secretpassword_local + POSTGRES_DB: manicode_db_local + volumes: + - db_data:/var/lib/postgresql/data +volumes: + # Store data from Postgres so they persist between Docker ups and downs + db_data: diff --git a/common/src/db/drizzle.config.ts b/common/src/db/drizzle.config.ts new file mode 100644 index 0000000000..46794e1f44 --- /dev/null +++ b/common/src/db/drizzle.config.ts @@ -0,0 +1,12 @@ +import { defineConfig } from 'drizzle-kit' +import { env } from './env.mjs' +import path from 'path' + +export default defineConfig({ + dialect: 'postgresql', + schema: path.join(__dirname, 'schema.ts'), + out: path.join(__dirname, './migrations'), + dbCredentials: { + url: env.DATABASE_URL, + }, +}) diff --git a/common/src/db/env.mjs b/common/src/db/env.mjs new file mode 100644 index 0000000000..b3ee173f75 --- /dev/null +++ b/common/src/db/env.mjs @@ -0,0 +1,22 @@ +import { createEnv } from '@t3-oss/env-core' +import { z } from 'zod' +import dotenv from 'dotenv' + +dotenv.config({ path: '../stack.env' }) +if (!process.env.ENVIRONMENT) { + console.error('ENVIRONMENT is not set, please check `stack.env`') + process.exit(1) +} + +const DOTENV_PATH = process.env.ENVIRONMENT === 'local' ? '..' : '/etc/secrets' +const path = `${DOTENV_PATH}/.env.${process.env.ENVIRONMENT}` +console.log(`Using environment: ${process.env.ENVIRONMENT} (path: ${path})`) +dotenv.config({ path }) + +export const env = createEnv({ + server: { + ENVIRONMENT: z.string().min(1), + DATABASE_URL: z.string().min(1), + }, + runtimeEnv: process.env, +}) diff --git a/common/src/db/index.ts b/common/src/db/index.ts new file mode 100644 index 0000000000..f5a932bd6c --- /dev/null +++ b/common/src/db/index.ts @@ -0,0 +1,11 @@ +import { drizzle } from 'drizzle-orm/node-postgres' +import { Pool } from 'pg' +import { env } from './env.mjs' +import * as schema from './schema' + +const pool = new Pool({ + connectionString: env.DATABASE_URL, +}) + +const db = drizzle(pool, { schema }) +export default db diff --git a/web/db/migrations/0000_funny_red_shift.sql b/common/src/db/migrations/0000_overjoyed_thing.sql similarity index 100% rename from web/db/migrations/0000_funny_red_shift.sql rename to common/src/db/migrations/0000_overjoyed_thing.sql diff --git a/web/db/migrations/meta/0000_snapshot.json b/common/src/db/migrations/meta/0000_snapshot.json similarity index 99% rename from web/db/migrations/meta/0000_snapshot.json rename to common/src/db/migrations/meta/0000_snapshot.json index 76ca27df8a..227892ae0b 100644 --- a/web/db/migrations/meta/0000_snapshot.json +++ b/common/src/db/migrations/meta/0000_snapshot.json @@ -1,5 +1,5 @@ { - "id": "4aec91d9-0902-42e5-97d4-eddf8a1c5f33", + "id": "fc85928a-5e41-46ac-beea-97292cd8f676", "prevId": "00000000-0000-0000-0000-000000000000", "version": "7", "dialect": "postgresql", diff --git a/web/db/migrations/meta/_journal.json b/common/src/db/migrations/meta/_journal.json similarity index 68% rename from web/db/migrations/meta/_journal.json rename to common/src/db/migrations/meta/_journal.json index b9e8fd2725..29cd08a450 100644 --- a/web/db/migrations/meta/_journal.json +++ b/common/src/db/migrations/meta/_journal.json @@ -5,8 +5,8 @@ { "idx": 0, "version": "7", - "when": 1726639583528, - "tag": "0000_funny_red_shift", + "when": 1726794860996, + "tag": "0000_overjoyed_thing", "breakpoints": true } ] diff --git a/web/db/schema.ts b/common/src/db/schema.ts similarity index 87% rename from web/db/schema.ts rename to common/src/db/schema.ts index 0679bee703..923a909b6b 100644 --- a/web/db/schema.ts +++ b/common/src/db/schema.ts @@ -6,8 +6,8 @@ import { integer, uuid, boolean, -} from 'drizzle-orm/pg-core'; -import type { AdapterAccount } from 'next-auth/adapters'; +} from 'drizzle-orm/pg-core' +import type { AdapterAccount } from 'next-auth/adapters' export const users = pgTable('user', { id: text('id') @@ -20,7 +20,7 @@ export const users = pgTable('user', { image: text('image'), isActive: boolean('isActive').notNull().default(false), stripeCustomerId: text('stripeCustomerId').unique(), -}); +}) export const accounts = pgTable( 'account', @@ -44,7 +44,7 @@ export const accounts = pgTable( columns: [account.provider, account.providerAccountId], }), }) -); +) export const sessions = pgTable('session', { sessionToken: text('sessionToken').notNull().primaryKey(), @@ -52,7 +52,7 @@ export const sessions = pgTable('session', { .notNull() .references(() => users.id, { onDelete: 'cascade' }), expires: timestamp('expires', { mode: 'date' }).notNull(), -}); +}) export const verificationTokens = pgTable( 'verificationToken', @@ -64,14 +64,4 @@ export const verificationTokens = pgTable( (vt) => ({ compoundKey: primaryKey({ columns: [vt.identifier, vt.token] }), }) -); - -// const models = { -// users, -// accounts, -// sessions, -// verificationTokens, -// }; -// type models = typeof models; - -// export default models; +) diff --git a/common/tsconfig.json b/common/tsconfig.json index d3bcdd3c94..4069e8142b 100644 --- a/common/tsconfig.json +++ b/common/tsconfig.json @@ -1,6 +1,7 @@ { "compilerOptions": { "composite": true, + "allowJs": true, "module": "CommonJS", "moduleResolution": "node", "esModuleInterop": true, diff --git a/package.json b/package.json index 45ec0029ce..1e8432d1ae 100644 --- a/package.json +++ b/package.json @@ -6,12 +6,14 @@ "workspaces": [ "common", "backend", - "npm-app" + "npm-app", + "web" ], "scripts": { "start-client": "bun --cwd npm-app start", - "start-web": "bun --cwd web dev", - "start-server": "bun --cwd backend start", + "start-db": "bun --cwd common db:start", + "start-web": "bun run start-db && bun --cwd web dev", + "start-server": "bun run start-db && bun --cwd backend start", "start-manifold": "bun --cwd npm-app start-manifold", "start-litestar": "bun --cwd npm-app start-litestar", "start-jpcsp": "bun --cwd npm-app start-jpcsp", diff --git a/test/__src__/setup.ts b/test/__src__/setup.ts index 82b623e382..4873e795f4 100644 --- a/test/__src__/setup.ts +++ b/test/__src__/setup.ts @@ -1,10 +1,10 @@ import { beforeAll } from 'bun:test' -import dotenv from 'dotenv' import path from 'path' +// import dotenv from 'dotenv' import { env } from './env.mjs' beforeAll(() => { - dotenv.config({ - processEnv: env, - }) + // dotenv.config({ + // processEnv: env, + // }) }) diff --git a/web/.vscode/extensions.json b/web/.vscode/extensions.json deleted file mode 100644 index 116d6852a6..0000000000 --- a/web/.vscode/extensions.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "recommendations": [ - "inlang.vs-code-extension" - ] -} \ No newline at end of file diff --git a/web/db/drizzle.config.ts b/web/db/drizzle.config.ts deleted file mode 100644 index 32404839ba..0000000000 --- a/web/db/drizzle.config.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { defineConfig } from 'drizzle-kit'; -import { env } from '@/env.mjs'; - -export default defineConfig({ - dialect: 'postgresql', - schema: './db/schema.ts', - out: './db/migrations', - dbCredentials: { - url: env.DATABASE_URL, - }, -}); diff --git a/web/db/index.ts b/web/db/index.ts deleted file mode 100644 index 74d7984b2a..0000000000 --- a/web/db/index.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { drizzle } from 'drizzle-orm/node-postgres'; -import { Pool } from 'pg'; -import { env } from '@/env.mjs'; -import * as models from './schema'; - -const pool = new Pool({ - connectionString: env.DATABASE_URL, -}); - -export const db = drizzle(pool, { schema: models }); diff --git a/web/package.json b/web/package.json index 0de571e356..979e28bb09 100644 --- a/web/package.json +++ b/web/package.json @@ -3,10 +3,8 @@ "version": "1.0.0", "private": true, "scripts": { - "dev": "next dev --turbo", + "dev": "next dev", "build": "next build", - "db:generate": "drizzle-kit generate --config=./db/drizzle.config.ts", - "db:migrate": "bun run drizzle-kit push --config=./db/drizzle.config.ts", "start": "next start", "preview": "next build && next start", "lint": "next lint", diff --git a/web/src/app/api/auth/[...nextauth]/auth-options.ts b/web/src/app/api/auth/[...nextauth]/auth-options.ts index 24bf79a71a..cc56569608 100644 --- a/web/src/app/api/auth/[...nextauth]/auth-options.ts +++ b/web/src/app/api/auth/[...nextauth]/auth-options.ts @@ -4,8 +4,8 @@ import { DrizzleAdapter } from '@auth/drizzle-adapter'; import { env } from '@/env.mjs'; import { stripeServer } from '@/lib/stripe'; -import { db } from '@@/db'; -import * as models from '@@/db/schema'; +import db from 'common/src/db'; +import * as models from 'common/db/schema'; import { eq } from 'drizzle-orm'; import { Adapter } from 'next-auth/adapters'; diff --git a/web/src/app/api/stripe/webhook/route.ts b/web/src/app/api/stripe/webhook/route.ts index 0347cc6d6b..779c99fd6e 100644 --- a/web/src/app/api/stripe/webhook/route.ts +++ b/web/src/app/api/stripe/webhook/route.ts @@ -4,8 +4,8 @@ import { eq } from 'drizzle-orm'; import { env } from '@/env.mjs'; import { stripeServer } from '@/lib/stripe'; -import { db } from '@@/db'; -import * as models from '@@/db/schema'; +import db from 'common/db'; +import * as models from 'common/db/schema'; const webhookHandler = async (req: NextRequest) => { try { diff --git a/web/tsconfig.json b/web/tsconfig.json index 47ea1752ff..92bc7b2eaf 100644 --- a/web/tsconfig.json +++ b/web/tsconfig.json @@ -21,9 +21,13 @@ ], "paths": { "@/*": ["./src/*"], - "@@/*": ["./*"] + "common/*": ["../common/src/*"] } }, + "references": [{ "path": "../common" }], "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"], - "exclude": ["node_modules"] + "exclude": ["node_modules"], + "ts-node": { + "require": ["tsconfig-paths/register"] + } }