Exemplo n.º 1
0
  generate_model : function ( args, model, scaffold ){
    var singular = inflection.singularize( inflection.tableize( model ));
    var code     = [ 'module.exports = {\n' ];

    if( scaffold ){
      code.push( '  statics : {\n' );
      code.push( '    create_or_update : function ( ' + singular + ', props, callback ){' );

      args.forEach( function ( arg ){
        var tmp    = lib.valid_model_prop_name( arg ).split( ':' );
        var prop   = tmp[ 0 ];

        code.push( '      ' + singular + '.' + prop + ' = props.' + prop + ';' );
      });

      code.push( '      ' + singular + '.save( callback );' );
      code.push( '    }' );
      code.push( '  }' );
    }

    code.push( '};\n' );

    code = code.join( '\n' );

    lib.create_file( 'app/models/' + model + '.js', code );
  }
function searchRequestBuilder(request) {
  var params = request.params;
  if (!params.tableName)
    throw new Error('no source is specified');

  var queryParams = request.query;

  var queries = {};
  var queryName = params.tableName;
  try {
    queryName = inflection.tableize(queryName);
    queryName = inflection.camelize(queryName, true);
  } catch(error) {
    queryName = params.tableName.toLowerCase();
  }
  queries[queryName] = buildSearchQuery(params.tableName, queryParams);
  if ('group_by' in queryParams) {
    var group_by_queries = queryParams.group_by;
    var keys = Object.keys(group_by_queries);
    keys.forEach(function(key) {
      queries[key] = buildGroupByQuery(queryName, group_by_queries[key]);
    });
  }

  var message = {};
  message.queries = queries;

  if ('timeout' in queryParams)
    message.timeout = getIntegerValue(queryParams, 'timeout');

  return message;
}
Exemplo n.º 3
0
  generate_model : function ( args, model, scaffold ){
    var singular = inflection.singularize( inflection.tableize( model ));
    var code     = [ 'var ' + model + ' = require( BASE_DIR + \'db/schema\' ).' + model + ';\n' ];

    if( scaffold ){
      code.push( model + '.statics = {\n' );
      code.push( '  create_or_update : function ( ' + singular + ', props, callback ){' );

      args.forEach( function ( arg ){
        var tmp    = lib.valid_model_prop_name( arg ).split( ':' );
        var prop   = tmp[ 0 ];

        code.push( '    ' + singular + '.' + prop + ' = props.' + prop + ';' );
      });

      code.push( '    ' + singular + '.save( callback );' );
      code.push( '  }' );
      code.push( '};\n' );
    }

    code.push( 'require( \'mongoose\' ).model( \'' + model + '\', ' + model + ' );' );

    code = code.join( '\n' );

    lib.create_file( 'app/models/' + model + '.js', code );
  }
Exemplo n.º 4
0
/**
 * The table generates all SQL queries.
 *
 * @param {String} schemaName The name of the schema.
 * @param {Model} model      The model the table belongs to.
 * @param {Datastore} datastore  The datastore the app is connected to.
 * @constructor
 */
function Table(schemaName, model, datastore) {
	if(!model) {
		throw new Error('Cannot create table with non-existing model.');
	}

	this.schemaName 	= schemaName;
	this.name			= inflection.tableize(model.getName());
	this.modelName 		= model.getName();
	this.model 			= model;
	this.datastore 		= datastore;
	this.knex 			= datastore.knex;
}
Exemplo n.º 5
0
			return function(property) {
				property.columnName 			= property.columnName + '_id';
				property.options.referenceName 	= toModelName(modelNameOrModel);
				property.options.belongsTo 		= true;

				var linkedPropertyName;
				if(options) {
					if(typeof options == 'string') {
						linkedPropertyName = options;
					}
					else {
						linkedPropertyName = options.linkedPropertyName;

						if(options.through) {
							throw new Error('You cannot specify a through model in BelongsTo. To create a many-to-many association, please use HasMany-HasMany.');
						}
					}
				}

				if(linkedPropertyName) {
					property.options.linkedPropertyName = linkedPropertyName;
				}

				var associatedModel = property.getAssociatedModel();
				if(associatedModel) {
					// Now, find any associations which reference this model
					var associations = associatedModel.findAssociationsTo(property.model, linkedPropertyName);

					if(associations.length > 1) {
						throw new Error('Multiple associations to `' + property.model.getName() + '` exists on `' + associatedModel.getName() + '`.');
					}
					else if(associations.length == 1) {
						var associatedProperty = associations[0];

						property.options.belongsTo = associatedProperty.name;

						if(associatedProperty.options.hasMany) {
							associatedProperty.options.hasMany = property.name;
						}
						else if(associatedProperty.options.hasOne) {
							associatedProperty.options.hasOne = property.name;
						}
						else {
							//
						}

						associatedProperty.options.relationshipVia = property;
						property.options.relationshipVia = associatedProperty;
					}
				}

				if(associatedModel) {
					return {
						index: 999,
						clause: Table.keywords.References(associatedModel.getTable().name),
						dataType: 'UUID'
					};
				}
				else {
					return {
						index: 999,
						clause: Table.keywords.References(inflection.tableize(property.options.referenceName)),
						dataType: 'UUID'
					};
				}
			};
