# nstd

> Update nested entities

Latest version **0.2.0** (published 2017-02-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install nstd
pnpm add nstd
yarn add nstd
bun add nstd
```

## 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 | 2017-02-20 |
| First published | 2017-02-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Oleksandr Bondarchuk |
| Maintainers | coockoo |
| Keywords | nested, update |

## Links

- npm: https://www.npmjs.com/package/nstd
- Repository: https://github.com/coockoo/nstd
- Homepage: https://github.com/coockoo/nstd#readme
- Issues: https://github.com/coockoo/nstd/issues
- npm.io page: https://npm.io/package/nstd

## Dependencies (3)

- [debug](https://npm.io/package/debug.md) ^2.6.0
- [lodash](https://npm.io/package/lodash.md) ^4.17.4
- [bluebird](https://npm.io/package/bluebird.md) ^3.4.7

## Recent versions

- 0.2.0 (latest) — 2017-02-20
- 0.1.1 — 2017-02-08
- 0.1.0 — 2017-02-07

## README

# nstd

Update nested entities

## Installation

`npm install --save nstd`

## Builder options

|    option        | type     | default value  |
|:---------------- | -------- |:-------------- |
| `primaryKey`     | String   | `"id"`         |
| `foreignKey`     | String   | `null`         |
| `methods`        | Object   | `{}`           |
| `methods.query`  | Function | `() => []`     |
| `methods.create` | Function | `_.identity`   |
| `methods.update` | Function | `_.identity`   |
| `methods.remove` | Function | `_.identity`   |

### Methods

`methods.query`

```javascript
...
methods: {
    query: (key, params) => {
        // Return values here by key
        return [];
    }
}
...
```

`methods.create`, `methods.update`, `methods.remove`

```javascript
methods: {
    // same for update and remove
    create: (item, params) => {
        // Actual creation/update/removal here
        return item;
    }
}
```



## Call options
`const createOrUpdate = nstd({ ... });`

`createOrUpdate(foreignKeyValue:<String|Number>, items:<Array>, params:<any>)`

## Example

```javascript
const _ = require('lodash');
const nstd = require('nstd');

let items = [
	{ id: 1, ref_id: 1, name: 'Kappa' },
	{ id: 2, ref_id: 1, name: 'KappaPride' },
	{ id: 3, ref_id: 1, name: 'Jebaited' },
	{ id: 4, ref_id: 1, name: 'PogChamp' },
	{ id: 5, ref_id: 2, name: 'KKona' }
];
let maxId = 5;

const createOrUpdate = nstd({
	foreignKey: 'ref_id',
	methods: {
		query: (refId) => _.filter(items, { ref_id: refId }),
		create: (params) => {
			// Persistent storage imitation
			const item = {
				id: ++maxId,
				ref_id: params.ref_id,
				name: params.name
			};
			items.push(item);
			return item;
		},
		// Update method is omitted
		remove: (params) => {
			items = _.filter(items, (item) => item.id !== params.id)
		}
	}
})


createOrUpdate(1, [
	{ id: 1, ref_id: 1, name: 'Kappa' },
	{ id: 2, ref_id: 1, name: 'KappaPride' },
	{ id: 3, ref_id: 1, name: 'Jebaited' },
	{ name: 'NotLikeThis' }
]).then(function (res) {
	console.log(res);
});

```

## Debug

`DEBUG=nstd node <path/to/your/app.js>`

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