Compare commits
13 Commits
Author | SHA1 | Date | |
---|---|---|---|
12119192cf | |||
ed0f497ac9 | |||
7f7c4a28ee | |||
4ee78554a4 | |||
34b90b438c | |||
40dcedf585 | |||
d630bd90a4 | |||
56d9072996 | |||
ed1c977387 | |||
7425a5c42d | |||
65d5d1f6d3 | |||
88fd33d8cc | |||
d11b63637f |
@ -5,7 +5,7 @@
|
|||||||
"githost": "code.foss.global",
|
"githost": "code.foss.global",
|
||||||
"gitscope": "push.rocks",
|
"gitscope": "push.rocks",
|
||||||
"gitrepo": "webstore",
|
"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",
|
"npmPackagename": "@push.rocks/webstore",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"projectDomain": "push.rocks",
|
"projectDomain": "push.rocks",
|
||||||
@ -19,7 +19,9 @@
|
|||||||
"data persistence",
|
"data persistence",
|
||||||
"storage solution",
|
"storage solution",
|
||||||
"typed requests",
|
"typed requests",
|
||||||
"performance optimization"
|
"performance optimization",
|
||||||
|
"API caching",
|
||||||
|
"efficient data management"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
29
package.json
29
package.json
@ -1,8 +1,8 @@
|
|||||||
{
|
{
|
||||||
"name": "@push.rocks/webstore",
|
"name": "@push.rocks/webstore",
|
||||||
"version": "2.0.14",
|
"version": "2.0.20",
|
||||||
"private": false,
|
"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",
|
"main": "dist_ts/index.js",
|
||||||
"typings": "dist_ts/index.d.ts",
|
"typings": "dist_ts/index.d.ts",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
@ -14,22 +14,22 @@
|
|||||||
"buildDocs": "tsdoc"
|
"buildDocs": "tsdoc"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@git.zone/tsbuild": "^2.1.72",
|
"@git.zone/tsbuild": "^2.1.80",
|
||||||
"@git.zone/tsrun": "^1.2.46",
|
"@git.zone/tsrun": "^1.2.46",
|
||||||
"@git.zone/tstest": "^1.0.90",
|
"@git.zone/tstest": "^1.0.90",
|
||||||
"@push.rocks/smartntml": "^2.0.4",
|
"@push.rocks/smartntml": "^2.0.4",
|
||||||
"@push.rocks/tapbundle": "^5.0.23",
|
"@push.rocks/tapbundle": "^5.0.23",
|
||||||
"@types/node": "^20.12.7"
|
"@types/node": "^20.12.12"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@apiglobal/typedrequest-interfaces": "^2.0.1",
|
"@api.global/typedrequest-interfaces": "^3.0.19",
|
||||||
"@push.rocks/lik": "^6.0.14",
|
"@push.rocks/lik": "^6.0.15",
|
||||||
"@push.rocks/smartenv": "^5.0.12",
|
"@push.rocks/smartenv": "^5.0.12",
|
||||||
"@push.rocks/smartjson": "^5.0.19",
|
"@push.rocks/smartjson": "^5.0.19",
|
||||||
"@push.rocks/smartpromise": "^4.0.3",
|
"@push.rocks/smartpromise": "^4.0.3",
|
||||||
"@push.rocks/smartrx": "^3.0.7",
|
"@push.rocks/smartrx": "^3.0.7",
|
||||||
"fake-indexeddb": "^5.0.2",
|
"@tempfix/idb": "^8.0.3",
|
||||||
"idb": "^8.0.0"
|
"fake-indexeddb": "^5.0.2"
|
||||||
},
|
},
|
||||||
"browserslist": [
|
"browserslist": [
|
||||||
"last 1 chrome versions"
|
"last 1 chrome versions"
|
||||||
@ -56,6 +56,13 @@
|
|||||||
"data persistence",
|
"data persistence",
|
||||||
"storage solution",
|
"storage solution",
|
||||||
"typed requests",
|
"typed requests",
|
||||||
"performance optimization"
|
"performance optimization",
|
||||||
]
|
"API caching",
|
||||||
}
|
"efficient data management"
|
||||||
|
],
|
||||||
|
"homepage": "https://code.foss.global/push.rocks/webstore",
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://code.foss.global/push.rocks/webstore.git"
|
||||||
|
}
|
||||||
|
}
|
6894
pnpm-lock.yaml
generated
6894
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
106
readme.md
106
readme.md
@ -1,5 +1,5 @@
|
|||||||
# @push.rocks/webstore
|
# @push.rocks/webstore
|
||||||
high performance storage in the browser using indexed db
|
High performance storage in the browser using IndexedDB.
|
||||||
|
|
||||||
## Install
|
## Install
|
||||||
To install `@push.rocks/webstore`, use the following npm command:
|
To install `@push.rocks/webstore`, use the following npm command:
|
||||||
@ -13,7 +13,7 @@ This will add it to your project's dependencies.
|
|||||||
## Usage
|
## 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.
|
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:
|
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
|
```typescript
|
||||||
@ -27,8 +27,8 @@ const myStore = new WebStore<{
|
|||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
### Initializing the Store
|
### Initialization
|
||||||
To ensure the IndexedDB is set up correctly, call the `init` method before executing CRUD operations:
|
To ensure that IndexedDB is set up correctly, call the `init` method before executing any CRUD operations:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
await myStore.init();
|
await myStore.init();
|
||||||
@ -79,9 +79,31 @@ const allKeys = await myStore.keys();
|
|||||||
console.log(allKeys); // ['myKey', ...]
|
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.
|
`@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`:
|
To set up a `TypedrequestCache`:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
@ -90,6 +112,7 @@ import { TypedrequestCache } from '@push.rocks/webstore';
|
|||||||
const myCache = new TypedrequestCache('domainIdentifier');
|
const myCache = new TypedrequestCache('domainIdentifier');
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Storing a Request and Its Response
|
||||||
Store a request and its response:
|
Store a request and its response:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
@ -100,6 +123,7 @@ await myCache.setByRequest({
|
|||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Retrieving a Cached Request
|
||||||
Retrieve a cached request by making a partial request:
|
Retrieve a cached request by making a partial request:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
@ -110,7 +134,77 @@ const cachedResponse = await myCache.getByRequest({
|
|||||||
console.log(cachedResponse); // { data: 'response data' }
|
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
|
## License and Legal Information
|
||||||
|
|
||||||
|
@ -3,6 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@push.rocks/webstore',
|
name: '@push.rocks/webstore',
|
||||||
version: '2.0.14',
|
version: '2.0.20',
|
||||||
description: 'high performance storage in the browser using indexed db'
|
description: 'A high-performance storage solution for web applications using IndexedDB.'
|
||||||
}
|
}
|
||||||
|
@ -8,11 +8,11 @@ import * as smartrx from '@push.rocks/smartrx';
|
|||||||
export { lik, smartenv, smartjson, smartpromise, smartrx };
|
export { lik, smartenv, smartjson, smartpromise, smartrx };
|
||||||
|
|
||||||
// apiglobal scope
|
// apiglobal scope
|
||||||
import * as typedrequestInterfaces from '@apiglobal/typedrequest-interfaces';
|
import * as typedrequestInterfaces from '@api.global/typedrequest-interfaces';
|
||||||
|
|
||||||
export { typedrequestInterfaces };
|
export { typedrequestInterfaces };
|
||||||
|
|
||||||
// thirdparty scope
|
// thirdparty scope
|
||||||
import * as idb from 'idb';
|
import * as idb from '@tempfix/idb';
|
||||||
|
|
||||||
export { idb };
|
export { idb };
|
||||||
|
Loading…
x
Reference in New Issue
Block a user