From 1a57c6a899fdb36e3ff5a2f3273dba4eaba22b62 Mon Sep 17 00:00:00 2001 From: Tianzhou Chen Date: Fri, 26 May 2023 22:27:28 +0800 Subject: [PATCH] feat: enable database-less mode with NEXT_PUBLIC_DATABASE_LESS --- README.md | 42 ++++++++++++++++++++++++++++-------------- process.d.ts | 8 ++++++-- src/utils/feature.ts | 16 ++++++++-------- 3 files changed, 42 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 33c9bc3..8ce45bd 100644 --- a/README.md +++ b/README.md @@ -47,32 +47,52 @@ to the database whitelist IP. Because sqlchat.AI is hosted on [Vercel](https://v docker run --name sqlchat --platform linux/amd64 -p 3000:3000 sqlchat/sqlchat ``` -Required environment variables: -- `DATABASE_URL`: Postgres connection string to store data. e.g. `postgresql://postgres:YOUR_PASSWORD@localhost:5432/sqlchat?schema=sqlchat`,[explicate](https://www.prisma.io/docs/concepts/database-connectors/postgresql). +### OpenAI related variables: + - `OPENAI_API_KEY`: OpenAI API key. You can get one from [here](https://beta.openai.com/docs/developer-quickstart/api-keys). -Optional environment variables: - - `OPENAI_API_ENDPOINT`: OpenAI API endpoint. Defaults to `https://api.openai.com`. + +### Database related variables: + +- `NEXT_PUBLIC_DATABASE_LESS`: Set to true to start SQL Chat in database-less mode. This will +disable following features: + 1. Account system. + 1. Per-user quota enforcement. + 1. Payment. + 1. Usage data collection. +- `DATABASE_URL`: Postgres connection string to store data. e.g. `postgresql://postgres:YOUR_PASSWORD@localhost:5432/sqlchat?schema=sqlchat`. + ```bash docker run --name sqlchat --platform linux/amd64 --env OPENAI_API_KEY=xxx --env OPENAI_API_ENDPOINT=yyy -p 3000:3000 sqlchat/sqlchat ``` ## Local Development +1. Install dependencies + + ```bash + pnpm i + ``` + 1. Make a copy of the example environment variables file: ```bash cp .env.example .env ``` -2. Add your [API key](https://platform.openai.com/account/api-keys) and OpenAI API Endpoint(optional) to the newly created `.env` file. +1. Add your [API key](https://platform.openai.com/account/api-keys) and OpenAI API Endpoint(optional) to the newly created `.env` file. -3. Start a Postgres instance. For mac, you can use [StackbBricks](https://stackbricks.app/), [DBngin](https://dbngin.com/) or [Postgres.app](https://postgresapp.com/). -4. Create a database: +### Setup database + +**You can skip this section with `NEXT_PUBLIC_DATABASE_LESS=true` if you don't build features requiring database** + +1. Start a Postgres instance. For mac, you can use [StackbBricks](https://stackbricks.app/), [DBngin](https://dbngin.com/) or [Postgres.app](https://postgresapp.com/). + +1. Create a database: ```sql CREATE DATABASE sqlchat; @@ -80,13 +100,7 @@ docker run --name sqlchat --platform linux/amd64 --env OPENAI_API_KEY=xxx --env In `.env` file, assign the connection string to environment variable `DATABASE_URL`. -5. Install dependencies - - ```bash - pnpm i - ``` - -6. Generate schema +1. Generate schema 1. Generate prisma client from the model diff --git a/process.d.ts b/process.d.ts index c0a5366..7d75ecf 100644 --- a/process.d.ts +++ b/process.d.ts @@ -2,8 +2,12 @@ declare namespace NodeJS { export interface ProcessEnv { // Required. Node environment. NODE_ENV: string; - // Required. Postgres database connection string to store the data. - // e.g. postgresql://postgres:YOUR_PASSWORD@localhost:5432/sqlchat?schema=sqlchat + // Optional. Set to true to disable the database. Defaults to false. + // We can't prefix DATABASE_URL with NEXT_PUBLIC_ because it contains sensitive information that + // should not be exposed to the client. + NEXT_PUBLIC_DATABASE_LESS: boolean; + // Required if NEXT_PUBLIC_DATABASE_LESS is false. Postgres database connection string to store + // the data. e.g. postgresql://postgres:YOUR_PASSWORD@localhost:5432/sqlchat?schema=sqlchat DATABASE_URL: string; // Required. Do not share your OpenAI API key with anyone! It should remain a secret. OPENAI_API_KEY: string; diff --git a/src/utils/feature.ts b/src/utils/feature.ts index 2acd2ba..ff5dcbe 100644 --- a/src/utils/feature.ts +++ b/src/utils/feature.ts @@ -3,17 +3,17 @@ type FeatureType = "debug" | "account" | "payment" | "quota" | "collect"; const matrix: { [key: string]: { [feature: string]: boolean } } = { development: { debug: true, - account: true, - payment: true, - quota: true, - collect: true, + account: !process.env.NEXT_PUBLIC_DATABASE_LESS, + payment: !process.env.NEXT_PUBLIC_DATABASE_LESS, + quota: !process.env.NEXT_PUBLIC_DATABASE_LESS, + collect: !process.env.NEXT_PUBLIC_DATABASE_LESS, }, production: { debug: false, - account: true, - payment: true, - quota: true, - collect: true, + account: !process.env.NEXT_PUBLIC_DATABASE_LESS, + payment: !process.env.NEXT_PUBLIC_DATABASE_LESS, + quota: !process.env.NEXT_PUBLIC_DATABASE_LESS, + collect: !process.env.NEXT_PUBLIC_DATABASE_LESS, }, };