# object-basin

> JavaScript/TypeScript library to stream updates to an object.

Latest version **2.1.0** (published 2024-12-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install object-basin
pnpm add object-basin
yarn add object-basin
bun add object-basin
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2024-12-09 |
| First published | 2023-02-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 12.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 48 |
| Author | Justin D. Harris |
| Maintainers | juharris |
| Keywords | JSONPath, JSON, object, stream, update, patch |

## Links

- npm: https://www.npmjs.com/package/object-basin
- Repository: https://github.com/microsoft/object-basin
- Issues: https://github.com/microsoft/object-basin/issues
- npm.io page: https://npm.io/package/object-basin

## Dependencies (2)

- [jsonpath](https://npm.io/package/jsonpath.md) ^1.1.1
- [fast-json-patch](https://npm.io/package/fast-json-patch.md) ^3.1.1

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 2.1.0 (latest) — 2024-12-09
- 2.0.1 — 2023-03-22
- 2.0.0 — 2023-03-15
- 1.2.0 — 2023-02-27
- 1.1.0 — 2023-02-25
- 1.0.1 — 2023-02-24
- 1.0.0 — 2023-02-24
- 0.2.0 — 2023-02-24
- 0.1.0 — 2023-02-24

## README

# Basin
JavaScript/TypeScript library to stream updates to an object using JSONPaths.
It is called Basin because streams flow into a basin.

See in [npmjs.com](https://www.npmjs.com/package/object-basin).

This library supports various ways to update objects or their contents:
* appending to the end of a string
* inserting anywhere in a string
* removing characters from a string
* appending to the end of a list
* inserting anywhere in a list
* overwriting an item in a list
* deleting items in a list
* setting a value in an object
* tracking different cursors for different streams

Learn more at https://github.com/microsoft/object-basin.

# Install
```bash
npm install object-basin
```

# Examples
```TypeScript
import { Basin } from 'object-basin'

const basin = new Basin<any>()

// Stream a string.
// `$.` is needed when the key does not exist yet.
basin.setCursor({ jsonPath: '$.message' })
basin.write("ello") // "ello"

// Append to the end of the string.
basin.setCursor({ jsonPath: 'message', position: -1 })
basin.write(" World") // "ello World"
basin.write("!") // "ello World!"
basin.items // { message: "ello World!" }

// Insert at the beginning of the string.
// `j` and `p` can be used to be more concise.
basin.setCursor({ j: 'message', p: 0 })
basin.write("H") // "Hello World!"
basin.items // { message: "Hello World!" }

// Stream a string.
basin.setCursor({ jsonPath: 'message', p: -1 })
basin.write(" It's") // "Hello World! It's"
basin.write(" nice ") // "Hello World! It's nice "
basin.write("to stream") // "Hello World! It's nice to stream"
basin.write(" to you.") // "Hello World! It's nice to stream to you."

// Stream parts of an object.
basin.setCursor({ jsonPath: '$.object' })
basin.write({ list: ["item 1"] }) // { list: ["item 1"] }

// Append to the end of a list.
basin.setCursor({ jsonPath: '$.object.list', position: -1 })
basin.write("item 2") // { list: ["item 1", "item 2"] }
basin.items // { message: "Hello World! It's nice to stream to you.", object: { list: [ 'item 1', 'item 2' ] } }

// Append to the end of a string in a list.
basin.setCursor({ jsonPath: 'object.list[1]', position: -1 })
basin.write(" is the best") // { list: [ 'item 1', 'item 2 is the best' ] }
basin.items
// {
//   message: "Hello World! It's nice to stream to you.",
//   object: { list: [ 'item 1', 'item 2 is the best' ] }
// }

// Insert in a list.
basin.setCursor({ jsonPath: 'object.list', position: 1 })
basin.write("item 1.5") // { list: ['item 1', 'item 1.5', 'item 2 is the best'] }

// Overwrite an item in a list.
basin.setCursor({ jsonPath: 'object.list[1]'})
basin.write("item 1.33") // { list: ['item 1', 'item 1.33', 'item 2 is the best'] }

// Add a few more items to the list.
basin.setCursor({ jsonPath: 'object.list', p: -1 })
basin.write('item 3') // { list: ['item 1', 'item 1.33', 'item 2 is the best', 'item 3'] }
basin.write('item 4') // { list: ['item 1', 'item 1.33', 'item 2 is the best', 'item 3', 'item 4'] }
basin.write('item 5') // { list: ['item 1', 'item 1.33', 'item 2 is the best', 'item 3', 'item 4', 'item 5'] }

// Delete the first item in the list.
basin.setCursor({ jsonPath: 'object.list', p: 0, deleteCount: 1 })
// The value given to `write` is ignored when deleting items from lists.
basin.write(undefined) // { list: ['item 1.33', 'item 2 is the best', 'item 3', 'item 4', 'item 5'] }

// Delete 2 items in the list starting at index 1.
// `d` can be used to be more concise.
basin.setCursor({ jsonPath: 'object.list', p: 1, d: 2 })
basin.write() // { list: ['item 1.33', 'item 4', 'item 5'] }

// Insert into a string and then delete the next 3 characters.
basin.setCursor({ jsonPath: 'object.list[0]', p: 6, d: 3 })
basin.write('!') // { list: ['item 1!', 'item 4', 'item 5'] }
```

See more examples in the [tests](src/__tests__/index.test.ts).

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