Currently, Cotton supports SQLite3, MySQL, and PostgreSQL. Cotton provides a connect
function which allows you to create a new connection to any supported database.
const db = await connect({
type: "sqlite",
database: "./db.sqlite3",
});
The type
option is required, which determine what type of database you're trying to connect. Then, you can pass the other configurations such as database
, port
, hostname
, username
, and password
.
The only configuration that the SQLite adapter care is the database
, which is a path to your database file.
const db = await connect({
type: "sqlite",
database: "./db.sqlite3",
});
Or, you can pass :memory:
if you just want to store it in memory.
const db = await connect({
type: "sqlite",
database: ":memory:",
});
Connecting to MySQL and PostgreSQL is pretty straight forward.
MySQL example:
const db = await connect({
type: "mysql",
port: 5432,
database: "mydb",
hostname: "localhost",
username: "root",
password: "12345",
});
PostgreSQL example:
const db = await connect({
type: "postgres",
port: 5432,
database: "mydb",
hostname: "localhost",
username: "root",
password: "12345",
});
Typically MySQL and PostgreSQL database ask for a username and password. However, if there is no password, you can leave it empty.
Once, you've finished using the database, disconnect it. This can clear more space for you.
await db.disconnect();