Skip to content

Commit 7a97bfc

Browse files
committed
Support both object and array syntax queries
- Support multiple parameters for querying, in both object and array syntax - Change documentation to ES5 format
1 parent 0e86668 commit 7a97bfc

File tree

2 files changed

+45
-16
lines changed

2 files changed

+45
-16
lines changed

README.md

+19-9
Original file line numberDiff line numberDiff line change
@@ -307,21 +307,31 @@ Additionaly for elasticsearch6 the number of shards, number of replicas, the ref
307307
Use the `firestore` type to support Google's Firestore database, and store your KeyFile in a location accessible by your application.
308308
309309
```javascript
310-
const options = {
311-
repository: {
312-
type: 'firestore',
313-
projectId: 'YOUR_PROJECT_ID',
314-
keyFilename: '/path/to/keyfile.json',
315-
},
316-
};
310+
const options = {
311+
repository: {
312+
type: 'firestore',
313+
projectId: 'YOUR_PROJECT_ID',
314+
keyFilename: '/path/to/keyfile.json'
315+
},
316+
};
317317
```
318318
319319
### Find Queries
320320
321-
Find queries in Firestore are not compatible with MongoDb syntax. The query parameter passed to `find` or `findOne` should be an array with the separate query fields as individual elements, as they will be `apply`'d and passed to the `where` function on the `Query` object.
321+
Simple (equality comparison) find queries are supported by passing javascript objects as the query parameter, or more complex queries can be executed via nested arrays. In the case of multiple key/value pairs or nested arrays, the composite predicates form logical ANDs.
322322
323323
``` javascript
324-
aRepo.find(['aProp', '==', 'aValue'], (err, vm) => {
324+
// Simple Object format
325+
aRepo.find({'aProp': 'aValue', 'secondProp': 'secondValue'}, function (err, vm) {
326+
if (err) {
327+
console.log('Repo find error', err);
328+
return;
329+
}
330+
console.log('Found', vm);
331+
});
332+
333+
// Nested array syntax, allows for more complex predicates
334+
aRepo.find([['aProp', '==', 'aValue'], ['secondProp', '<', 10000]], function (err, vm) {
325335
if (err) {
326336
console.log('Repo find error', err);
327337
return;

lib/databases/firestore.js

+26-7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,24 @@ function implementError (callback) {
2121
throw err;
2222
}
2323

24+
function parseFirestoreQuery(query) {
25+
if (_.isArray(query)) {
26+
return query;
27+
} else if (_.isPlainObject(query)) {
28+
return _.map(query, function(value, key) {
29+
return [key, '==', value];
30+
});
31+
}
32+
throw new Error('Unknown query type');
33+
};
34+
35+
function firestoreQueryParser(collectionRef, queryParams) {
36+
var params = parseFirestoreQuery(queryParams);
37+
return _.reduce(params, function(acc, q) {
38+
return acc.where.apply(acc, q);
39+
}, collectionRef);
40+
}
41+
2442
_.extend(Firestore.prototype, {
2543

2644
connect: function (callback) {
@@ -72,14 +90,14 @@ _.extend(Firestore.prototype, {
7290
},
7391

7492
find: function (queryParams, queryOptions, callback) {
93+
// NOTE: queryOptions is ignored
94+
7595
this.checkConnection();
7696

77-
// NOTE: Query should be an array, e.g., ['foo', '==', 'bar']
78-
// queryOptions is ignored
7997
var self = this;
80-
8198
var collectionRef = this.db.collection(this.collection);
82-
var query = collectionRef.where.apply(collectionRef, queryParams);
99+
100+
var query = firestoreQueryParser(collectionRef, queryParams);
83101
query.get().then(function (querySnapshot) {
84102
var vms = _.map(querySnapshot.docs, function(documentSnapshot) {
85103
var vm = new ViewModel(documentSnapshot.data(), self);
@@ -90,13 +108,14 @@ _.extend(Firestore.prototype, {
90108
});
91109
},
92110

93-
findOne: function (query, queryOptions, callback) {
111+
findOne: function (queryParams, queryOptions, callback) {
112+
// NOTE: queryOptions is ignored
94113
this.checkConnection();
95114

96-
// NOTE: Query should be an array, e.g., ['foo', '==', 'bar']
97115
var self = this;
98116
var collectionRef = this.db.collection(this.collection);
99-
var query = collectionRef.where.apply(collectionRef, query);
117+
118+
var query = firestoreQueryParser(collectionRef, queryParams);
100119
query.limit(1).get().then(function (querySnapshot) {
101120
if (querySnapshot.size == 0) {
102121
callback(null, null);

0 commit comments

Comments
 (0)