Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
@this-dot/rxidb
Advanced tools
RxIDB is a library for storing and retrieving data from indexedDB databases, using an RxJS based API.
It supports:
✅ Creating new IndexedDB databases and Object Stores
✅ Making CRUD operations on the above-mentioned stores
✅ Reading data out of indexedDB
npm install @this-dot/rxidb
yarn add @this-dot/rxidb
The connectIndexedDb
function is used to connect to an IndexedDB database. It takes a string as an argument, which is the name of the database. It returns an Observable that emits an IDBDatabase
instance. Using the getObjectStore
operator, you can create an Object Store. It also returns the store if it already exists.
import { connectIndexedDb, getObjectStore } from '@this-dot/rxidb';
import { Observable } from 'rxjs';
// Connect to an IndexedDB database (create if the database does not exist)
const database$: Observable<IDBDatabase> = connectIndexedDb('example_database');
// Create an Object Store or get an existing one.
const store$: Observable<IDBObjectStore> = database$.pipe(getObjectStore('example_store'));
store$.subscribe((store) => {
// Do something with the store
});
The getObjectStore
function takes a string as an argument, which is the name of the Object Store. It returns an Observable that emits an IDBObjectStore
instance. The second (optional) parameter allows you to configure your Object Store, for example, you can set it to autoIncrement
.
import { connectIndexedDb, getObjectStore } from '@this-dot/rxidb';
import { Observable } from 'rxjs';
// Connect to an IndexedDB database (create if the database does not exist)
const database$: Observable<IDBDatabase> = connectIndexedDb('example_database');
// Create an Object Store or get an existing one with autoIncrement.
const store$: Observable<IDBObjectStore> = database$.pipe(
getObjectStore('example_store', { autoIncrement: true })
);
store$.subscribe((store) => {
// Do something with the autoIncrement store
});
The second optional parameter is of type IDBObjectStoreParameters
, where you can set the keyPath
and autoIncrement
properties.
interface IDBObjectStoreParameters {
autoIncrement?: boolean;
keyPath?: string | string[] | null;
}
The connectIndexedDb
method allows you to set your own database version, however, database versioning is handled by the library when it is needed. When a store needs to be created, the database connection must be an upgrade process. That is handled internally by the library, by always implementing the database version by 1
.
You can set key-value pairs in an Object Store using the setItem
operator. This allows you to store or update existing values in the store.
import { connectIndexedDb, getObjectStore, setItem } from '@this-dot/rxidb';
import { Observable } from 'rxjs';
// Connect to an IndexedDB database (create if the database does not exist)
const database$: Observable<IDBDatabase> = connectIndexedDb('example_database');
// Create an Object Store or get an existing one.
const store$: Observable<IDBObjectStore> = database$.pipe(getObjectStore('example_store'));
function saveToDatabase<T>(key: string, value: T): void {
store$.pipe(setItem(key, value)).subscribe();
}
saveToDatabase('example_key', 'example_value');
saveToDatabase('example_key2', 1337);
If you have an Object Store, that was set to autoIncrement
, you can use the addItem
operator to add a new value to the store.
import { connectIndexedDb, getObjectStore, addItem } from '@this-dot/rxidb';
import { Observable } from 'rxjs';
// Connect to an IndexedDB database (create if the database does not exist)
const database$: Observable<IDBDatabase> = connectIndexedDb('example_database');
// Create an Object Store or get an existing one with autoIncrement.
const store$: Observable<IDBObjectStore> = database$.pipe(
getObjectStore('example_store', { autoIncrement: true })
);
function saveToDatabase<T>(value: T): void {
store$.pipe(addItem(value)).subscribe();
}
saveToDatabase('example_value');
saveToDatabase(1337);
Please note, that if you try to use the addItem
operator on a non-autoIncrement Object Store, you will get an error: "DOMException: Failed to execute 'add' on 'IDBObjectStore': The object store uses out-of-line keys and has no key generator and the key parameter was not provided."
You can use the deleteItem()
operator to delete a value from the store.
import { connectIndexedDb, getObjectStore, setItem, deleteItem } from '@this-dot/rxidb';
import { Observable } from 'rxjs';
// Connect to an IndexedDB database (create if the database does not exist)
const database$: Observable<IDBDatabase> = connectIndexedDb('example_database');
// Create an Object Store or get an existing one.
const store$: Observable<IDBObjectStore> = database$.pipe(getObjectStore('example_store'));
store$.pipe(setItem('example_key', 'example_value')).subscribe();
store$.pipe(deleteItem('example_key')).subscribe();
If you want to read a specific key's value from the database, you can use the read()
operator.
import { connectIndexedDb, getObjectStore, read, setItem, deleteItem } from '@this-dot/rxidb';
import { Observable } from 'rxjs';
// Connect to an IndexedDB database (create if the database does not exist)
const database$: Observable<IDBDatabase> = connectIndexedDb('example_database');
// Create an Object Store or get an existing one.
const store$: Observable<IDBObjectStore> = database$.pipe(getObjectStore('example_store'));
// set up the read stream.
const exampleKey$: Observable<string> = store$.pipe(read('example_key'));
exampleKey$.subscribe(console.log); // emits every time the value changes.
// exampleKey$ will emit 4 times;
store$
.pipe(
setItem('example_key', 'example_value_1'),
setItem('example_key', 'example_value_2'),
setItem('example_key', 'example_value_3'),
deleteItem('example_key')
)
.subscribe();
You can get all the keys of a database using the keys()
operator. This will return an Observable
of all the keys in the store. With the values()
operator you can get all the values of the store.
import { connectIndexedDb, getObjectStore, addItem, keys, values } from '@this-dot/rxidb';
import { Observable } from 'rxjs';
// Connect to an IndexedDB database (create if the database does not exist)
const database$: Observable<IDBDatabase> = connectIndexedDb('example_database');
// Create an Object Store or get an existing one with autoIncrement.
const store$: Observable<IDBObjectStore> = database$.pipe(
getObjectStore('example_store', { autoIncrement: true })
);
// returns an Observable of all the keys in the store.
const keys$: Observable<IDBValidKey[]> = store$.pipe(keys());
// returns an Observable of all the values in the store.
const values$: Observable<any[]> = store$.pipe(values());
keys$.subscribe(console.log); // emits every time the keys in the database change. (add a new key, remove a key, etc)
values$.subscribe(console.log); // emits every time the values change. Either by adding a new value, or by updating or deleting an existing value.
You can retrieve an array of key-value pairs using the entries()
operator.
import { connectIndexedDb, getObjectStore, addItem, keys, values } from '@this-dot/rxidb';
import { Observable } from 'rxjs';
// Connect to an IndexedDB database (create if the database does not exist)
const database$: Observable<IDBDatabase> = connectIndexedDb('example_database');
// Create an Object Store or get an existing one with autoIncrement.
const store$: Observable<IDBObjectStore> = database$.pipe(
getObjectStore('example_store', { autoIncrement: true })
);
// returns an Observable of all the key-value pairs in the store.
const entries$: Observable<{ key: IDBValidKey; value: any }[]> = store$.pipe(entries());
entries$.subscribe(console.log); // emits every time the keys or the values in the database change. (add a new key, remove a key, update a value, etc)
The deleteDatabase()
operator can be used to delete a database. This will complete the existing observables subscribed to database events.
import { connectIndexedDb, getObjectStore, read, setItem, deleteItem } from '@this-dot/rxidb';
import { Observable } from 'rxjs';
// Connect to an IndexedDB database (create if the database does not exist)
const database$: Observable<IDBDatabase> = connectIndexedDb('example_database');
// Create an Object Store or get an existing one.
const store$: Observable<IDBObjectStore> = database$.pipe(getObjectStore('example_store'));
// set up the read stream.
const exampleKey$: Observable<string> = store$.pipe(read('example_key'));
exampleKey$.subscribe(console.log); // emits every time the value changes, and emits a `null` value when the database is deleted.
// exampleKey$ will emit 4 times;
store$
.pipe(
setItem('example_key', 'example_value_1'),
setItem('example_key', 'example_value_2'),
setItem('example_key', 'example_value_3'),
deleteItem('example_key')
)
.subscribe();
// deletes the database
deleteDatabase('example_database').subscribe();
It throws an error if the database does not exist.
Please keep in mind, that if you would like to use the observables after you delete the database, you need to reinitalise them again.
FAQs
A helper library to handle IndexedDb operations using RxJS
The npm package @this-dot/rxidb receives a total of 1 weekly downloads. As such, @this-dot/rxidb popularity was classified as not popular.
We found that @this-dot/rxidb demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.