Class: DataStorage<Key, Value, CacheDisabled>
Defined in: packages/rx/src/data-storage/DataStorage.ts:58
Type Parameters
Key
Key extends StorageKey
Value
Value extends StorageValue
CacheDisabled
CacheDisabled extends boolean = false
Implements
IDataStorage<Key,Value,CacheDisabled>
Constructors
Constructor
new DataStorage<
Key,Value,CacheDisabled>(name?,options?):DataStorage<Key,Value,CacheDisabled>
Defined in: packages/rx/src/data-storage/DataStorage.ts:179
A wrapper for reading and writing to LocalStorage (browser) or JSON files (NodeJS), providing a Map-like interface with advanced features like search, filtering, and sorting.
Notes:
- Performance:
DataStorageis optimized for small to medium datasets.- For datasets > 1MB, consider increasing the
delayoption to reduce write frequency. - It is NOT recommended for datasets larger than 3MB due to synchronous serialization costs.
- For datasets > 1MB, consider increasing the
- RxJS Integration: Built on RxJS for reactive data handling, though no prior RxJS knowledge is required.
- Storage Behavior:
- If
nameis omitted, the instance operates in-memory only and data is not persisted to storage. - If
cacheDisabledistrue, data is not kept in memory; every read/write operation accesses the underlying storage directly.
- If
Parameters
name?
string | null
options?
StorageOptions<Key, Value, CacheDisabled>
Returns
DataStorage<Key, Value, CacheDisabled>
Examples
Browser Usage
import { DataStorage } from '@superutils/rx'
import fetch from '@superutils/fetch'
const storage = new DataStorage('products')
const { products } = await fetch('[DUMMYJSON-DOT-COM]/products')
// save all items to storage
storage.setAll(
new Map(products.map(p => [p.id, p])), // convert to Map
)
// print product with id `1`
console.log(storage.get(1))
// search for items
const searchResult = storage.search({
query: { availabilityStatus: 'low' }
})
console.log(searchResult)NodeJS Usage
import { DataStorage } from '@superutils/rx'
import fetch from '@superutils/fetch'
import { LocalStorage } from 'node-localstorage'
// Add localStorage alternative for NodeJS that reads and writes to JSON files.
// This is not necessary for browsers.
globalThis.localStorage = new LocalStorage('./data', 1e7)
const storage = new DataStorage('products')
const { products } = await fetch('[DUMMYJSON-DOT-COM]/products')
// save all items to storage
storage.setAll(
new Map(products.map(p => [p.id, p])), // convert to Map
)
// print product with id `1`
console.log(storage.get(1))
// search for items
const searchResult = storage.search({
query: { availabilityStatus: 'low' }
})
console.log(searchResult)Advanced: onChange and RxJS subject
Internally, DataStorage uses RxJS subject which is exposed as subject property. You can use this to subscribe to changes and do additional operations such as logging or sanitization etc.
Alternatively, you can also set the onChange callback which is triggered whenever the subject changes and does not require maintaining a subscription or knowledge of RxJS subject.
import { DataStorage } from '@superutils/rx'
const storage = new DataStorage('my-data')
const sub = storage.subject.subscribe(data => {
// Write to the database whenever data changes
console.log('Saving to database...', data)
})
// unsubscribe from subject
setTimeout(()=> sub.unsbuscribe(), 1000)
// add an entry to storage
storage.set('bob', { age: 99, id: 'bob', name: 'Bob' })Properties
cacheDisabled
readonlycacheDisabled:CacheDisabled
Defined in: packages/rx/src/data-storage/DataStorage.ts:63
Disable in-memory cache and only directly read/write from storage (local storage or JSON fle)
Implementation of
delay
readonlydelay:number
Defined in: packages/rx/src/data-storage/DataStorage.ts:65
Debounce/throttle delay duration in milliseconds for writing to storage when caching is enabled.
Increasing this value can improve performance when dealing with large datasets or frequent updates by reducing the number of write operations.
Default: 300
Implementation of
delayOptions?
readonlyoptionaldelayOptions:DelayOptions
Defined in: packages/rx/src/data-storage/DataStorage.ts:68
Debounce and throttle related options
Implementation of
filter
readonlyfilter:StorageFilter<Key,Value>
Defined in: packages/rx/src/data-storage/DataStorage.ts:248
Filter items by predicate
Implementation of
find
readonlyfind:StorageFind<Key,Value>
Defined in: packages/rx/src/data-storage/DataStorage.ts:242
Find an item by predicate or search criteria
Implementation of
initialized
readonlyinitialized:boolean=false
Defined in: packages/rx/src/data-storage/DataStorage.ts:70
Indicates wherether storage has been initialized (init() function invoked).
Implementation of
map
readonlymap:StorageMap<Key,Value>
Defined in: packages/rx/src/data-storage/DataStorage.ts:351
Map each item on the data to an Array
Implementation of
name
readonlyname:string
Defined in: packages/rx/src/data-storage/DataStorage.ts:72
Storage name. Filename (NodeJS) or property name (browser LocalStorage). If empty string or undefined, data will not be saved to storage and will only work in-memory.
Default: ''
Implementation of
onChange?
optionalonChange:StorageOnChangeFn<Key,Value>
Defined in: packages/rx/src/data-storage/DataStorage.ts:356
Callback to be invoked whenever value change is triggered.
If onChange invocation fails, it will be ignored gracefully.
Implementation of
onError?
optionalonError:StorageOnErrorFn
Defined in: packages/rx/src/data-storage/DataStorage.ts:358
Callback to be invoked whenever read/write operation fails.
If onError invocation failure will be ignored gracefully.
Implementation of
parse?
readonlyoptionalparse:StorageParseFn<Key,Value>
Defined in: packages/rx/src/data-storage/DataStorage.ts:360
A callback to customize the deserialization of data read from storage.
This can be used to override the default JSON.parse behavior and serves as the counterpart to stringify. If this function is not provided, throws an error, or returns undefined, the default JSON.parse will be used as a fallback.
Implementation of
search
readonlysearch:StorageSearch<Key,Value>
Defined in: packages/rx/src/data-storage/DataStorage.ts:383
Search items
Implementation of
sort
readonlysort:StorageSort<Key,Value>
Defined in: packages/rx/src/data-storage/DataStorage.ts:401
Sort items in the storage.
Param
Criteria to sort by. Accepts one of the following:
function: A comparator function to sort the data.string: A property name of the value object to sort by.true: Sorts the map by its keys.
Param
(optional) Sorting options.
Param
(optional) Whether to save the sorted data back to storage (localStorage/file).
Returns
The sorted Map.
Implementation of
spaces?
optionalspaces:number
Defined in: packages/rx/src/data-storage/DataStorage.ts:78
Number of spaces to use when stringifying. Default: undefined
Implementation of
storage?
readonlyoptionalstorage:StorageCompact|null
Defined in: packages/rx/src/data-storage/DataStorage.ts:80
LocalStorage or equivalent storage instance to be used as the underlying storage and to read & write from.
Notes:
- Ignored when
nameis falsy (in-memory only mode) - For NodeJS or equivalent, an instance of
LocalStoragefrom "node-localstoarge" NPM module can be used. - If
undefined, will not attempt to useglobalThis.localStorage, if available - If
null, will not attempt to useglobalThis.localStorage
Default:
- browser:
localStorage - node:
undefined(in-memory mode)
Implementation of
stringify?
readonlyoptionalstringify:StorageStringifyFn<Key,Value>
Defined in: packages/rx/src/data-storage/DataStorage.ts:415
Callback function to customize the serialization of data to be stored to storage.
Useful when data needs to be sanitised before storing and/or remove circlar references.
Example
import fetch from '@superutils/fetch'
import { DataStorage } from '@superutils/rx'
import { LocalStorage } from 'node-localstorage'
// Create a localStorage alternative for NodeJS that reads and writes to JSON files.
// This is not necessary for browsers
globalThis.localStorage = new LocalStorage('./data', 1e7)
const storage = new DataStorage('products.json')
storage.stringify = data => Array
.from(data)
.map(([key, product]) => [
key,
{ id: product.id, title: product.title } // only store what's needed
])
const { products } = await fetch('[DUMMYJSON-DOT-COM]/products)
const productsMap = result.products.map(p => [p.id, p])
storage.setAll(productsMap, true)
console.log(storage.getAll())Implementation of
subject
readonlysubject:CacheDisabledextendstrue?Subject<Map<Key,Value>> :BehaviorSubject<Map<Key,Value>>
Defined in: packages/rx/src/data-storage/DataStorage.ts:82
The underlying RxJS Subject that serves as the primary reactive interface for observing data modifications.
Its implementation type is determined by the caching strategy:
- BehaviorSubject: Used when caching is enabled. It maintains the current state and emits it immediately to new subscribers.
- Subject: Used when caching is disabled. It acts as a pure event pipe, emitting updates only at the moment they occur without retaining an in-memory copy.
Implementation of
toJSON
readonlytoJSON:StorageToJSON<Key,Value>
Defined in: packages/rx/src/data-storage/DataStorage.ts:419
Convert list of items (Map) to JSON string of 2D Array
Implementation of
Accessors
size
Get Signature
get size():
number
Defined in: packages/rx/src/data-storage/DataStorage.ts:74
Get the number of items
Returns
number
Get the number of items
Implementation of
Methods
clear()
readonlyclear():DataStorage<Key,Value,CacheDisabled>
Defined in: packages/rx/src/data-storage/DataStorage.ts:227
Clear all items
Returns
DataStorage<Key, Value, CacheDisabled>
Implementation of
delete()
readonlydelete(keys):DataStorage<Key,Value,CacheDisabled>
Defined in: packages/rx/src/data-storage/DataStorage.ts:232
Delete one or more items by their respective keys
Parameters
keys
Key | Key[]
Returns
DataStorage<Key, Value, CacheDisabled>
Implementation of
get()
readonlyget(key):Value|undefined
Defined in: packages/rx/src/data-storage/DataStorage.ts:278
Get item by key
Parameters
key
Key
Returns
Value | undefined
Implementation of
getAll()
readonlygetAll(forceRead):Map<Key,Value>
Defined in: packages/rx/src/data-storage/DataStorage.ts:280
Get all items
Parameters
forceRead
boolean = false
Returns
Map<Key, Value>
Implementation of
has()
readonlyhas(key):boolean
Defined in: packages/rx/src/data-storage/DataStorage.ts:295
Check if key exists
Parameters
key
Key
Returns
boolean
Implementation of
init()
readonlyinit(initialValue?):boolean
Defined in: packages/rx/src/data-storage/DataStorage.ts:297
Parameters
initialValue?
Map<Key, Value>
An optional map to initialize the storage with if it's currently empty.
Returns
boolean
true if initialization was successful, or false if the storage was already initialized.
Implementation of
keys()
readonlykeys():Key[]
Defined in: packages/rx/src/data-storage/DataStorage.ts:349
Get all keys
Returns
Key[]
Implementation of
read()
readonlyread():Map<Key,Value>
Defined in: packages/rx/src/data-storage/DataStorage.ts:362
Read directly from the localStorage (browser) or file (NodeJS) without triggering the this.subject.
Returns
Map<Key, Value>
Implementation of
set()
readonlyset(key,value):DataStorage<Key,Value,CacheDisabled>
Defined in: packages/rx/src/data-storage/DataStorage.ts:386
Set item by key
Parameters
key
Key
value
Value
Returns
DataStorage<Key, Value, CacheDisabled>
Implementation of
setAll()
readonlysetAll(data,replace):DataStorage<Key,Value,CacheDisabled>
Defined in: packages/rx/src/data-storage/DataStorage.ts:391
Set multiple entries at once and/or replace the storage entries
Parameters
data
Map<Key, Value> = ...
replace
boolean = false
Returns
DataStorage<Key, Value, CacheDisabled>
Implementation of
toArray()
readonlytoArray(): [Key,Value][]
Defined in: packages/rx/src/data-storage/DataStorage.ts:417
Convert list of items (Map) to 2D Array
Returns
[Key, Value][]
Implementation of
toObject()
readonlytoObject():Record<Key,Value>
Defined in: packages/rx/src/data-storage/DataStorage.ts:440
Convert list of items into an object
Returns
Record<Key, Value>
Implementation of
toString()
readonlytoString(data?):string
Defined in: packages/rx/src/data-storage/DataStorage.ts:447
Convert list of items (Map) to JSON string of 2D Array
Parameters
data?
Map<Key, Value>
Returns
string
Implementation of
unsubscribe()
readonlyunsubscribe():void
Defined in: packages/rx/src/data-storage/DataStorage.ts:455
Unsubscribe from all internal subscriptions.
This will result in:
- Automatic writing to storage being disabled (manual writes via
instance.write()will still work). - The
onChangecallback no longer being triggered. - The instance stopping listening to force update cache triggers.
Returns
void
Implementation of
values()
readonlyvalues():Value[]
Defined in: packages/rx/src/data-storage/DataStorage.ts:457
Get all values as an array
Returns
Value[]
Implementation of
write()
readonlywrite(data?):boolean
Defined in: packages/rx/src/data-storage/DataStorage.ts:459
Parameters
data?
Map<Key, Value>
(optional) Data to write.
- If provided, it overwrites the storage.
- If not provided, the current in-memory data is used (if cache is enabled).
Returns
boolean
true if the write was successful, false otherwise.
Implementation of
forceUpdateCache()
staticforceUpdateCache(name):void
Defined in: packages/rx/src/data-storage/DataStorage.ts:274
Trigger forced update of cached data from storage.
Parameters
name
determines which storage instances to be updated.
- name (
string|string[]): update all instances with a specific name(s) - global (
true): update all instances globally
string | true | string[]
Returns
void
Example
import { DataStorage } from '@superutils/rx'
// Update all DataStorage instances with specific name(s)
const name = 'products'
DataStorage.forceUpdateCache([name])
// Update every single instance of DataStorage that uses storage (has a "name")
DataStorage.forceUpdateCache(true)