不要怂,就是干,撸起袖子干!

Commit f0f55ae0 by Sascha Depold

pluralize tablenames, singularize identifier fields

1 parent 0ca2d91c
...@@ -11,7 +11,7 @@ var BelongsTo = module.exports = function(srcModel, targetModel, options) { ...@@ -11,7 +11,7 @@ var BelongsTo = module.exports = function(srcModel, targetModel, options) {
BelongsTo.prototype.injectAttributes = function() { BelongsTo.prototype.injectAttributes = function() {
var newAttributes = {} var newAttributes = {}
this.identifier = this.options.foreignKey || Utils._.underscoredIf(this.target.tableName + "Id", this.source.options.underscored) this.identifier = this.options.foreignKey || Utils._.underscoredIf(Utils.singularize(this.target.tableName) + "Id", this.source.options.underscored)
newAttributes[this.identifier] = { type: DataTypes.INTEGER } newAttributes[this.identifier] = { type: DataTypes.INTEGER }
Utils._.extend(this.source.attributes, Utils.simplifyAttributes(newAttributes)) Utils._.extend(this.source.attributes, Utils.simplifyAttributes(newAttributes))
...@@ -20,7 +20,7 @@ BelongsTo.prototype.injectAttributes = function() { ...@@ -20,7 +20,7 @@ BelongsTo.prototype.injectAttributes = function() {
BelongsTo.prototype.injectGetter = function(obj) { BelongsTo.prototype.injectGetter = function(obj) {
var self = this var self = this
, accessor = Utils._.camelize('get_' + (this.options.as || this.target.tableName)) , accessor = Utils._.camelize('get_' + (this.options.as || Utils.singularize(this.target.tableName)))
obj[accessor] = function() { obj[accessor] = function() {
var id = obj[self.identifier] var id = obj[self.identifier]
...@@ -32,7 +32,7 @@ BelongsTo.prototype.injectGetter = function(obj) { ...@@ -32,7 +32,7 @@ BelongsTo.prototype.injectGetter = function(obj) {
BelongsTo.prototype.injectSetter = function(obj) { BelongsTo.prototype.injectSetter = function(obj) {
var self = this var self = this
, accessor = Utils._.camelize('set_' + (this.options.as || this.target.tableName)) , accessor = Utils._.camelize('set_' + (this.options.as || Utils.singularize(this.target.tableName)))
obj[accessor] = function(associatedObject) { obj[accessor] = function(associatedObject) {
obj[self.identifier] = associatedObject ? associatedObject.id : null obj[self.identifier] = associatedObject ? associatedObject.id : null
......
var Utils = require("./../utils") var Utils = require("./../utils")
, DataTypes = require('./../data-types') , DataTypes = require('./../data-types')
var HasMany = module.exports = function(srcModel, targetModel, options) { var HasMany = module.exports = function(srcModel, targetModel, options) {
this.source = srcModel this.source = srcModel
this.target = targetModel this.target = targetModel
this.options = options this.options = options
this.accessors = {
get: Utils._.camelize('get_' + (this.options.as || this.target.tableName + 's')), var as = (this.options.as || Utils.pluralize(this.target.tableName))
set: Utils._.camelize('set_' + (this.options.as || this.target.tableName + 's')) this.accessors = {
get: Utils._.camelize('get_' + as),
set: Utils._.camelize('set_' + as)
} }
} }
// the id is in the target table // the id is in the target table
// or in an extra table which connects two tables // or in an extra table which connects two tables
HasMany.prototype.injectAttributes = function() { HasMany.prototype.injectAttributes = function() {
this.identifier = this.options.foreignKey || Utils._.underscoredIf(this.source.tableName + "Id", this.options.underscored) this.identifier = this.options.foreignKey || Utils._.underscoredIf(Utils.singularize(this.source.tableName) + "Id", this.options.underscored)
if (this.target.associations.hasOwnProperty(this.source.tableName)) { if (this.target.associations.hasOwnProperty(this.source.tableName)) {
// remove the obsolete association identifier from the source // remove the obsolete association identifier from the source
......
...@@ -6,8 +6,8 @@ var HasOne = module.exports = function(srcModel, targetModel, options) { ...@@ -6,8 +6,8 @@ var HasOne = module.exports = function(srcModel, targetModel, options) {
this.target = targetModel this.target = targetModel
this.options = options this.options = options
this.accessors = { this.accessors = {
get: Utils._.camelize('get_' + (this.options.as || this.target.tableName)), get: Utils._.camelize('get_' + (this.options.as || Utils.singularize(this.target.tableName))),
set: Utils._.camelize('set_' + (this.options.as || this.target.tableName)) set: Utils._.camelize('set_' + (this.options.as || Utils.singularize(this.target.tableName)))
} }
} }
...@@ -15,7 +15,7 @@ var HasOne = module.exports = function(srcModel, targetModel, options) { ...@@ -15,7 +15,7 @@ var HasOne = module.exports = function(srcModel, targetModel, options) {
HasOne.prototype.injectAttributes = function() { HasOne.prototype.injectAttributes = function() {
var newAttributes = {} var newAttributes = {}
this.identifier = this.options.foreignKey || Utils._.underscoredIf(this.source.tableName + "Id", this.options.underscored) this.identifier = this.options.foreignKey || Utils._.underscoredIf(Utils.singularize(this.source.tableName) + "Id", this.options.underscored)
newAttributes[this.identifier] = { type: DataTypes.INTEGER } newAttributes[this.identifier] = { type: DataTypes.INTEGER }
Utils._.extend(this.target.attributes, Utils.simplifyAttributes(newAttributes)) Utils._.extend(this.target.attributes, Utils.simplifyAttributes(newAttributes))
......
...@@ -9,7 +9,7 @@ var ModelDefinition = module.exports = function(name, attributes, options) { ...@@ -9,7 +9,7 @@ var ModelDefinition = module.exports = function(name, attributes, options) {
this.options = options || {} this.options = options || {}
this.options.timestamps = this.options.hasOwnProperty('timestamps') ? this.options.timestamps : true this.options.timestamps = this.options.hasOwnProperty('timestamps') ? this.options.timestamps : true
this.name = name this.name = name
this.tableName = name this.tableName = Utils.pluralize(name)
this.attributes = Utils.simplifyAttributes(attributes) this.attributes = Utils.simplifyAttributes(attributes)
this.modelManager = null // defined by model-manager during addModel this.modelManager = null // defined by model-manager during addModel
this.associations = {} this.associations = {}
......
...@@ -125,6 +125,12 @@ var Utils = module.exports = { ...@@ -125,6 +125,12 @@ var Utils = module.exports = {
, chars2 = Utils._.chars(tableName2.toLowerCase()) , chars2 = Utils._.chars(tableName2.toLowerCase())
return (chars1[0] < chars2[0]) ? (tableName1 + tableName2) : (tableName2 + tableName1) return (chars1[0] < chars2[0]) ? (tableName1 + tableName2) : (tableName2 + tableName1)
},
singularize: function(s) {
return Utils.Lingo.en.isSingular(s) ? s : Utils.Lingo.en.singularize(s)
},
pluralize: function(s) {
return Utils.Lingo.en.isPlural(s) ? s : Utils.Lingo.en.pluralize(s)
} }
} }
...@@ -140,4 +146,5 @@ CustomEventEmitter.prototype.run = function() { ...@@ -140,4 +146,5 @@ CustomEventEmitter.prototype.run = function() {
return this return this
} }
Utils.QueryChainer = require("./query-chainer") Utils.QueryChainer = require("./query-chainer")
\ No newline at end of file Utils.Lingo = require("lingo")
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!