Configs2026年6月1日·1 分钟阅读

LokiJS — Fast In-Memory Database for JavaScript

A lightweight, in-memory document-oriented database for JavaScript that supports indexing, dynamic views, persistence adapters, and runs in Node.js, browsers, and Cordova/NativeScript environments.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
LokiJS Overview
直接安装命令
npx -y tokrepo@latest install 79f84ab9-5d72-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

Introduction

LokiJS is a fast, in-memory document database written in JavaScript. It stores JSON documents in collections with binary-index support for fast queries, and provides pluggable persistence adapters for saving data to disk, localStorage, or IndexedDB. LokiJS is designed for applications that need embedded database functionality without a server process.

What LokiJS Does

  • Stores JSON documents in named collections with auto-generated IDs and metadata tracking
  • Provides a MongoDB-style query language with operators like $eq, $gt, $in, $regex, and logical combinators
  • Supports binary indices and unique constraints for fast lookups on specific fields
  • Offers DynamicViews that maintain pre-filtered, sorted result sets that update automatically when data changes
  • Persists data through adapters for filesystem (Node.js), localStorage (browser), and IndexedDB

Architecture Overview

LokiJS keeps all data in memory as JavaScript arrays within collection objects. Each collection maintains optional binary indices (sorted arrays of document positions) that enable O(log n) lookups on indexed fields. The query engine evaluates filter expressions against the in-memory data, using indices when available. DynamicViews are materialized subsets that re-evaluate incrementally when documents are inserted, updated, or removed. Persistence is decoupled through an adapter pattern: the database serializes to JSON and hands it to an adapter (fs-adapter, localStorage-adapter, IndexedDB-adapter) for storage. Autosave and autoload options automate the persistence lifecycle.

Self-Hosting & Configuration

  • Install via npm for Node.js or include via CDN/bundler for browser environments
  • Create a database instance with optional autosave and autoload configuration
  • Add collections with index declarations for fields that need fast queries
  • Choose a persistence adapter: fs-structured-adapter for Node.js, localStorage for browsers, or IndexedDB for large browser datasets
  • Configure autosave interval to periodically persist changes without manual save calls

Key Features

  • Sub-millisecond query performance for in-memory datasets with binary indexing
  • DynamicViews for maintaining live, filtered, sorted result sets that update on data changes
  • Pluggable persistence with built-in adapters for Node.js filesystem, localStorage, and IndexedDB
  • Changes API that tracks insert, update, and delete operations for sync and replication scenarios
  • Runs in Node.js, browsers, Electron, Cordova, NativeScript, and React Native

Comparison with Similar Tools

  • NeDB — similar embedded JS database; LokiJS offers better indexing performance and DynamicViews
  • PouchDB — CouchDB-compatible with sync; LokiJS is lighter and faster for local-only use cases
  • Dexie.js — IndexedDB wrapper for browsers; LokiJS works in-memory and across multiple environments
  • RxDB — reactive database with replication; LokiJS is simpler when real-time sync is not needed
  • SQLite (via sql.js) — relational model compiled to WebAssembly; LokiJS provides a native JavaScript document model with less overhead

FAQ

Q: How much data can LokiJS handle? A: LokiJS works well with datasets up to a few hundred thousand documents per collection. Beyond that, memory constraints and serialization time become factors.

Q: Does LokiJS support transactions? A: LokiJS does not have formal transaction support. Operations on a single collection are atomic, but multi-collection transactions require application-level coordination.

Q: Can I use LokiJS with TypeScript? A: Yes. Type definitions are available via @types/lokijs, providing typed collections and query operations.

Q: What happens if the application crashes before a save? A: Unsaved in-memory changes are lost. Use the autosave option with a short interval or call db.saveDatabase() after critical writes to minimize data loss risk.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产