@basepurpose/rxdb-sqlite

中文文档
0.1.0发布于 2026/04/29MITTypeScriptESM + CJS零依赖近 30 天下载 2647

A free, open-source, cross-platform SQLite storage adapter for RxDB

原始使用文档
文档目录25 节
  1. Why This Exists
  2. Platform Support
  3. Installation
  4. Platform-Specific Dependencies
  5. Quick Start
  6. Platform-Specific Imports
  7. Features
  8. Mango Query Support
  9. Reactive Queries
  10. Attachments
  11. Full-Text Search (FTS5)
  12. Index Advisor
  13. Replication
  14. Multi-Tab Support (Browser)
  15. RxDB Plugin Compatibility
  16. Configuration
  17. API Reference
  18. getRxStorageSQLite(options?)
  19. Utility Functions
  20. Browser Requirements
  21. Error Codes
  22. Contributing
  23. License
  24. Acknowledgments
  25. Built By

@basepurpose/rxdb-sqlite

A free, open-source, cross-platform SQLite storage adapter for RxDB

MIT License TypeScript RxDB

Looking for the official, production-optimized SQLite storage? See RxDB Premium SQLite.

This is a community-built alternative, scoped under @basepurpose.

Why This Exists

RxDB is an excellent local-first, offline-capable database for JavaScript applications. Its production-ready SQLite storage requires a paid premium license, which can be a barrier for solo developers, startups, open source projects, students, and non-profits.

This project provides a free, MIT-licensed SQLite storage adapter that works across multiple platforms.

Platform Support

PlatformSQLite ImplementationTesting Status
React Native (Android)expo-sqliteVerified on device
React Native (iOS)expo-sqliteTests pass, needs device verification
Browser@sqlite.org/sqlite-wasm + OPFSTests pass, needs device verification
Node.jsbetter-sqlite3Tests pass, needs device verification
Bunbun:sqlite (native)Tests pass, needs device verification
Electronbetter-sqlite3Tests pass, needs device verification
Capacitor (iOS/Android)@capacitor-community/sqliteTests pass, needs device verification
Tauri (Windows/macOS/Linux)@tauri-apps/plugin-sqlTests pass, needs device verification

671 tests pass across 28 test suites. Only React Native on Android has been verified on a real device so far. Community help testing other platforms is welcome -- please open an issue with your results.

Installation

npm install @basepurpose/rxdb-sqlite

Platform-Specific Dependencies

PlatformAdditional Install
BrowserNone (WASM bundled)
Node.jsnpm install better-sqlite3
BunNone (native SQLite)
React Nativenpx expo install expo-sqlite
Electronnpm install better-sqlite3
Capacitornpm install @capacitor-community/sqlite && npx cap sync
TauriAdd tauri-plugin-sql to Cargo.toml

Quick Start

import { createRxDatabase } from 'rxdb';
import { getRxStorageSQLite } from '@basepurpose/rxdb-sqlite';

const db = await createRxDatabase({
  name: 'mydb',
  storage: getRxStorageSQLite()
});

await db.addCollections({
  users: {
    schema: {
      version: 0,
      primaryKey: 'id',
      type: 'object',
      properties: {
        id: { type: 'string', maxLength: 100 },
        name: { type: 'string' },
        email: { type: 'string' }
      },
      required: ['id', 'name']
    }
  }
});

await db.users.insert({ id: 'user-1', name: 'Alice', email: '[email protected]' });

Platform-Specific Imports

// Browser (WASM + OPFS)
import { getRxStorageSQLite } from '@basepurpose/rxdb-sqlite/browser';

// Node.js
import { getRxStorageSQLite } from '@basepurpose/rxdb-sqlite/node';

// Bun
import { getRxStorageSQLite } from '@basepurpose/rxdb-sqlite/bun';

// React Native (Expo)
import { getRxStorageSQLite } from '@basepurpose/rxdb-sqlite/react-native';

// Electron
import { getRxStorageSQLite } from '@basepurpose/rxdb-sqlite/electron';

// Capacitor (iOS/Android)
import { getRxStorageSQLite } from '@basepurpose/rxdb-sqlite/capacitor';

// Tauri (Desktop)
import { getRxStorageSQLite } from '@basepurpose/rxdb-sqlite/tauri';

Features

Mango Query Support

All standard RxDB query operators work:

const results = await db.users.find({
  selector: {
    $and: [
      { age: { $gte: 18 } },
      { status: { $in: ['active', 'pending'] } },
      { 'address.country': 'US' }
    ]
  },
  sort: [{ name: 'asc' }],
  limit: 10
}).exec();

Supported operators: $eq, $ne, $gt, $gte, $lt, $lte, $in, $nin, $exists, $type, $and, $or, $not, $nor, $elemMatch, $size, $all, $regex, $text

Reactive Queries

db.users.find().$.subscribe(users => {
  console.log('Users updated:', users);
});

