Skip to content

Commit 532af4c

Browse files
committed
Fix orderBy for fields containing Date
Internally date is now stored as Date until returned, where it is converted to Timestamp. This fixes orderBy.
1 parent 2ff0039 commit 532af4c

10 files changed

+60
-14
lines changed

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"lodash.clone": "^4.5.0",
3636
"lodash.clonedeep": "^4.5.0",
3737
"lodash.clonedeepwith": "^4.5.0",
38+
"lodash.clonewith": "^4.5.0",
3839
"lodash.compact": "^3.0.1",
3940
"lodash.difference": "^4.5.0",
4041
"lodash.every": "^4.6.0",

src/firebase.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ function extractName(path) {
695695
}
696696

697697
function render(datum) {
698-
if (datum && _.isPlainObject(datum)) {
698+
if (datum && _.isObject(datum)) {
699699
var keys = _.keys(datum);
700700

701701
if (_.every(keys, RegExp.prototype.test.bind(/^\d+$/))) {
@@ -714,10 +714,10 @@ function render(datum) {
714714
return array;
715715
}
716716
} else {
717-
return _.cloneDeep(datum);
717+
return _.cloneDeepWith(datum, utils.cloneCustomizer);
718718
}
719719
} else {
720-
return _.clone(datum);
720+
return _.cloneWith(datum, utils.cloneCustomizer);
721721
}
722722
}
723723

src/firestore-document-snapshot.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
'use strict';
22

33
var _ = require('./lodash');
4+
var utils = require('./utils');
45

56
function MockFirestoreDocumentSnapshot (id, ref, data) {
67
this.id = id;
78
this.ref = ref;
89
this._snapshotdata = _.cloneDeep(data) || null;
910
this.data = function() {
10-
return _.cloneDeep(this._snapshotdata);
11+
return _.cloneDeepWith(this._snapshotdata, utils.cloneCustomizer);
1112
};
1213
this.exists = this._snapshotdata !== null;
1314
}

src/firestore-document.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ MockFirestoreDocument.prototype.create = function (data, callback) {
111111
var base = self._getData();
112112
err = err || self._validateDoesNotExist(base);
113113
if (err === null) {
114-
var time = Timestamp.fromMillis(utils.getServerTime());
115-
var result = new WriteResult(time);
114+
var timestamp = Timestamp.fromMillis(utils.getServerTime());
115+
var result = new WriteResult(timestamp);
116116
data = utils.removeEmptyFirestoreProperties(data);
117117
self._dataChanged(data);
118118
resolve(result);

src/firestore-query.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ MockFirestoreQuery.prototype.get = function () {
7575
if (self.orderedProperties.length === 0) {
7676
_.forEach(self.data, function(data, key) {
7777
if (self.limited <= 0 || limit < self.limited) {
78-
results[key] = _.cloneDeep(data);
78+
results[key] = _.cloneDeepWith(data, utils.cloneCustomizer);
7979
limit++;
8080
}
8181
});
@@ -92,7 +92,7 @@ MockFirestoreQuery.prototype.get = function () {
9292

9393
queryable.forEach(function(q) {
9494
if (self.limited <= 0 || limit < self.limited) {
95-
results[q.key] = _.cloneDeep(q.data);
95+
results[q.key] = _.cloneDeepWith(q.data, utils.cloneCustomizer);
9696
limit++;
9797
}
9898
});

src/lodash.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module.exports = {
22
assign: require('lodash.assign'),
33
bind: require('lodash.bind'),
44
clone: require('lodash.clone'),
5+
cloneWith: require('lodash.clonewith'),
56
cloneDeep: require('lodash.clonedeep'),
67
cloneDeepWith: require('lodash.clonedeepwith'),
78
compact: require('lodash.compact'),

src/utils.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ exports.restoreServerClock = function restoreServerTime() {
9696

9797
exports.isServerTimestamp = function isServerTimestamp(data) {
9898
return _.isObject(data) && data['.sv'] === 'timestamp';
99-
}
99+
};
100100

101101
exports.removeEmptyRtdbProperties = function removeEmptyRtdbProperties(obj) {
102102
var t = typeof obj;
@@ -138,9 +138,10 @@ exports.removeEmptyFirestoreProperties = function removeEmptyFirestoreProperties
138138
var value = removeEmptyFirestoreProperties(obj[s]);
139139
if (FieldValue.delete().isEqual(value)) {
140140
delete obj[s];
141-
}
142-
if (FieldValue.serverTimestamp().isEqual(value)) {
143-
obj[s] = Timestamp.fromMillis(exports.getServerTime());
141+
} else if (FieldValue.serverTimestamp().isEqual(value)) {
142+
obj[s] = new Date(exports.getServerTime());
143+
} else if (value instanceof Timestamp) {
144+
obj[s] = value.toDate();
144145
}
145146
}
146147
}
@@ -223,3 +224,9 @@ exports.createThenableReference = function(reference, promise) {
223224
};
224225
return reference;
225226
};
227+
228+
exports.cloneCustomizer = function(value) {
229+
if (value instanceof Date) {
230+
return Timestamp.fromMillis(value.getTime());
231+
}
232+
};

test/unit/firestore-collection.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ chai.use(require('sinon-chai'));
1010
var expect = chai.expect;
1111
var _ = require('../../src/lodash');
1212
var Firestore = require('../../').MockFirestore;
13+
var Timestamp = require('../../src/timestamp');
1314

1415
describe('MockFirestoreCollection', function () {
1516

@@ -332,6 +333,36 @@ describe('MockFirestoreCollection', function () {
332333
done();
333334
}).catch(done);
334335
});
336+
337+
it('returns documents ordered by date', function(done) {
338+
db.collection('group').doc().create({
339+
name: 'a',
340+
date: Timestamp.fromMillis(1000)
341+
}).catch(done);
342+
db.flush();
343+
db.collection('group').add({
344+
name: 'b',
345+
date: Timestamp.fromMillis(2000)
346+
}).catch(done);
347+
db.flush();
348+
349+
db.collection('group').orderBy('date', 'asc').get().then(function (snap) {
350+
expect(snap.size).to.equal(2);
351+
expect(snap.docs[0].data().name).to.equal('a');
352+
expect(snap.docs[0].data().date).to.have.property('seconds');
353+
expect(snap.docs[1].data().name).to.equal('b');
354+
expect(snap.docs[1].data().date).to.have.property('seconds');
355+
356+
db.collection('group').orderBy('date', 'desc').get().then(function (snap) {
357+
expect(snap.size).to.equal(2);
358+
expect(snap.docs[0].data().name).to.equal('b');
359+
expect(snap.docs[1].data().name).to.equal('a');
360+
done();
361+
}).catch(done);
362+
db.flush();
363+
}).catch(done);
364+
db.flush();
365+
});
335366
});
336367

337368
describe('#limit', function () {

test/unit/firestore-document.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ describe('MockFirestoreDocument', function () {
303303

304304
doc.get().then(function (snap) {
305305
expect(snap.exists).to.equal(true);
306-
expect(snap.get('date').getTime()).to.equal(nextDate.getTime());
306+
expect(snap.get('date').toDate().getTime()).to.equal(nextDate.getTime());
307307
done();
308308
}).catch(done);
309309

@@ -432,7 +432,7 @@ describe('MockFirestoreDocument', function () {
432432

433433
doc.get()
434434
.then(function (snap) {
435-
expect(snap.get('date').getTime()).to.equal(nextDate.getTime());
435+
expect(snap.get('date').toDate().getTime()).to.equal(nextDate.getTime());
436436
done();
437437
})
438438
.catch(done);

0 commit comments

Comments
 (0)