Permalink
Browse files

feat(project): add decorators

  • Loading branch information...
RWOverdijk committed Mar 13, 2017
1 parent 4bed0be commit 6d3766c5a923b8dc081cd47c3a347229d06e407c
Showing with 146 additions and 47 deletions.
  1. +2 −2 src/Mapping.ts
  2. +144 −45 src/decorators/Mapping.ts
@@ -352,7 +352,7 @@ export class Mapping<T> {
*/
public getColumnNames(properties: Array<string>): Array<string> {
if (!Array.isArray(properties)) {
properties = [properties];
properties = [(properties as string)];
}

return properties.map(column => this.getColumnName(column));
@@ -1004,7 +1004,7 @@ export class Field {
*
* @return {Field}
*/
public primary() {
public primary(): this {
this.mapping.primary(this.property);

return this;
@@ -1,26 +1,5 @@
import {Mapping} from "../Mapping";
import {EntityRepository} from "../EntityRepository";

/**
* Decorate a property as a field. Examples:
*
* - Default (property) name
* @field('username', {type: 'string', length: 255})
* username: string;
*
* - Custom name
* @field('password', {type: 'string', name: 'passwd'})
* password: string;
*
* @param {{}} options
*
* @return {Mapping}
*/
export function field(options: {type: string, size?: number, [key: string]: any}) {
return (target: Object, property: string) => {
Mapping.forEntity(target).field(property, options);
};
}
import {Mapping, JoinColumn, Relationship, JoinTable, FieldOptions} from '../Mapping';
import {EntityRepository} from '../EntityRepository';

/**
* Decorate an entity. Examples:
@@ -69,28 +48,71 @@ export function index(indexName: string | Array<string>, fields?: string | Array
}

/**
* Decorate a property to be the primary key. Example:
* Decorate your entity with a uniqueConstraint
*
* - Compound:
* @uniqueConstraint('something_unique', ['property1', 'property2'])
*
* - Single:
* @uniqueConstraint('something_unique', ['property'])
* @uniqueConstraint('something_unique', 'property')
*
* - Generated uniqueConstraint name:
* @uniqueConstraint('property')
* @uniqueConstraint(['property1', 'property2'])
*
* @param {Array|string} constraintName
* @param {Array|string} [fields]
*
* @return {Function}
*/
export function uniqueConstraint(constraintName: string | Array<string>, fields?: string | Array<string>) {
return (target: Object) => {
Mapping.forEntity(target).uniqueConstraint(constraintName, fields);
};
}

/**
* Decorate a property as a field. Examples:
*
* @primary('id')
* public id: number;
* - Default (property) name
* @field('username', {type: 'string', length: 255})
* username: string;
*
* - Custom name
* @field('password', {type: 'string', name: 'passwd'})
* password: string;
*
* @param {{}} options
*
* @return {Mapping}
*/
export function field(options: FieldOptions) {
return (target: Object, property: string) => {
Mapping.forEntity(target).field(property, options);
};
}

/**
* Map to be the primary key.
*
* @return {Field}
*/
export function primary() {
return (target: Object, property: string) => {
Mapping.forEntity(target).primary(property);
};
}

/**
* Decorate your property with generatedValues. Example:
* Map generatedValues. Examples:
*
* @generatedValue('autoIncrement')
* public id: number;
* // Auto increment
* mapping.generatedValue('autoIncrement');
*
* @param {string} type
*
* @return {Mapping}
* @return {Field}
*/
export function generatedValue(type: string) {
return (target: Object, property: string) => {
@@ -99,26 +121,103 @@ export function generatedValue(type: string) {
}

/**
* Decorate your entity with a uniqueConstraint
* Set cascade values.
*
* - Compound:
* @uniqueConstraint('something_unique', ['property1', 'property2'])
* @param {string[]} cascades
*
* - Single:
* @uniqueConstraint('something_unique', ['property'])
* @uniqueConstraint('something_unique', 'property')
* @returns {Field}
*/
export function cascade(cascades: Array<string>) {
return (target: Object, property: string) => {
Mapping.forEntity(target).cascade(property, cascades);
};
}

/**
* Convenience method for auto incrementing values.
*
* - Generated uniqueConstraint name:
* @uniqueConstraint('property')
* @uniqueConstraint(['property1', 'property2'])
* @returns {Field}
*/
export function increments() {
return (target: Object, property: string) => {
Mapping.forEntity(target).increments(property);
};
}

/**
* Map a relationship.
*
* @param {Array|string} constraintName
* @param {Array|string} [fields]
* @param {Relationship} options
*
* @return {Function}
* @returns {Field}
*/
export function uniqueConstraint(constraintName: string | Array<string>, fields?: string | Array<string>) {
return (target: Object) => {
Mapping.forEntity(target).uniqueConstraint(constraintName, fields);
export function oneToOne(options: Relationship) {
return (target: Object, property: string) => {
Mapping.forEntity(target).oneToOne(property, options);
};
}

/**
* Map a relationship.
*
* @param {Relationship} options
*
* @returns {Field}
*/
export function oneToMany(options: Relationship) {
return (target: Object, property: string) => {
Mapping.forEntity(target).oneToMany(property, options);
};
}

/**
* Map a relationship.
*
* @param {Relationship} options
*
* @returns {Field}
*/
export function manyToOne(options: Relationship) {
return (target: Object, property: string) => {
Mapping.forEntity(target).manyToOne(property, options);
};
}

/**
* Map a relationship.
*
* @param {Relationship} options
*
* @returns {Field}
*/
export function manyToMany(options: Relationship) {
return (target: Object, property: string) => {
Mapping.forEntity(target).manyToMany(property, options);
};
}

/**
* Register a join table.
*
* @param {JoinTable} options
*
* @returns {Field}
*/
export function joinTable(options: JoinTable) {
return (target: Object, property: string) => {
Mapping.forEntity(target).joinTable(property, options);
};
}

/**
* Register a join column.
*
* @param {JoinTable} options
*
* @returns {Field}
*/
export function joinColumn(options: JoinColumn) {
return (target: Object, property: string) => {
Mapping.forEntity(target).joinColumn(property, options);
};
}

0 comments on commit 6d3766c

Please sign in to comment.