Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
56d9072996 | |||
ed1c977387 | |||
7425a5c42d | |||
65d5d1f6d3 |
@ -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"
|
||||
]
|
||||
}
|
||||
},
|
||||
|
@ -1,8 +1,8 @@
|
||||
{
|
||||
"name": "@push.rocks/webstore",
|
||||
"version": "2.0.15",
|
||||
"version": "2.0.17",
|
||||
"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"
|
||||
]
|
||||
}
|
||||
|
16
pnpm-lock.yaml
generated
16
pnpm-lock.yaml
generated
@ -26,12 +26,12 @@ importers:
|
||||
'@push.rocks/smartrx':
|
||||
specifier: ^3.0.7
|
||||
version: 3.0.7
|
||||
'@tempfix/idb':
|
||||
specifier: ^8.0.3
|
||||
version: 8.0.3
|
||||
fake-indexeddb:
|
||||
specifier: ^5.0.2
|
||||
version: 5.0.2
|
||||
idb:
|
||||
specifier: ^8.0.0
|
||||
version: 8.0.0
|
||||
devDependencies:
|
||||
'@git.zone/tsbuild':
|
||||
specifier: ^2.1.76
|
||||
@ -608,6 +608,9 @@ packages:
|
||||
resolution: {integrity: sha512-+PmQX0PiAYPMeVYe237LJAYvOMYW1j2rH5YROyS3b4CTVJum34HfRvKvAzozHAQG0TnHNdUfY9nCeUyRAs//cw==}
|
||||
engines: {node: '>=14.16'}
|
||||
|
||||
'@tempfix/idb@8.0.3':
|
||||
resolution: {integrity: sha512-hPJQKO7+oAIY+pDNImrZ9QAINbz9KmwT+yO4iRVwdPanok2YKpaUxdJzIvCUwY0YgAawlvYdffbLvRLV5hbs2g==}
|
||||
|
||||
'@tempfix/watcher@2.3.0':
|
||||
resolution: {integrity: sha512-a2qVQffcrnetehvwsN+LdipxQ6jejwZLgAvS9/91+C0gP4CKyikY01c0tSs0I4tSL7qHdCw1Fx0quLw+A9uyLA==}
|
||||
|
||||
@ -1620,9 +1623,6 @@ packages:
|
||||
idb@7.1.1:
|
||||
resolution: {integrity: sha512-gchesWBzyvGHRO9W8tzUWFDycow5gwjvFKfyV9FF32Y7F50yZMp7mP+T2mJIWFx49zicqyC4uefHM17o6xKIVQ==}
|
||||
|
||||
idb@8.0.0:
|
||||
resolution: {integrity: sha512-l//qvlAKGmQO31Qn7xdzagVPPaHTxXx199MhrAFuVBTPqydcPYBWjkrbv4Y0ktB+GmWOiwHl237UUOrLmQxLvw==}
|
||||
|
||||
ieee754@1.2.1:
|
||||
resolution: {integrity: sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==}
|
||||
|
||||
@ -3925,6 +3925,8 @@ snapshots:
|
||||
dependencies:
|
||||
defer-to-connect: 2.0.1
|
||||
|
||||
'@tempfix/idb@8.0.3': {}
|
||||
|
||||
'@tempfix/watcher@2.3.0':
|
||||
dependencies:
|
||||
stubborn-fs: 1.2.5
|
||||
@ -5138,8 +5140,6 @@ snapshots:
|
||||
|
||||
idb@7.1.1: {}
|
||||
|
||||
idb@8.0.0: {}
|
||||
|
||||
ieee754@1.2.1: {}
|
||||
|
||||
ignore@5.3.1: {}
|
||||
|
106
readme.md
106
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
|
||||
|
||||
|
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/webstore',
|
||||
version: '2.0.15',
|
||||
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.'
|
||||
}
|
||||
|
@ -13,6 +13,6 @@ import * as typedrequestInterfaces from '@apiglobal/typedrequest-interfaces';
|
||||
export { typedrequestInterfaces };
|
||||
|
||||
// thirdparty scope
|
||||
import * as idb from 'idb';
|
||||
import * as idb from '@tempfix/idb';
|
||||
|
||||
export { idb };
|
||||
|
Reference in New Issue
Block a user