0.2.0 • Published 7 years ago

nstd v0.2.0

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

nstd

Update nested entities

Installation

npm install --save nstd

Builder options

optiontypedefault value
primaryKeyString"id"
foreignKeyStringnull
methodsObject{}
methods.queryFunction() => []
methods.createFunction_.identity
methods.updateFunction_.identity
methods.removeFunction_.identity

Methods

methods.query

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

methods.create, methods.update, methods.remove

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

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>