knex-model
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -16,3 +16,8 @@ 'use strict'; | ||
Base.find = function() { | ||
Base.find = function(query) { | ||
// default find all | ||
if(typeof query === 'undefined') { | ||
Array.prototype.push.call(arguments, {}); | ||
} | ||
var self = this; | ||
@@ -40,1 +45,5 @@ var knex = this._knex(this._tableName); | ||
}; | ||
Base.prototype.toJSON = function() { | ||
return this._meta; | ||
}; |
@@ -7,3 +7,3 @@ 'use strict'; | ||
var KenxModel = { | ||
VERSION: '0.0.1', | ||
VERSION: '0.0.2', | ||
define: define | ||
@@ -21,3 +21,3 @@ }; | ||
function define(name, definition) { | ||
function define(name, definition, prototype, classProps) { | ||
if(typeof name !== 'string') { | ||
@@ -33,5 +33,9 @@ definition = name; | ||
}; | ||
_.extend(model.prototype, Base.prototype); | ||
_.extend(model, Base, { _tableName: tableName }); | ||
// save super class prototype | ||
model.prototype.super = Base.prototype; | ||
_.extend(model.prototype, Base.prototype, prototype); | ||
_.extend(model, Base, classProps,{ _tableName: tableName }); | ||
relationBuilder(model, definition); | ||
@@ -70,3 +74,15 @@ | ||
} | ||
if(definition.hasOne) { | ||
var hasOne = definition.hasOne; | ||
hasOne = _.isArray(hasOne) ? hasOne : [ hasOne ]; | ||
_.each(hasOne, function(item) { | ||
var name = item.name || item.model; | ||
proto.__defineGetter__(name, function() { | ||
var target = momery[item.model]; | ||
return new Relation(this, 'hasOne', target, item.key); | ||
}); | ||
}); | ||
} | ||
} | ||
}; |
@@ -46,5 +46,5 @@ 'use strict'; | ||
var foreign = this.target; | ||
if(this.type == 'hasMany') { | ||
if(this.type === 'hasMany' || this.type === 'hasOne') { | ||
query[this.keyField] = source.id; | ||
} else if(this.type == 'belongsTo') { | ||
} else if(this.type === 'belongsTo') { | ||
query.id = source._meta[this.keyField]; | ||
@@ -51,0 +51,0 @@ } |
{ | ||
"name": "knex-model", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -55,2 +55,2 @@ # knex-model | ||
- [ ] Relation `through` | ||
- [ ] Realtion `hasOne` | ||
- [x] Realtion `hasOne` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6082
151