feat(database): add support for ServerValue.increment (#3561)

Co-authored-by: Mike Diarmid <mike.diarmid@gmail.com

[publish]
This commit is contained in:
Elliot Hesp
2020-06-01 18:41:04 +01:00
committed by GitHub
parent 6732ceb5f3
commit 759d67e6fb
3 changed files with 60 additions and 0 deletions

View File

@@ -15,7 +15,13 @@
*
*/
const { PATH, wipe } = require('./helpers');
const TEST_PATH = `${PATH}/statics`;
describe('database.X', () => {
after(() => wipe(TEST_PATH));
describe('ServerValue.TIMESTAMP', () => {
it('returns a valid object', () => {
const { TIMESTAMP } = firebase.database.ServerValue;
@@ -24,4 +30,36 @@ describe('database.X', () => {
TIMESTAMP['.sv'].should.eql('timestamp');
});
});
describe('ServerValue.increment', () => {
it('returns a valid object', () => {
const incrementObject = firebase.database.ServerValue.increment(1);
should.equal(Object.keys(incrementObject).length, 1);
incrementObject.should.have.property('.sv');
incrementObject['.sv'].should.have.property('increment');
});
it('increments on the server', async () => {
const ref = firebase.database().ref(`${TEST_PATH}/increment`);
await ref.set({ increment: 0 });
const res1 = await ref.once('value');
res1.val().increment.should.equal(0);
await ref.set({ increment: firebase.database.ServerValue.increment(1) });
const res2 = await ref.once('value');
res2.val().increment.should.equal(1);
});
it('increments on the server when no value is present', async () => {
const ref = firebase.database().ref(`${TEST_PATH}/increment-empty`);
await ref.set({ increment: firebase.database.ServerValue.increment(2) });
const res = await ref.once('value');
res.val().increment.should.equal(2);
});
});
});

View File

@@ -20,5 +20,12 @@ export default {
TIMESTAMP: {
'.sv': 'timestamp',
},
increment(delta: number) {
return {
'.sv': {
increment: delta,
},
};
},
},
};

View File

@@ -73,6 +73,21 @@ export namespace FirebaseDatabaseTypes {
* ```
*/
TIMESTAMP: object;
/**
* Returns a placeholder value that can be used to atomically increment the current database value by the provided delta.
*
* #### Example
*
* ```js
* firebase.database().ref('posts/123').update({
* likes: firebase.database.ServerValue.increment(1),
* });
* ```
*
* @param delta The amount to modify the current value atomically.
*/
increment(delta: number): object;
}
/**