Documentation

createDB

stable

Create a database instance

Creates a new database instance with the specified name and storage driver.

Signature

createDB(nameOrOptions: string | DBOptions): DB

Parameters

nameOrOptions

string | DBOptionsRequired

Database name as a string, or an options object with name and driver

Returns

Returns

DB

Database instance with async key-value methods

Examples

Basic Usage

Create a database with just a name (uses localStorage by default):

import { createDB } from 'qortex-db';
// Simple - uses localStorage
const db = createDB('myapp');
// With options - use IndexedDB
const db = createDB({ name: 'myapp', driver: 'indexedDB' });

DB Methods

The returned database instance provides these async methods: • get<T>(key) - Retrieve a value by key • set(key, value) - Store a value • has(key) - Check if key exists • del(key) - Delete a key • scan(pattern) - Find keys by pattern (`*` wildcard) • drop() - Delete all data for this database

get(key)

Retrieve a value by key. Returns `undefined` if the key doesn't exist:

// With type inference
const user = await db.get<User>('user:1');
// user is User | undefined
if (user) {
console.log(user.name);
}

set(key, value)

Store a value with the given key. Values are JSON serialized:

// Store any JSON-serializable value
await db.set('user:1', { name: 'John', age: 30 });
await db.set('settings', { theme: 'dark' });
await db.set('counter', 42);

has(key)

Check if a key exists:

if (await db.has('user:1')) {
console.log('User exists!');
}

del(key)

Delete a key-value pair:

await db.del('user:1');

scan(pattern)

Find keys matching a pattern. Supports `*` wildcard for prefix matching:

// Get all keys
const allKeys = await db.scan('*');
// Get keys starting with 'user:'
const userKeys = await db.scan('user:*');
// ['user:1', 'user:2', 'user:3']

drop()

Delete all data belonging to this database instance:

// Clear everything for this database
await db.drop();

DBOptions Type

Options for creating a database:

type DBOptions = {
name: string; // Unique database name
driver?: DriverType; // 'local' | 'session' | 'indexedDB'
};
// Examples
createDB({ name: 'app', driver: 'local' }); // localStorage
createDB({ name: 'app', driver: 'session' }); // sessionStorage
createDB({ name: 'app', driver: 'indexedDB' }); // IndexedDB