2.0.1 • Published 10 years ago

knex-dal-has-many v2.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
10 years ago

knex-dal-has-many

Has-many relation mixin for knex-dal

Installation

npm install knex-dal-has-many

Usage

Given dal configuration

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:

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

2.0.1

10 years ago

2.0.0

10 years ago

1.3.0

10 years ago

1.2.0

11 years ago

1.1.0

11 years ago