Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
@intershare/hooks.indexdb
Advanced tools
`indexDbStore` is a comprehensive library designed to simplify interactions with IndexedDB in a React application. It provides an easy-to-use API for performing common database operations like initializing the database, CRUD operations, and managing data
Readme
indexDbStore
is a comprehensive library designed to simplify interactions with IndexedDB in a React application. It provides an easy-to-use API for performing common database operations like initializing the database, CRUD operations, and managing data efficiently with an integrated garbage collector.
To install the indexDbStore
library, run the following command in your project directory:
npm install @intershare/hooks.indexdb
Or, if you are using yarn
:
yarn add @intershare/hooks.indexdb
Before using indexDbStore
, you need to initialize the IndexedDB database. Here's how you can do it:
import { indexDbStore } from '@intershare/hooks.indexdb'
const dbStore = indexDbStore()
dbStore.initIndexedDb('myDatabaseName')
This will create (or open, if it already exists) an IndexedDB database named 'myDatabaseName'
.
To save data to a specific table:
const id = 'uniqueId'
const data = { key: 'value' }
dbStore.saveData(id, data, ObjectStoresEnum.files)
To retrieve data by ID:
const data = await dbStore.getData('uniqueId', ObjectStoresEnum.files)
To remove data by ID:
await dbStore.removeData('uniqueId', ObjectStoresEnum.files)
indexDbStore
comes with an integrated garbage collector to manage storage space efficiently. The garbage collector can be configured as follows:
dbStore.setConfig({
maxSizeByTable: 1000000, // Max size in bytes
garbageCollector: {
enabled: true,
interval: 60000, // Interval in milliseconds
strategy: 'lru', // Currently, only 'lru' (Least Recently Used) strategy is implemented
},
})
This configuration enables the garbage collector, which will run every 60 seconds and ensure that the size of each table does not exceed 1MB, based on the 'Least Recently Used' strategy.
indexDbStore
uses several TypeScript types for better code management and error handling:
TindexDbStore
: Main store type.ObjectStoresEnum
: Enumeration type for table names.TConfig
: Configuration type for the store and garbage collector.// Example component to initialize the IndexedDB
import React from 'react'
import { indexDbStore } from '@intershare/hooks.indexdb'
const DatabaseInitializer: React.FC = () => {
const dbStore = indexDbStore()
dbStore.initIndexedDb('myDatabase')
return <div>Database Initialized</div>
}
export default DatabaseInitializer
// Example component to save and retrieve data
import React, { useState } from 'react'
import { indexDbStore, ObjectStoresEnum } from '@intershare/hooks.indexdb'
const DataHandler: React.FC = () => {
const [data, setData] = useState(null)
const dbStore = indexDbStore()
const saveData = async () => {
await dbStore.saveData(
'myId',
{ sampleData: 'Hello' },
ObjectStoresEnum.files
)
alert('Data Saved')
}
const loadData = async () => {
const retrievedData = await dbStore.getData('myId', ObjectStoresEnum.files)
setData(retrievedData)
}
return (
<div>
<button onClick={saveData}>Save Data</button>
<button onClick={loadData}>Load Data</button>
{data && <pre>{JSON.stringify(data, null, 2)}</pre>}
</div>
)
}
export default DataHandler
indexDbStore
The getAllKeys
method allows you to retrieve all the keys from a specific table in the IndexedDB. This can be useful for scenarios where you need to list or iterate over all entries in a table.
// Example component to retrieve all keys from a table
import React, { useState } from 'react'
import { indexDbStore, ObjectStoresEnum } from '@intershare/hooks.indexdb'
const AllKeysRetriever: React.FC = () => {
const [keys, setKeys] = useState<IDBValidKey[]>([])
const dbStore = indexDbStore()
const loadAllKeys = async () => {
const allKeys = await dbStore.getAllKeys(ObjectStoresEnum.files)
setKeys(allKeys || [])
}
return (
<div>
<button onClick={loadAllKeys}>Load All Keys</button>
<ul>
{keys.map((key, index) => (
<li key={index}>{key.toString()}</li>
))}
</ul>
</div>
)
}
export default AllKeysRetriever
The getTableStats
method provides statistics for a specified table in the database, such as the total size of the table in bytes. This can be particularly useful for monitoring the database's size and managing storage efficiently.
// Example component to get stats of a table
import React, { useState } from 'react'
import { indexDbStore, ObjectStoresEnum } from '@intershare/hooks.indexdb'
const TableStatsViewer: React.FC = () => {
const [stats, setStats] = useState<number | null>(null)
const dbStore = indexDbStore()
const loadStats = async () => {
const tableStats = await dbStore.getTableStats(ObjectStoresEnum.files)
setStats(tableStats?.size || null)
}
return (
<div>
<button onClick={loadStats}>Load Table Stats</button>
{stats !== null && <p>Size of 'files' table: {stats} bytes</p>}
</div>
)
}
export default TableStatsViewer
The indexDbStore
library includes a garbage collection feature to help manage data storage efficiently. The garbage collector ensures that the size of each table does not exceed a specified limit, removing older entries based on a specified strategy.
You can configure the garbage collector by setting the maxSizeByTable
and garbageCollector
properties in the configuration. The garbageCollector
object includes properties such as enabled
, interval
, and strategy
.
dbStore.setConfig({
maxSizeByTable: 500000, // Maximum size in bytes
garbageCollector: {
enabled: true,
interval: 30000, // Interval in milliseconds
strategy: 'lru', // 'lru' - Least Recently Used
},
})
This configuration will enable the garbage collector, which runs every 30 seconds and ensures the size of each table does not exceed 500KB, using the 'Least Recently Used' strategy.
indexDbStore
We welcome contributions to the indexDbStore
library! Whether it's improving the code, fixing bugs, or adding new features, your input is valuable. Here's how you can contribute:
Our code is hosted on GitHub. Visit our repository to view the source code, fork the project, or submit a pull request:
🔗 ipfs-client-galacfetch on GitHub
Have a suggestion or a bug fix? We encourage you to submit a pull request:
Please ensure your code adheres to the project's coding standards and include tests for new functionality.
Encountered a bug or have a feature request? Open an issue on our GitHub page:
Provide as much information as possible, such as steps to reproduce the bug and the expected behavior.
indexDbStore
is available as an npm package. You can find it here for easy installation and updates:
For direct inquiries or specific concerns, feel free to reach via email at kenneth@interplanetaryshare.com.
FAQs
`indexDbStore` is a comprehensive library designed to simplify interactions with IndexedDB in a React application. It provides an easy-to-use API for performing common database operations like initializing the database, CRUD operations, and managing data
The npm package @intershare/hooks.indexdb receives a total of 0 weekly downloads. As such, @intershare/hooks.indexdb popularity was classified as not popular.
We found that @intershare/hooks.indexdb demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer 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.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.