Skip to content

Commit 1d8e0b3

Browse files
committed
fix(firestore): doc update/set data
feat: add fromDate for Timestamp
1 parent 38db60c commit 1d8e0b3

File tree

4 files changed

+87
-18
lines changed

4 files changed

+87
-18
lines changed

apps/demo/src/plugin-demos/firebase-firestore.ts

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Observable, EventData, Page } from '@nativescript/core';
22
import { DemoSharedFirebaseFirestore } from '@demo/shared';
3-
import { Firestore, GeoPoint } from '@nativescript/firebase-firestore';
3+
import { Firestore, GeoPoint, Timestamp } from '@nativescript/firebase-firestore';
44
import { firebase } from '@nativescript/firebase-core';
55

66
export function navigatingTo(args: EventData) {
@@ -9,13 +9,18 @@ export function navigatingTo(args: EventData) {
99
}
1010

1111
export class DemoModel extends DemoSharedFirebaseFirestore {
12-
firestore: Firestore;
1312

1413
constructor() {
1514
super();
16-
this.firestore = firebase().firestore();
17-
console.log(this.firestore.settings);
18-
this.firestore
15+
Promise.all(
16+
[
17+
this.invalid_field_path()
18+
]
19+
)
20+
}
21+
22+
async init() {
23+
firebase().firestore()
1924
.collection('users')
2025
.add({
2126
first: 'Ada',
@@ -30,7 +35,7 @@ export class DemoModel extends DemoSharedFirebaseFirestore {
3035
});
3136

3237
const geo = new GeoPoint(10, -10);
33-
this.firestore
38+
firebase().firestore()
3439
.collection('geo')
3540
.add({
3641
thing: 'it',
@@ -44,10 +49,31 @@ export class DemoModel extends DemoSharedFirebaseFirestore {
4449
});
4550

4651

47-
this.firestore.collection('items')
52+
firebase().firestore().collection('items')
4853
.get()
49-
.then(items =>{
54+
.then(items => {
5055
console.log(items.docs[0].data())
5156
})
5257
}
58+
59+
async invalid_field_path() {
60+
try {
61+
await firebase().firestore()
62+
.collection("products")
63+
.doc("Oq4eU5p4Lj7Eh6dfjbBX")
64+
.set({
65+
name: "Product 1",
66+
ts: Timestamp.fromDate(new Date())
67+
});
68+
await firebase().firestore()
69+
.collection("products")
70+
.doc("Oq4eU5p4Lj7Eh6dfjbBX")
71+
.update({
72+
name: "Product 2",
73+
ts: Timestamp.fromDate(new Date())
74+
});
75+
} catch (error) {
76+
console.log('error', error);
77+
}
78+
}
5379
}

packages/firebase-firestore/index.android.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,6 +1330,26 @@ export class Timestamp implements ITimestamp {
13301330
return null;
13311331
}
13321332

1333+
static #dateFormat: java.text.SimpleDateFormat;
1334+
1335+
1336+
static fromDate(date: Date) {
1337+
if (date instanceof Date) {
1338+
const ts = new Timestamp(0, 0, true);
1339+
1340+
if (!this.#dateFormat) {
1341+
const dateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
1342+
const tz = java.util.TimeZone.getTimeZone('UTC');
1343+
dateFormat.setTimeZone(tz);
1344+
this.#dateFormat = dateFormat;
1345+
}
1346+
1347+
ts.#native = new com.google.firebase.Timestamp(this.#dateFormat.parse(date.toISOString()))
1348+
return ts;
1349+
}
1350+
return null;
1351+
}
1352+
13331353
get nanoseconds(): number {
13341354
return this.native.getNanoseconds();
13351355
}

packages/firebase-firestore/index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ export class Timestamp implements ITimestamp {
217217

218218
readonly seconds: number;
219219

220+
static fromDate(date: Date): Timestamp;
221+
220222
readonly android: any;
221223
readonly ios: any;
222224
readonly native: any;
@@ -288,7 +290,7 @@ export declare class Firestore implements IFirestore {
288290
}
289291

290292
declare module '@nativescript/firebase-core' {
291-
export interface Firebase extends FirebaseFirestore {}
293+
export interface Firebase extends FirebaseFirestore { }
292294
}
293295

294296
export interface FirebaseFirestore {

packages/firebase-firestore/index.ios.ts

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -159,17 +159,29 @@ function serializeItems(value) {
159159

160160
function createDictionary(field: any, value?: any, moreFieldsAndValues?: any) {
161161
const data = {};
162-
if (field instanceof FieldPath) {
163-
data[field.native as any] = value?.native || value;
162+
if (data && !value && !moreFieldsAndValues) {
163+
Object.entries(field).forEach((item) => {
164+
const key = <any>item[0];
165+
const value = <any>item[1];
166+
if (key instanceof FieldPath) {
167+
data[key.native as any] = value?.native || value;
168+
} else {
169+
data[key] = value?.native || value;
170+
}
171+
});
164172
} else {
165-
data[field] = value?.native || value;
166-
}
173+
if (field instanceof FieldPath) {
174+
data[field.native as any] = value?.native || value;
175+
} else {
176+
data[field] = value?.native || value;
177+
}
167178

168-
if (Array.isArray(moreFieldsAndValues)) {
169-
for (let i = 0; i < moreFieldsAndValues.length; i += 2) {
170-
const key = moreFieldsAndValues[i];
171-
const value = moreFieldsAndValues[i + 1];
172-
data[key?.native || key] = value?.native || value;
179+
if (Array.isArray(moreFieldsAndValues)) {
180+
for (let i = 0; i < moreFieldsAndValues.length; i += 2) {
181+
const key = moreFieldsAndValues[i];
182+
const value = moreFieldsAndValues[i + 1];
183+
data[key?.native || key] = value?.native || value;
184+
}
173185
}
174186
}
175187

@@ -1160,6 +1172,15 @@ export class Timestamp implements ITimestamp {
11601172
return null;
11611173
}
11621174

1175+
static fromDate(date: Date) {
1176+
if (date instanceof Date) {
1177+
const ts = new Timestamp(0, 0, true);
1178+
ts.#native = FIRTimestamp.timestampWithDate(date);
1179+
return ts;
1180+
}
1181+
return null;
1182+
}
1183+
11631184
get nanoseconds(): number {
11641185
return this.native.nanoseconds;
11651186
}

0 commit comments

Comments
 (0)