Skip to content

Commit 8caf4c8

Browse files
authored
Merge pull request #1108 from strongloop/fix_geo_2.x
Fix the bug when near filter is used
2 parents ff184ae + fde4c0b commit 8caf4c8

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
@@ -1798,174 +1798,174 @@ DataAccessObject.find = function find(query, options, cb) {
17981798
var near = query && geo.nearFilter(query.where);
17991799
var supportsGeo = !!connector.buildNearFilter;
18001800

1801-
if (near) {
1802-
if (supportsGeo) {
1803-
// convert it
1804-
connector.buildNearFilter(query, near);
1805-
} else if (query.where) {
1806-
// do in memory query
1807-
// using all documents
1808-
// TODO [fabien] use default scope here?
1801+
if (query.where && near && !supportsGeo) {
1802+
// do in memory query
1803+
// using all documents
1804+
// TODO [fabien] use default scope here?
18091805

1810-
if (options.notify === false) {
1811-
queryGeo(query);
1812-
} else {
1813-
withNotifyGeo();
1814-
}
1806+
if (options.notify === false) {
1807+
queryGeo(query);
1808+
} else {
1809+
withNotifyGeo();
1810+
}
18151811

1816-
function withNotifyGeo() {
1817-
var context = {
1818-
Model: self,
1819-
query: query,
1820-
hookState: hookState,
1821-
options: options,
1822-
};
1823-
self.notifyObserversOf('access', context, function(err, ctx) {
1824-
if (err) return cb(err);
1825-
queryGeo(ctx.query);
1826-
});
1827-
}
1828-
function queryGeo(query) {
1829-
function geoCallback(err, data) {
1830-
var memory = new Memory();
1831-
var modelName = self.modelName;
1812+
function withNotifyGeo() {
1813+
var context = {
1814+
Model: self,
1815+
query: query,
1816+
hookState: hookState,
1817+
options: options,
1818+
};
1819+
self.notifyObserversOf('access', context, function(err, ctx) {
1820+
if (err) return cb(err);
1821+
queryGeo(ctx.query);
1822+
});
1823+
}
18321824

1833-
if (err) {
1834-
cb(err);
1835-
} else if (Array.isArray(data)) {
1836-
memory.define({
1837-
properties: self.dataSource.definitions[self.modelName].properties,
1838-
settings: self.dataSource.definitions[self.modelName].settings,
1839-
model: self,
1840-
});
1825+
function queryGeo(query) {
1826+
function geoCallback(err, data) {
1827+
var memory = new Memory();
1828+
var modelName = self.modelName;
18411829

1842-
data.forEach(function(obj) {
1843-
memory.create(modelName, obj, options, function() {
1844-
// noop
1845-
});
1846-
});
1830+
if (err) {
1831+
cb(err);
1832+
} else if (Array.isArray(data)) {
1833+
memory.define({
1834+
properties: self.dataSource.definitions[self.modelName].properties,
1835+
settings: self.dataSource.definitions[self.modelName].settings,
1836+
model: self,
1837+
});
18471838

1848-
// FIXME: apply "includes" and other transforms - see allCb below
1849-
memory.all(modelName, query, options, cb);
1850-
} else {
1851-
cb(null, []);
1852-
}
1853-
}
1839+
data.forEach(function(obj) {
1840+
memory.create(modelName, obj, options, function() {
1841+
// noop
1842+
});
1843+
});
18541844

1855-
if (connector.all.length === 4) {
1856-
connector.all(self.modelName, {}, options, geoCallback);
1845+
// FIXME: apply "includes" and other transforms - see allCb below
1846+
memory.all(modelName, query, options, cb);
18571847
} else {
1858-
connector.all(self.modelName, {}, geoCallback);
1848+
cb(null, []);
18591849
}
1860-
// already handled
1861-
return cb.promise;
18621850
}
1863-
}
1864-
}
18651851

1866-
var allCb = function(err, data) {
1867-
if (!err && Array.isArray(data)) {
1868-
async.map(data, function(item, next) {
1869-
var Model = self.lookupModel(item);
1870-
var obj = new Model(item, { fields: query.fields, applySetters: false, persisted: true });
1852+
if (connector.all.length === 4) {
1853+
connector.all(self.modelName, {}, options, geoCallback);
1854+
} else {
1855+
connector.all(self.modelName, {}, geoCallback);
1856+
}
1857+
// already handled
1858+
return cb.promise;
1859+
}
1860+
} else {
1861+
if (near && supportsGeo) {
1862+
connector.buildNearFilter(query, near);
1863+
}
18711864

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

1892-
// Promote the included model as a direct property
1893-
var included = obj.__cachedRelations[relationName];
1894-
if (Array.isArray(included)) {
1895-
included = new List(included, null, obj);
1896-
}
1897-
if (included) obj.__data[relationName] = included;
1898-
});
1899-
delete obj.__data.__cachedRelations;
1891+
// Promote the included model as a direct property
1892+
var included = obj.__cachedRelations[relationName];
1893+
if (Array.isArray(included)) {
1894+
included = new List(included, null, obj);
1895+
}
1896+
if (included) obj.__data[relationName] = included;
1897+
});
1898+
delete obj.__data.__cachedRelations;
1899+
}
19001900
}
1901-
}
1902-
if (obj !== undefined) {
1903-
if (options.notify === false) {
1904-
next(null, obj);
1905-
} else {
1906-
context = {
1907-
Model: Model,
1908-
instance: obj,
1909-
isNewInstance: false,
1910-
hookState: hookState,
1911-
options: options,
1912-
};
1901+
if (obj !== undefined) {
1902+
if (options.notify === false) {
1903+
next(null, obj);
1904+
} else {
1905+
context = {
1906+
Model: Model,
1907+
instance: obj,
1908+
isNewInstance: false,
1909+
hookState: hookState,
1910+
options: options,
1911+
};
19131912

1914-
Model.notifyObserversOf('loaded', context, function(err) {
1915-
if (err) return next(err);
1913+
Model.notifyObserversOf('loaded', context, function(err) {
1914+
if (err) return next(err);
19161915

1917-
next(null, obj);
1918-
});
1916+
next(null, obj);
1917+
});
1918+
}
1919+
} else {
1920+
next();
19191921
}
1920-
} else {
1921-
next();
1922-
}
1923-
},
1924-
function(err, results) {
1925-
if (err) return cb(err);
1922+
},
1923+
function(err, results) {
1924+
if (err) return cb(err);
19261925

1927-
// When applying query.collect, some root items may not have
1928-
// any related/linked item. We store `undefined` in the results
1929-
// array in such case, which is not desirable from API consumer's
1930-
// point of view.
1931-
results = results.filter(isDefined);
1926+
// When applying query.collect, some root items may not have
1927+
// any related/linked item. We store `undefined` in the results
1928+
// array in such case, which is not desirable from API consumer's
1929+
// point of view.
1930+
results = results.filter(isDefined);
19321931

1933-
if (data && data.countBeforeLimit) {
1934-
results.countBeforeLimit = data.countBeforeLimit;
1935-
}
1936-
if (!supportsGeo && near) {
1937-
results = geo.filter(results, near);
1938-
}
1932+
if (data && data.countBeforeLimit) {
1933+
results.countBeforeLimit = data.countBeforeLimit;
1934+
}
1935+
if (!supportsGeo && near) {
1936+
results = geo.filter(results, near);
1937+
}
19391938

1940-
cb(err, results);
1941-
});
1942-
} else {
1943-
cb(err, data || []);
1944-
}
1945-
};
1939+
cb(err, results);
1940+
});
1941+
} else {
1942+
cb(err, data || []);
1943+
}
1944+
};
19461945

