vault-storage
Advanced tools
Comparing version 1.0.1 to 1.0.2
const RW = 'readwrite'; | ||
const R = 'readonly'; | ||
let db = null; | ||
const storeName = "vault"; | ||
const vault = new Proxy({ | ||
db: null, | ||
store: "vault", | ||
init(database, store) { | ||
this.db = database; | ||
this.store = store; | ||
async setItem(key, value) { | ||
return $(RW, store => store.put({ key, value })); | ||
}, | ||
async set(key, value) { | ||
return $(this, RW, store => store.put({ key, value })); | ||
}, | ||
async get(key) { | ||
return $(this, R, store => store.get(key)) | ||
async getItem(key) { | ||
return $(R, store => store.get(key)) | ||
.then(result => result ? result.value : null); | ||
}, | ||
async remove(key) { | ||
return $(this, RW, store => store.delete(key)); | ||
async removeItem(key) { | ||
return $(RW, store => store.delete(key)); | ||
}, | ||
async clear() { | ||
return $(this, RW, store => store.clear()); | ||
return $(RW, store => store.clear()); | ||
}, | ||
async length() { | ||
return $(this, R, store => store.count()); | ||
return $(R, store => store.count()); | ||
} | ||
}, { | ||
get(target, key) { | ||
if (typeof target[key] === 'function' || key in target) { | ||
return target[key]; | ||
} | ||
else { | ||
return target.get(key); | ||
} | ||
return target[key] || target.getItem(key); | ||
}, | ||
set(target, key, value) { | ||
if (typeof target[key] === 'function' || key in target) { | ||
target[key] = value; | ||
} | ||
else { | ||
target.set(key, value); | ||
} | ||
target.setItem(key, value); | ||
return true; | ||
}, | ||
deleteProperty(target, key) { | ||
return target.removeItem(key); | ||
} | ||
}); | ||
async function $(v, operationType, operation) { | ||
let { db, store: storeName } = v; | ||
async function $(operationType, operation) { | ||
db = db || await initDB(location.host, storeName); | ||
v.db = db; | ||
return new Promise((resolve, reject) => { | ||
@@ -50,0 +37,0 @@ const request = operation(db.transaction(storeName, operationType) |
{ | ||
"name": "vault-storage", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Vault, a tiny yet robuts storage library for your browser applications", | ||
@@ -19,7 +19,4 @@ "main": "dist/index.js", | ||
"@types/jasmine": "^5.1.4", | ||
"@types/jest": "^29.5.7", | ||
"@types/node": "^20.10.6", | ||
"jasmine-core": "^5.1.1", | ||
"jest": "^29.7.0", | ||
"jest-environment-jsdom": "^29.7.0", | ||
"karma": "^6.4.2", | ||
@@ -30,3 +27,2 @@ "karma-chrome-launcher": "^3.2.0", | ||
"karma-typescript": "^5.5.4", | ||
"ts-jest": "^29.1.1", | ||
"ts-node": "^10.9.2", | ||
@@ -33,0 +29,0 @@ "typescript": "^5.3.3" |
108
README.md
# vault | ||
`vault` is a sophisticated browser-based storage library that leverages the power of IndexedDB, offering significant improvements over traditional LocalStorage. As a high-performance, asynchronous solution for client-side storage, `vault` provides an intuitive and easy-to-use API to interact with IndexedDB, making client-side data storage efficient and scalable. | ||
`vault` is a sophisticated browser-based storage library that leverages the power | ||
of IndexedDB, offering significant improvements over traditional LocalStorage. | ||
As a high-performance, asynchronous solution for client-side storage, `vault` | ||
provides an intuitive and easy-to-use API to interact with IndexedDB, making | ||
client-side data storage efficient and scalable. | ||
## Features | ||
- **Simple API**: Easy-to-use asynchronous methods for common IndexedDB operations. | ||
- **Proxy-based Access**: Intuitive syntax for data manipulation using JavaScript Proxy. | ||
- **Flexible and Lightweight**: Minimal setup required, suitable for various web applications. | ||
- **Similar API**: Easy to use, similar to LocalStorage. | ||
- **Lightweight**: No dependencies, small footprint (~800 bytes minified, ~400 bytes gzipped). | ||
- **Asynchronous**: Non-blocking, asynchronous API. | ||
- **Structured Data**: Supports structured data, including objects and arrays. | ||
@@ -33,17 +38,47 @@ ## Installation | ||
### Initializing | ||
### Initializing and Setup | ||
Before performing any operations, initialize `vault-storage`: | ||
By default, the `vault` does not need any special initialization or setup!!! | ||
In this way, it behaves similar to the local and session storages, It uses | ||
default database and store names. | ||
> **Just start using it!** | ||
```javascript | ||
// Initialize with default settings | ||
await vault.init(); | ||
// Set the values. | ||
vault.key1 = "value1"; | ||
vault.key2 = "value2"; | ||
// Get the values. Remember to use await! As it's asynchronous. | ||
const value1 = await vault.key1; // "value1" | ||
const value2 = await vault.key2; // "value2" | ||
``` | ||
However, if you want to change the default database and store names, you can | ||
initialize the `vault` with your custom database and store names before using | ||
it. | ||
```javascript | ||
// Initialize the vault with custom database and store names. | ||
vault.init('myDatabase', 'myStore'); | ||
``` | ||
> **Note:** This will change the default database and store names for all | ||
> instances of `vault` in your application. | ||
### Setting Values | ||
Store data using the `set` method: | ||
Store data using the `setItem` method, indexer syntax, or dot notation: | ||
```javascript | ||
await vault.set('yourKey', { any: 'data' }); | ||
// For set operation you can ignore await unless you want to wait for the | ||
// operation to complete or you want to catch any errors. | ||
vault.setItem('yourKey', { any: 'data' }); | ||
// Indexer syntax. | ||
vault['yourKey'] = { any: 'data' }; | ||
// Dot notation. | ||
vault.yourKey = { any: 'data' }; | ||
``` | ||
@@ -53,7 +88,14 @@ | ||
Retrieve data using the `get` method: | ||
Retrieve data using the `getItem` method, indexer syntax, or dot notation. For get | ||
operations you must use await as it's asynchronous. | ||
```javascript | ||
const data = await vault.get('yourKey'); | ||
console.log(data); | ||
// Get the value using the getItem method. | ||
const data = await vault.getItem('yourKey'); | ||
// Indexer syntax. | ||
const data = await vault['yourKey']; | ||
// Dot notation. | ||
const data = await vault.yourKey; | ||
``` | ||
@@ -63,6 +105,13 @@ | ||
Remove data using the `remove` method: | ||
Remove data using the `removeItem` method: | ||
```javascript | ||
await vault.remove('yourKey'); | ||
// Remove the value using the remove method. | ||
vault.removeItem('yourKey'); | ||
// Indexer syntax. | ||
delete vault['yourKey']; | ||
// Dot notation. | ||
delete vault.yourKey; | ||
``` | ||
@@ -89,9 +138,20 @@ | ||
- `init(database?: IDBDatabase, store?: string)`: Initialize the database. Optional custom database and store name can be provided. | ||
- `set(key: string, value: any)`: Store data in the database. | ||
- `get(key: string)`: Retrieve data from the database. | ||
- `remove(key: string)`: Remove data from the database. | ||
- `setItem(key: string, value: any)`: Store data in the database. | ||
- `getItem(key: string)`: Retrieve data from the database. | ||
- `removeItem(key: string)`: Remove data from the database. | ||
- `clear()`: Clear all data from the database. | ||
- `length()`: Get the count of entries in the database. | ||
## Comparing `vault` with LocalStorage | ||
| Feature | `vault` (IndexedDB) | LocalStorage | | ||
|--------------------------|--------------------------|------------------------| | ||
| **API Complexity** | Simple, intuitive API | Simple, intuitive API | | ||
| **Capacity** | Large (up to browser limit, often no less than 250MB) | Limited (5MB typical) | | ||
| **Data Types** | Supports structured data, including objects and arrays | Only stores strings | | ||
| **Performance** | Asynchronous, non-blocking | Synchronous, can block UI | | ||
| **Transaction Support** | Complete transaction support for reliable data operations | None | | ||
| **Query Capabilities** | Complex querying and indexing | Only key-based access | | ||
| **Data Integrity** | Robust with versioning and error handling | Prone to data conflicts | | ||
## Contributing | ||
@@ -104,11 +164,1 @@ | ||
`vault-storage` is [MIT licensed](./LICENSE). | ||
--- | ||
### Notes: | ||
- Adjust and expand the sections as necessary to fit the specifics of `vault-storage`. | ||
- If you have additional configuration options, advanced usage, or a more complex API, include those details in the README. | ||
- Consider adding a `LICENSE` file to your repository if not already present. | ||
- Include information about testing, contribution guidelines, and any other relevant documentation links if available. | ||
- Personalize the introduction and description to reflect the unique features and benefits of `vault-storage`. |
@@ -5,2 +5,7 @@ // Constants for read and write operations | ||
// The database instance | ||
let db = null as IDBDatabase | null | ||
// The name of the store within the database | ||
const storeName = "vault" | ||
/** | ||
@@ -10,19 +15,4 @@ * The vault, an object provides an easy to | ||
const vault = new Proxy({ | ||
// The database instance | ||
db: null as IDBDatabase | null, | ||
// The name of the store within the database | ||
store: "vault", | ||
/** | ||
* Initialize the database and store name. | ||
* @param {IDBDatabase} database - The database instance. | ||
* @param {string} store - The name of the store within the database. | ||
*/ | ||
init(database: IDBDatabase, store: string): void { | ||
this.db = database; | ||
this.store = store; | ||
}, | ||
/** | ||
* Set a value in the store. | ||
* Sets a value in the store. | ||
* @param {string} key - The key to set. | ||
@@ -32,6 +22,7 @@ * @param {any} value - The value to set. | ||
*/ | ||
async set(key: string, value: any): Promise<void> { | ||
return $(this, RW, store => store.put({ key, value })); | ||
async setItem(key: string, value: any): Promise<void> { | ||
return $(RW, store => store.put({ key, value })); | ||
}, | ||
/** | ||
@@ -42,4 +33,4 @@ * Get a value from the store. | ||
*/ | ||
async get(key: string): Promise<any> { | ||
return $(this, R, store => store.get(key)) | ||
async getItem(key: string): Promise<any> { | ||
return $(R, store => store.get(key)) | ||
.then(result => result ? result.value : null); | ||
@@ -53,4 +44,4 @@ }, | ||
*/ | ||
async remove(key: string): Promise<void> { | ||
return $(this, RW, store => store.delete(key)); | ||
async removeItem(key: string): Promise<void> { | ||
return $(RW, store => store.delete(key)); | ||
}, | ||
@@ -63,3 +54,3 @@ | ||
async clear(): Promise<void> { | ||
return $(this, RW, store => store.clear()); | ||
return $(RW, store => store.clear()); | ||
}, | ||
@@ -72,3 +63,3 @@ | ||
async length(): Promise<number> { | ||
return $(this, R, store => store.count()); | ||
return $(R, store => store.count()); | ||
} | ||
@@ -82,7 +73,3 @@ }, { | ||
get(target: any, key: string) { | ||
if (typeof target[key] === 'function' || key in target) { | ||
return target[key]; | ||
} else { | ||
return target.get(key); | ||
} | ||
return target[key] || target.getItem(key); | ||
}, | ||
@@ -96,8 +83,12 @@ | ||
set(target: any, key: string, value: any) { | ||
if (typeof target[key] === 'function' || key in target) { | ||
target[key] = value; | ||
} else { | ||
target.set(key, value); | ||
} | ||
target.setItem(key, value); | ||
return true; // Return true immediately, indicating the set was "handled" | ||
}, | ||
/** | ||
* Proxy deleteProperty handler. | ||
* Deletes the value from the store. | ||
*/ | ||
deleteProperty(target, key) { | ||
return target.removeItem(key); | ||
} | ||
@@ -114,9 +105,6 @@ }); | ||
async function $( | ||
v: typeof vault, | ||
operationType: string, | ||
operationType: IDBTransactionMode, | ||
operation: (store: IDBObjectStore) => IDBRequest | ||
): Promise<any> { | ||
let { db, store: storeName } = v; // Rename destructured 'store' to 'storeName' | ||
db = db || await initDB(location.host, storeName); // Function to initialize the default database | ||
v.db = db; // Update the vault's db property | ||
@@ -160,2 +148,2 @@ return new Promise((resolve, reject) => { | ||
// Export the vault object | ||
export default vault; | ||
export default vault |
@@ -6,5 +6,8 @@ import vault from '../dist/index.js'; | ||
vault.name = "John Doe2"; | ||
const value = await vault.name; | ||
const value = await vault.getItem("name") | ||
expect(value).toBe("John Doe2"); | ||
delete vault["name"]; | ||
expect(await vault.name).toBe(null); | ||
}); | ||
}) |
Sorry, the diff of this file is not supported yet
28465
10
160
341