-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add
Model#findOne(conditions, options)
and `Model#findAll(conditi…
…ons, options)`
- Loading branch information
Showing
5 changed files
with
89 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,23 +27,34 @@ const Criteria = Function.inherits('Alchemy.Criteria.Criteria', function Model(m | |
* | ||
* @author Jelle De Loecker <[email protected]> | ||
* @since 1.2.5 | ||
* @version 1.3.4 | ||
* @version 1.4.0 | ||
* | ||
* @param {Object} obj The thing that should be a criteria | ||
* @param {Model} model The model that it probably belongs to | ||
* @param {Object} conditions The thing that should be a criteria | ||
* @param {Object} options The options to apply | ||
* @param {Model} model The model that it probably belongs to | ||
* | ||
* @return {Criteria} | ||
*/ | ||
Criteria.setStatic(function cast(obj, model) { | ||
Criteria.setStatic(function cast(conditions, options, model) { | ||
|
||
if (Criteria.isCriteria(conditions)) { | ||
return conditions; | ||
} | ||
|
||
if (Criteria.isCriteria(obj)) { | ||
return obj; | ||
if (arguments.length == 2) { | ||
model = options; | ||
options = conditions; | ||
conditions = null; | ||
} | ||
|
||
let instance = new Criteria(model); | ||
|
||
if (obj) { | ||
instance.applyOldOptions(obj); | ||
if (options) { | ||
instance.applyOldOptions(options); | ||
} | ||
|
||
if (conditions) { | ||
instance.applyConditions(conditions); | ||
} | ||
|
||
return instance; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
const CriteriaNS = Function.getNamespace('Alchemy.Criteria'); | ||
|
||
let class_cache = new Map(), | ||
fallback_datasource, | ||
TABLE = Symbol('table'); | ||
|
@@ -668,6 +670,38 @@ Model.setMethod(function getAliasModel(alias) { | |
return result; | ||
}); | ||
|
||
/** | ||
* Find one record | ||
* | ||
* @author Jelle De Loecker <[email protected]> | ||
* @since 1.4.0 | ||
* @version 1.4.0 | ||
* | ||
* @param {string|Object|Criteria} conditions | ||
* | ||
* @return {Pledge|Criteria} | ||
*/ | ||
Model.setMethod(function findOne(conditions, options) { | ||
conditions = CriteriaNS.Model.cast(conditions, options, this); | ||
return this.find('first', conditions); | ||
}); | ||
|
||
/** | ||
* Find all records | ||
* | ||
* @author Jelle De Loecker <[email protected]> | ||
* @since 1.4.0 | ||
* @version 1.4.0 | ||
* | ||
* @param {string|Object|Criteria} conditions | ||
* | ||
* @return {Pledge|Criteria} | ||
*/ | ||
Model.setMethod(function findAll(conditions, options) { | ||
conditions = CriteriaNS.Model.cast(conditions, options, this); | ||
return this.find('all', conditions); | ||
}); | ||
|
||
/** | ||
* Query the database | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters