Skip to content

Commit fde4c0b

Browse files
committed
Fix bug when near filter is used
1 parent da57136 commit fde4c0b

File tree

2 files changed

+157
-144
lines changed

2 files changed

+157
-144
lines changed

lib/dao.js

Lines changed: 144 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,174 +1791,174 @@ DataAccessObject.find = function find(query, options, cb) {
17911791
var near = query && geo.nearFilter(query.where);
17921792
var supportsGeo = !!connector.buildNearFilter;
17931793

1794-
if (near) {
1795-
if (supportsGeo) {
1796-
// convert it
1797-
connector.buildNearFilter(query, near);
1798-
} else if (query.where) {
1799-
// do in memory query
1800-
// using all documents
1801-
// TODO [fabien] use default scope here?
1794+
if (query.where && near && !supportsGeo) {
1795+
// do in memory query
1796+
// using all documents
1797+
// TODO [fabien] use default scope here?
18021798

1803-
if (options.notify === false) {
1804-
queryGeo(query);
1805-
} else {
1806-
withNotifyGeo();
1807-
}
1799+
if (options.notify === false) {
1800+
queryGeo(query);
1801+
} else {
1802+
withNotifyGeo();
1803+
}
18081804

1809-
function withNotifyGeo() {
1810-
var context = {
1811-
Model: self,
1812-
query: query,
1813-
hookState: hookState,
1814-
options: options,
1815-
};
1816-
self.notifyObserversOf('access', context, function(err, ctx) {
1817-
if (err) return cb(err);
1818-
queryGeo(ctx.query);
1819-
});
1820-
}
1821-
function queryGeo(query) {
1822-
function geoCallback(err, data) {
1823-
var memory = new Memory();
1824-
var modelName = self.modelName;
1805+
function withNotifyGeo() {
1806+
var context = {
1807+
Model: self,
1808+
query: query,
1809+
hookState: hookState,
1810+
options: options,
1811+
};
1812+
self.notifyObserversOf('access', context, function(err, ctx) {
1813+
if (err) return cb(err);
1814+
queryGeo(ctx.query);
1815+
});
1816+
}
18251817

1826-
if (err) {
1827-
cb(err);
1828-
} else if (Array.isArray(data)) {
1829-
memory.define({
1830-
properties: self.dataSource.definitions[self.modelName].properties,
1831-
settings: self.dataSource.definitions[self.modelName].settings,
1832-
model: self,
1833-
});
1818+
function queryGeo(query) {
1819+
function geoCallback(err, data) {
1820+
var memory = new Memory();
1821+
var modelName = self.modelName;
18341822

1835-
data.forEach(function(obj) {
1836-
memory.create(modelName, obj, options, function() {
1837-
// noop
1838-
});
1839-
});
1823+
if (err) {
1824+
cb(err);
1825+
} else if (Array.isArray(data)) {
1826+
memory.define({
1827+
properties: self.dataSource.definitions[self.modelName].properties,
1828+
settings: self.dataSource.definitions[self.modelName].settings,
1829+
model: self,
1830+
});
18401831

1841-
// FIXME: apply "includes" and other transforms - see allCb below
1842-
memory.all(modelName, query, options, cb);
1843-
} else {
1844-
cb(null, []);
1845-
}
1846-
}
1832+
data.forEach(function(obj) {
1833+
memory.create(modelName, obj, options, function() {
1834+
// noop
1835+
});
1836+
});
18471837

1848-
if (connector.all.length === 4) {
1849-
connector.all(self.modelName, {}, options, geoCallback);
1838+
// FIXME: apply "includes" and other transforms - see allCb below
1839+
memory.all(modelName, query, options, cb);
18501840
} else {
1851-
connector.all(self.modelName, {}, geoCallback);
1841+
cb(null, []);
18521842
}
1853-
// already handled
1854-
return cb.promise;
18551843
}
1856-
}
1857-
}
18581844

1859-
var allCb = function(err, data) {
1860-
if (!err && Array.isArray(data)) {
1861-
async.map(data, function(item, next) {
1862-
var Model = self.lookupModel(item);
1863-
var obj = new Model(item, { fields: query.fields, applySetters: false, persisted: true });
1845+
if (connector.all.length === 4) {
1846+
connector.all(self.modelName, {}, options, geoCallback);
1847+
} else {
1848+
connector.all(self.modelName, {}, geoCallback);
1849+
}
1850+
// already handled
1851+
return cb.promise;
1852+
}
1853+
} else {
1854+
if (near && supportsGeo) {
1855+
connector.buildNearFilter(query, near);
1856+
}
18641857

1865-
if (query && query.include) {
1866-
if (query.collect) {
1867-
// The collect property indicates that the query is to return the
1868-
// standalone items for a related model, not as child of the parent object
1869-
// For example, article.tags
1870-
obj = obj.__cachedRelations[query.collect];
1871-
if (obj === null) {
1872-
obj = undefined;
1873-
}
1874-
} else {
1875-
// This handles the case to return parent items including the related
1876-
// models. For example, Article.find({include: 'tags'}, ...);
1877-
// Try to normalize the include
1878-
var includes = Inclusion.normalizeInclude(query.include || []);
1879-
includes.forEach(function(inc) {
1880-
var relationName = inc;
1881-
if (utils.isPlainObject(inc)) {
1882-
relationName = Object.keys(inc)[0];
1858+
var allCb = function(err, data) {
1859+
if (!err && Array.isArray(data)) {
1860+
async.map(data, function(item, next) {
1861+
var Model = self.lookupModel(item);
1862+
var obj = new Model(item, { fields: query.fields, applySetters: false, persisted: true });
1863+
1864+
if (query && query.include) {
1865+
if (query.collect) {
1866+
// The collect property indicates that the query is to return the
1867+
// standalone items for a related model, not as child of the parent object
1868+
// For example, article.tags
1869+
obj = obj.__cachedRelations[query.collect];
1870+
if (obj === null) {
1871+
obj = undefined;
18831872
}
1873+
} else {
1874+
// This handles the case to return parent items including the related
1875+
// models. For example, Article.find({include: 'tags'}, ...);
1876+
// Try to normalize the include
1877+
var includes = Inclusion.normalizeInclude(query.include || []);
1878+
includes.forEach(function(inc) {
1879+
var relationName = inc;
1880+
if (utils.isPlainObject(inc)) {
1881+
relationName = Object.keys(inc)[0];
1882+
}
18841883

1885-
// Promote the included model as a direct property
1886-
var included = obj.__cachedRelations[relationName];
1887-
if (Array.isArray(included)) {
1888-
included = new List(included, null, obj);
1889-
}
1890-
if (included) obj.__data[relationName] = included;
1891-
});
1892-
delete obj.__data.__cachedRelations;
1884+
// Promote the included model as a direct property
1885+
var included = obj.__cachedRelations[relationName];
1886+
if (Array.isArray(included)) {
1887+
included = new List(included, null, obj);
1888+
}
1889+
if (included) obj.__data[relationName] = included;
1890+
});
1891+
delete obj.__data.__cachedRelations;
1892+
}
18931893
}
1894-
}
1895-
if (obj !== undefined) {
1896-
if (options.notify === false) {
1897-
next(null, obj);
1898-
} else {
1899-
context = {
1900-
Model: Model,
1901-
instance: obj,
1902-
isNewInstance: false,
1903-
hookState: hookState,
1904-
options: options,
1905-
};
1894+
if (obj !== undefined) {
1895+
if (options.notify === false) {
1896+
next(null, obj);
1897+
} else {
1898+
context = {
1899+
Model: Model,
1900+
instance: obj,
1901+
isNewInstance: false,
1902+
hookState: hookState,
1903+
options: options,
1904+
};
19061905

1907-
Model.notifyObserversOf('loaded', context, function(err) {
1908-
if (err) return next(err);
1906+
Model.notifyObserversOf('loaded', context, function(err) {
1907+
if (err) return next(err);
19091908

1910-
next(null, obj);
1911-
});
1909+
next(null, obj);
1910+
});
1911+
}
1912+
} else {
1913+
next();
19121914
}
1913-
} else {
1914-
next();
1915-
}
1916-
},
1917-
function(err, results) {
1918-
if (err) return cb(err);
1915+
},
1916+
function(err, results) {
1917+
if (err) return cb(err);
19191918

1920-
// When applying query.collect, some root items may not have
1921-
// any related/linked item. We store `undefined` in the results
1922-
// array in such case, which is not desirable from API consumer's
1923-
// point of view.
1924-
results = results.filter(isDefined);
1919+
// When applying query.collect, some root items may not have
1920+
// any related/linked item. We store `undefined` in the results
1921+
// array in such case, which is not desirable from API consumer's
1922+
// point of view.
1923+
results = results.filter(isDefined);
19251924

1926-
if (data && data.countBeforeLimit) {
1927-
results.countBeforeLimit = data.countBeforeLimit;
1928-
}
1929-
if (!supportsGeo && near) {
1930-
results = geo.filter(results, near);
1931-
}
1925+
if (data && data.countBeforeLimit) {
1926+
results.countBeforeLimit = data.countBeforeLimit;
1927+
}
1928+
if (!supportsGeo && near) {
1929+
results = geo.filter(results, near);
1930+
}
19321931

1933-
cb(err, results);
1934-
});
1935-
} else {
1936-
cb(err, data || []);
1937-
}
1938-
};
1932+
cb(err, results);
1933+
});
1934+
} else {
1935+
cb(err, data || []);
1936+
}
1937+
};
19391938

1940-
if (options.notify === false) {
1941-
if (connector.all.length === 4) {
1942-
connector.all(self.modelName, query, options, allCb);
1939+
if (options.notify === false) {
1940+
if (connector.all.length === 4) {
1941+
connector.all(self.modelName, query, options, allCb);
1942+
} else {
1943+
connector.all(self.modelName, query, allCb);
1944+
}
19431945
} else {
1944-
connector.all(self.modelName, query, allCb);
1945-
}
1946-
} else {
1947-
var context = {
1948-
Model: this,
1949-
query: query,
1950-
hookState: hookState,
1951-
options: options,
1952-
};
1953-
this.notifyObserversOf('access', context, function(err, ctx) {
1954-
if (err) return cb(err);
1946+
var context = {
1947+
Model: this,
1948+
query: query,
1949+
hookState: hookState,
1950+
options: options,
1951+
};
1952+
this.notifyObserversOf('access', context, function(err, ctx) {
1953+
if (err) return cb(err);
19551954

1956-
connector.all.length === 4 ?
1957-
connector.all(self.modelName, ctx.query, options, allCb) :
1958-
connector.all(self.modelName, ctx.query, allCb);
1959-
});
1955+
connector.all.length === 4 ?
1956+
connector.all(self.modelName, ctx.query, options, allCb) :
1957+
connector.all(self.modelName, ctx.query, allCb);
1958+
});
1959+
}
1960+
return cb.promise;
19601961
}
1961-
return cb.promise;
19621962
};
19631963

19641964
function isDefined(value) {

test/persistence-hooks.suite.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@ module.exports = function(dataSource, should, connectorCapabilities) {
8686
});
8787
});
8888

89+
it('triggers correct hooks when near filter is used', function(done) {
90+
monitorHookExecution();
91+
var query = { where:
92+
{ location: { near: '10,20', maxDistance: '10', unit: 'meters' }},
93+
};
94+
95+
TestModel.find(query, function(err, list) {
96+
if (err) return done(err);
97+
hookMonitor.names.should.eql(['access']);
98+
done();
99+
});
100+
});
101+
89102
it('should not trigger hooks, if notify is false', function(done) {
90103
monitorHookExecution();
91104
TestModel.find(

0 commit comments

Comments
 (0)