feat: enable database-less mode with NEXT_PUBLIC_DATABASE_LESS

This commit is contained in:
Tianzhou Chen
2023-05-26 22:27:28 +08:00
parent 1a7c9b70d0
commit 1a57c6a899
3 changed files with 42 additions and 24 deletions

View File

@ -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

8
process.d.ts vendored
View File

@ -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;

View File

@ -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,
},
};