https://github.com/jacoblincool/file-mapping
A library that maps JSON file and in-memory data, and using lazy-writing strategy to reduce disk I/O.
https://github.com/jacoblincool/file-mapping
file javascript json mapping typescript
Last synced: about 1 year ago
JSON representation
A library that maps JSON file and in-memory data, and using lazy-writing strategy to reduce disk I/O.
- Host: GitHub
- URL: https://github.com/jacoblincool/file-mapping
- Owner: JacobLinCool
- License: mit
- Created: 2022-06-06T20:05:44.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-12-26T00:41:09.000Z (over 1 year ago)
- Last Synced: 2024-12-26T01:23:24.113Z (over 1 year ago)
- Topics: file, javascript, json, mapping, typescript
- Language: TypeScript
- Homepage: https://jacoblincool.github.io/file-mapping/
- Size: 205 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# File Mapping
[](https://www.npmjs.com/package/file-mapping)
A library that maps JSON file and in-memory data, and using lazy-writing strategy to reduce disk I/O.
## Usage
### Simple Usage
```ts
import { mapping } from "file-mapping";
const data = mapping("./data.json", {});
data.name = "Jacob";
data.age = 19;
// Then, the file should be written automatically and only once.
```
### Nested Data
```ts
import { mapping } from "file-mapping";
const data = mapping("./data.json", {});
data.collection = {};
data.collection.document = {};
data.collection.document.something = "something";
// Nested data are also supported.
```
### Listen to file write
```ts
import { mapping } from "file-mapping";
const data = mapping("./data.json", {}, (data, changes) => {
console.log(`write ${changes} changes into disk`, data);
});
for (let i = 0; i < 1000; i++) {
data[`key-${i}`] = `value-${i}`;
}
```
### Event Emitter style
`Mapping` also count the number of write operations internally, and you can use `.data`, `.file`, `.written` to get the informations.
```ts
import { Mapping } from "file-mapping";
const mapping = new Mapping("./data.json", {});
const data = mapping.data;
mapping.on("write", (data, changes) => {
console.log(`write ${changes} changes into disk`, data);
});
for (let i = 0; i < 1000; i++) {
data[`key-${i}`] = `value-${i}`;
}
```
## Links
- NPM Package:
- Documentation:
- GitHub Repository: