|
|
|
@ -1,5 +1,5 @@
|
|
|
|
|
# @push.rocks/webstore
|
|
|
|
|
high performance storage in the browser using indexed db
|
|
|
|
|
High performance storage in the browser using IndexedDB.
|
|
|
|
|
|
|
|
|
|
## Install
|
|
|
|
|
To install `@push.rocks/webstore`, use the following npm command:
|
|
|
|
@ -13,7 +13,7 @@ This will add it to your project's dependencies.
|
|
|
|
|
## Usage
|
|
|
|
|
The `@push.rocks/webstore` module provides a high-performance storage solution for web applications, leveraging IndexedDB. This guide demonstrates how to use `@push.rocks/webstore` to store, retrieve, check, and manage data in the browser efficiently.
|
|
|
|
|
|
|
|
|
|
### Setting Up
|
|
|
|
|
### Basic Setup
|
|
|
|
|
Before using `@push.rocks/webstore`, you must import and instantiate a `WebStore` class. Specify a database name (`dbName`) and a store name (`storeName`) in the options:
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
@ -27,8 +27,8 @@ const myStore = new WebStore<{
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Initializing the Store
|
|
|
|
|
To ensure the IndexedDB is set up correctly, call the `init` method before executing CRUD operations:
|
|
|
|
|
### Initialization
|
|
|
|
|
To ensure that IndexedDB is set up correctly, call the `init` method before executing any CRUD operations:
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
await myStore.init();
|
|
|
|
@ -79,9 +79,31 @@ const allKeys = await myStore.keys();
|
|
|
|
|
console.log(allKeys); // ['myKey', ...]
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Typed Request Caching
|
|
|
|
|
### Error Handling
|
|
|
|
|
One of the strengths of `WebStore` is its built-in error handling using `smartpromise`. All the asynchronous operations (`init`, `get`, `set`, `delete`, `clear`, `keys`) inherently handle errors gracefully by catching them and allowing you to handle them with standard JavaScript try-catch or using `.catch` on the promises.
|
|
|
|
|
|
|
|
|
|
#### Error Handling Example
|
|
|
|
|
In TypeScript, you can write try-catch blocks to handle errors:
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
try {
|
|
|
|
|
await myStore.set('myKey', { some: 'data' });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error storing data:', error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const myData = await myStore.get('myKey');
|
|
|
|
|
console.log(myData);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error retrieving data:', error);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Advanced Features: Typed Request Caching
|
|
|
|
|
`@push.rocks/webstore` also includes a feature for caching typed requests using the `TypedrequestCache` class. This is particularly useful for caching API requests and their responses.
|
|
|
|
|
|
|
|
|
|
#### Setting Up a Typed Request Cache
|
|
|
|
|
To set up a `TypedrequestCache`:
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
@ -90,6 +112,7 @@ import { TypedrequestCache } from '@push.rocks/webstore';
|
|
|
|
|
const myCache = new TypedrequestCache('domainIdentifier');
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Storing a Request and Its Response
|
|
|
|
|
Store a request and its response:
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
@ -100,6 +123,7 @@ await myCache.setByRequest({
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Retrieving a Cached Request
|
|
|
|
|
Retrieve a cached request by making a partial request:
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
@ -110,7 +134,77 @@ const cachedResponse = await myCache.getByRequest({
|
|
|
|
|
console.log(cachedResponse); // { data: 'response data' }
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
`@push.rocks/webstore` provides a comprehensive API to leverage browser storage efficiently while maintaining high performance.
|
|
|
|
|
### Comprehensive Usage Example
|
|
|
|
|
Here is a comprehensive example that covers initialization, data manipulation, and error handling:
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
import { WebStore, TypedrequestCache } from '@push.rocks/webstore';
|
|
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
|
const myStore = new WebStore<{
|
|
|
|
|
[key: string]: any;
|
|
|
|
|
}>({
|
|
|
|
|
dbName: 'myDatabase',
|
|
|
|
|
storeName: 'myStore',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await myStore.init();
|
|
|
|
|
|
|
|
|
|
// Set data
|
|
|
|
|
await myStore.set('myKey', { some: 'data' });
|
|
|
|
|
console.log('Data set successfully.');
|
|
|
|
|
|
|
|
|
|
// Get data
|
|
|
|
|
const myData = await myStore.get('myKey');
|
|
|
|
|
console.log('Retrieved data:', myData); // { some: 'data' }
|
|
|
|
|
|
|
|
|
|
// Check data existence
|
|
|
|
|
const exists = await myStore.check('myKey');
|
|
|
|
|
console.log('Key exists:', exists); // true
|
|
|
|
|
|
|
|
|
|
// Delete data
|
|
|
|
|
await myStore.delete('myKey');
|
|
|
|
|
console.log('Data deleted.');
|
|
|
|
|
|
|
|
|
|
// Clear store
|
|
|
|
|
await myStore.clear();
|
|
|
|
|
console.log('Store cleared.');
|
|
|
|
|
|
|
|
|
|
// Fetch all keys
|
|
|
|
|
const allKeys = await myStore.keys();
|
|
|
|
|
console.log('All keys:', allKeys); // []
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error during storage operations:', error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Typed Request Caching
|
|
|
|
|
const myCache = new TypedrequestCache('exampleDomain');
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await myCache.setByRequest({
|
|
|
|
|
method: 'GET',
|
|
|
|
|
request: 'https://example.com/api/data',
|
|
|
|
|
response: { data: 'response data' }
|
|
|
|
|
});
|
|
|
|
|
console.log('Typed request cached.');
|
|
|
|
|
|
|
|
|
|
const cachedResponse = await myCache.getByRequest({
|
|
|
|
|
method: 'GET',
|
|
|
|
|
request: 'https://example.com/api/data'
|
|
|
|
|
});
|
|
|
|
|
console.log('Cached response:', cachedResponse); // { data: 'response data' }
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error during typed request caching operations:', error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
main().catch(console.error);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Conclusion
|
|
|
|
|
The `@push.rocks/webstore` package provides a flexible and efficient way to handle browser storage and caching. With features like error handling, typed request caching, and easy-to-use APIs, it can significantly improve performance and maintainability in web applications. For further exploration, feel free to explore the source code and tests provided in the repository.
|
|
|
|
|
|
|
|
|
|
## License and Legal Information
|
|
|
|
|
|
|
|
|
|