Quick Start
Quick Start Guide
Section titled “Quick Start Guide”This guide will help you get up and running with Helper.db quickly. We’ll cover the basic concepts and show you how to perform common database operations.
Installation
Section titled “Installation”First, make sure you have Helper.db installed:
npm install helper.dbyarn add helper.dbpnpm add helper.dbbun add helper.dbBasic Setup
Section titled “Basic Setup”Create a new database instance and start working with data:
const { Database } = require('helper.db');
// Create a new database instanceconst db = new Database('myapp.db');
// The database file will be created automaticallyconsole.log('Database created successfully!');Core Concepts
Section titled “Core Concepts”Database Instance
Section titled “Database Instance”The Database class is the main entry point for all operations:
const { Database } = require('helper.db');
// Basic databaseconst db = new Database('data.db');
// Database with optionsconst dbWithOptions = new Database('data.db', { autoSave: true, saveInterval: 5000});Setting Data
Section titled “Setting Data”Store data using the set method:
// Simple key-valuedb.set('username', 'john_doe');db.set('score', 1500);
// Nested objectsdb.set('user.profile.name', 'John Doe');db.set('user.profile.email', 'john@example.com');
// Arraysdb.set('tags', ['javascript', 'database', 'nodejs']);Getting Data
Section titled “Getting Data”Retrieve data using the get method:
// Get simple valuesconst username = db.get('username');const score = db.get('score');
// Get nested valuesconst name = db.get('user.profile.name');
// Get with default valueconst theme = db.get('settings.theme', 'dark');
console.log('Username:', username);console.log('Score:', score);console.log('Name:', name);console.log('Theme:', theme);Complete Example
Section titled “Complete Example”Here’s a complete example showing common operations:
const { Database } = require('helper.db');
// Initialize databaseconst db = new Database('example.db');
// Set user datadb.set('users.1', { id: 1, name: 'Alice Johnson', email: 'alice@example.com', score: 2500, achievements: ['first-login', 'score-1000']});
db.set('users.2', { id: 2, name: 'Bob Smith', email: 'bob@example.com', score: 1800, achievements: ['first-login']});
// Get user dataconst user1 = db.get('users.1');const user2 = db.get('users.2');
console.log('User 1:', user1);console.log('User 2:', user2);
// Update user scoredb.set('users.1.score', 2750);
// Add achievementconst achievements = db.get('users.2.achievements');achievements.push('score-1500');db.set('users.2.achievements', achievements);
// Get all usersconst allUsers = db.get('users');console.log('All users:', allUsers);
// Check if key existsif (db.has('users.1.premium')) { console.log('User 1 is premium');} else { console.log('User 1 is not premium');}
// Delete datadb.delete('users.2.achievements.0'); // Remove first achievement
console.log('Operations completed successfully!');Running Your Code
Section titled “Running Your Code”Save your code to a file and run it:
node app.jsyarn node app.jspnpm node app.jsbun app.jsMethod Reference
Section titled “Method Reference”Here are some essential methods you’ll use frequently:
set
set(key: string, value: any) → DatabaseSets a value in the database. Supports dot notation for nested objects.
Parameters
| Name | Type | Description |
|---|---|---|
key | string | The key to set the value for |
value | any | The value to store |
get
get(key: string, defaultValue?: any) → anyGets a value from the database. Returns undefined if key doesn't exist.
Parameters
| Name | Type | Description |
|---|---|---|
key | string | The key to retrieve the value for |
defaultValue? | any | Default value if key doesn't exist |
has
has(key: string) → booleanChecks if a key exists in the database.
Parameters
| Name | Type | Description |
|---|---|---|
key | string | The key to check for existence |
delete
delete(key: string) → booleanDeletes a key from the database. Returns true if the key existed.
Parameters
| Name | Type | Description |
|---|---|---|
key | string | The key to delete |
Next Steps
Section titled “Next Steps”Now that you understand the basics, you can:
- Explore the Database API Reference for all available methods
- Learn about Advanced Features like encryption and compression
- Check out Real-world Examples for inspiration
Happy coding with Helper.db! 🚀