Multi-database service support

About 2 min...

Besides LeanCloud, Waline also supports a variety of databases, including MySQL, PostgreSQL, SQLite and MongoDB.

You only need to configure environment variables, and Waline will automatically switch to the corresponding data storage service based on the environment variables you configure.

MongoDB

https://mongodb.comopen in new window official provides 512M MongoDB database support for free. The following are the environment variables that need to be configured to use MongoDB database.

Environment VariableRequiredDefaultDescription
MONGO_HOST127.0.0.1MongoDB server address, support array format
MONGO_PORT27017MongoDB server port, support array format
MONGO_DBMongoDB database name
MONGO_USERMongoDB server username
MONGO_PASSWORDMongoDB server password
MONGO_REPLICASETMongoDB replica set
MONGO_AUTHSOURCEMongoDB auth source
MONGO_OPT_SSLuse SSL connection

Here is an example configuration for mongodb.com. Please note that you need set as JSON style for MONGO_HOST and MONGO_PORT when you has mulitple hosts.

MONGO_HOST=["cluster0-shard-00-00.p4edw.mongodb.net","cluster0-shard-00-01.p4edw.mongodb.net","cluster0-shard-00-02.p4edw.mongodb.net"]
MONGO_PORT=[27017,27017,27017,27017]
MONGO_DB=waline
MONGO_USER=admin
MONGO_PASSWORD=xxxx
MONGO_REPLICASET=atlas-12cebf-shard-0
MONGO_AUTHSOURCE=admin
MONGO_OPT_SSL=true

MySQL

Using MySQL to store data is also a good choise. Besides our own MySQL service, we can also use https://freedb.techopen in new window, which provides 100M of database support for free, or PlanetScaleopen in new window which provides 10GB.

If you want to use MySQL as storage, you need to import waline.sqlopen in new window first to create table and table structure, then set these environment variables in project.

Environment VariableRequiredDefaultDescription
MYSQL_HOST127.0.0.1MySQL server address
MYSQL_PORT3306MySQL server port
MYSQL_DBMySQL database name
MYSQL_USERMySQL server username
MYSQL_PASSWORDMySQL server password
MYSQL_PREFIXwl_MySQL table prefix
MYSQL_CHARSETutf8mb4MySQL table charset
MYSQL_SSLset to true to use SSL connection

SQLite

Download waline.sqliteopen in new window to your server if you want to use SQLite. Then set these environment variables in project.

Environment VariableRequiredDefaultDescription
SQLITE_PATHSQLite storage file path, not include file name
SQLITE_DBwalineSQLite storage file name, change it if your filenamed is not waline
SQLITE_PREFIXwl_SQLite table prefix
JWT_TOKENRandom String for login token generator

PostgreSQL

elephantSQLopen in new window provides 20M PG database support for free. Same as MySQL, you need to import waline.pgsqlopen in new window to create table and table structure before using PostgreSQL.

Environment VariableRequiredDefaultDescription
PG_HOST127.0.0.1PostgreSQL server address
PG_PORT3211PostgreSQL server port
PG_DBPostgreSQL database name
PG_USERPostgreSQL server username
PG_PASSWORDPostgreSQL server password
PG_PREFIXwl_PostgreSQL table prefix
PG_SSLset to true to use SSL connection

GitHub

Waline supports storing comment data in a CSV file on GitHub. To use GitHub as data storage, you need to apply for Personal access tokens. You can click Generate new token to apply it at https://github.com/settings/tokensopen in new window. Check the repo option in permission to obtain read and write permissions for repositories.

Environment VariableRequiredDefaultDescription
GITHUB_TOKENPersonal access tokensopen in new window
GITHUB_REPOrepository name, such as walinejs/waline
GITHUB_PATHThe data storage directory, such as data means it is stored in the data directory, root directory by default

Note

Due to preformance, using GitHub is not recommanded.

Deta Base

Deta provides Deta Baseopen in new window free database support, which can be used even if it is not deployed on Deta. If deployed on Deta, there is no need to configure any environment variables. By default, Waline will use Deta Base as the database to store data. If it is deployed elsewhere, the following environment variables need to be configured.

Environment VariableRequiredDefaultDescription
DETA_PROJECT_KEYDeta project secret key

Custom

Besides above database storage, support for other storage services can also be added.

If you want to help Waline supporting more storage services, you can fork the project and inherit the base classopen in new window and then implement the select(), add(), update() and delete() methods of the corresponding storage service and submit the PR.

TIPS: The comment area is only for demo. If you have any questions, please go to Github Discussion to ask.
Comments
Powered by Waline v2.6.1