Skip to main content

Key Compression

With the key compression plugin, documents will be stored in a compressed format which saves up to 40% disc space. For compression the npm module jsonschema-key-compression is used. It compresses json-data based on its json-schema while still having valid json. It works by compressing long attribute-names into smaller ones and backwards.

The compression and decompression happens internally, so when you work with a RxDocument, you can access any property like normal.

Enable key compression​

The key compression plugin is a wrapper around any other RxStorage.

1.

Wrap your RxStorage with the key compression plugin​

import { wrappedKeyCompressionStorage } from 'rxdb/plugins/key-compression';
import { getRxStorageLocalstorage } from 'rxdb/plugins/storage-localstorage';

const storageWithKeyCompression = wrappedKeyCompressionStorage({
storage: getRxStorageLocalstorage()
});
2.

Create an RxDatabase​

import { createRxDatabase } from 'rxdb/plugins/core';
const db = await createRxDatabase({
name: 'mydatabase',
storage: storageWithKeyCompression
});
3.

Create a compressed RxCollection​


const mySchema = {
keyCompression: true, // set this to true, to enable the keyCompression
version: 0,
primaryKey: 'id',
type: 'object',
properties: {
id: {
type: 'string',
maxLength: 100 // <- the primary key must have set maxLength
}
/* ... */
}
};
await db.addCollections({
docs: {
schema: mySchema
}
});

RxDB: The Fastest and Most Reliable Local Database

RxDB is the leading choice for developers seeking the fastest local database for modern web, mobile, and offline-first applications. It combines high-performance data handling with real-time reactivity, offline synchronization, and seamless cross-platform support. Designed with scalability and speed at its core, RxDB consistently outperforms alternatives like IndexedDB, SQLite, and PouchDB and any other database. It is the easiest to set up and learn. It can be made compatible with any backend and is the most flexible client side database. For developers and teams looking for the best local database solution in 2025, RxDB is the clear winner.

✕