https://github.com/smartacephale/lskdb
Local Storage Key DB
https://github.com/smartacephale/lskdb
cdn db keydb localstorage typescript
Last synced: about 1 month ago
JSON representation
Local Storage Key DB
- Host: GitHub
- URL: https://github.com/smartacephale/lskdb
- Owner: smartacephale
- License: mit
- Created: 2024-08-18T20:18:56.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-09-30T03:42:17.000Z (over 1 year ago)
- Last Synced: 2025-03-25T20:33:05.850Z (about 1 year ago)
- Topics: cdn, db, keydb, localstorage, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/lskdb
- Size: 27.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# LSKDB
## Local Storage Key DB

This library provides a simple, type-safe API for managing keys in localStorage with a defined prefix.
### Installation
```bash
npm i lskdb
```
```
...
const { LSKDB } = window.lskdb;
```
### Usage
Import the `LSKDB` class and create an instance:
```typescript
import { LSKDB } from 'lskdb';
const db = new LSKDB();
```
**Available methods:**
* **getAllKeys()**: Returns an array of all keys starting with the configured prefix, without the prefix itself.
* **getKeys(n: number, remove?: boolean)**: Gets a limited number (`n`) of keys with the prefix and optionally removes them from localStorage. Returns an array of keys without the prefix.
* **hasKey(key: string)**: Checks if a specific key with the prefix exists.
* **removeKey(key: string | number)**: Removes a specific key with the prefix from localStorage.
* **setKey(key: string | number)**: Sets an key with the prefix in localStorage.
* **isLocked()**: Checks if the database is locked based on a lock timeout.
* **lock(value: boolean)**: Locks or unlocks the database.
**Example:**
```typescript
db.setKey('user-data');
if (!db.hasKey('settings')) {
db.setKey('settings');
}
const recentKeys = db.getKeys(5); // Get 5 recent keys without removing
console.log(recentKeys);
db.removeKey('user-data');
if (!db.isLocked()) {
// Perform actions while the database is unlocked
db.lock(true); // Lock after use
}
```
### Configuration (Optional)
You can configure the prefix used for keys and the lock key name during initialization:
```typescript
const db = new LSKDB('my-app-', 'lock');
```
**Note:** Lock functionality is based on a simple time-based check. Consider using a more robust locking mechanism for critical applications.