About
ST.db is a powerful npm package that provides an easy-to-use key-value interface for managing various types of databases, both local and cloud-based. This package simplifies the process of reading, writing, and seamlessly switching between different database engines. Whether you're working with local databases or cloud-based solutions, st.db offers a straightforward and efficient way to interact with your data.
Features ✨
-
Multiple Database Support: st.db supports a variety of database types, including Firebase, Cache, JSON, YAML, XML, MongoDB, PostgreSQL, MySQL, and SQL. This versatility allows you to choose the most suitable database for your project's requirements.
-
Simplified Interface: With st.db, you can access and manipulate your databases using a simple key-value interface. This means you can easily store and retrieve data using intuitive methods.
-
Seamless Switching: One of the standout features of st.db is its ability to seamlessly switch between different database drivers. This means you can transition from a local JSON database to a cloud-based Firebase database without extensive code changes.
-
Efficient Caching: The CacheDriver provided by st.db enables efficient caching of frequently accessed data, optimizing the speed of your data retrieval operations.
-
Cross-Platform Compatibility: st.db is designed to work seamlessly across various platforms and environments. Whether you're developing a web application, mobile app, or desktop software, you can rely on st.db to handle your database needs.
-
Encryption and Security: Protect your sensitive data with built-in encryption options. st.db offers encryption features to ensure that your data remains secure, both at rest and during transmission.
-
Customizable Configurations: Each database driver in st.db comes with a range of customizable configurations. Tailor the settings to match your project's performance and scalability requirements.
-
Comprehensive Documentation: Our package comes with comprehensive and easy-to-understand documentation. Whether you're a beginner or an experienced developer, you'll find the information you need to get started and make the most of st.db.
Installation
To install the st.db package and its associated database drivers, follow these steps:
- Install the main st.db package:
npm install st.db
-
Depending on the specific database drivers you intend to use, you will need to install the corresponding packages. Here are the drivers and their associated packages:
Note: These packages are marked as optional, meaning you only need to install the ones that correspond to the specific database drivers you plan to use. If you're not using a particular driver, you can skip installing its associated package.
-
Once you have installed the main st.db package and the necessary database driver packages, you're ready to start using st.db in your project!
Getting Started
To begin using st.db in your project, you can import the necessary database drivers and start interacting with your databases using the key-value interface. For detailed usage instructions and examples, refer to the documentation or examples provided for each individual driver.
Usage
Using Node.js require()
const { Database } = require('st.db');
TypeScript/ES Module support
import { Database } from 'st.db';
import { Database } from 'npm:st.db';
Configuring Database Behavior Using DatabaseOptions
The DatabaseOptions
object allows you to customize the behavior of the database when initializing it. Here's a detailed explanation of the available options:
-
encryptionOptions
: This property accepts an object that defines additional options for encryption. These options can be used to fine-tune encryption behavior based on your security requirements.
-
autoDecrypt
(optional): When set to true
, this property enables automatic decryption of encrypted data as it's retrieved from the database.
-
encryption
: This property accepts an object that provides information for data encryption. It includes the following properties:
password
: The password used for encryption and decryption.digest
(optional): A cryptographic digest algorithm used to derive the encryption key from the provided password. If not specified, the default is SHA-256.
-
driver
: By setting this property, you can specify the desired database driver from the Drivers
type mentioned earlier. Choose the appropriate driver that matches your intended database technology.
Example Configuration
Here's an example of how you might configure the st.db package using the DatabaseOptions
object:
Using JSONDriver
The JSONDriver
allows you to store data in a local JSON file. Here's how you can use it:
import { Database, JSONDriver } from "st.db";
const options = {
driver: new JSONDriver("data.json"),
};
const db = new Database(options);
Using YAMLDriver
The JSONDriver
allows you to store data in a local YAML file. Here's how you can use it:
import { Database, YAMLDriver } from "st.db";
const options = {
driver: new YAMLDriver("data.yaml"),
};
const db = new Database(options);
Using XMLDriver
The XMLDriver
allows you to store data in a local XML file. Here's how you can use it:
import { Database, XMLDriver } from "st.db";
const options = {
driver: new XMLDriver("data.xml"),
};
const db = new Database(options);
Using MySQLDriver
The MySQLDriver
connects to MySQL databases and provides data management features. Here's how to set it up:
import { Database, MySQLDriver } from "st.db";
const tableName = "my_table";
const mysqlConfig = {
host: "localhost",
user: "username",
password: "password",
database: "my_database",
};
const options = {
driver: new MySQLDriver(mysqlConfig,tableName),
};
const db = new Database(options);
Using MongoDriver
The MongoDriver
enables interaction with MongoDB databases. Here's how to use it:
import { Database, MongoDriver } from "st.db";
const mongoUrl = "mongodb://localhost:27017";
const dbName = "my_database";
const collectionName = "my_collection";
const options = {
driver: new MongoDriver(mongoUrl, dbName, collectionName),
};
const db = new Database(options);
Using FirebaseDriver
The FirebaseDriver
enables interaction with Firestore databases. Here's how to use it:
import { Database, FirebaseDriver } from "st.db";
const firebaseConfig = {
apiKey: "your-api-key",
authDomain: "your-auth-domain",
projectId: "your-project-id",
storageBucket: "your-storage-bucket",
messagingSenderId: "your-messaging-sender-id",
appId: "your-app-id",
};
const collectionName = "my_collection";
const options = {
driver: new FirebaseDriver(firebaseConfig, collectionName),
};
const db = new Database(options);
Using CacheDriver
The CacheDriver
provides an in-memory caching mechanism. Here's how to utilize it:
import { Database, CacheDriver } from "st.db";
const options = {
driver: new CacheDriver(),
};
const db = new Database(options);
Using PostgresDriver
The PostgresDriver
enables interaction with PostgreSQL databases. Here's how to use it:
import { Database, PostgresDriver } from "st.db";
const tableName = "my_table";
const postgresConfig = {
user: "username",
password: "password",
host: "localhost",
database: "my_database",
};
const options = {
driver: new PostgresDriver(postgresConfig, tableName),
};
const db = new Database(options);
Using SQLDriver
The SQLDriver
provides an interface for general SQL databases. Here's how you can use it:
import { Database, SQLDriver } from "st.db";
const tableName = "my_table";
const options = {
driver: new SQLDriver("data.sqlite", tableName),
};
const db = new Database(options);
Example of methods
db.set("version", "v7");
const version = await db.get("version");
const exists = await db.has("version");
await db.remove("version");
const allElements = await db.all();
await db.math("points", "+", 150);
await db.clear();
await db.deleteAll();
const startsWithElements = await db.startsWith("st.");
const endsWithElements = await db.endsWith(".db");
const filteredElements = await db.filter("key", (element) => element.data === "test");
const includesElements = await db.includes("st.db");
const valueType = await db.type("version");
const allValues = await db.valuesAll();
const allKeys = await db.keysAll();
const encryptedValue = db.encryptString("secret");
const uptime = db.uptime;
const decryptedValue = db.decryptString(encryptedValue);
const isEncrypted = db.isEncryptString(encryptedValue);
await db.push("myArray", "newElement");
const poppedValue = await db.pop("myArray");
const shiftedValue = await db.shift("myArray");
await db.pull("myArray", "elementToRemove");
await db.unshift("myArray", "newElement");
await db.add("count", 5);
await db.multiply("total", 2);
await db.double("amount");
await db.subtract("balance", 50);
The Table
class extends the functionality of the Database
class to provide additional features for managing data in a specific table.
import { Table } from "st.db";
const table = new Table("users", db);
await table.set("user1", { name: "Alice", age: 30 });
const userData = await table.get("user1");
console.log("User data:", userData);
const userExists = await table.has("user1");
console.log("User exists:", userExists);
const allData = await table.all();
console.log("All data:", allData);
await table.clear();
Supported Drivers
- FirebaseDriver: Connect to Firebase Realtime Database.
- CacheDriver: Utilize an efficient caching mechanism.
- JSONDriver: Store data in a local JSON file.
- YAMLDriver: Manage data using YAML files.
- XMLDriver: Store and retrieve data in XML format.
- MongoDriver: Connect to MongoDB databases.
- PostgresDriver: Interact with PostgreSQL databases.
- MySQLDriver: Manage MySQL database operations.
- SQLDriver: Interface for SQL databases.
Links
Help
If you don't understand something in the documentation, you are experiencing problems, or you just need a gentle
nudge in the right direction, please don't hesitate to join our official Discord Server .
Did you find it difficult to use?
You can see the Documentation of the st.db package to know all the details
License
CC BY-NC-ND 4.0