Skip to content

Commit

Permalink
✨ Add Model#findOne(conditions, options) and `Model#findAll(conditi…
Browse files Browse the repository at this point in the history
…ons, options)`
  • Loading branch information
skerit committed Feb 13, 2024
1 parent 799bb9f commit 4a620e6
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* Remove the deprecated `DbQuery` class
* Let field/association constraints be a `Criteria` instance
* Add "AQL" (Alchemy Query Language) implementation
* Add `Model#findOne(conditions, options)` and `Model#findAll(conditions, options)`

## 1.3.22 (2023-12-21)

Expand Down
4 changes: 2 additions & 2 deletions lib/app/helper_model/00-base_criteria.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ Criteria.setStatic(function isCriteria(instance) {
* @since 1.4.0
* @version 1.4.0
*
* @param {Object} conditions The conditions
* @param {Object} options The thing that should be a criteria
* @param {Object|string|Criteria} conditions
* @param {Object} options
*
* @return {Criteria}
*/
Expand Down
27 changes: 19 additions & 8 deletions lib/app/helper_model/10-model_criteria.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
34 changes: 34 additions & 0 deletions lib/app/helper_model/model.js
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');
Expand Down Expand Up @@ -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
*
Expand Down
33 changes: 33 additions & 0 deletions test/03-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,39 @@ describe('Model', function() {
});
});

describe('#findOne(string)', () => {
it('should find a single document by an AQL query', async () => {

const Person = Model.get('Person');

let record = await Person.findOne('_id = "' + _id + '"');
assert.strictEqual(String(_id), String(record._id));

record = await Person.findOne('birthdate < "1980-01-01"');
assert.strictEqual(record.firstname, 'Griet');

record = await Person.findOne('birthdate > "1980-01-01"');
assert.strictEqual(record.firstname, 'Jelle');
});
});

describe('#findAll(string)', () => {
it('should find all documents matching an AQL query', async () => {

const Person = Model.get('Person');

let records = await Person.findAll('_id = "' + _id + '"');
assert.strictEqual(records.length, 1);
assert.strictEqual(String(_id), String(records[0]._id));

records = await Person.findAll('birthdate < "2000-01-01"');
assert.strictEqual(records.length, 2);

records = await Person.findAll('birthdate > "1980-01-01"');
assert.strictEqual(records.length, 1);
});
});

describe('#findById(object_id, callback)', function() {
it('should find a single document by ObjectId instance', function(done) {
Model.get('Person').findById(_id, function gotPerson(err, person) {
Expand Down

0 comments on commit 4a620e6

Please sign in to comment.