Baselet
raw JSON →Baselet is a lightweight, file-system based database library for JavaScript and TypeScript applications, currently at version 0.3.0. It leverages `disklet` for persistent storage, allowing it to store data directly on disk without requiring external database servers. This makes it suitable for embedded applications, local storage, or serverless environments where a simple, durable key-value or document store is needed. Its primary differentiators include its small footprint, direct disk interaction via `disklet`, and a focus on simplicity. As a 0.x package, it is still under active development, and its API might evolve with subsequent minor versions, though a regular release cadence isn't yet established. Developers should anticipate potential API adjustments.
Common errors
error TypeError: db.getJson is not a function ↓
makeBaselet correctly with a Disklet instance and a string path, and that Baselet is properly imported and the correct methods are being called. error Error: Path exists but is not a directory ↓
error Module not found: Error: Can't resolve 'baselet' or Cannot find module 'baselet' ↓
npm install baselet disklet or yarn add baselet disklet to ensure both Baselet and its core disklet dependency are installed and available. Warnings
breaking As a 0.x package, the API of Baselet is subject to change without strict adherence to semantic versioning. Methods, signatures, or error handling might be modified in minor or patch releases. ↓
gotcha Baselet's persistence depends entirely on the underlying 'disklet' instance. If using a memory-based disklet (e.g., `makeMemoryDisklet`), data will not persist across application restarts. ↓
gotcha Directly manipulating files within the Baselet's storage path via disklet (or the underlying file system) outside of Baselet's API can lead to data corruption or unexpected behavior. ↓
Install
npm install baselet yarn add baselet pnpm add baselet Imports
- makeBaselet wrong
const makeBaselet = require('baselet')correctimport { makeBaselet } from 'baselet' - Baselet wrong
import type Baselet from 'baselet'correctimport { Baselet } from 'baselet' - makeBaselet (CommonJS)
const { makeBaselet } = require('baselet')
Quickstart
import { makeBaselet, Baselet } from 'baselet'
import { makeMemoryDisklet } from 'disklet' // Or makeNodeDisklet for Node.js, makeReactNativeDisklet for React Native
async function runBaseletExample() {
// 1. Create a disklet instance (using in-memory for this example, use file-system for persistence)
const disklet = makeMemoryDisklet()
const dbPath = 'my-app-data'
// 2. Initialize Baselet database
const db: Baselet = makeBaselet(disklet, dbPath)
console.log(`Baselet database initialized at path: ${dbPath}`)
// 3. Store some JSON data
const userId = 'user123'
const userData = { name: 'Alice', email: 'alice@example.com', age: 30 }
await db.setJson(userId, userData)
console.log(`Stored user data for ${userId}`)
// 4. Retrieve JSON data
const retrievedUserData = await db.getJson(userId)
console.log(`Retrieved user data for ${userId}:`, retrievedUserData)
// 5. Store a simple string
await db.setText('last-access', new Date().toISOString())
console.log('Stored last access time.')
// 6. Retrieve simple string
const lastAccess = await db.getText('last-access')
console.log('Last access time:', lastAccess)
// 7. Attempt to retrieve a non-existent key (should throw or reject)
try {
await db.getJson('non-existent-key')
} catch (error) {
console.log(`Attempted to get non-existent key: ${(error as Error).message}`)
}
}
runBaseletExample().catch(console.error)