Attachments

Store binary data alongside documents:

const doc = await db.posts.findOne('post-1').exec();
await doc.putAttachment({
  id: 'image.jpg',
  data: imageBase64String,
  type: 'image/jpeg'
});

Full-Text Search (FTS5)

const schema = {
  // ... your schema
  fulltext: {
    fields: ['title', 'content'],
    tokenizer: 'unicode61' // 'unicode61' | 'porter' | 'ascii' | 'trigram'
  }
};

const results = await db.articles.find({
  selector: { $text: 'javascript tutorial' }
}).exec();

Index Advisor

Analyze queries and get index suggestions:

import { analyzeQuery, suggestIndexes } from '@basepurpose/rxdb-sqlite';

const suggestions = suggestIndexes(
  { selector: { status: 'active', age: { $gte: 18 } } },
  'users'
);

Replication

Works with all RxDB replication plugins (CouchDB, Supabase, GraphQL, etc.):

import { replicateCouchDB } from 'rxdb/plugins/replication-couchdb';

const replication = replicateCouchDB({
  collection: db.users,
  url: 'https://your-couchdb.example.com/users',
  live: true,
  pull: {},
  push: {}
});

Multi-Tab Support (Browser)

Changes in one tab automatically sync to other tabs via BroadcastChannel.

RxDB Plugin Compatibility

Works with CRDT, Encryption, Replication, Backup, and Migration plugins.

Configuration

getRxStorageSQLite({
  debug: true,

  pragma: {
    journal_mode: 'WAL',
    synchronous: 'NORMAL',
    cache_size: -64000,   // 64MB cache
    temp_store: 'MEMORY'
  }
});

API Reference

getRxStorageSQLite(options?)

Creates an RxStorage instance. Platform is auto-detected, or use a platform-specific import.

OptionTypeDescription
debugbooleanEnable debug logging (default: false)
adapterSQLiteAdapterFactoryCustom adapter factory
pragmaSQLitePragmaSettingsSQLite PRAGMA settings
browserBrowserAdapterOptionsBrowser-specific options
capacitorCapacitorAdapterOptionsCapacitor-specific options
tauriTauriAdapterOptionsTauri-specific options
nodeNodeAdapterOptionsNode.js-specific options
bunBunAdapterOptionsBun-specific options
reactNativeReactNativeAdapterOptionsReact Native-specific options
electronElectronAdapterOptionsElectron-specific options

Utility Functions

import {
  closeAllSQLiteConnections,
  isDatabaseOpen,
  translateMangoToSQL,
  buildSelectQuery,
  buildCountQuery,
} from '@basepurpose/rxdb-sqlite';

Browser Requirements

For optimal performance, serve with these headers to enable SharedArrayBuffer:

Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp

The storage falls back to a slower mode if these headers aren't available.

Error Codes

CodeDescription
SQLITE_STORAGE_CLOSEDOperation on closed instance
SQLITE_ATTACHMENT_NOT_FOUNDAttachment doesn't exist
SQLITE_DIGEST_MISMATCHAttachment digest verification failed
SQLITE_INVALID_DOCUMENT_IDInvalid document ID
MANGO_QUERY_ERRORInvalid query operator or syntax

Contributing

Contributions are welcome. See CONTRIBUTING.md for guidelines.

git clone https://github.com/basepurpose/rxdb-sqlite.git
cd rxdb-sqlite
bun install
bun test
bun run build

License

MIT -- see LICENSE.

Acknowledgments

Built By

basepurpose -- a maker lab building open-source tools.

版本与发布记录

@basepurpose/rxdb-sqlite 共发布 1 个版本,首次发布于 2026/04/29;过去 12 个月发布了 1 个版本。

版本发布日期距今
0.1.0latest2026/04/296 个月前

关于 @basepurpose/rxdb-sqlite 的常见问题

@basepurpose/rxdb-sqlite 是什么?

A free, open-source, cross-platform SQLite storage adapter for RxDB

如何安装 @basepurpose/rxdb-sqlite?

在项目目录运行 npm install @basepurpose/rxdb-sqlite,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:rxdb(>=15.0.0)、rxjs(>=7.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@basepurpose/rxdb-sqlite 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@basepurpose/rxdb-sqlite 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@basepurpose/rxdb-sqlite 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@basepurpose/rxdb-sqlite 可以商用吗?

@basepurpose/rxdb-sqlite 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@basepurpose/rxdb-sqlite 的下载量有多少?

根据 npm 官方下载统计,@basepurpose/rxdb-sqlite 在 2026/10/08 – 2026/11/06 这 30 天内被下载 2,647 次,日均约 88 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@basepurpose/rxdb-sqlite 还在维护吗?

最新版本 0.1.0 发布于 2026/04/29(6 个月前);至今共发布 1 个版本,其中过去 12 个月发布了 1 个;首次发布于 2026/04/29。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。