https://github.com/idleberg/bun-storage
A ponyfill for the Storage API, utilizing SQLite
https://github.com/idleberg/bun-storage
bun localstorage polyfill sessionstorage storage-api
Last synced: 9 months ago
JSON representation
A ponyfill for the Storage API, utilizing SQLite
- Host: GitHub
- URL: https://github.com/idleberg/bun-storage
- Owner: idleberg
- License: mit
- Created: 2023-10-13T22:24:01.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2025-04-13T06:03:46.000Z (9 months ago)
- Last Synced: 2025-04-13T07:19:37.458Z (9 months ago)
- Topics: bun, localstorage, polyfill, sessionstorage, storage-api
- Language: TypeScript
- Homepage: https://www.npmjs.org/package/bun-storage
- Size: 90.8 KB
- Stars: 5
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# bun-storage
> A ponyfill for the Storage API, utilizing SQLite
[](https://github.com/idleberg/bun-storage/blob/main/LICENSE)
[](https://www.npmjs.org/package/bun-storage)
[](https://github.com/idleberg/bun-storage/actions)
## Installation
`bun install bun-storage`
## Usage
### API
#### `createLocalStorage`
Usage: `createLocalStorage(dbFile: string)`
Returns: `[Storage, EventEmitter]`
Creates an instance of the [`localStorage`](https://developer.mozilla.org/docs/Web/API/Window/localStorage) API and a corresponding EventEmitter.
**Example:**
```typescript
import { createLocalStorage } from "bun-storage";
const [localStorage, emitter] = createLocalStorage("./db.sqlite");
// Listen for storage changes
emitter.on("storage", console.log);
```
#### `createSessionStorage`
Usage: `createSessionStorage()`
Returns: `[Storage, EventEmitter]`
Creates an instance of the [`sessionStorage`](https://developer.mozilla.org/docs/Web/API/Window/sessionStorage) API and a corresponding EventEmitter.
**Example:**
```typescript
import { createSessionStorage } from "bun-storage";
const [sessionStorage, emitter] = createSessionStorage();
// Listen for storage changes
emitter.on("storage", console.log);
```
#### `Storage` (Advanced Usage)
Usage: `new Storage(filePath: string | ':memory:', options: StorageEventOptions)`
This class is used internally by both of the above factory functions. However, instantiating the class allows you more control over the EventEmitter, i.e. you could re-use an existing one from your application code.
**Example:**
```typescript
import { Storage } from "bun-storage";
import EventEmitter from "events";
const myEmitter = new EventEmitter();
const localStorage = new Storage("./db.sqlite", {
emitter: myEmitter,
});
// Listen for storage changes
myEmitter.on("storage", console.log);
```
## Related
- [`@idleberg/local-storage`](https://www.npmjs.com/package/@idleberg/local-storage): a NodeJS implementation of this package
## License
This work is licensed under [The MIT License](https://opensource.org/licenses/MIT).