docs-src/docs/articles/ionic-storage.md
When building Ionic apps, developers face the challenge of choosing a robust Ionic storage mechanism that supports:
RxDB (Reactive Database) offers all these features in a single, local-first database solution tailored to Ionic and other hybrid frameworks. Keep reading to learn how RxDB solves the most common storage pitfalls in hybrid app development while providing unmatched flexibility.
<center> <a href="https://rxdb.info/"></a>
Offline functionality is crucial for modern mobile applications, particularly when devices encounter unreliable or slow networks. RxDB stores all data locally so your Ionic app can run seamlessly without needing a continuous internet connection. When a network is available again, RxDB automatically synchronizes changes with your backend - no extra code required.
Securing on-device data is paramount when handling sensitive information. RxDB includes encryption plugins that let you:
This built-in encryption sets RxDB apart from many other Ionic storage options that lack integrated security.
Large or repetitive data can significantly slow down devices with minimal memory. RxDB's key-compression feature decreases document size stored on the device, improving overall performance by:
In addition to functioning fully offline, RxDB supports advanced replication options. Your Ionic app can instantly sync updates with any backend (CouchDB, Firestore, GraphQL, or custom REST), maintaining a real-time user experience. Plus, RxDB handles conflicts gracefully - meaning less worry about clashing user edits.
<p align="center"> </p>RxDB runs with a NoSQL approach and integrates seamlessly into Ionic Angular or other frameworks you might use with Ionic. You can extend or replace storage backends, add encryption, or build advanced offline-first features with minimal overhead.
<center> <a href="https://rxdb.info/"></a>
For a simple proof-of-concept or testing environment in Ionic, you can use localstorage as your underlying storage. Later, if you need better native performance, you can switch to the SQLite storage offered by the RxDB Premium plugins.
npm install rxdb rxjs
import { createRxDatabase } from 'rxdb/plugins/core';
import { getRxStorageLocalstorage } from 'rxdb/plugins/storage-localstorage';
async function initDB() {
const db = await createRxDatabase({
name: 'myionicdb',
storage: getRxStorageLocalstorage(),
multiInstance: false // or true if you plan multi-tab usage
// Note: If you need encryption, set `password` here
});
await db.addCollections({
notes: {
schema: {
title: 'notes schema',
version: 0,
type: 'object',
primaryKey: 'id',
properties: {
id: { type: 'string', maxLength: 100 },
content: { type: 'string' },
timestamp: { type: 'number' }
},
required: ['id']
}
}
});
return db;
}
When you need the best performance on mobile devices, purchase the RxDB Premium SQLite Storage and replace getRxStorageLocalstorage() with getRxStorageSQLite() - your app logic remains largely the same. You only have to change the configuration.
To secure local data, add the crypto-js encryption plugin (free version) or the premium web-crypto plugin. Below is an example using the free crypto-js plugin:
import {
wrappedKeyEncryptionCryptoJsStorage
} from 'rxdb/plugins/encryption-crypto-js';
import {
getRxStorageLocalstorage
} from 'rxdb/plugins/storage-localstorage';
import { createRxDatabase } from 'rxdb/plugins/core';
async function initEncryptedDB() {
const encryptedStorage = wrappedKeyEncryptionCryptoJsStorage({
storage: getRxStorageLocalstorage()
});
const db = await createRxDatabase({
name: 'secureIonicDB',
storage: encryptedStorage,
password: 'myS3cretP4ssw0rd'
});
await db.addCollections({
secrets: {
schema: {
title: 'secret schema',
version: 0,
type: 'object',
primaryKey: 'id',
properties: {
id: { type: 'string', maxLength: 100 },
text: { type: 'string' }
},
required: ['id'],
// all fields in this array will be stored encrypted:
encrypted: ['text']
}
}
});
return db;
}
With encryption enabled:
text is automatically encrypted at rest.To minimize the storage footprint, RxDB offers a key-compression feature. You can enable it in your schema:
await db.addCollections({
logs: {
schema: {
title: 'logs schema',
version: 0,
keyCompression: true, // enable compression
type: 'object',
primaryKey: 'id',
properties: {
id: { type: 'string', maxLength: 100 },
message: { type: 'string' },
createdAt: { type: 'string', format: 'date-time' }
}
}
}
});
With keyCompression: true, RxDB shortens field names internally, significantly reducing document size. This helps both stored data and network transport during replication.
Ionic Native Storage or Capacitor-based key-value stores may handle small amounts of data but lack advanced features like:
For Ionic storage that supports offline-first operations, built-in encryption, optional data compression, and live syncing with any backend, RxDB provides a powerful solution. Start quickly with localstorage for local development and testing - then scale up to the premium SQLite storage for optimal performance on production mobile devices.
Ready to learn more?
RxDB - The ultimate toolkit for Ionic developers seeking offline-first, secure, and compressed local data, with real-time sync to any server.