Mapping
As demonstrated on the previous chapter, an entity is a class that has a static method called setMapping()
. This method will be used by wetland to create your entity's schema. In this chapter we will describe all the methods you can use to create your schema or to fetch information about your entity.
.addRelation()
This method adds a relation to the mapping. The options available to create your relationship are described here. Although you can add your relations using this method, we recommend you to use .oneToOne()
, .oneToMany()
, .manyToMany()
or .manyToOne()
instead, for practical reasons.
mapping.addRelation('property', {targetedEntity: 'target', type: 'oneToMany', inversedBy: 'field'});
.cascade()
Sets cascade options to persist
, delete
or both. The cascade options must be passed as an array, regardless of how many cascade options the property has.
mapping.cascade('property', ['persist']);
// or
mapping.forProperty('property').cascade(['persist']);
.entity()
Maps an entity, allowing you to create a custom attributes for your entity. By using this method you can customize the repository
, name
, tableName
and store
attributes of your entity.
mapping.entity({repository: MyRepository, name: 'custom', store: MyOtherStore});
.extendField()
Extend the field options for a property. This method is used internally by other methods in the Mapping class. The field options can be found here.
mapping.extendField('property', {type: 'integer'});
.field()
Maps a field for an entity. Here is the list of field options you can use to map your property accordingly.
mapping.field('property', {type: 'string', size: 20});
// or
mapping.forProperty('property').field({type: 'string', size: 20});
forEntity()
Static method to get the mapping for a specific entity. Returns the entity's mapping instance.
forEntity('entity');
.forProperty()
Convenience method to map a property. This method stores the name of the property, erasing the need to specify a target when chaining manipulating methods to map a property.
mapping.forProperty('property');
.generatedValue()
This method maps the generated values of a property. It calls .extendField()
on the property, adding a type to the generatedValue
field.
mapping.generatedValue('id', 'autoIncrement');
// or
mapping.forProperty('id').generatedValue('autoIncrement');
mapping.getField('property');
.getFields()
Gets the fields for mapped entity, returning an array of objects containing the field options for each property.
mapping.getFields();
mapping.getJoinColumn('property');
mapping.getJoinTable('property', entityManager);
mapping.getUniqueConstraints();
.increments()
Convenience method to set auto increment. This method calls .extendField()
on the property, setting the generatedValue
field option to autoIncrement
.
mapping.increments('property');
// or
mapping.forProperty('property').increments();
// Compound
mapping.index('idx_something', ['property1', 'property2']);
// Single
mapping.index('idx_something', ['property']);
mapping.index('idx_something', 'property');
// Generated index name "idx_property"
mapping.index('property');
mapping.index(['property1', 'property2']);
mapping.isRelation('property');
.joinColumn()
Register a join column. This method calls .extendField()
on the property, assigning the options given to the joinColumn
field. The full list of options can be found here.
mapping.joinColumn('property', {});
// or
mapping.forProperty('property').joinColumn({});
.joinTable()
Register a join table. This method also uses .extendField()
to assign the options given to the joinTable
field. The full list of options can be found here.
mapping.joinTable('property', {});
// or
mapping.forProperty('property').joinTable({});
.manyToMany()
Convenience method to add a many-to-many relationship. A property in this kind of relationship can be either mappedBy
or inversedBy
, depending on if it is or not on the owning side, respectively.
mapping.manyToMany('property', {targetEntity: 'target', mappedBy: 'field'});
// or
mapping.forProperty('property').manyToMany({targetEntity: 'entity', mappedBy: 'field'});
.manyToOne()
Maps a many-to-one relationship. In this type of relationship, a property can only be inversedBy
.
mapping.manyToOne('property', {targetEntity: 'target', inversedBy: 'field'});
// or
mapping.forProperty('property').manyToOne({targetEntity: 'target', inversedBy: 'field'});
.oneToMany()
Maps a one-to-many relationship. In this case, the property is the owning side, so it can only be set as mappedBy
.
mapping.oneToMany('property', {targetEntity: 'target', mappedBy: 'field'});
// or
mapping.forProperty('property').oneToMany({targetEntity: 'target', mappedBy: 'field'});
.oneToOne()
Maps a one to one relationship. A property in this kind of relationship can be either mappedBy
or inversedBy
, just like the many-to-many example.
mapping.oneToOne('property', {targetEntity: 'target', mappedBy: 'field'});
// or
mapping.forProperty('property').oneToOne({targetEntity: 'target', mappedBy: 'field'});
.primary()
Maps a property to be the primary key. This method uses .extendField()
to set the primary
field to true.
mapping.primary('property');
// or
mapping.forProperty('property').primary();
// Compound:
mapping.uniqueConstraint('something_unique', ['property1', 'property2']);
// Single:
mapping.uniqueConstraint('something_unique', ['property']);
mapping.uniqueConstraint('something_unique', 'property');
// Generated uniqueConstraint name:
mapping.uniqueConstraint('property');
mapping.uniqueConstraint(['property1', 'property2']);
Field
Options | Type |
---|---|
cascades | array |
comment | string |
defaultTo | any |
enumeration | array |
generatedValue | string |
joinColumn | object |
joinTable | object |
name | string |
nullable | boolean |
precision | number |
primary | boolean |
relationship | object |
scale | number |
size | number |
textType | string |
type | string |
unsigned | boolean |
[key: string] | any |
Join column
Options | Type |
---|---|
indexName | string |
name | string |
nullable | boolean |
onDelete | string |
onUpdate | string |
referencedColumnName | string |
size | number |
type | string |
unique | boolean |
Join table
Options | Type |
---|---|
inverseJoinColumns | array |
joinColumns | array |
name | string |
Relationship
Options | Type |
---|---|
inversedBy | string |
mappedBy | string |
targetEntity | string, {new ()} |
type | string |