knex-model
Advanced tools
Comparing version 0.0.5 to 0.1.1
'use strict'; | ||
var _ = require('lodash'); | ||
@@ -9,3 +8,3 @@ module.exports = Base; | ||
this.id = params.id; | ||
}; | ||
} | ||
@@ -17,41 +16,2 @@ Base.new = function() { | ||
Base.find = function(query) { | ||
var self = this; | ||
var knex = this._knex(this._tableName); | ||
if(query) { | ||
knex = knex.where.apply(knex, arguments); | ||
} | ||
return knex.select().map(function(item) { | ||
return self.new(item); | ||
}); | ||
}; | ||
Base.first = function(query) { | ||
var self = this; | ||
var knex = this._knex(this._tableName); | ||
if(query) { | ||
knex = knex.where.apply(knex, arguments); | ||
} | ||
return knex.first().then(function(result) { | ||
return result ? self.new(result) : undefined; | ||
}); | ||
}; | ||
Base.where = function() { | ||
var knex = this._knex(this._tableName); | ||
return knex.where.apply(knex, arguments); | ||
}; | ||
Base.create = function(params) { | ||
var self = this; | ||
return this._knex(this._tableName) | ||
.insert(params) | ||
.then(function(insertId) { | ||
return self.first('id', insertId[0]); | ||
}); | ||
}; | ||
Base.prototype.delete = function() { | ||
@@ -58,0 +18,0 @@ var _class = this.__proto__.class; |
'use strict'; | ||
var _ = require('lodash'); | ||
var util = require("util"); | ||
var pjson = require('../package.json'); | ||
var momery = require('./momery'); | ||
var utils = require('./utils'); | ||
var Builder = require('./builder'); | ||
var methods = require('./methods'); | ||
module.exports = function(knex) { | ||
var KenxModel = { | ||
VERSION: '0.0.5', | ||
define: define | ||
}; | ||
/** store models **/ | ||
var momery = {}; | ||
var Relation = require('./relation'); | ||
@@ -18,2 +14,8 @@ var Base = require('./base'); | ||
var KenxModel = { | ||
VERSION: pjson.version, | ||
define: define, | ||
BaseModel: Base | ||
}; | ||
return KenxModel; | ||
@@ -30,3 +32,2 @@ | ||
Base.call(this, params); | ||
this.__proto__.class = model; | ||
}; | ||
@@ -36,3 +37,6 @@ | ||
// save super class prototype | ||
/** save Model reference */ | ||
model.prototype.class = model; | ||
/** save super class prototype reference */ | ||
model.prototype.super = Base.prototype; | ||
@@ -43,44 +47,48 @@ | ||
relationBuilder(model, definition); | ||
buildRelation(model, definition); | ||
momery[model._name] = model; | ||
buildKnexMethod(model); | ||
/** save model to momery */ | ||
momery.set(model._name, model); | ||
return model; | ||
} | ||
function relationBuilder(model, definition) { | ||
function buildKnexMethod(model) { | ||
methods | ||
.concat(_.keys(Builder.prototype)) | ||
.forEach(function(method) { | ||
model[method] = function() { | ||
var builder = new Builder(model); | ||
return builder[method].apply(builder, arguments); | ||
}; | ||
}); | ||
model.getKnex = function() { | ||
return new Builder(model); | ||
}; | ||
} | ||
function buildRelation(model, definition) { | ||
var proto = model.prototype; | ||
if(definition.hasMany) { | ||
var hasMany = definition.hasMany; | ||
hasMany = _.isArray(hasMany) ? hasMany : [ hasMany ]; | ||
_.each(hasMany, function(item) { | ||
var name = item.name || item.model; | ||
proto.__defineGetter__(name, function() { | ||
var target = momery[item.model]; | ||
return new Relation(this, 'hasMany', target, item.key, item.where); | ||
}); | ||
}); | ||
buildRelation('hasMany', definition.hasMany); | ||
} | ||
if(definition.belongsTo) { | ||
var belongsTo = definition.belongsTo; | ||
belongsTo = _.isArray(belongsTo) ? belongsTo : [ belongsTo ]; | ||
_.each(belongsTo, function(item) { | ||
var name = item.name || item.model; | ||
proto.__defineGetter__(name, function() { | ||
var target = momery[item.model]; | ||
return new Relation(this, 'belongsTo', target, item.key, item.where); | ||
}); | ||
}); | ||
buildRelation('belongsTo', definition.belongsTo); | ||
} | ||
if(definition.hasOne) { | ||
var hasOne = definition.hasOne; | ||
hasOne = _.isArray(hasOne) ? hasOne : [ hasOne ]; | ||
_.each(hasOne, function(item) { | ||
buildRelation('hasOne', definition.hasOne); | ||
} | ||
function buildRelation(type, options) { | ||
options = _.isArray(options) ? options : [ options ]; | ||
_.each(options, 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, item.where); | ||
var target = utils.getModel(item.model); | ||
return new Relation(this, type, target, item); | ||
}); | ||
@@ -87,0 +95,0 @@ }); |
'use strict'; | ||
var _ = require('lodash'); | ||
var utils = require('./utils'); | ||
module.exports = Relation; | ||
function Relation(model, type, target, keyField, where) { | ||
function Relation(model, type, target, option) { | ||
this.model = model; | ||
this.type = type; | ||
this.target = target; | ||
this.keyField = keyField; | ||
this.where = where; | ||
}; | ||
this.keyField = option.key; | ||
this.where = option.where; | ||
this.through = option.through; | ||
} | ||
Relation.prototype.find = function(params) { | ||
var knex = this.target.getKnex(); | ||
var query = getQuery.call(this); | ||
var source = this.model; | ||
var foreign = this.target; | ||
query = _.extend({}, params, query); | ||
if(this.type === 'belongsTo' || this.type === 'hasOne') { | ||
return foreign.first(query); | ||
return knex.findOne(query); | ||
} | ||
return foreign.find(query); | ||
if(this.through) { | ||
knex = buildThrough.call(this, knex); | ||
delete query[this.keyField]; | ||
} | ||
return knex.find(query); | ||
}; | ||
Relation.prototype.findOne = function(params) { | ||
var knex = this.target.getKnex(); | ||
var query = getQuery.call(this); | ||
query = _.extend({}, params, query); | ||
if(this.type === 'hasMany' && this.through) { | ||
knex = buildThrough.call(this, knex); | ||
delete query[this.keyField]; | ||
} | ||
return knex.findOne(query); | ||
}; | ||
Relation.prototype.create = function(params) { | ||
@@ -42,14 +61,30 @@ | ||
Relation.prototype.delete = function(params) { | ||
var knex = this.target.getKnex(); | ||
var query = getQuery.call(this); | ||
var source = this.model; | ||
var foreign = this.target; | ||
query = _.extend({}, params, query); | ||
return foreign.where(query).delete(); | ||
if(this.type === 'hasMany' && this.through) { | ||
knex = buildThrough.call(this, knex); | ||
delete query[this.keyField]; | ||
} | ||
return knex.where(query).delete(); | ||
}; | ||
function buildThrough(knex) { | ||
var sourceId = this.model.id; | ||
var through = this.through; | ||
var throughTable = utils.getModel(through.model)._tableName; | ||
var throughFk = through.throughFk; | ||
var otherKey = through.otherKey; | ||
knex.whereIn(this.keyField, function() { | ||
this.select(otherKey).where(throughFk, sourceId).from(throughTable); | ||
}); | ||
return knex; | ||
} | ||
function getQuery() { | ||
var query = {}; | ||
var source = this.model; | ||
var foreign = this.target; | ||
@@ -56,0 +91,0 @@ if(this.where) { |
{ | ||
"name": "knex-model", | ||
"version": "0.0.5", | ||
"version": "0.1.1", | ||
"description": "Small ORM", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,2 +0,2 @@ | ||
# knex-model [![Build Status](https://travis-ci.org/wcp1231/knex-model.svg?branch=master)](https://travis-ci.org/wcp1231/knex-model) [![Coverage Status](https://coveralls.io/repos/wcp1231/knex-model/badge.png?branch=master)](https://coveralls.io/r/wcp1231/knex-model?branch=master) | ||
# knex-model [![Build Status](https://travis-ci.org/wcp1231/knex-model.svg?branch=master)](https://travis-ci.org/wcp1231/knex-model) [![Coverage Status](https://coveralls.io/repos/wcp1231/knex-model/badge.png?branch=master)](https://coveralls.io/r/wcp1231/knex-model?branch=master) [![Code Climate](https://codeclimate.com/github/wcp1231/knex-model/badges/gpa.svg)](https://codeclimate.com/github/wcp1231/knex-model) | ||
@@ -60,3 +60,3 @@ Small ORM. | ||
- [x] example | ||
- [ ] Relation `through` | ||
- [x] Relation `through` | ||
- [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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
11057
12
331
1