Permalink
Browse files

feat(Mapping): add convenience methods for pk, updatedAt and createdAt

  • Loading branch information...
RWOverdijk committed Jan 11, 2018
1 parent df16e70 commit 8156fcac2316e18370a976b4712a629cb9e92836
Showing with 91 additions and 0 deletions.
  1. +80 −0 src/Mapping.ts
  2. +11 −0 src/decorators/Mapping.ts
@@ -517,6 +517,42 @@ export class Mapping<T> {
return this;
}
/**
* Convenience method that automatically sets a PK id.
*
* @returns {Mapping}
*/
public autoPK(): this {
return this.increments('id').primary('id');
}
/**
* Convenience method that automatically sets a createdAt.
*
* @returns {Mapping}
*/
public autoCreatedAt(): this {
return this.field('createdAt', { type: 'datetime', defaultTo: this.now() });
}
/**
* Convenience method that automatically sets an updatedAt.
*
* @returns {Mapping}
*/
public autoUpdatedAt(): this {
return this.field('createdAt', { type: 'datetime', defaultTo: this.now() });
}
/**
* Convenience method that automatically sets a PK, updatedAt and createdAt.
*
* @returns {Mapping}
*/
public autoFields(): this {
return this.autoPK().autoCreatedAt().autoUpdatedAt()
}
/**
* Get the column name for the primary key.
*
@@ -1010,6 +1046,50 @@ export class Field {
return this;
}
/**
* Generate a PK field id
*
* @return {Field}
*/
public autoPK() {
this.mapping.autoPK();
return this;
}
/**
* Generate a createdAt field
*
* @return {Field}
*/
public autoCreatedAt() {
this.mapping.autoCreatedAt();
return this;
}
/**
* Generate an updatedAt field
*
* @return {Field}
*/
public autoUpdatedAt() {
this.mapping.autoUpdatedAt();
return this;
}
/**
* Generate a PK, createdAt and updatedAt field
*
* @return {Field}
*/
public autoFields() {
this.mapping.autoFields();
return this;
}
/**
* Map generatedValues. Examples:
*
@@ -22,6 +22,17 @@ export function entity(options?: {repository?: typeof EntityRepository, name?: s
};
}
/**
* Decorate autoFields (id, createdAt, updatedAt) for an entity.
*
* @return {Mapping}
*/
export function autoFields() {
return (target: Object) => {
Mapping.forEntity(target).autoFields();
};
}
/**
* Decorate your entity with an index. Examples:
*

0 comments on commit 8156fca

Please sign in to comment.