diff --git a/.env.example b/.env.example index 6c2dfde8c..b4e7d3ed4 100644 --- a/.env.example +++ b/.env.example @@ -1,6 +1,10 @@ # [Recommended] Important application information NEXT_PUBLIC_PRIVACY_POLICY_URL= +# [Recommended, optional] Postgres Database (for server-side storage, see prisma/schema.prisma) +POSTGRES_PRISMA_URL= +POSTGRES_URL_NON_POOLING= + # [Recommended for local deployments] Backend API key for OpenAI, so that users don't need one (UI > this > '') OPENAI_API_KEY= # [Optional] Sets the "OpenAI-Organization" header field to support organization users (UI > this > '') diff --git a/package-lock.json b/package-lock.json index c9547f1eb..1d212ec3e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,6 +7,7 @@ "": { "name": "big-agi", "version": "1.3.5", + "hasInstallScript": true, "dependencies": { "@dqbd/tiktoken": "^1.0.7", "@emotion/cache": "^11.11.0", @@ -16,6 +17,7 @@ "@mui/icons-material": "^5.14.3", "@mui/joy": "^5.0.0-beta.2", "@next/bundle-analyzer": "^13.4.19", + "@prisma/client": "^5.4.2", "@tanstack/react-query": "^4.33.0", "@trpc/client": "^10.37.1", "@trpc/next": "^10.37.1", @@ -51,6 +53,7 @@ "eslint": "^8.47.0", "eslint-config-next": "^13.4.19", "prettier": "^3.0.2", + "prisma": "^5.4.2", "typescript": "^5.1.6" }, "engines": { @@ -1026,6 +1029,38 @@ "url": "https://opencollective.com/popperjs" } }, + "node_modules/@prisma/client": { + "version": "5.4.2", + "resolved": "https://registry.npmjs.org/@prisma/client/-/client-5.4.2.tgz", + "integrity": "sha512-2xsPaz4EaMKj1WS9iW6MlPhmbqtBsXAOeVttSePp8vTFTtvzh2hZbDgswwBdSCgPzmmwF+tLB259QzggvCmJqA==", + "hasInstallScript": true, + "dependencies": { + "@prisma/engines-version": "5.4.1-2.ac9d7041ed77bcc8a8dbd2ab6616b39013829574" + }, + "engines": { + "node": ">=16.13" + }, + "peerDependencies": { + "prisma": "*" + }, + "peerDependenciesMeta": { + "prisma": { + "optional": true + } + } + }, + "node_modules/@prisma/engines": { + "version": "5.4.2", + "resolved": "https://registry.npmjs.org/@prisma/engines/-/engines-5.4.2.tgz", + "integrity": "sha512-fqeucJ3LH0e1eyFdT0zRx+oETLancu5+n4lhiYECyEz6H2RDskPJHJYHkVc0LhkU4Uv7fuEnppKU3nVKNzMh8g==", + "devOptional": true, + "hasInstallScript": true + }, + "node_modules/@prisma/engines-version": { + "version": "5.4.1-2.ac9d7041ed77bcc8a8dbd2ab6616b39013829574", + "resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-5.4.1-2.ac9d7041ed77bcc8a8dbd2ab6616b39013829574.tgz", + "integrity": "sha512-wvupDL4AA1vf4TQNANg7kR7y98ITqPsk6aacfBxZKtrJKRIsWjURHkZCGcQliHdqCiW/hGreO6d6ZuSv9MhdAA==" + }, "node_modules/@rushstack/eslint-patch": { "version": "1.3.3", "resolved": "https://registry.npmjs.org/@rushstack/eslint-patch/-/eslint-patch-1.3.3.tgz", @@ -5166,6 +5201,22 @@ "url": "https://github.com/prettier/prettier?sponsor=1" } }, + "node_modules/prisma": { + "version": "5.4.2", + "resolved": "https://registry.npmjs.org/prisma/-/prisma-5.4.2.tgz", + "integrity": "sha512-GDMZwZy7mysB2oXU+angQqJ90iaPFdD0rHaZNkn+dio5NRkGLmMqmXs31//tg/qXT3iB0cTQwnGGQNuirhSTZg==", + "devOptional": true, + "hasInstallScript": true, + "dependencies": { + "@prisma/engines": "5.4.2" + }, + "bin": { + "prisma": "build/index.js" + }, + "engines": { + "node": ">=16.13" + } + }, "node_modules/prismjs": { "version": "1.29.0", "resolved": "https://registry.npmjs.org/prismjs/-/prismjs-1.29.0.tgz", diff --git a/package.json b/package.json index 1c89dd151..863201def 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,11 @@ "dev": "next dev", "build": "next build", "start": "next start", - "lint": "next lint" + "lint": "next lint", + "env:pull": "npx vercel env pull .env.development.local", + "postinstall": "prisma generate", + "db:push": "prisma db push", + "db:studio": "prisma studio" }, "dependencies": { "@dqbd/tiktoken": "^1.0.7", @@ -20,6 +24,7 @@ "@mui/icons-material": "^5.14.3", "@mui/joy": "^5.0.0-beta.2", "@next/bundle-analyzer": "^13.4.19", + "@prisma/client": "^5.4.2", "@tanstack/react-query": "^4.33.0", "@trpc/client": "^10.37.1", "@trpc/next": "^10.37.1", @@ -55,6 +60,7 @@ "eslint": "^8.47.0", "eslint-config-next": "^13.4.19", "prettier": "^3.0.2", + "prisma": "^5.4.2", "typescript": "^5.1.6" } } diff --git a/prisma/schema.prisma b/prisma/schema.prisma new file mode 100644 index 000000000..892e39128 --- /dev/null +++ b/prisma/schema.prisma @@ -0,0 +1,26 @@ +// Prisma is the ORM for server-side (API) access to the database +// +// This file defines the schema for the database. +// - make sure to run 'prisma generate' after making changes to this file +// - make sure to run 'prisma db push' to sync the remote database with the schema +// +// Database is optional: when the environment variables are not set, the database is not used at all, +// and the storage of data in Big-AGI is limited to client-side (browser) storage. +// +// The database is used for: +// - the 'sharing' function, to let users share the chats with each other + +generator client { + provider = "prisma-client-js" +} + +datasource db { + provider = "postgresql" + url = env("POSTGRES_PRISMA_URL") // uses connection pooling + directUrl = env("POSTGRES_URL_NON_POOLING") // uses a direct connection +} + +model Sharing { + id String @id @default(uuid()) + // TODO: to be defined later +} diff --git a/src/common/types/env.d.ts b/src/common/types/env.d.ts index 33cba0add..d1bf77a27 100644 --- a/src/common/types/env.d.ts +++ b/src/common/types/env.d.ts @@ -5,6 +5,10 @@ declare namespace NodeJS { // available to the server-side interface ProcessEnv { + // Postgres, for optional storage via Prisma + POSTGRES_PRISMA_URL?: string; + POSTGRES_URL_NON_POOLING?: string; + // LLM: OpenAI OPENAI_API_KEY: string; OPENAI_API_ORG_ID: string;