lib/knex-backend.js
function(transaction, bucket, key, values){
		contract(arguments)
			.params('array', 'string', 'string|number','string|array|number')
			.end()
		;
		
		var self = this;
		var table = '';
		values = Array.isArray(values) ? values : [values]; // we always want to have an array for values
		
		transaction.push(function(cb){
			
			if (bucket.indexOf('allows') != -1) {
				table = self.prefix + self.buckets.permissions;
				self.db
					.select('key', 'value')
					.from(table)
					.where({'key': bucket})
					.then(function(result) {
						var json = {};
						
						if (result.length === 0) {
							
							// if no results found do a fresh insert
							json[key] = values;
							return self.db(table)
								.insert({key: bucket, value: json})
							;
						} else {
							
							// if we have found the key in the table then lets refresh the data
							if (_.has(result[0].value, key)) {
								result[0].value[key] = _.union(values, result[0].value[key]);
							} else {
								result[0].value[key] = values;
							}
							
							return self.db(table)
								.where('key', bucket)
								.update({key: bucket, value: result[0].value})
							;
						}
					})
					.then(function() {
						cb(undefined);
					})
				;
			} else {
				table = self.prefix + bucket;
				self.db
					.select('key', 'value')
					.from(table)
					.where({'key': key})
					.then(function(result) {
						
						if (result.length === 0) {
							
							// if no results found do a fresh insert
							return self.db(table)
								.insert({key: key, value: values})
							;
						} else {
							
							// if we have found the key in the table then lets refresh the data
							return self.db(table)
								.where('key', key)
								.update({value: _.union(values, result[0].value)})
							;
						}
					})
					.then(function() {
						cb(undefined);
					})
				;
			}
		});
	}
function(transaction, bucket, key, values){
		contract(arguments)
			.params('array', 'string', 'string|number','string|array|number')
			.end()
		;
		
		var self = this;
		var table = '';
		values = Array.isArray(values) ? values : [values]; // we always want to have an array for values
		
		transaction.push(function(cb){
			
			if (bucket.indexOf('allows') != -1) {
				table = self.prefix + self.buckets.permissions;
				self.db
					.select('key', 'value')
					.from(table)
					.where({'key': bucket})
					.then(function(result) {
						var json = {};
						
						if (result.length === 0) {
							
							// if no results found do a fresh insert
							json[key] = values;
							return self.db(table)
								.insert({key: bucket, value: json})
							;
						} else {
							
							// if we have found the key in the table then lets refresh the data
							if (_.has(result[0].value, key)) {
								result[0].value[key] = _.union(values, result[0].value[key]);
							} else {
								result[0].value[key] = values;
							}
							
							return self.db(table)
								.where('key', bucket)
								.update({key: bucket, value: result[0].value})
							;
						}
					})
					.then(function() {
						cb(undefined);
					})
				;
			} else {
				table = self.prefix + bucket;
				self.db
					.select('key', 'value')
					.from(table)
					.where({'key': key})
					.then(function(result) {
						
						if (result.length === 0) {
							
							// if no results found do a fresh insert
							return self.db(table)
								.insert({key: key, value: values})
							;
						} else {
							
							// if we have found the key in the table then lets refresh the data
							return self.db(table)
								.where('key', key)
								.update({value: _.union(values, result[0].value)})
							;
						}
					})
					.then(function() {
						cb(undefined);
					})
				;
			}
		});
	}
function ( transaction , bucket , key , values ) { contract ( arguments ) . params ( 'array' , 'string' , 'string|number' , 'string|array|number' ) . end ( ) ; var self = this ; var table = '' ; values = Array . isArray ( values ) ? values : [ values ] ; // we always want to have an array for values transaction . push ( function ( cb ) { if ( bucket . indexOf ( 'allows' ) != - 1 ) { table = self . prefix + self . buckets . permissions ; self . db . select ( 'key' , 'value' ) . from ( table ) . where ( { 'key' : bucket } ) . then ( function ( result ) { var json = { } ; if ( result . length === 0 ) { // if no results found do a fresh insert json [ key ] = values ; return self . db ( table ) . insert ( { key : bucket , value : json } ) ; } else { // if we have found the key in the table then lets refresh the data if ( _ . has ( result [ 0 ] . value , key ) ) { result [ 0 ] . value [ key ] = _ . union ( values , result [ 0 ] . value [ key ] ) ; } else { result [ 0 ] . value [ key ] = values ; } return self . db ( table ) . where ( 'key' , bucket ) . update ( { key : bucket , value : result [ 0 ] . value } ) ; } } ) . then ( function ( ) { cb ( undefined ) ; } ) ; } else { table = self . prefix + bucket ; self . db . select ( 'key' , 'value' ) . from ( table ) . where ( { 'key' : key } ) . then ( function ( result ) { if ( result . length === 0 ) { // if no results found do a fresh insert return self . db ( table ) . insert ( { key : key , value : values } ) ; } else { // if we have found the key in the table then lets refresh the data return self . db ( table ) . where ( 'key' , key ) . update ( { value : _ . union ( values , result [ 0 ] . value ) } ) ; } } ) . then ( function ( ) { cb ( undefined ) ; } ) ; } } ) ; }
Adds values to a given key inside a table.
Adds values to a given key inside a table .