Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 71439ec

Browse files
Increment Feature in Firestore. #1225
1 parent 67a8f98 commit 71439ec

File tree

9 files changed

+7821
-3117
lines changed

9 files changed

+7821
-3117
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
- [Firebase iOS SDK Changelog](https://firebase.google.com/support/release-notes/ios)
44
- [Firebase Android SDK Changelog](https://firebase.google.com/support/release-notes/android)
55

6+
## 8.3.0 (2019, April ?)
7+
[Fixes & Enhancements](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/milestone/99?closed=1)
8+
9+
610
## 8.2.0 (2019, April 10)
711
[Fixes & Enhancements](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/milestone/98?closed=1)
812

demo-ng/app/tabs/firestore/firestore.component.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ export class FirestoreComponent {
246246
updateTs: firebase.firestore().FieldValue().serverTimestamp(),
247247
// just fyi - both of these work:
248248
colors: firestore.FieldValue.arrayUnion("red", "blue"),
249+
ageInt: firebase.firestore().FieldValue().increment(2),
249250
messages: firebase.firestore().FieldValue().arrayUnion({
250251
message: "Test 1",
251252
source: "central",
@@ -262,6 +263,7 @@ export class FirestoreComponent {
262263
last: "Updated From 'arrayRemove'",
263264
updateTs: firebase.firestore().FieldValue().serverTimestamp(),
264265
colors: firestore.FieldValue.arrayUnion("red"),
266+
ageDouble: firebase.firestore().FieldValue().increment(1.1),
265267
messages: firebase.firestore().FieldValue().arrayRemove({
266268
message: "Test 1",
267269
source: "central",

docs/FIRESTORE.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ During plugin installation you'll be asked whether or not you use Firestore.
66
In case you're upgrading and you have the `firebase.nativescript.json` file in your project root, edit it and add: `"firestore": true`.
77
Then run `rm -rf platforms && rm -rf node_modules && npm i`.
88

9-
## Functions
9+
## API
1010
All of these are 100% compatible with the Firestore Web API to make it easy to share code between web and native, and you can
1111
refer to the [Firestore web api docs](https://firebase.google.com/docs/firestore/data-model) (make sure to look at the 'WEB' tab of those code samples).
1212

@@ -268,7 +268,7 @@ sanFranciscoDocument.delete().then(() => {
268268
To delete one or more fields in a document, do this (showing two flavors, use whatever you fancy):
269269

270270
```typescript
271-
import { firestore } from "nativescript-plugin-firebase");
271+
import { firestore } from "nativescript-plugin-firebase";
272272
const firebase = require("nativescript-plugin-firebase/app");
273273

274274
firebase.firestore().collection("dogs").doc("fave")
@@ -278,6 +278,20 @@ firebase.firestore().collection("dogs").doc("fave")
278278
});
279279
```
280280

281+
### Increment numbers
282+
To easily increment numbers (`int` or `float`), you can use the `increment` feature since plugin version 8.3.0.
283+
284+
```typescript
285+
import { firestore } from "nativescript-plugin-firebase";
286+
const firebase = require("nativescript-plugin-firebase/app");
287+
288+
firebase.firestore().collection("dogs").doc("fave")
289+
.update({
290+
age: firestore.FieldValue.increment(1),
291+
lifePercentage: firebase.firestore().FieldValue().increment(0.0027),
292+
});
293+
```
294+
281295
### Adding to (`arrayUnion`) and removing from (`arrayRemove`) Arrays
282296
If your document contains an array field, you can use `arrayUnion()` and `arrayRemove()` to add and remove elements.
283297

src/app/firestore/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ export namespace firestore {
1818
serverTimestamp: () => "SERVER_TIMESTAMP",
1919
delete: () => "DELETE_FIELD",
2020
arrayUnion: (...elements: any[]) => new firebase.firestore.FieldValue("ARRAY_UNION", elements),
21-
arrayRemove: (...elements: any[]) => new firebase.firestore.FieldValue("ARRAY_REMOVE", elements)
21+
arrayRemove: (...elements: any[]) => new firebase.firestore.FieldValue("ARRAY_REMOVE", elements),
22+
increment: (n: number) => new firebase.firestore.FieldValue("INCREMENT", n)
2223
};
2324
}
2425

src/firebase-common.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export class FieldValue {
1818
static delete = () => "DELETE_FIELD";
1919
static arrayUnion = (...elements: any[]) => new FieldValue("ARRAY_UNION", elements);
2020
static arrayRemove = (...elements: any[]) => new FieldValue("ARRAY_REMOVE", elements);
21+
static increment = (n: number) => new firebase.firestore.FieldValue("INCREMENT", n);
2122
}
2223

2324
export class GeoPoint {

src/firebase.android.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ firebase.toHashMap = obj => {
150150
let values: Array<any> = Array.isArray(fieldValue.value[0]) ? fieldValue.value[0] : fieldValue.value;
151151
values = values.map(v => typeof (v) === "object" ? firebase.toHashMap(v) : v);
152152
node.put(property, com.google.firebase.firestore.FieldValue.arrayRemove(values));
153+
} else if (fieldValue.type === "INCREMENT") {
154+
node.put(property, com.google.firebase.firestore.FieldValue.increment(fieldValue.value));
153155
} else {
154156
console.log("You found a bug! Please report an issue at https://github.com/EddyVerbruggen/nativescript-plugin-firebase/issues, mention fieldValue.type = '" + fieldValue.type + "'. Thanks!");
155157
}

src/firebase.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ export namespace firestore {
903903
commit(): Promise<void>;
904904
}
905905

906-
export type FieldValueType = "ARRAY_UNION" | "ARRAY_REMOVE";
906+
export type FieldValueType = "ARRAY_UNION" | "ARRAY_REMOVE" | "INCREMENT";
907907

908908
export class FieldValue {
909909
constructor(type: FieldValueType, value: any);
@@ -912,6 +912,7 @@ export namespace firestore {
912912
static delete: () => "DELETE_FIELD";
913913
static arrayUnion: (...elements: any[]) => FieldValue;
914914
static arrayRemove: (...elements: any[]) => FieldValue;
915+
static increment: (n: number) => FieldValue;
915916
}
916917

917918
export interface SnapshotListenOptions {

src/firebase.ios.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2178,6 +2178,13 @@ function fixSpecialField(item): any {
21782178
return FIRFieldValue.fieldValueForArrayUnion(Array.isArray(fieldValue.value[0]) ? fieldValue.value[0] : fieldValue.value);
21792179
} else if (fieldValue.type === "ARRAY_REMOVE") {
21802180
return FIRFieldValue.fieldValueForArrayRemove(Array.isArray(fieldValue.value[0]) ? fieldValue.value[0] : fieldValue.value);
2181+
} else if (fieldValue.type === "INCREMENT") {
2182+
const isInt = fieldValue.value % 1 === 0;
2183+
if (isInt) {
2184+
return FIRFieldValue.fieldValueForIntegerIncrement(fieldValue.value);
2185+
} else {
2186+
return FIRFieldValue.fieldValueForDoubleIncrement(fieldValue.value);
2187+
}
21812188
} else {
21822189
console.log("You found a bug! Please report an issue at https://github.com/EddyVerbruggen/nativescript-plugin-firebase/issues, mention fieldValue.type = '" + fieldValue.type + "'. Thanks!");
21832190
}

0 commit comments

Comments
 (0)