Permalink
Browse files

feat(mapping): Auto column names to underscore

  • Loading branch information...
Rawphs
Rawphs committed Oct 25, 2016
1 parent 27dae2f commit 0560d6241626cc7e4ddcd69843927e02a48fd38b
Showing with 20 additions and 1 deletion.
  1. +17 −1 src/Mapping.ts
  2. +3 −0 src/Wetland.ts
@@ -172,13 +172,29 @@ export class Mapping<T> {
* @return {Mapping}
*/
public field(property: string, options: FieldOptions): this {
Homefront.merge(this.mapping.fetchOrPut(`fields.${property}`, {name: property}), options);
if (!this.entityManager) {
this.stageOrGetManager('field', arguments);
}
let config = this.entityManager.getConfig();
let propertyName = config.fetch('mapping.defaultNamesToUnderscore')
? this.nameToUnderscore(property)
: property;
Homefront.merge(this.mapping.fetchOrPut(`fields.${property}`, {name: propertyName}), options);
this.mapColumn(this.getColumnName(property), property);
return this;
}
private nameToUnderscore(property: string): string {
let name = property[0].toLowerCase() + property.slice(1);
return name.replace(/[A-Z]/g, '_$&').replace('__', '_').toLowerCase();
}
/**
* Get the repository class for this mapping's entity.
*
@@ -26,6 +26,9 @@ export class Wetland {
debug : false,
dataDirectory: path.resolve(process.cwd(), '.data'),
defaultStore : 'defaultStore',
mapping: {
defaultNamesToUnderscore: false
},
entityManager: {
refreshCreated: true,
refreshUpdated: true

0 comments on commit 0560d62

Please sign in to comment.