} Skip to content

Quick Start

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.

First, make sure you have Helper.db installed:

Terminal window
npm install helper.db

Create a new database instance and start working with data:

app.js
const { Database } = require('helper.db');
// Create a new database instance
const db = new Database('myapp.db');
// The database file will be created automatically
console.log('Database created successfully!');

The Database class is the main entry point for all operations:

Creating Database
const { Database } = require('helper.db');
// Basic database
const db = new Database('data.db');
// Database with options
const dbWithOptions = new Database('data.db', {
autoSave: true,
saveInterval: 5000
});

Store data using the set method:

Setting Data
// Simple key-value
db.set('username', 'john_doe');
db.set('score', 1500);
// Nested objects
db.set('user.profile.name', 'John Doe');
db.set('user.profile.email', 'john@example.com');
// Arrays
db.set('tags', ['javascript', 'database', 'nodejs']);

Retrieve data using the get method:

Getting Data
// Get simple values
const username = db.get('username');
const score = db.get('score');
// Get nested values
const name = db.get('user.profile.name');
// Get with default value
const theme = db.get('settings.theme', 'dark');
console.log('Username:', username);
console.log('Score:', score);
console.log('Name:', name);
console.log('Theme:', theme);

Here’s a complete example showing common operations:

complete-example.js
const { Database } = require('helper.db');
// Initialize database
const db = new Database('example.db');
// Set user data
db.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 data
const user1 = db.get('users.1');
const user2 = db.get('users.2');
console.log('User 1:', user1);
console.log('User 2:', user2);
// Update user score
db.set('users.1.score', 2750);
// Add achievement
const achievements = db.get('users.2.achievements');
achievements.push('score-1500');
db.set('users.2.achievements', achievements);
// Get all users
const allUsers = db.get('users');
console.log('All users:', allUsers);
// Check if key exists
if (db.has('users.1.premium')) {
console.log('User 1 is premium');
} else {
console.log('User 1 is not premium');
}
// Delete data
db.delete('users.2.achievements.0'); // Remove first achievement
console.log('Operations completed successfully!');

Save your code to a file and run it:

Terminal window
node app.js

Here are some essential methods you’ll use frequently:

set

set(key: string, value: any) → Database

Sets a value in the database. Supports dot notation for nested objects.

Parameters

NameTypeDescription
keystringThe key to set the value for
valueanyThe value to store

get

get(key: string, defaultValue?: any) → any

Gets a value from the database. Returns undefined if key doesn't exist.

Parameters

NameTypeDescription
keystringThe key to retrieve the value for
defaultValue?anyDefault value if key doesn't exist

has

has(key: string) → boolean

Checks if a key exists in the database.

Parameters

NameTypeDescription
keystringThe key to check for existence

delete

delete(key: string) → boolean

Deletes a key from the database. Returns true if the key existed.

Parameters

NameTypeDescription
keystringThe key to delete

Now that you understand the basics, you can:

Happy coding with Helper.db! 🚀