objection
Advanced tools
Comparing version 0.4.0 to 0.5.0-alpha.0
@@ -370,4 +370,5 @@ 'use strict'; | ||
key: 'join', | ||
value: function join(builder, joinMethod) { | ||
value: function join(builder, joinMethod, relatedTableAlias) { | ||
joinMethod = joinMethod || 'join'; | ||
relatedTableAlias = relatedTableAlias || this.relatedTableAlias(); | ||
@@ -378,4 +379,2 @@ var joinTable = this.joinTable; | ||
var joinTableAlias = this.joinTableAlias(); | ||
var relatedTableAlias = this.relatedTableAlias(); | ||
var joinTableAsAlias = joinTable + ' as ' + joinTableAlias; | ||
@@ -382,0 +381,0 @@ var relatedTableAsAlias = relatedTable + ' as ' + relatedTableAlias; |
@@ -487,2 +487,3 @@ 'use strict'; | ||
* @param {string=} joinMethod | ||
* @param {string=} relatedTableAlias | ||
* @returns {QueryBuilder} | ||
@@ -493,8 +494,7 @@ */ | ||
key: 'join', | ||
value: function join(builder, joinMethod) { | ||
value: function join(builder, joinMethod, relatedTableAlias) { | ||
joinMethod = joinMethod || 'join'; | ||
relatedTableAlias = relatedTableAlias || this.relatedTableAlias(); | ||
var relatedTable = this.relatedModelClass.tableName; | ||
var relatedTableAlias = this.relatedTableAlias(); | ||
var relatedTableAsAlias = relatedTable + ' as ' + relatedTableAlias; | ||
@@ -501,0 +501,0 @@ var relatedCol = _lodash2.default.map(this.relatedCol, function (col) { |
{ | ||
"name": "objection", | ||
"version": "0.4.0", | ||
"version": "0.5.0-alpha.0", | ||
"description": "An SQL-friendly ORM for Node.js", | ||
@@ -15,2 +15,5 @@ "main": "lib/objection.js", | ||
}, | ||
"publishConfig": { | ||
"tag": "next" | ||
}, | ||
"author": { | ||
@@ -17,0 +20,0 @@ "name": "Sami Koskimäki", |
@@ -37,3 +37,5 @@ [![Build Status](https://travis-ci.org/Vincit/objection.js.svg?branch=master)](https://travis-ci.org/Vincit/objection.js) [![Coverage Status](https://coveralls.io/repos/Vincit/objection.js/badge.svg?branch=master&service=github)](https://coveralls.io/github/Vincit/objection.js?branch=master) | ||
[Objection.js API reference](http://vincit.github.io/objection.js/#api-reference) | ||
Shortcuts: | ||
* [Objection.js API reference](http://vincit.github.io/objection.js/#api-reference) | ||
* [Changelog](http://vincit.github.io/objection.js/#changelog) | ||
@@ -40,0 +42,0 @@ Tutorials and blogs: |
@@ -50,2 +50,6 @@ import _ from 'lodash'; | ||
this.clearCustomImpl(); | ||
this.internalContext().runBefore = []; | ||
this.internalContext().runAfter = []; | ||
this.internalContext().onBuild = []; | ||
} | ||
@@ -719,2 +723,9 @@ | ||
let builderIntCtx = builder.internalContext(); | ||
let intCtx = this.internalContext(); | ||
builderIntCtx.runBefore = intCtx.runBefore.slice(); | ||
builderIntCtx.runAfter = intCtx.runAfter.slice(); | ||
builderIntCtx.onBuild = intCtx.onBuild.slice(); | ||
builder._calledWriteMethod = this._calledWriteMethod; | ||
@@ -1072,4 +1083,5 @@ builder._explicitRejectValue = this._explicitRejectValue; | ||
let builder = this.clone(); | ||
let promise = Promise.resolve(); | ||
let context = builder.context() || {}; | ||
let promise = Promise.resolve(); | ||
let internalContext = builder.internalContext(); | ||
@@ -1082,10 +1094,6 @@ if (builder.isFindQuery()) { | ||
if (_.isFunction(context.runBefore)) { | ||
promise = promise.then(result => context.runBefore.call(builder, result, builder)); | ||
} | ||
promise = chainBuilderFuncs(promise, builder, context.runBefore); | ||
promise = chainBuilderFuncs(promise, builder, internalContext.runBefore); | ||
promise = chainBuilderFuncs(promise, builder, builder._hooks.before); | ||
_.forEach(builder._hooks.before, func => { | ||
promise = promise.then(result => func.call(builder, result, builder)); | ||
}); | ||
// Resolve all before hooks before building and executing the query | ||
@@ -1109,5 +1117,3 @@ // and the rest of the hooks. | ||
_.forEach(builder._hooks.afterModelCreate, func => { | ||
promise = promise.then(result => func.call(builder, result, builder)); | ||
}); | ||
promise = chainBuilderFuncs(promise, builder, builder._hooks.afterModelCreate); | ||
@@ -1118,10 +1124,6 @@ if (builder._eagerExpression) { | ||
if (_.isFunction(context.runAfter)) { | ||
promise = promise.then(result => context.runAfter.call(builder, result, builder)); | ||
} | ||
promise = chainBuilderFuncs(promise, builder, context.runAfter); | ||
promise = chainBuilderFuncs(promise, builder, internalContext.runAfter); | ||
promise = chainBuilderFuncs(promise, builder, builder._hooks.after); | ||
_.forEach(builder._hooks.after, func => { | ||
promise = promise.then(result => func.call(builder, result, builder)); | ||
}); | ||
return promise; | ||
@@ -1337,2 +1339,163 @@ }); | ||
/** | ||
* Joins a relation. | ||
* | ||
* The joined table is aliased with the relation's name. | ||
* | ||
* ```js | ||
* Person | ||
* .query() | ||
* .joinRelation('pets') | ||
* .where('pets.species', 'dog'); | ||
* ``` | ||
* | ||
* @param {string} relationName | ||
* @returns {QueryBuilder} | ||
*/ | ||
joinRelation(relationName) { | ||
return this.$$joinRelation(relationName, 'join'); | ||
} | ||
/** | ||
* Joins a relation. | ||
* | ||
* The joined table is aliased with the relation's name. | ||
* | ||
* ```js | ||
* Person | ||
* .query() | ||
* .innerJoinRelation('pets') | ||
* .where('pets.species', 'dog'); | ||
* ``` | ||
* | ||
* @param {string} relationName | ||
* @returns {QueryBuilder} | ||
*/ | ||
innerJoinRelation(relationName) { | ||
return this.$$joinRelation(relationName, 'innerJoin'); | ||
} | ||
/** | ||
* Joins a relation. | ||
* | ||
* The joined table is aliased with the relation's name. | ||
* | ||
* ```js | ||
* Person | ||
* .query() | ||
* .outerJoinRelation('pets') | ||
* .where('pets.species', 'dog'); | ||
* ``` | ||
* | ||
* @param {string} relationName | ||
* @returns {QueryBuilder} | ||
*/ | ||
outerJoinRelation(relationName) { | ||
return this.$$joinRelation(relationName, 'outerJoin'); | ||
} | ||
/** | ||
* Joins a relation. | ||
* | ||
* The joined table is aliased with the relation's name. | ||
* | ||
* ```js | ||
* Person | ||
* .query() | ||
* .leftJoinRelation('pets') | ||
* .where('pets.species', 'dog'); | ||
* ``` | ||
* | ||
* @param {string} relationName | ||
* @returns {QueryBuilder} | ||
*/ | ||
leftJoinRelation(relationName) { | ||
return this.$$joinRelation(relationName, 'leftJoin'); | ||
} | ||
/** | ||
* Joins a relation. | ||
* | ||
* The joined table is aliased with the relation's name. | ||
* | ||
* ```js | ||
* Person | ||
* .query() | ||
* .leftOuterJoinRelation('pets') | ||
* .where('pets.species', 'dog'); | ||
* ``` | ||
* | ||
* @param {string} relationName | ||
* @returns {QueryBuilder} | ||
*/ | ||
leftOuterJoinRelation(relationName) { | ||
return this.$$joinRelation(relationName, 'leftOuterJoin'); | ||
} | ||
/** | ||
* Joins a relation. | ||
* | ||
* The joined table is aliased with the relation's name. | ||
* | ||
* ```js | ||
* Person | ||
* .query() | ||
* .rightJoinRelation('pets') | ||
* .where('pets.species', 'dog'); | ||
* ``` | ||
* | ||
* @param {string} relationName | ||
* @returns {QueryBuilder} | ||
*/ | ||
rightJoinRelation(relationName) { | ||
return this.$$joinRelation(relationName, 'rightJoin'); | ||
} | ||
/** | ||
* Joins a relation. | ||
* | ||
* The joined table is aliased with the relation's name. | ||
* | ||
* ```js | ||
* Person | ||
* .query() | ||
* .rightOuterJoinRelation('pets') | ||
* .where('pets.species', 'dog'); | ||
* ``` | ||
* | ||
* @param {string} relationName | ||
* @returns {QueryBuilder} | ||
*/ | ||
rightOuterJoinRelation(relationName) { | ||
return this.$$joinRelation(relationName, 'rightOuterJoin'); | ||
} | ||
/** | ||
* Joins a relation. | ||
* | ||
* The joined table is aliased with the relation's name. | ||
* | ||
* ```js | ||
* Person | ||
* .query() | ||
* .fullOuterJoinRelation('pets') | ||
* .where('pets.species', 'dog'); | ||
* ``` | ||
* | ||
* @param {string} relationName | ||
* @returns {QueryBuilder} | ||
*/ | ||
fullOuterJoinRelation(relationName) { | ||
return this.$$joinRelation(relationName, 'fullOuterJoin'); | ||
} | ||
/** | ||
* @private | ||
*/ | ||
$$joinRelation(relationName, joinMethod) { | ||
let relation = this._modelClass.getRelation(relationName); | ||
relation.join(this, joinMethod, relation.name); | ||
return this; | ||
} | ||
/** | ||
* Shortcut for finding a model by id. | ||
@@ -1358,2 +1521,17 @@ * | ||
/** | ||
* @override | ||
* @inheritDoc | ||
*/ | ||
withSchema(schema) { | ||
this.internalContext().onBuild.push((builder) => { | ||
if (!builder.has(/withSchema/)) { | ||
// If the builder already has a schema, don't override it. | ||
builder.callKnexMethod('withSchema', [schema]); | ||
} | ||
}); | ||
return this; | ||
} | ||
/** | ||
* Creates an insert query. | ||
@@ -2203,3 +2381,4 @@ * | ||
function build(builder) { | ||
var context = builder.context() || {}; | ||
let context = builder.context() || {}; | ||
let internalContext = builder.internalContext(); | ||
@@ -2211,10 +2390,6 @@ if (!builder.has(/from|table|into/)) { | ||
if (_.isFunction(context.onBuild)) { | ||
context.onBuild.call(builder, builder); | ||
} | ||
callBuilderFuncs(builder, context.onBuild); | ||
callBuilderFuncs(builder, internalContext.onBuild); | ||
callBuilderFuncs(builder, builder._hooks.onBuild); | ||
_.forEach(builder._hooks.onBuild, function (func) { | ||
func.call(builder, builder); | ||
}); | ||
// noinspection JSUnresolvedVariable | ||
@@ -2230,2 +2405,30 @@ return QueryBuilderBase.prototype.build.call(builder); | ||
return _.map(batches, batch => queryBuilder.clone().insert(batch)); | ||
} | ||
/** | ||
* @private | ||
*/ | ||
function callBuilderFuncs(builder, func) { | ||
if (_.isFunction(func)) { | ||
func.call(builder, builder); | ||
} else if (_.isArray(func)) { | ||
_.each(func, func => { | ||
func.call(builder, builder); | ||
}); | ||
} | ||
} | ||
/** | ||
* @private | ||
*/ | ||
function chainBuilderFuncs(promise, builder, func) { | ||
if (_.isFunction(func)) { | ||
promise = promise.then(result => func.call(builder, result, builder)); | ||
} else if (_.isArray(func)) { | ||
_.each(func, func => { | ||
promise = promise.then(result => func.call(builder, result, builder)); | ||
}); | ||
} | ||
return promise; | ||
} |
@@ -22,3 +22,5 @@ import _ from 'lodash'; | ||
this._knexMethodCalls = []; | ||
this._context = {}; | ||
this._context = { | ||
userContext: {} | ||
}; | ||
} | ||
@@ -50,3 +52,3 @@ | ||
/** | ||
* Sets/gets the query full internal context. | ||
* Sets/gets the query's internal context. | ||
* | ||
@@ -131,3 +133,3 @@ * For internal use only. | ||
builder._knexMethodCalls = this._knexMethodCalls.slice(); | ||
builder._context = this._context; | ||
builder._context = _.clone(this._context); | ||
} | ||
@@ -218,2 +220,32 @@ | ||
/** | ||
* @private | ||
*/ | ||
callKnexMethod(methodName, args) { | ||
for (let i = 0, l = args.length; i < l; ++i) { | ||
if (_.isUndefined(args[i])) { | ||
// None of the query builder methods should accept undefined. Do nothing if | ||
// one of the arguments is undefined. This enables us to do things like | ||
// `.where('name', req.query.name)` without checking if req.query has the | ||
// property `name`. | ||
return this; | ||
} else if (args[i] instanceof QueryBuilderBase) { | ||
// Convert QueryBuilderBase instances into knex query builders. | ||
args[i] = args[i].build(); | ||
} else if (_.isFunction(args[i])) { | ||
// If an argument is a function, knex calls it with a query builder as | ||
// `this` context. We call the function with a QueryBuilderBase as | ||
// `this` context instead. | ||
args[i] = wrapFunctionArg(args[i], this); | ||
} | ||
} | ||
this._knexMethodCalls.push({ | ||
method: methodName, | ||
args: args | ||
}); | ||
return this; | ||
} | ||
/** | ||
* See <a href="http://knexjs.org">knex documentation</a> | ||
@@ -1298,31 +1330,3 @@ * @returns {QueryBuilderBase} | ||
descriptor.value = function () { | ||
let args = new Array(arguments.length); | ||
let context = this.internalContext(); | ||
for (let i = 0, l = arguments.length; i < l; ++i) { | ||
if (_.isUndefined(arguments[i])) { | ||
// None of the query builder methods should accept undefined. Do nothing if | ||
// one of the arguments is undefined. This enables us to do things like | ||
// `.where('name', req.query.name)` without checking if req.query has the | ||
// property `name`. | ||
return this; | ||
} else if (arguments[i] instanceof QueryBuilderBase) { | ||
// Convert QueryBuilderBase instances into knex query builders. | ||
args[i] = arguments[i].internalContext(context).build(); | ||
} else if (_.isFunction(arguments[i])) { | ||
// If an argument is a function, knex calls it with a query builder as | ||
// `this` context. We call the function with a QueryBuilderBase as | ||
// `this` context instead. | ||
args[i] = wrapFunctionArg(arguments[i], this); | ||
} else { | ||
args[i] = arguments[i]; | ||
} | ||
} | ||
this._knexMethodCalls.push({ | ||
method: overrideMethodName || methodName, | ||
args: args | ||
}); | ||
return this; | ||
return this.callKnexMethod(overrideMethodName || methodName, _.toArray(arguments)); | ||
}; | ||
@@ -1338,4 +1342,3 @@ }; | ||
if (isKnexQueryBuilder(this)) { | ||
let context = query.internalContext(); | ||
let builder = new QueryBuilderBase(query._knex).internalContext(context); | ||
let builder = new QueryBuilderBase(query._knex); | ||
func.call(builder, builder); | ||
@@ -1342,0 +1345,0 @@ builder.buildInto(this); |
@@ -243,4 +243,5 @@ import _ from 'lodash'; | ||
*/ | ||
join(builder, joinMethod) { | ||
join(builder, joinMethod, relatedTableAlias) { | ||
joinMethod = joinMethod || 'join'; | ||
relatedTableAlias = relatedTableAlias || this.relatedTableAlias(); | ||
@@ -251,4 +252,2 @@ let joinTable = this.joinTable; | ||
let joinTableAlias = this.joinTableAlias(); | ||
let relatedTableAlias = this.relatedTableAlias(); | ||
let joinTableAsAlias = joinTable + ' as ' + joinTableAlias; | ||
@@ -255,0 +254,0 @@ let relatedTableAsAlias = relatedTable + ' as ' + relatedTableAlias; |
@@ -394,10 +394,10 @@ import _ from 'lodash'; | ||
* @param {string=} joinMethod | ||
* @param {string=} relatedTableAlias | ||
* @returns {QueryBuilder} | ||
*/ | ||
join(builder, joinMethod) { | ||
join(builder, joinMethod, relatedTableAlias) { | ||
joinMethod = joinMethod || 'join'; | ||
relatedTableAlias = relatedTableAlias || this.relatedTableAlias(); | ||
let relatedTable = this.relatedModelClass.tableName; | ||
let relatedTableAlias = this.relatedTableAlias(); | ||
let relatedTableAsAlias = relatedTable + ' as ' + relatedTableAlias; | ||
@@ -404,0 +404,0 @@ let relatedCol = _.map(this.relatedCol, col => relatedTableAlias + '.' + col); |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
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
671471
20546
46