@capaj/objection
Advanced tools
Comparing version 1.6.13 to 1.6.14
@@ -728,2 +728,10 @@ 'use strict'; | ||
Object.defineProperties(Model, { | ||
isObjectionModelClass: { | ||
enumerable: false, | ||
writable: false, | ||
value: true | ||
} | ||
}); | ||
Object.defineProperties(Model.prototype, { | ||
@@ -730,0 +738,0 @@ $isObjectionModel: { |
@@ -165,4 +165,10 @@ 'use strict'; | ||
return option.indexOf(node.relationPathKey) !== -1; | ||
} else if (typeof option === 'boolean') { | ||
return option; | ||
} else if (option === undefined) { | ||
return false; | ||
} else { | ||
return !!option; | ||
throw new Error( | ||
`expected ${optionName} option value "${option}" to be an instance of boolean or array of strings` | ||
); | ||
} | ||
@@ -169,0 +175,0 @@ } |
@@ -96,4 +96,24 @@ 'use strict'; | ||
const deleteNodes = currentGraph.nodes.filter(currentNode => !graph.nodeForNode(currentNode)); | ||
const roots = findRoots(deleteNodes); | ||
removeBranchesFromGraph(findRoots(deleteNodes), currentGraph); | ||
// Don't delete relations the current graph doesn't even mention. | ||
// So if the parent node doesn't even have the relation, it's not | ||
// supposed to be deleted. | ||
const rootsNotInRelation = roots.filter(deleteRoot => { | ||
if (!deleteRoot.parentNode) { | ||
return false; | ||
} | ||
const { relation } = deleteRoot.parentEdge; | ||
const parentNode = graph.nodeForNode(deleteRoot.parentNode); | ||
if (!parentNode) { | ||
return false; | ||
} | ||
return parentNode.obj[relation.name] === undefined; | ||
}); | ||
removeBranchesFromGraph(roots, currentGraph); | ||
removeNodesFromGraph(new Set(rootsNotInRelation), currentGraph); | ||
} | ||
@@ -129,2 +149,6 @@ | ||
removeNodesFromGraph(nodesToRemove, graph); | ||
} | ||
function removeNodesFromGraph(nodesToRemove, graph) { | ||
const edgesToRemove = new Set(); | ||
@@ -131,0 +155,0 @@ |
@@ -6,3 +6,3 @@ 'use strict'; | ||
const { EagerOperation } = require('./EagerOperation'); | ||
const { isMsSql, isOracle } = require('../../../utils/knexUtils'); | ||
const { isMsSql, isOracle, isSqlite } = require('../../../utils/knexUtils'); | ||
const { asArray, flatten, chunk } = require('../../../utils/objectUtils'); | ||
@@ -29,2 +29,5 @@ const { ValidationErrorType } = require('../../../model/ValidationError'); | ||
return 1000; | ||
} else if (isSqlite(knex)) { | ||
// SQLITE_MAX_VARIABLE_NUMBER is 999 by default | ||
return 999; | ||
} else { | ||
@@ -31,0 +34,0 @@ // I'm sure there is some kind of limit for other databases too, but let's lower |
@@ -255,4 +255,12 @@ 'use strict'; | ||
Object.defineProperties(QueryBuilderOperation, { | ||
isObjectionQueryBuilderOperationClass: { | ||
enumerable: false, | ||
writable: false, | ||
value: true | ||
} | ||
}); | ||
module.exports = { | ||
QueryBuilderOperation | ||
}; |
@@ -29,7 +29,7 @@ 'use strict'; | ||
onBuild(builder) { | ||
super.onBuild(builder); | ||
if (!this.skipIdWhere) { | ||
builder.findById(this.id); | ||
} | ||
super.onBuild(builder); | ||
} | ||
@@ -43,7 +43,12 @@ | ||
// Clone `this` query builder so that we get the correct | ||
// operation factories in case of `$relatedQuery` etc. | ||
return builder | ||
.modelClass() | ||
.query() | ||
.emptyInstance() | ||
.childQueryOf(builder) | ||
.findById(this.id) | ||
.modify(builder => { | ||
if (!this.skipIdWhere) { | ||
builder.findById(this.id); | ||
} | ||
}) | ||
.castTo(builder.resultModelClass()) | ||
@@ -50,0 +55,0 @@ .then(fetched => { |
@@ -1002,5 +1002,3 @@ /* | ||
console.warn( | ||
`Duplicate relation "${ | ||
expr.$name | ||
}" in a relation expression. You should use "a.[b, c]" instead of "[a.b, a.c]". This will cause an error in objection 2.0` | ||
`Duplicate relation "${expr.$name}" in a relation expression. You should use "a.[b, c]" instead of "[a.b, a.c]". This will cause an error in objection 2.0` | ||
); | ||
@@ -1007,0 +1005,0 @@ |
@@ -455,3 +455,3 @@ 'use strict'; | ||
clone() { | ||
const builder = new this.constructor(this.modelClass()); | ||
const builder = this.emptyInstance(); | ||
@@ -469,2 +469,8 @@ // Call the super class's clone implementation. | ||
return builder; | ||
} | ||
emptyInstance() { | ||
const builder = new this.constructor(this.modelClass()); | ||
builder._findOperationFactory = this._findOperationFactory; | ||
@@ -717,8 +723,11 @@ builder._insertOperationFactory = this._insertOperationFactory; | ||
// Turn the properties into a hash for performance. | ||
properties = properties.reduce((obj, prop) => { | ||
obj[prop] = true; | ||
return obj; | ||
}, {}); | ||
if (Array.isArray(properties)) { | ||
// Turn the properties into a hash for performance. | ||
properties = properties.reduce((obj, prop) => { | ||
obj[prop] = true; | ||
return obj; | ||
}, {}); | ||
} else { | ||
properties = { [properties]: true }; | ||
} | ||
return this.traverse(modelClass, model => { | ||
@@ -999,3 +1008,3 @@ model.$omit(properties); | ||
range(...args) { | ||
return this.addOperation(new RangeOperation('range'), args); | ||
return this.clear(RangeOperation).addOperation(new RangeOperation('range'), args); | ||
} | ||
@@ -1208,5 +1217,3 @@ | ||
type: ValidationErrorType.RelationExpression, | ||
message: `Duplicate relation name "${ | ||
err.relationName | ||
}" in relation expression "${exp}". Use "a.[b, c]" instead of "[a.b, a.c]".` | ||
message: `Duplicate relation name "${err.relationName}" in relation expression "${exp}". Use "a.[b, c]" instead of "[a.b, a.c]".` | ||
}); | ||
@@ -1213,0 +1220,0 @@ } else { |
@@ -5,5 +5,3 @@ 'use strict'; | ||
const { isSubclassOf } = require('../utils/classUtils'); | ||
const { isString, isFunction, isRegExp, last } = require('../utils/objectUtils'); | ||
const { QueryBuilderOperation } = require('./operations/QueryBuilderOperation'); | ||
const { QueryBuilderContextBase } = require('./QueryBuilderContextBase'); | ||
@@ -472,3 +470,6 @@ const { QueryBuilderUserContext } = require('./QueryBuilderUserContext'); | ||
return op => op.name === operationSelector; | ||
} else if (isSubclassOf(operationSelector, QueryBuilderOperation)) { | ||
} else if ( | ||
isFunction(operationSelector) && | ||
operationSelector.isObjectionQueryBuilderOperationClass | ||
) { | ||
return op => op.is(operationSelector); | ||
@@ -475,0 +476,0 @@ } else if (isFunction(operationSelector)) { |
'use strict'; | ||
const { RelationProperty } = require('./RelationProperty'); | ||
const getModel = () => require('../model/Model').Model; | ||
@@ -12,3 +11,2 @@ const { RelationFindOperation } = require('./RelationFindOperation'); | ||
const { ref } = require('../queryBuilder/ReferenceBuilder'); | ||
const { isSubclassOf } = require('../utils/classUtils'); | ||
const { resolveModel } = require('../utils/resolveModel'); | ||
@@ -252,2 +250,10 @@ const { get, isFunction } = require('../utils/objectUtils'); | ||
Object.defineProperties(Relation, { | ||
isObjectionRelationClass: { | ||
enumerable: false, | ||
writable: false, | ||
value: true | ||
} | ||
}); | ||
Object.defineProperties(Relation.prototype, { | ||
@@ -272,3 +278,3 @@ isObjectionRelation: { | ||
function checkOwnerModelClass(ctx) { | ||
if (!isSubclassOf(ctx.ownerModelClass, getModel())) { | ||
if (!isFunction(ctx.ownerModelClass) || !ctx.ownerModelClass.isObjectionModelClass) { | ||
throw ctx.createError(`Relation's owner is not a subclass of Model`); | ||
@@ -309,3 +315,3 @@ } | ||
if (!isSubclassOf(ctx.mapping.relation, Relation)) { | ||
if (!isFunction(ctx.mapping.relation) || !ctx.mapping.relation.isObjectionRelationClass) { | ||
throw ctx.createError('relation is not a subclass of Relation'); | ||
@@ -344,5 +350,3 @@ } | ||
throw ctx.createError( | ||
`join: relation name and join property '${ | ||
ctx.name | ||
}' cannot have the same name. If you cannot change one or the other, you can use $parseDatabaseJson and $formatDatabaseJson methods to convert the column name.` | ||
`join: relation name and join property '${ctx.name}' cannot have the same name. If you cannot change one or the other, you can use $parseDatabaseJson and $formatDatabaseJson methods to convert the column name.` | ||
); | ||
@@ -366,5 +370,3 @@ } | ||
throw ctx.createError( | ||
`join: either \`from\` or \`to\` must point to the owner model table and the other one to the related table. It might be that specified table '${ | ||
err.tableName | ||
}' is not correct` | ||
`join: either \`from\` or \`to\` must point to the owner model table and the other one to the related table. It might be that specified table '${err.tableName}' is not correct` | ||
); | ||
@@ -371,0 +373,0 @@ } else if (err instanceof RelationProperty.InvalidReferenceError) { |
@@ -45,3 +45,3 @@ 'use strict'; | ||
this._propSetters = paths.map(it => createSetter(it.path)); | ||
this._patchers = refs.map(it => createPatcher(it)); | ||
this._patchers = refs.map((it, i) => createPatcher(it, paths[i].path)); | ||
} | ||
@@ -231,5 +231,5 @@ | ||
function createPatcher(ref) { | ||
function createPatcher(ref, path) { | ||
if (ref.isPlainColumnRef) { | ||
return (patch, value) => (patch[ref.column] = value); | ||
return (patch, value) => (patch[path[0]] = value); | ||
} else { | ||
@@ -236,0 +236,0 @@ // Objection `patch`, `update` etc. methods understand field expressions. |
'use strict'; | ||
const Bluebird = require('bluebird'); | ||
const { Model } = require('./model/Model'); | ||
const promiseUtils = require('./utils/promiseUtils'); | ||
const { isSubclassOf } = require('./utils/classUtils'); | ||
const { isFunction } = require('./utils/objectUtils'); | ||
@@ -21,3 +19,3 @@ | ||
if (!isSubclassOf(args[0], Model) && isFunction(args[0].transaction)) { | ||
if (!isModelClass(args[0]) && isFunction(args[0].transaction)) { | ||
let knex = args[0]; | ||
@@ -39,3 +37,3 @@ args = args.slice(1); | ||
for (i = 0; i < modelClasses.length; ++i) { | ||
if (!isSubclassOf(modelClasses[i], Model)) { | ||
if (!isModelClass(modelClasses[i])) { | ||
return Bluebird.reject( | ||
@@ -82,3 +80,3 @@ new Error('objection.transaction: all but the last argument should be Model subclasses') | ||
if (isSubclassOf(modelClassOrKnex, Model)) { | ||
if (isModelClass(modelClassOrKnex)) { | ||
knex = modelClassOrKnex.knex(); | ||
@@ -110,4 +108,8 @@ } | ||
function isModelClass(maybeModel) { | ||
return isFunction(maybeModel) && maybeModel.isObjectionModelClass; | ||
} | ||
module.exports = { | ||
transaction | ||
}; |
@@ -9,5 +9,3 @@ 'use strict'; | ||
throw new Error( | ||
`one of the identifier columns [${ids}] is null or undefined. Have you specified the correct identifier column for the model '${ | ||
modelClass.name | ||
}' using the 'idColumn' property?` | ||
`one of the identifier columns [${ids}] is null or undefined. Have you specified the correct identifier column for the model '${modelClass.name}' using the 'idColumn' property?` | ||
); | ||
@@ -14,0 +12,0 @@ } |
'use strict'; | ||
const { isFunction } = require('./objectUtils'); | ||
function isSubclassOf(Constructor, SuperConstructor) { | ||
if (!isFunction(SuperConstructor)) { | ||
return false; | ||
} | ||
while (isFunction(Constructor)) { | ||
if (Constructor === SuperConstructor) { | ||
return true; | ||
} | ||
Constructor = Object.getPrototypeOf(Constructor); | ||
} | ||
return false; | ||
} | ||
function inherit(Constructor, BaseConstructor) { | ||
@@ -30,4 +12,3 @@ Constructor.prototype = Object.create(BaseConstructor.prototype); | ||
module.exports = { | ||
isSubclassOf, | ||
inherit | ||
}; |
'use strict'; | ||
const path = require('path'); | ||
const { once, isString, isFunction } = require('../utils/objectUtils'); | ||
const { isSubclassOf } = require('../utils/classUtils'); | ||
const { isString, isFunction } = require('../utils/objectUtils'); | ||
const getModel = once(() => require('../model/Model').Model); | ||
class ResolveError extends Error {} | ||
@@ -19,7 +17,7 @@ | ||
} else { | ||
if (isFunction(modelRef) && !isSubclassOf(modelRef, getModel())) { | ||
if (isFunction(modelRef) && !isModelClass(modelRef)) { | ||
modelRef = modelRef(); | ||
} | ||
if (!isSubclassOf(modelRef, getModel())) { | ||
if (!isModelClass(modelRef)) { | ||
throw new ResolveError( | ||
@@ -62,3 +60,2 @@ `is not a subclass of Model or a file path to a module that exports one. You may be dealing with a require loop. See the documentation section about require loops.` | ||
function requireModel(modelPath) { | ||
const Model = getModel(); | ||
/** | ||
@@ -73,5 +70,5 @@ * Wrap path string in template literal to prevent | ||
if (isSubclassOf(mod, Model)) { | ||
if (isModelClass(mod)) { | ||
modelClass = mod; | ||
} else if (isSubclassOf(mod.default, Model)) { | ||
} else if (isModelClass(mod.default)) { | ||
// Babel 6 style of exposing default export. | ||
@@ -83,3 +80,3 @@ modelClass = mod.default; | ||
if (isSubclassOf(exp, Model)) { | ||
if (isModelClass(exp)) { | ||
if (modelClass !== null) { | ||
@@ -96,3 +93,3 @@ throw new ResolveError( | ||
if (!isSubclassOf(modelClass, Model)) { | ||
if (!isModelClass(modelClass)) { | ||
throw new ResolveError(`${modelPath} is an invalid file path to a model class`); | ||
@@ -108,4 +105,8 @@ } | ||
function isModelClass(maybeModel) { | ||
return isFunction(maybeModel) && maybeModel.isObjectionModelClass; | ||
} | ||
module.exports = { | ||
resolveModel | ||
}; |
{ | ||
"name": "@capaj/objection", | ||
"version": "1.6.13", | ||
"version": "1.6.14", | ||
"description": "An SQL-friendly ORM for Node.js", | ||
@@ -73,3 +73,3 @@ "main": "lib/objection.js", | ||
"@babel/polyfill": "^7.4.4", | ||
"@types/node": "^10.14.7", | ||
"@types/node": "^12.7.5", | ||
"babel-eslint": "^10.0.1", | ||
@@ -79,17 +79,17 @@ "chai": "^4.2.0", | ||
"coveralls": "^3.0.4", | ||
"cross-env": "^5.2.0", | ||
"cross-env": "^6.0.0", | ||
"eslint": "^5.16.0", | ||
"eslint-plugin-prettier": "^3.1.0", | ||
"expect.js": "^0.3.1", | ||
"fs-extra": "^7.0.1", | ||
"fs-extra": "^8.1.0", | ||
"glob": "^7.1.3", | ||
"knex": "0.17.0", | ||
"mocha": "^5.2.0", | ||
"knex": "^0.17.0", | ||
"mocha": "^6.2.0", | ||
"mysql": "^2.17.1", | ||
"nyc": "^14.1.1", | ||
"pg": "^7.11.0", | ||
"prettier": "1.17.1", | ||
"prettier": "1.18.2", | ||
"sqlite3": "^4.0.8", | ||
"typescript": "^3.5.1", | ||
"vuepress": "0.14.11" | ||
"typescript": "^3.6.4", | ||
"vuepress": "1.0.4" | ||
}, | ||
@@ -96,0 +96,0 @@ "nyc": { |
@@ -1,2 +0,2 @@ | ||
[![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/github/Vincit/objection.js/badge.svg?branch=master&service=github)](https://coveralls.io/github/Vincit/objection.js?branch=master) [![Join the chat at https://gitter.im/Vincit/objection.js](https://badges.gitter.im/Vincit/objection.js.svg)](https://gitter.im/Vincit/objection.js?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) | ||
[![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/github/Vincit/objection.js/badge.svg?branch=master&u=1)](https://coveralls.io/github/Vincit/objection.js?branch=master) [![Join the chat at https://gitter.im/Vincit/objection.js](https://badges.gitter.im/Vincit/objection.js.svg)](https://gitter.im/Vincit/objection.js?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) | ||
@@ -3,0 +3,0 @@ # [Objection.js](https://vincit.github.io/objection.js) |
@@ -350,3 +350,3 @@ // Type definitions for Objection.js | ||
interface JoinRelation { | ||
<QM extends Model>(relationName: string, opt?: RelationOptions): QueryBuilder<QM, QM[]>; | ||
<QM extends Model>(expr: RelationExpression, opt?: RelationOptions): QueryBuilder<QM, QM[]>; | ||
} | ||
@@ -726,3 +726,3 @@ | ||
type PartialUpdate<QM extends Model> = { | ||
[P in keyof QM]?: QM[P] | Raw | Reference | QueryBuilder<any, any[]> | ||
[P in keyof QM]?: QM[P] | Raw | Reference | QueryBuilder<any, any[]>; | ||
}; | ||
@@ -736,4 +736,3 @@ | ||
findById(id: Id): QueryBuilderYieldingOneOrNone<QM>; | ||
findById(idOrIds: IdOrIds): this; | ||
findById(idOrIds: IdOrIds): QueryBuilderYieldingOneOrNone<QM>; | ||
findByIds(ids: Id[] | Id[][]): this; | ||
@@ -1115,4 +1114,4 @@ /** findOne is shorthand for .where(...whereArgs).first() */ | ||
// Union | ||
union: SetOperations<QM>; | ||
unionAll: SetOperations<QM>; | ||
union: Union<QM>; | ||
unionAll: Union<QM>; | ||
intersect: SetOperations<QM>; | ||
@@ -1317,12 +1316,54 @@ | ||
interface SetOperations<QM extends Model> { | ||
type QBOrCallback<QM extends Model> = | ||
| QueryBuilder<QM, QM[]> | ||
| ((this: QueryBuilder<QM, QM[]>, queryBuilder: QueryBuilder<QM, QM[]>) => void); | ||
interface Union<QM extends Model> extends BaseSetOperations<QM> { | ||
(...args: QBOrCallback<QM>[]): QueryBuilder<QM, QM[]>; | ||
(arg1: QBOrCallback<QM>, wrap?: boolean): QueryBuilder<QM, QM[]>; | ||
(arg1: QBOrCallback<QM>, arg2: QBOrCallback<QM>, wrap?: boolean): QueryBuilder<QM, QM[]>; | ||
( | ||
callback: (this: QueryBuilder<QM, QM[]>, queryBuilder: QueryBuilder<QM, QM[]>) => void, | ||
arg1: QBOrCallback<QM>, | ||
arg2: QBOrCallback<QM>, | ||
arg3: QBOrCallback<QM>, | ||
wrap?: boolean | ||
): QueryBuilder<QM, QM[]>; | ||
( | ||
callbacks: ((this: QueryBuilder<QM, QM[]>, queryBuilder: QueryBuilder<QM, QM[]>) => void)[], | ||
arg1: QBOrCallback<QM>, | ||
arg2: QBOrCallback<QM>, | ||
arg3: QBOrCallback<QM>, | ||
arg4: QBOrCallback<QM>, | ||
wrap?: boolean | ||
): QueryBuilder<QM, QM[]>; | ||
( | ||
arg1: QBOrCallback<QM>, | ||
arg2: QBOrCallback<QM>, | ||
arg3: QBOrCallback<QM>, | ||
arg4: QBOrCallback<QM>, | ||
arg5: QBOrCallback<QM>, | ||
wrap?: boolean | ||
): QueryBuilder<QM, QM[]>; | ||
( | ||
arg1: QBOrCallback<QM>, | ||
arg2: QBOrCallback<QM>, | ||
arg3: QBOrCallback<QM>, | ||
arg4: QBOrCallback<QM>, | ||
arg5: QBOrCallback<QM>, | ||
arg6: QBOrCallback<QM>, | ||
wrap?: boolean | ||
): QueryBuilder<QM, QM[]>; | ||
( | ||
arg1: QBOrCallback<QM>, | ||
arg2: QBOrCallback<QM>, | ||
arg3: QBOrCallback<QM>, | ||
arg4: QBOrCallback<QM>, | ||
arg5: QBOrCallback<QM>, | ||
arg6: QBOrCallback<QM>, | ||
arg7: QBOrCallback<QM>, | ||
wrap?: boolean | ||
): QueryBuilder<QM, QM[]>; | ||
} | ||
interface SetOperations<QM extends Model> extends BaseSetOperations<QM> { | ||
( | ||
...callbacks: ((this: QueryBuilder<QM, QM[]>, queryBuilder: QueryBuilder<QM, QM[]>) => void)[] | ||
@@ -1332,2 +1373,13 @@ ): QueryBuilder<QM, QM[]>; | ||
interface BaseSetOperations<QM extends Model> { | ||
( | ||
callback: (this: QueryBuilder<QM, QM[]>, queryBuilder: QueryBuilder<QM, QM[]>) => void, | ||
wrap?: boolean | ||
): QueryBuilder<QM, QM[]>; | ||
( | ||
callbacks: ((this: QueryBuilder<QM, QM[]>, queryBuilder: QueryBuilder<QM, QM[]>) => void)[], | ||
wrap?: boolean | ||
): QueryBuilder<QM, QM[]>; | ||
} | ||
// commons | ||
@@ -1513,3 +1565,3 @@ | ||
/** | ||
* fallback raw string for custom formats, | ||
* fallback raw string for custom formats, | ||
* or formats that aren't in the standard yet | ||
@@ -1516,0 +1568,0 @@ */ |
No License Found
License(Experimental) License information could not be found
Found 1 instance in 1 package
603714
18755
1