src/schema/v0026.js
function expression_query(parent, prop, type_name, type_prop, fields) {

				fields = (fields || [{'query':'*'}]);

				var map = {};

				var query = "SELECT "+fields.map(function(f, i) {
					var k;
					if(f.key) {
						k = 'p__' + i;
						map[k] = f;
						return f.query + ' AS ' + k;
					} else {
						return f.query;
					}
				}).join(', ')+" FROM documents WHERE type = $1 AND "+get_pg_prop(type_prop)+" = $2";

				var rows = plv8.execute(query, [type_name, parent.id]);

				if(!is_array(rows)) {
					return rows;
				}

				var errors = [];

				rows = rows.map(function(obj) {
					Object.keys(obj).forEach(function(key) {
						if(!map.hasOwnProperty(key)) {
							return;
						}
						var spec = map[key];
						if(!is_object(spec)) {
							errors.push( error('mapped resource missing') );
							return;
						}
						if(!is_string(spec.key)) {
							errors.push( error('key missing') );
							return;
						}
						var value = obj[key];
						delete obj[key];

						if(is_string(spec.datakey)) {
							if(!obj[spec.datakey]) {
								obj[spec.datakey] = {};
							}
							obj[spec.datakey][spec.key] = value;
						} else {
							obj[spec.key] = value;
						}
					});
					return obj;
				});

				if(errors.length >= 1) {
					return errors.shift();
				}

				return rows;
			}
function expression_query(parent, prop, type_name, type_prop, fields) {

				fields = (fields || [{'query':'*'}]);

				var map = {};

				var query = "SELECT "+fields.map(function(f, i) {
					var k;
					if(f.key) {
						k = 'p__' + i;
						map[k] = f;
						return f.query + ' AS ' + k;
					} else {
						return f.query;
					}
				}).join(', ')+" FROM documents WHERE type = $1 AND "+get_pg_prop(type_prop)+" = $2";

				var rows = plv8.execute(query, [type_name, parent.id]);

				if(!is_array(rows)) {
					return rows;
				}

				var errors = [];

				rows = rows.map(function(obj) {
					Object.keys(obj).forEach(function(key) {
						if(!map.hasOwnProperty(key)) {
							return;
						}
						var spec = map[key];
						if(!is_object(spec)) {
							errors.push( error('mapped resource missing') );
							return;
						}
						if(!is_string(spec.key)) {
							errors.push( error('key missing') );
							return;
						}
						var value = obj[key];
						delete obj[key];

						if(is_string(spec.datakey)) {
							if(!obj[spec.datakey]) {
								obj[spec.datakey] = {};
							}
							obj[spec.datakey][spec.key] = value;
						} else {
							obj[spec.key] = value;
						}
					});
					return obj;
				});

				if(errors.length >= 1) {
					return errors.shift();
				}

				return rows;
			}
function expression_query ( parent , prop , type_name , type_prop , fields ) { fields = ( fields || [ { 'query' : '*' } ] ) ; var map = { } ; var query = "SELECT " + fields . map ( function ( f , i ) { var k ; if ( f . key ) { k = 'p__' + i ; map [ k ] = f ; return f . query + ' AS ' + k ; } else { return f . query ; } } ) . join ( ', ' ) + " FROM documents WHERE type = $1 AND " + get_pg_prop ( type_prop ) + " = $2" ; var rows = plv8 . execute ( query , [ type_name , parent . id ] ) ; if ( ! is_array ( rows ) ) { return rows ; } var errors = [ ] ; rows = rows . map ( function ( obj ) { Object . keys ( obj ) . forEach ( function ( key ) { if ( ! map . hasOwnProperty ( key ) ) { return ; } var spec = map [ key ] ; if ( ! is_object ( spec ) ) { errors . push ( error ( 'mapped resource missing' ) ) ; return ; } if ( ! is_string ( spec . key ) ) { errors . push ( error ( 'key missing' ) ) ; return ; } var value = obj [ key ] ; delete obj [ key ] ; if ( is_string ( spec . datakey ) ) { if ( ! obj [ spec . datakey ] ) { obj [ spec . datakey ] = { } ; } obj [ spec . datakey ] [ spec . key ] = value ; } else { obj [ spec . key ] = value ; } } ) ; return obj ; } ) ; if ( errors . length >= 1 ) { return errors . shift ( ) ; } return rows ; }
Execute query for document expressions
@param parent {object} The parent document object
@param prop {string} The name of the property where results are saved in the parent
@param type_name {string} The name of the type of related documents
@param type_prop {string} The property name in the related document for the UUID of parent
@param fields {array} The fields in the related document that should be returned
Execute query for document expressions