1947-
if (options.notify === false) {
1948-
if (connector.all.length === 4) {
1949-
connector.all(self.modelName, query, options, allCb);
1946+
if (options.notify === false) {
1947+
if (connector.all.length === 4) {
1948+
connector.all(self.modelName, query, options, allCb);
1949+
} else {
1950+
connector.all(self.modelName, query, allCb);
1951+
}
19501952
} else {
1951-
connector.all(self.modelName, query, allCb);
1952-
}
1953-
} else {
1954-
var context = {
1955-
Model: this,
1956-
query: query,
1957-
hookState: hookState,
1958-
options: options,
1959-
};
1960-
this.notifyObserversOf('access', context, function(err, ctx) {
1961-
if (err) return cb(err);
1953+
var context = {
1954+
Model: this,
1955+
query: query,
1956+
hookState: hookState,
1957+
options: options,
1958+
};
1959+
this.notifyObserversOf('access', context, function(err, ctx) {
1960+
if (err) return cb(err);
19621961

1963-
connector.all.length === 4 ?
1964-
connector.all(self.modelName, ctx.query, options, allCb) :
1965-
connector.all(self.modelName, ctx.query, allCb);
1966-
});
1962+
connector.all.length === 4 ?
1963+
connector.all(self.modelName, ctx.query, options, allCb) :
1964+
connector.all(self.modelName, ctx.query, allCb);
1965+
});
1966+
}
1967+
return cb.promise;
19671968
}
1968-
return cb.promise;
19691969
};
19701970

19711971
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)