# level-objectify

> compile the contents of a leveldb read stream into an object and create a patch for changes as a batch update

Latest version **0.2.0** (published 2014-05-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install level-objectify
pnpm add level-objectify
yarn add level-objectify
bun add level-objectify
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2014-05-12 |
| First published | 2014-05-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Nicholas Westlake |
| Maintainers | nrw |
| Keywords | level, patch, diff, update, object, changeset, changes |

## Links

- npm: https://www.npmjs.com/package/level-objectify
- Repository: https://github.com/nrw/level-objectify
- Issues: https://github.com/nrw/level-objectify/issues
- npm.io page: https://npm.io/package/level-objectify

## Dependencies (2)

- [patcher](https://npm.io/package/patcher.md) 0.0.6
- [stream-reduce](https://npm.io/package/stream-reduce.md) 1.0.3

## Recent versions

- 0.2.0 (latest) — 2014-05-12
- 0.1.0 — 2014-05-12

## README

# level-objectify [![build status](https://secure.travis-ci.org/nrw/level-objectify.png)](http://travis-ci.org/nrw/level-objectify)

Compile the contents of a leveldb read stream into an object and create a patch for changes as a batch update.

[![testling badge](https://ci.testling.com/nrw/level-objectify.png)](https://ci.testling.com/nrw/level-objectify)

## Example

```js
level = require('level-test')()
objectify = require('../')

db = level 'level-objectify-test', {valueEncoding: 'json'}

var c1 = {
  person: {
    gaius: {name: 'Gaius Baltar'},
    six: {name: 'Six'}
  }
}

// {depth: 1} = use the first layer of keys as a prefix to their children
var batch = objectify({depth: 1}).computeBatch({}, c1)
// returns a valid leveldb batch of operations.
// [
//   { key: 'personÿgaius', type: 'put', value: { name: 'Gaius Baltar' } },
//   { key: 'personÿsix', type: 'put', value: { name: 'Six' } }
// ]

db.batch(batch)

// with data already in the db, the contents can be objectified
db.readStream().pipe(objectify({depth: 1}).compile(function (err, result) {
  if (err) return console.log('problem', err)

  // `result` matches the object we started with!
  assert.deepEqual(c1, result)
  // see the tests for more examples.
}))
```

## Usage

### var obj = objectify(opts={})

#### options

- `opts.depth = 0` the depth of key that should be used as a prefix for each property
- `opts.separator = '\xff'` the string to use to separate sections of the prefix.

## Methods

### obj.compile(callback(err, result))

Returns a writable stream that expects data in the format `{key: ..., value: ...}`
(like the data emitted by `level.readStream()`). When the stream ends, `callback`
is called with any error and the compiled result.

### obj.computeBatch(prev, next)

Returns a batch operations array (compatible with `leveldb`) that will patch the
database in the state `prev` to match the state of `next`.

Note: you will lose any information about the object's state that stops short of
that `opts.depth`. The default depth is `0`.

```js
// in the database used with objectify({depth: 2}), this is one document.
// {key: 'aÿbÿc', value: {d: 'e'}}
var prev = { a: { b: { c: {d: 'e'} } } }
var next = { a: {} }

var batch = objectify({depth: 2}).computeBatch(prev, next)
// this will simply delete the one document. {a: {}} will be lost.
// [{type: 'del', key: 'aÿbÿc'}]
```

### obj.computePatch(prev, next)

Exports `computePatch()` from [patcher](https://www.npmjs.org/package/patcher).

## Contributing

Please make any changes to the `.coffee` source files and `npm build` before
sending a pull request.

## License

MIT

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