https://github.com/shevernitskiy/persistent-object
Persistent object with proxy features. Every mutation synced to storage.
https://github.com/shevernitskiy/persistent-object
deno persistence typescript
Last synced: about 1 month ago
JSON representation
Persistent object with proxy features. Every mutation synced to storage.
- Host: GitHub
- URL: https://github.com/shevernitskiy/persistent-object
- Owner: shevernitskiy
- License: mit
- Created: 2023-02-18T17:34:46.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-04-30T15:24:12.000Z (about 3 years ago)
- Last Synced: 2025-09-19T23:00:54.655Z (9 months ago)
- Topics: deno, persistence, typescript
- Language: TypeScript
- Homepage:
- Size: 12.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# JS/TS Persistent object
This tiny function provide persistences for json serializable object. It's possible with using proxy and set/get
callbacks. Everytime you mutate the object, it calls defined `save` callback. Default value needed for deep nesting
support.
## Example
Example with storage in simple json file.
```ts
import { persistent } from "https://deno.land/x/persistent@v1.2.0/mod.ts";
type Schema = {
name: string;
age: number;
job: {
salary: number;
};
};
const path = "./state.json";
const defaults: Schema = {
name: "",
age: 0,
job: {
salary: 0,
},
};
const state = await persistent(defaults, {
read: () => {
let data: Schema = defaults;
try {
data = JSON.parse(Deno.readTextFileSync(path));
} catch {
//
}
return data;
},
save: (value: Schema) => Deno.writeTextFileSync(path, JSON.stringify(value, null, 2)),
}); // read json from path and return proxyfied object
state.job.salary = 35; // saved with provided callback
```
# Contribution
Pull request, issues and feedback are very welcome. Code style is formatted with `deno fmt`.
# License
Copyright 2023, shevernitskiy. MIT license.