# knex-dal-has-many

> has-many relation mixin for knex-dal

Latest version **2.0.1** (published 2016-11-21) · ISC license · 0 weekly downloads

## Install

```sh
npm install knex-dal-has-many
pnpm add knex-dal-has-many
yarn add knex-dal-has-many
bun add knex-dal-has-many
```

## 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.0.1 |
| Published | 2016-11-21 |
| First published | 2015-10-21 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Egor Gumenyuk |
| Maintainers | boo1ean |
| Keywords | knex, database, db, dal, model, has-many, knex-dal |

## Links

- npm: https://www.npmjs.com/package/knex-dal-has-many
- npm.io page: https://npm.io/package/knex-dal-has-many

## Dependencies (2)

- [lodash](https://npm.io/package/lodash.md) ^4.17.2
- [bluebird](https://npm.io/package/bluebird.md) ^2.10.2

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 2.0.1 (latest) — 2016-11-21
- 2.0.0 — 2016-11-21
- 1.3.0 — 2016-11-16
- 1.2.0 — 2015-10-29
- 1.1.0 — 2015-10-21

## README

## knex-dal-has-many

Has-many relation mixin for [knex-dal](https://github.com/boo1ean/knex-dal)

## Installation

```
npm install knex-dal-has-many
```

## Usage

Given dal configuration

```javascript
var hasMany = require('knex-dal-has-many');
var dal = require('knex-dal');
var itemsDal = require('./items/dal');
var knex = require('../services/db');

var table = 'grids';
var createAndUpdateFields = ['title'];

module.exports = dal({
	table: table,
	knex: knex,
	softDeleteColumn: softDeleteColumn,
	mixins: [
		hasMany({
			methods: {
				create: 'createWithItems',
				update: 'updateWithItems',
			},
			relations: [{
				table: 'items',
				foreignKey: 'grid_id',
				field: 'items',
				methods: {
					create: itemsDal.create.bind(itemsDal),
					update: itemsDal.update.bind(itemsDal),
					remove: itemsDal.remove.bind(itemsDal)
				}
			}]
		})
	],
	pick: {
		create: createAndUpdateFields,
		update: createAndUpdateFields
	}
});
```

Allows to store hierarchical data with one function call:

```javascript
var gridsDal = require('./grids/dal');

gridsDal.createWithItems({
	title: 'new grid',
	items: [
		{ title: 'item 1' },
		{ title: 'item 2' }
	]
});
```
Above code will create record in `grids` table and then create two related records in `grid_items` table.

## Available methods

### create

Create record with child records

### update

Update record with child records

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