Permalink
Browse files

feat(Entity): add base entity class for toObject method

  • Loading branch information...
RWOverdijk committed Jan 4, 2017
1 parent 56a6014 commit 9d3d4efe55578efb8eaef353264a88ac3ea94920
Showing with 33 additions and 0 deletions.
  1. +31 −0 src/Entity.ts
  2. +2 −0 src/index.ts
@@ -0,0 +1,31 @@
import {Mapping} from './Mapping';
export class Entity {
private mapping: Mapping<this>;
private fieldNames: Array<string>;
constructor() {
this.mapping = Mapping.forEntity(this);
}
getFieldNames(): Array<string> {
if (!this.fieldNames) {
this.fieldNames = this.mapping.getFieldNames();
}
return this.fieldNames;
}
public static toObject(source: Object, mapping?: Mapping<{new ()}>, fieldNames?): Object {
return (fieldNames || mapping.getFieldNames()).reduce((asObject, fieldName) => {
asObject[fieldName] = source[fieldName];
return asObject;
}, {});
}
public toObject(): Object {
return Entity.toObject(this, null, this.getFieldNames());
}
}
@@ -17,6 +17,8 @@ export {Mapping, Field, FieldOptions, JoinColumn, JoinTable, Relationship} from
export {QueryBuilder} from './QueryBuilder';
export {EntityManager} from './EntityManager';
export {Scope} from './Scope';
export {Populate} from './Populate';
export {Criteria} from './Criteria/Criteria';
export {Where} from './Criteria/Where';
export {Having} from './Criteria/Having';
export {Entity} from './Entity';

0 comments on commit 9d3d4ef

Please sign in to comment.