SQLite RxStorage
This storage is based on SQLite and is made to work with Node.js, Electron, React Native, Cordova and Capacitor.
Pros
- Much faster compared to the PouchDB+SQLite
RxStorage
- Small build size
Cons
- It is part of RxDB Premium
- At the moment it is not possible to use regex queries with the SQLite RxStorage.
Requirements
The SQlite RxStorage works on SQLite libraries that use SQLite in version 3.38.0
or higher, because it uses the SQLite JSON methods like JSON_EXTRACT
. If you get an error like [Error: no such function: JSON_EXTRACT (code 1 SQLITE_ERROR[1])
, you might have a too old version of SQLite.
Usage with Node.js SQLite
import {
createRxDatabase
} from 'rxdb';
import {
getRxStorageSQLite,
getSQLiteBasicsNode
} from 'rxdb-premium/plugins/sqlite';
/**
* In Node.js, we get use the SQLite database
* from the 'sqlite' npm module.
* @link https://www.npmjs.com/package/sqlite3
*/
import sqlite3 from 'sqlite3';
const myRxDatabase = await createRxDatabase({
name: 'exampledb',
storage: getRxStorageSQLite({
/**
* Different runtimes have different interfaces to SQLite.
* For example in node.js we have a callback API,
* while in capacitor sqlite we have Promises.
* So we need a helper object that is capable of doing the basic
* sqlite operations.
*/
sqliteBasics: getSQLiteBasicsNode(sqlite3)
})
});
Usage with React Native
- Install the react-native-quick-sqlite npm module
- Import
getSQLiteBasicsQuickSQLite
from the SQLite plugin and use it to create a RxDatabase:
import {
createRxDatabase
} from 'rxdb';
import {
getRxStorageSQLite,
getSQLiteBasicsQuickSQLite
} from 'rxdb-premium/plugins/sqlite';
import { openDatabase } from 'react-native-quick-sqlite';
// create database
const myRxDatabase = await createRxDatabase({
name: 'exampledb',
multiInstance: false, // <- Set multiInstance to false when using RxDB in React Native
storage: getRxStorageSQLite({
sqliteBasics: getSQLiteBasicsQuickSQLite(openDatabase)
})
});
Usage with SQLite Capacitor
- Install the sqlite capacitor npm module
- Add the iOS database location to your capacitor config
{
"plugins": {
"CapacitorSQLite": {
"iosDatabaseLocation": "Library/CapacitorDatabase"
}
}
}
- Use the function
getSQLiteBasicsCapacitor
to get the capacitor sqlite wrapper.
import {
createRxDatabase
} from 'rxdb';
import {
getRxStorageSQLite,
getSQLiteBasicsCapacitor
} from 'rxdb-premium/plugins/sqlite';
/**
* Import SQLite from the capacitor plugin.
*/
import {
CapacitorSQLite,
SQLiteConnection
} from '@capacitor-community/sqlite';
import { Capacitor } from '@capacitor/core';
const sqlite = new SQLiteConnection(CapacitorSQLite);
const myRxDatabase = await createRxDatabase({
name: 'exampledb',
storage: getRxStorageSQLite({
/**
* Different runtimes have different interfaces to SQLite.
* For example in node.js we have a callback API,
* while in capacitor sqlite we have Promises.
* So we need a helper object that is capable of doing the basic
* sqlite operations.
*/
sqliteBasics: getSQLiteBasicsCapacitor(sqlite, Capacitor)
})
});
Known Problems
- expo-sqlite cannot be used on android (but it works on iOS) because it uses and outdated SQLite version