# simple-persist

> Typescript decorator for persisting data in browser applications

Latest version **0.0.5** (published 2023-10-09) · ISC license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install simple-persist
pnpm add simple-persist
yarn add simple-persist
bun add simple-persist
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.0.5 |
| Published | 2023-10-09 |
| First published | 2023-10-05 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 44.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Balazs Kovacs |
| Maintainers | kobalazs |

## Links

- npm: https://www.npmjs.com/package/simple-persist
- Repository: https://github.com/kobalazs/simple-persist
- Homepage: https://github.com/kobalazs/simple-persist#readme
- Issues: https://github.com/kobalazs/simple-persist/issues
- npm.io page: https://npm.io/package/simple-persist

## Recent versions

- 0.0.5 (latest) — 2023-10-09
- 0.0.4 — 2023-10-09
- 0.0.3 — 2023-10-06
- 0.0.2 — 2023-10-05
- 0.0.1 — 2023-10-05

## README

# SimplePersist
TypeScript property decorator for easy client-side persistance

## Installation
```
npm install simple-persist
```

## Usage
Add `@Persist()` decorator to a class property

```
class Foo {
  @Persist() public bar;
}
```

## Options

### Custom key
By default SimplePersist saves your property to storage using its name as key. This can easily become an issue for identical property names:
```
class FooA {
  @Persist() public bar;
}
class FooB {
  @Persist() public bar; // Conflicts with FooA.bar!
}
```
To avoid conflict, use a custom key:
```
class FooA {
  @Persist({ key: 'FooA.bar' }) public bar;
}
class FooB {
  @Persist({ key: 'FooB.bar' }) public bar;
}
```

### Custom storage
By default SimplePersist uses `localStorage`. You can switch to `sessionStorage` by setting storage config:
```
class Foo {
  @Persist({ storage: PersistStorage.SessionStorage }) public bar;
}
```

## Caveats

### Types
This package can persist only scalars or objects & arrays containing scalars. (Basically only stuff that survives `JSON.parse(JSON.stringify(value))`.)

### Multi-instance use
Class instances are not observed, meaning the example below causes unexpected behavior:
```
class Foo {
  @Persist() public bar;
}
const foo1 = new Foo();
foo1.bar = 'baz';
const foo2 = new Foo();
console.log(foo2.bar); // will be 'baz'
```
SimplePersist in its current state is the best fit for singleton use.

## Collaboration

SimplePersist is an infant project but I'm willing to extend its functionality with time. Feel free to [open an issue](https://github.com/kobalazs/simple-persist/issues) with feature requests or [contribute](https://github.com/kobalazs/simple-persist/pulls)!

---
_Source: https://npm.io/package/simple-persist · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
