Example #1
0
		let models = _.map(resources, function(resource) {
			
			let resourceIdentity = resource.name.toLowerCase(); // needs to be lower case, per Waterline docs

			let attributes = {};
			
			
			// Configure the attributes
			_.each(resource.attributes, function(attribute) {
				
				attributes[attribute.name] = {
					type: attribute.dbType,
					defaultsTo: attribute.defaultValue
				};
				
				// undefined, null, and false all cause an index to be created
				// so we have to go with this solution for now
				if (attribute.index) {
					attributes[attribute.name]['index'] = true;
				}
				
				// Same with the unique field
				if (attribute.unique) {
					attributes[attribute.name]['unique'] = true;
				}

			});

			// Configure the relationships
			_.each(resource.relationships, function(relationship) {
				
				let targetResourceName = relationship.resourceName.toLowerCase(); // has to be lower case
				
				let association: {};
				
				switch (relationship.type) {
					case RelationshipType.HasOne: {
						
						association = {
							model: targetResourceName
						};
						
						if (relationship.targetRelationshipName) {
							association['via'] = relationship.targetRelationshipName;
						}
						
						break;
					}
					case RelationshipType.HasMany: {
						
						association = {
							collection: targetResourceName
						};
						
						if (relationship.targetRelationshipName) {
							association['via'] = relationship.targetRelationshipName;
						}
						
						break;
					}
				}
				
				attributes[relationship.name] = association;
				
			});
	
			// Finally, return the configured collection
			let coll =  Waterline.Collection.extend({
				identity: resourceIdentity,
				connection: resource.connection,
				attributes: attributes
			});

			return coll;

		});
Example #2
0
 .forEach(e => waterline_obj.loadCollection(Waterline.Collection.extend(e)));