Exemplo n.º 6
0
export function table(Model) {
  return Inflection.tableize(Model.name)
}
Exemplo n.º 7
0
 static get table () {
   return this.$table ? this.$table : tableize(this.name)
 }
Exemplo n.º 8
0
 it( 'should tableize the given word', function (){
   inflection.tableize( 'people' ).should.equal( 'people' );
   inflection.tableize( 'MessageBusProperty' ).should.equal( 'message_bus_properties' );
 });
Exemplo n.º 9
0
  model.constructor.relations.forEach(relation => {

    if (relation._isPrepared) return;

    // console.log('setRelationsDefaults', model, relation)   

    // shorthand method to quickly check if relation is of hasMany type
    Object.defineProperty(relation, "isHasMany", {
      get: function() {
        return this.type === 'hasMany'
      }
    });

    // shorthand method to quickly check if relation is of hasOne type
    Object.defineProperty(relation, "isHasOne", {
      get: function() {
        return this.type === 'hasOne'
      }
    });

    // set initialValue for relation property
    if (relation.isHasMany) {
      relation.initialValue = [];
    } else if (relation.isHasOne) {
      relation.initialValue = null;
    }

    if (isString(relation.relatedModel)) {
      relation.relatedModel = model.constructor.getModel(relation.relatedModel);
    }

    // property name on model instance to relation(s)
    if (!relation.propertyName) {
      relation.propertyName = lowercaseFirstLetter(relation.relatedModel.name);

      if (relation.isHasMany) {
        relation.propertyName = pluralize(relation.propertyName)
      }
    }

    // json key for embedded json
    if (!relation.jsonKey) {
      relation.jsonKey = underscore(relation.propertyName);
    }

    // key in top level json
    if (!relation.topLevelJsonKey) {
      relation.topLevelJsonKey = tableize(relation.propertyName);
    }

    // foreign key with ids of relations
    if (!relation.foreignKey) {
      if (relation.isHasMany) {
        relation.foreignKey = foreign_key(singularize(relation.propertyName)) + 's';
      } else if (relation.isHasOne) {
        relation.foreignKey = foreign_key(relation.propertyName);
      }
    }

    let name = upperCaseFirstLetter(relation.propertyName);
    if (relation.isHasMany) name = singularize(name);

    // method name to add single relation, will be used as alias
    if (!relation.setMethodName) {      
      relation.setMethodName = `set${name}`;
    }

    // method name to remove single relation, will be used as alias
    if (!relation.removeMethodName) {
      relation.removeMethodName = `remove${name}`;
    }

    let reverseRelation = relation.reverseRelation;

    if (reverseRelation) {      

      if (isBoolean(reverseRelation)) {
        reverseRelation = relation.reverseRelation = {};
      }

      if (!reverseRelation.onDestroy && reverseRelation.onDestroy !== false) {
        reverseRelation.onDestroy = 'removeSelf'
      }

      if (!reverseRelation.propertyName) {
        reverseRelation.propertyName = lowercaseFirstLetter(model.constructor.name);
      }

      let name = upperCaseFirstLetter(reverseRelation.propertyName);

      if (!reverseRelation.setMethodName) {        
        reverseRelation.setMethodName = `set${name}`;
      }

      if (!reverseRelation.removeMethodName) {        
        reverseRelation.removeMethodName = `remove${name}`;
      }

      //console.log('setRelationsDefaults reverseRelation is true', relation.reverseRelation, relation)

    }

    relation._isPrepared = true;

  });
Exemplo n.º 10
0
  valid_controller_name : function ( controller ){
    var tmp = _.valid_name( controller, regex.has_none_characters_but_slash );

    return inflection.tableize( tmp );
  },