From ed1c9773873e7e37871fb975c4090305241fdfec Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Fri, 17 May 2024 17:46:56 +0200 Subject: [PATCH] fix(core): update --- npmextra.json | 6 ++- package.json | 8 +-- readme.md | 106 ++++++++++++++++++++++++++++++++++++--- ts/00_commitinfo_data.ts | 4 +- 4 files changed, 111 insertions(+), 13 deletions(-) diff --git a/npmextra.json b/npmextra.json index ff5d523..6b10b05 100644 --- a/npmextra.json +++ b/npmextra.json @@ -5,7 +5,7 @@ "githost": "code.foss.global", "gitscope": "push.rocks", "gitrepo": "webstore", - "description": "high performance storage in the browser using indexed db", + "description": "A high-performance storage solution for web applications using IndexedDB.", "npmPackagename": "@push.rocks/webstore", "license": "MIT", "projectDomain": "push.rocks", @@ -19,7 +19,9 @@ "data persistence", "storage solution", "typed requests", - "performance optimization" + "performance optimization", + "API caching", + "efficient data management" ] } }, diff --git a/package.json b/package.json index 195f9bc..5c361ce 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "@push.rocks/webstore", "version": "2.0.16", "private": false, - "description": "high performance storage in the browser using indexed db", + "description": "A high-performance storage solution for web applications using IndexedDB.", "main": "dist_ts/index.js", "typings": "dist_ts/index.d.ts", "type": "module", @@ -56,6 +56,8 @@ "data persistence", "storage solution", "typed requests", - "performance optimization" + "performance optimization", + "API caching", + "efficient data management" ] -} +} \ No newline at end of file diff --git a/readme.md b/readme.md index c9fce81..287d2a2 100644 --- a/readme.md +++ b/readme.md @@ -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 diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index f8467b6..ff8a608 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@push.rocks/webstore', - version: '2.0.16', - description: 'high performance storage in the browser using indexed db' + version: '2.0.17', + description: 'A high-performance storage solution for web applications using IndexedDB.' }