<commit_before>var actions = require( './db.js' )( 'actions.db' );

function changeRoles( actionname, roles, verb ) {
	var mutation = {},
		op = verb === 'add' ? '$addToSet' : '$pull',
		comp = verb === 'add' ? '$each' : '$in',
		command = { roles: {} };
	command.roles[ comp ] = roles;
	mutation[ op ] = command;
	return actions.update( { name: actionname }, mutation );
}

function create( actionname, resource ) {
	return actions.upsert( { name: actionname }, { $set: { name: actionname, resource: resource }, $push: { roles: [] } } );
}

function getList( continuation ) {
	continuation = continuation || { sort: { name: 1 } };
	continuation.sort = continuation.sort || { name: 1 };
	return actions.fetch( {}, undefined, continuation );
}

function getRoles( actionname ) {
	return actions.fetch( { name: actionname }, function( x ) { return x.roles; } )
		.then( function( list ) {
			return list.length ? list[ 0 ] : [];
		} );
}

function purge( actionname ) {
	return actions.purge( { name: actionname } );
}

module.exports = {
	changeRoles: changeRoles,
	create: create,
	'delete': purge,
	getList: getList,
	getRoles: getRoles
};<commit_msg>Prevent pushing an empty array as a new element to roles array on actions.<commit_after>var actions = require( './db.js' )( 'actions.db' );

function changeRoles( actionname, roles, verb ) {
	var mutation = {},
		op = verb === 'add' ? '$addToSet' : '$pull',
		comp = verb === 'add' ? '$each' : '$in',
		command = { roles: {} };
	command.roles[ comp ] = roles;
	mutation[ op ] = command;
	return actions.update( { name: actionname }, mutation );
}

function create( actionname, resource ) {
	return actions.upsert( { name: actionname }, { $set: { name: actionname, resource: resource } } );
}

function getList( continuation ) {
	continuation = continuation || { sort: { name: 1 } };
	continuation.sort = continuation.sort || { name: 1 };
	return actions.fetch( {}, undefined, continuation );
}

function getRoles( actionname ) {
	return actions.fetch( { name: actionname }, function( x ) { return x.roles; } )
		.then( function( list ) {
			return list.length ? list[ 0 ] : [];
		} );
}

function purge( actionname ) {
	return actions.purge( { name: actionname } );
}

module.exports = {
	changeRoles: changeRoles,
	create: create,
	'delete': purge,
	getList: getList,
	getRoles: getRoles
};
{'fraction_non_alphanumeric': 0.1694408877507469, 'fraction_numerical': 0.002560819462227913, 'mean_word_length': 3.9451476793248945, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2839255', 'n_tokens_mistral': 799, 'n_tokens_neox': 764, 'n_words': 215}