diff --git a/README.md b/README.md index 88ad0475..281d7565 100644 --- a/README.md +++ b/README.md @@ -138,18 +138,25 @@ FLOWISE_PASSWORD=1234 Flowise support different environment variables to configure your instance. You can specify the following variables in the `.env` file inside `packages/server` folder. Read [more](https://docs.flowiseai.com/environment-variables) | Variable | Description | Type | Default | -| -------------------------- | ---------------------------------------------------------------- | ------------------------------------------------ | ----------------------------------- | +| ---------------- | ---------------------------------------------------------------- | ------------------------------------------------ | ----------------------------------- | | PORT | The HTTP port Flowise runs on | Number | 3000 | -| FLOWISE_USERNAME | Username to login | String | -| FLOWISE_PASSWORD | Password to login | String | -| DEBUG | Print logs onto terminal/console | Boolean | -| LOG_PATH | Location where log files are stored | String | `your-path/Flowise/packages/server` | -| LOG_LEVEL | Different log levels for loggers to be saved | Enum String: `error`, `info`, `verbose`, `debug` | `info` | -| DATABASE_PATH | Location where database is saved | String | `your-home-dir/.flowise` | +| FLOWISE_USERNAME | Username to login | String | | +| FLOWISE_PASSWORD | Password to login | String | | +| DEBUG | Print logs from components | Boolean | | +| LOG_PATH | Location where log files are stored | String | `your-path/Flowise/logs` | +| LOG_LEVEL | Different levels of logs | Enum String: `error`, `info`, `verbose`, `debug` | `info` | | APIKEY_PATH | Location where api keys are saved | String | `your-path/Flowise/packages/server` | | EXECUTION_MODE | Whether predictions run in their own process or the main process | Enum String: `child`, `main` | `main` | | TOOL_FUNCTION_BUILTIN_DEP | NodeJS built-in modules to be used for Tool Function | String | | | TOOL_FUNCTION_EXTERNAL_DEP | External modules to be used for Tool Function | String | | +| OVERRIDE_DATABASE | Override current database with default | Enum String: `true`, `false` | `true` | +| DATABASE_TYPE | Type of database to store the flowise data | Enum String: `sqlite`, `mysql`, `postgres` | `sqlite` | +| DATABASE_PATH | Location where database is saved (When DATABASE_TYPE is sqlite) | String | `your-home-dir/.flowise` | +| DATABASE_HOST | Host URL or IP address (When DATABASE_TYPE is not sqlite) | String | | +| DATABASE_PORT | Database port (When DATABASE_TYPE is not sqlite) | String | | +| DATABASE_USERNAME | Database username (When DATABASE_TYPE is not sqlite) | String | | +| DATABASE_PASSWORD | Database password (When DATABASE_TYPE is not sqlite) | String | | +| DATABASE_NAME | Database name (When DATABASE_TYPE is not sqlite) | String | | You can also specify the env variables when using `npx`. For example: diff --git a/packages/server/.env.example b/packages/server/.env.example index 1317dd98..923c62a0 100644 --- a/packages/server/.env.example +++ b/packages/server/.env.example @@ -2,10 +2,23 @@ PORT=3000 # FLOWISE_USERNAME=user # FLOWISE_PASSWORD=1234 # DEBUG=true -# DATABASE_PATH=/your_database_path/.flowise # APIKEY_PATH=/your_api_key_path/.flowise # LOG_PATH=/your_log_path/.flowise/logs # LOG_LEVEL=debug (error | warn | info | verbose | debug) # EXECUTION_MODE=main (child | main) + +OVERRIDE_DATABASE="true" +DATABASE_TYPE="sqlite" # sqlite, mysql, postgres + +# When database is sqlite +# DATABASE_PATH=/your_database_path/.flowise + +# When database is not sqlite +# DATABASE_PORT="" +# DATABASE_HOST="" +# DATABASE_NAME="flowise" +# DATABASE_USER="" +# DATABASE_PASSWORD="" + # TOOL_FUNCTION_BUILTIN_DEP=crypto,fs # TOOL_FUNCTION_EXTERNAL_DEP=moment,lodash diff --git a/packages/server/README.md b/packages/server/README.md index 644eed2f..e95a1643 100644 --- a/packages/server/README.md +++ b/packages/server/README.md @@ -34,18 +34,25 @@ FLOWISE_PASSWORD=1234 Flowise support different environment variables to configure your instance. You can specify the following variables in the `.env` file inside `packages/server` folder. Read [more](https://docs.flowiseai.com/environment-variables) | Variable | Description | Type | Default | -| -------------------------- | ---------------------------------------------------------------- | ------------------------------------------------ | ----------------------------------- | +| ---------------- | ---------------------------------------------------------------- | ------------------------------------------------ | ----------------------------------- | | PORT | The HTTP port Flowise runs on | Number | 3000 | -| FLOWISE_USERNAME | Username to login | String | -| FLOWISE_PASSWORD | Password to login | String | -| DEBUG | Print logs onto terminal/console | Boolean | -| LOG_PATH | Location where log files are stored | String | `your-path/Flowise/packages/server` | -| LOG_LEVEL | Different log levels for loggers to be saved | Enum String: `error`, `info`, `verbose`, `debug` | `info` | -| DATABASE_PATH | Location where database is saved | String | `your-home-dir/.flowise` | +| FLOWISE_USERNAME | Username to login | String | | +| FLOWISE_PASSWORD | Password to login | String | | +| DEBUG | Print logs from components | Boolean | | +| LOG_PATH | Location where log files are stored | String | `your-path/Flowise/logs` | +| LOG_LEVEL | Different levels of logs | Enum String: `error`, `info`, `verbose`, `debug` | `info` | | APIKEY_PATH | Location where api keys are saved | String | `your-path/Flowise/packages/server` | | EXECUTION_MODE | Whether predictions run in their own process or the main process | Enum String: `child`, `main` | `main` | | TOOL_FUNCTION_BUILTIN_DEP | NodeJS built-in modules to be used for Tool Function | String | | | TOOL_FUNCTION_EXTERNAL_DEP | External modules to be used for Tool Function | String | | +| OVERRIDE_DATABASE | Override current database with default | Enum String: `true`, `false` | `true` | +| DATABASE_TYPE | Type of database to store the flowise data | Enum String: `sqlite`, `mysql`, `postgres` | `sqlite` | +| DATABASE_PATH | Location where database is saved (When DATABASE_TYPE is sqlite) | String | `your-home-dir/.flowise` | +| DATABASE_HOST | Host URL or IP address (When DATABASE_TYPE is not sqlite) | String | | +| DATABASE_PORT | Database port (When DATABASE_TYPE is not sqlite) | String | | +| DATABASE_USERNAME | Database username (When DATABASE_TYPE is not sqlite) | String | | +| DATABASE_PASSWORD | Database password (When DATABASE_TYPE is not sqlite) | String | | +| DATABASE_NAME | Database name (When DATABASE_TYPE is not sqlite) | String | | You can also specify the env variables when using `npx`. For example: diff --git a/packages/server/package.json b/packages/server/package.json index 10b7fb3b..dbf2cc91 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -55,6 +55,8 @@ "flowise-ui": "*", "moment-timezone": "^0.5.34", "multer": "^1.4.5-lts.1", + "mysql": "^2.18.1", + "pg": "^8.11.1", "reflect-metadata": "^0.1.13", "socket.io": "^4.6.1", "sqlite3": "^5.1.6", diff --git a/packages/server/src/ChildProcess.ts b/packages/server/src/ChildProcess.ts index 27629480..a93906c0 100644 --- a/packages/server/src/ChildProcess.ts +++ b/packages/server/src/ChildProcess.ts @@ -142,14 +142,58 @@ export class ChildProcess { * @returns {DataSource} */ async function initDB() { - const homePath = process.env.DATABASE_PATH ?? path.join(getUserHome(), '.flowise') - const childAppDataSource = new DataSource({ - type: 'sqlite', - database: path.resolve(homePath, 'database.sqlite'), - synchronize: true, - entities: [ChatFlow, ChatMessage, Tool], - migrations: [] - }) + let childAppDataSource + let homePath + switch (process.env.DATABASE_TYPE) { + case 'sqlite': + homePath = process.env.DATABASE_PATH ?? path.join(getUserHome(), '.flowise') + childAppDataSource = new DataSource({ + type: 'sqlite', + database: path.resolve(homePath, 'database.sqlite'), + synchronize: process.env.OVERRIDE_DATABASE == 'true', + entities: [ChatFlow, ChatMessage, Tool], + migrations: [] + }) + break + case 'mysql': + childAppDataSource = new DataSource({ + type: 'mysql', + host: process.env.DATABASE_HOST, + port: parseInt(process.env.DATABASE_PORT || '3306'), + username: process.env.DATABASE_USER, + password: process.env.DATABASE_PASSWORD, + database: process.env.DATABASE_NAME, + charset: 'utf8mb4', + synchronize: process.env.OVERRIDE_DATABASE == 'true', + entities: [ChatFlow, ChatMessage, Tool], + migrations: [] + }) + break + case 'postgres': + childAppDataSource = new DataSource({ + type: 'postgres', + host: process.env.DATABASE_HOST, + port: parseInt(process.env.DATABASE_PORT || '5432'), + username: process.env.DATABASE_USER, + password: process.env.DATABASE_PASSWORD, + database: process.env.DATABASE_NAME, + synchronize: process.env.OVERRIDE_DATABASE == 'true', + entities: [ChatFlow, ChatMessage, Tool], + migrations: [] + }) + break + default: + homePath = process.env.DATABASE_PATH ?? path.join(getUserHome(), '.flowise') + childAppDataSource = new DataSource({ + type: 'sqlite', + database: path.resolve(homePath, 'database.sqlite'), + synchronize: process.env.OVERRIDE_DATABASE == 'true', + entities: [ChatFlow, ChatMessage, Tool], + migrations: [] + }) + break + } + return await childAppDataSource.initialize() } diff --git a/packages/server/src/DataSource.ts b/packages/server/src/DataSource.ts index 03b9d5ce..958074cc 100644 --- a/packages/server/src/DataSource.ts +++ b/packages/server/src/DataSource.ts @@ -9,15 +9,56 @@ import { getUserHome } from './utils' let appDataSource: DataSource export const init = async (): Promise => { - const homePath = process.env.DATABASE_PATH ?? path.join(getUserHome(), '.flowise') - - appDataSource = new DataSource({ - type: 'sqlite', - database: path.resolve(homePath, 'database.sqlite'), - synchronize: true, - entities: [ChatFlow, ChatMessage, Tool], - migrations: [] - }) + let homePath + switch (process.env.DATABASE_TYPE) { + case 'sqlite': + homePath = process.env.DATABASE_PATH ?? path.join(getUserHome(), '.flowise') + appDataSource = new DataSource({ + type: 'sqlite', + database: path.resolve(homePath, 'database.sqlite'), + synchronize: process.env.OVERRIDE_DATABASE == 'true', + entities: [ChatFlow, ChatMessage, Tool], + migrations: [] + }) + break + case 'mysql': + appDataSource = new DataSource({ + type: 'mysql', + host: process.env.DATABASE_HOST, + port: parseInt(process.env.DATABASE_PORT || '3306'), + username: process.env.DATABASE_USER, + password: process.env.DATABASE_PASSWORD, + database: process.env.DATABASE_NAME, + charset: 'utf8mb4', + synchronize: process.env.OVERRIDE_DATABASE == 'true', + entities: [ChatFlow, ChatMessage, Tool], + migrations: [] + }) + break + case 'postgres': + appDataSource = new DataSource({ + type: 'postgres', + host: process.env.DATABASE_HOST, + port: parseInt(process.env.DATABASE_PORT || '5432'), + username: process.env.DATABASE_USER, + password: process.env.DATABASE_PASSWORD, + database: process.env.DATABASE_NAME, + synchronize: process.env.OVERRIDE_DATABASE == 'true', + entities: [ChatFlow, ChatMessage, Tool], + migrations: [] + }) + break + default: + homePath = process.env.DATABASE_PATH ?? path.join(getUserHome(), '.flowise') + appDataSource = new DataSource({ + type: 'sqlite', + database: path.resolve(homePath, 'database.sqlite'), + synchronize: process.env.OVERRIDE_DATABASE == 'true', + entities: [ChatFlow, ChatMessage, Tool], + migrations: [] + }) + break + } } export function getDataSource(): DataSource { diff --git a/packages/server/src/entity/ChatFlow.ts b/packages/server/src/entity/ChatFlow.ts index e1d212cf..adaf0917 100644 --- a/packages/server/src/entity/ChatFlow.ts +++ b/packages/server/src/entity/ChatFlow.ts @@ -10,7 +10,7 @@ export class ChatFlow implements IChatFlow { @Column() name: string - @Column() + @Column({ type: "text" }) flowData: string @Column({ nullable: true }) diff --git a/packages/server/src/entity/ChatMessage.ts b/packages/server/src/entity/ChatMessage.ts index 3e4e41d2..46972f6a 100644 --- a/packages/server/src/entity/ChatMessage.ts +++ b/packages/server/src/entity/ChatMessage.ts @@ -14,7 +14,7 @@ export class ChatMessage implements IChatMessage { @Column() chatflowid: string - @Column() + @Column({ type: "text" }) content: string @Column({ nullable: true }) diff --git a/packages/server/src/entity/Tool.ts b/packages/server/src/entity/Tool.ts index 222fd766..4fc89519 100644 --- a/packages/server/src/entity/Tool.ts +++ b/packages/server/src/entity/Tool.ts @@ -10,7 +10,7 @@ export class Tool implements ITool { @Column() name: string - @Column() + @Column({ type: "text" }) description: string @Column()