# atomic-file

> store data in a file, but gaurantee there is never a partial write.

Latest version **2.1.1** (published 2020-08-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install atomic-file
pnpm add atomic-file
yarn add atomic-file
bun add atomic-file
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.1 |
| Published | 2020-08-19 |
| First published | 2016-08-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 8.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 8 |
| Author | 'Dominic Tarr' |
| Maintainers | arj03, christianbundy, dominictarr, regular |

## Links

- npm: https://www.npmjs.com/package/atomic-file
- Repository: https://github.com/dominictarr/atomic-file
- Issues: https://github.com/dominictarr/atomic-file/issues
- npm.io page: https://npm.io/package/atomic-file

## Dependencies (3)

- [mutexify](https://npm.io/package/mutexify.md) ^1.2.0
- [flumecodec](https://npm.io/package/flumecodec.md) 0.0.1
- [idb-kv-store](https://npm.io/package/idb-kv-store.md) ^4.5.0

## Recent versions

- 2.1.1 (latest) — 2020-08-19
- 2.1.0 — 2020-08-15
- 2.0.1 — 2019-06-14
- 2.0.0 — 2019-03-25
- 1.1.5 — 2018-05-05
- 1.1.4 — 2017-12-06
- 1.1.3 — 2017-08-12
- 1.1.2 — 2017-08-12
- 1.1.1 — 2017-08-12
- 1.1.0 — 2017-08-04
- 1.0.0 — 2017-08-04
- 0.3.0 — 2016-12-12
- 0.2.0 — 2016-12-11
- 0.1.0 — 2016-10-09
- 0.0.1 — 2016-08-24

## README

# atomic-file

store data in a file, but gaurantee there is never a partial write.

This uses a simple trick to make writes to the file system atomic:
first write the file to `{filename}~`, then `mv {filename}~ {filename}`
although it's possible a program to crash while it's part way through writing a file,
the rename operation (`mv`) can only either succeed or fail.

``` js
var AtomicFile = require('atomic-file') //default, json encoding
// var AtomicFile = require('atomic-file/buffer') //binary

var af = AtomicFile(filename)
//get the current contents
af.get(function (err, value) {
  //write a new value
  af.set(value, function (err) {

  })
})
```

note, get stores the read value in memory, so subsequent
calls to get will be instant. Successful writes will update the value.

## af = AtomicFile(filename, suffix='~', codec=json)

create a new atomic-file instance

### af.get(cb(err, value))

read the current value. will callback synchronously if the value
is already in memory.

### af.set(value, cb)

write `value` to the file atomically. will callback when it's
definitely on disk.

### af.destroy(cb)

delete the underlying file. callback when definitely deleted.

## License

MIT

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