Database Connections and Setup
In verse.db
, we use the built-in .connect
method to establish connections to databases. Let's dive into the key options and how to use them effectively.
Installation command
npm install verse.db@latest
Adapter Selection
The adapter option is crucial for defining the type of database you want to connect to. You can choose from the following adapters:
- JSON: Use
"json"
as the adapter value. - YAML: Specify
"yaml"
for the YAML adapter. - SQL: For SQL databases, set the adapter to
"sql"
.
Here's an example of connecting to a JSON database:
const db = new versedb.connect({
adapter: "json",
});
You can also create multiple connections simultaneously, each with a different adapter:
const jsonDb = new versedb.connect({
adapter: "json",
});
const yamlDb = new versedb.connect({
adapter: "yaml",
});
const sqlDb = new versedb.connect({
adapter: "sql",
});
Currently, we support three adapters: json
, yaml
, and sql
. Expect more adapters in future updates.
Data Path Configuration
The dataPath option specifies the directory where your data resides. Set it to the appropriate path:
const db = new versedb.connect({
adapter: "adapter", // Choose the desired adapter (json, yaml, or sql)
dataPath: "./path/to/the/data/folder", // Specify the path to the data directory (a folder)
});
Developer Logs
Our database includes a flexible logger system that you can enable or disable for each connection. Here's how to configure it:
const db = new versedb.connect({
adapter: "adapter", // Choose the desired adapter (json, yaml, or sql)
dataPath: "./path/to/the/data/folder", // Specify the path to the data directory (a folder)
devLogs: {
enable: true, // Set to true to enable dev logs (writes logs to files in the logs folder)
path: "./path/to/the/logs/folder", // Specify the logs folder path
},
});
The logger system can also be used externally. Refer to the Logger System page for further details.
Secure Data (Secure)
To enhance security and reduce data file sizes, we provide a built-in encoder and encryptor. By default, we use the secret key "versedb"
for secure. However, you can set your own secret:
const db = new versedb.connect({
adapter: "adapter", // Choose the desired adapter (json, yaml, or sql)
dataPath: "./path/to/the/data/folder", // Specify the path to the data directory (a folder)
devLogs: {
enable: true, // Set to true to enable dev logs (writes logs to files in the logs folder)
path: "./path/to/the/logs/folder", // Specify the logs folder path
},
secure: {
enable: true,
secret: "put-your-secret-here", // Set your custom secret
},
});
Feel free to explore these options and tailor your verse.db
connections to your specific needs! 🚀