aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firestore/Example/SwiftBuildTest/main.swift
blob: 6773511da138d34b6528d9b3f3e4f6d60ce1f525 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
 * Copyright 2017 Google
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import Foundation

import FirebaseFirestore

func main() {
  let db = initializeDb()

  let (collectionRef, documentRef) = makeRefs(database: db)

  let query = makeQuery(collection: collectionRef)

  writeDocument(at: documentRef)

  writeDocuments(at: documentRef, database: db)

  addDocument(to: collectionRef)

  readDocument(at: documentRef)
  readDocumentWithSource(at: documentRef)

  readDocuments(matching: query)
  readDocumentsWithSource(matching: query)

  listenToDocument(at: documentRef)

  listenToDocuments(matching: query)

  enableDisableNetwork(database: db)

  types()
}

func initializeDb() -> Firestore {
  // Initialize with ProjectID.
  let firestore = Firestore.firestore()

  // Apply settings
  let settings = FirestoreSettings()
  settings.host = "localhost"
  settings.isPersistenceEnabled = true
  settings.areTimestampsInSnapshotsEnabled = true
  firestore.settings = settings

  return firestore
}

func makeRefs(database db: Firestore) -> (CollectionReference, DocumentReference) {
  var collectionRef = db.collection("my-collection")

  var documentRef: DocumentReference
  documentRef = collectionRef.document("my-doc")
  // or
  documentRef = db.document("my-collection/my-doc")

  // deeper collection (my-collection/my-doc/some/deep/collection)
  collectionRef = documentRef.collection("some/deep/collection")

  // parent doc (my-collection/my-doc/some/deep)
  documentRef = collectionRef.parent!

  // print paths.
  print("Collection: \(collectionRef.path), document: \(documentRef.path)")

  return (collectionRef, documentRef)
}

func makeQuery(collection collectionRef: CollectionReference) -> Query {
  let query = collectionRef.whereField(FieldPath(["name"]), isEqualTo: "Fred")
    .whereField("age", isGreaterThanOrEqualTo: 24)
    .whereField("tags", arrayContains: "active")
    .whereField(FieldPath(["tags"]), arrayContains: "active")
    .whereField(FieldPath.documentID(), isEqualTo: "fred")
    .order(by: FieldPath(["age"]))
    .order(by: "name", descending: true)
    .limit(to: 10)

  return query
}

func writeDocument(at docRef: DocumentReference) {
  let setData = [
    "foo": 42,
    "bar": [
      "baz": "Hello world!",
    ],
  ] as [String: Any]

  let updateData = [
    "bar.baz": 42,
    FieldPath(["foobar"]): 42,
    "server_timestamp": FieldValue.serverTimestamp(),
    "array_union": FieldValue.arrayUnion(["a", "b"]),
    "array_remove": FieldValue.arrayRemove(["a", "b"]),
    "field_delete": FieldValue.delete(),
  ] as [AnyHashable: Any]

  docRef.setData(setData)

  // Completion callback (via trailing closure syntax).
  docRef.setData(setData) { error in
    if let error = error {
      print("Uh oh! \(error)")
      return
    }

    print("Set complete!")
  }

  // merge
  docRef.setData(setData, merge: true)
  docRef.setData(setData, merge: true) { error in
    if let error = error {
      print("Uh oh! \(error)")
      return
    }

    print("Set complete!")
  }

  docRef.updateData(updateData)
  docRef.delete()

  docRef.delete { error in
    if let error = error {
      print("Uh oh! \(error)")
      return
    }

    print("Set complete!")
  }
}

func enableDisableNetwork(database db: Firestore) {
  // closure syntax
  db.disableNetwork(completion: { error in
    if let e = error {
      print("Uh oh! \(e)")
      return
    }
  })
  // trailing block syntax
  db.enableNetwork { error in
    if let e = error {
      print("Uh oh! \(e)")
      return
    }
  }
}

func writeDocuments(at docRef: DocumentReference, database db: Firestore) {
  var batch: WriteBatch

  batch = db.batch()
  batch.setData(["a": "b"], forDocument: docRef)
  batch.setData(["a": "b"], forDocument: docRef, merge: true)
  batch.setData(["c": "d"], forDocument: docRef)
  // commit without completion callback.
  batch.commit()
  print("Batch write without completion complete!")

  batch = db.batch()
  batch.setData(["a": "b"], forDocument: docRef)
  batch.setData(["c": "d"], forDocument: docRef)
  // commit with completion callback via trailing closure syntax.
  batch.commit { error in
    if let error = error {
      print("Uh oh! \(error)")
      return
    }
    print("Batch write callback complete!")
  }
  print("Batch write with completion complete!")
}

func addDocument(to collectionRef: CollectionReference) {
  collectionRef.addDocument(data: ["foo": 42])
  // or
  collectionRef.document().setData(["foo": 42])
}

func readDocument(at docRef: DocumentReference) {
  // Trailing closure syntax.
  docRef.getDocument { document, error in
    if let document = document {
      // Note that both document and document.data() is nullable.
      if let data = document.data() {
        print("Read document: \(data)")
      }
      if let data = document.data(with: .estimate) {
        print("Read document: \(data)")
      }
      if let foo = document.get("foo") {
        print("Field: \(foo)")
      }
      if let foo = document.get("foo", serverTimestampBehavior: .previous) {
        print("Field: \(foo)")
      }
      // Fields can also be read via subscript notation.
      if let foo = document["foo"] {
        print("Field: \(foo)")
      }
    } else {
      // TODO(mikelehen): There may be a better way to do this, but it at least demonstrates
      // the swift error domain / enum codes are renamed appropriately.
      if let errorCode = error.flatMap({
        ($0._domain == FirestoreErrorDomain) ? FirestoreErrorCode(rawValue: $0._code) : nil
      }) {
        switch errorCode {
        case .unavailable:
          print("Can't read document due to being offline!")
        case _:
          print("Failed to read.")
        }
      } else {
        print("Unknown error!")
      }
    }
  }
}

func readDocumentWithSource(at docRef: DocumentReference) {
  docRef.getDocument(source: FirestoreSource.default) { document, error in
  }
  docRef.getDocument(source: .server) { document, error in
  }
  docRef.getDocument(source: FirestoreSource.cache) { document, error in
  }
}

func readDocuments(matching query: Query) {
  query.getDocuments { querySnapshot, error in
    // TODO(mikelehen): Figure out how to make "for..in" syntax work
    // directly on documentSet.
    for document in querySnapshot!.documents {
      print(document.data())
    }
  }
}

func readDocumentsWithSource(matching query: Query) {
  query.getDocuments(source: FirestoreSource.default) { querySnapshot, error in
  }
  query.getDocuments(source: .server) { querySnapshot, error in
  }
  query.getDocuments(source: FirestoreSource.cache) { querySnapshot, error in
  }
}

func listenToDocument(at docRef: DocumentReference) {
  let listener = docRef.addSnapshotListener { document, error in
    if let error = error {
      print("Uh oh! Listen canceled: \(error)")
      return
    }

    if let document = document {
      // Note that document.data() is nullable.
      if let data: [String: Any] = document.data() {
        print("Current document: \(data)")
      }
      if document.metadata.isFromCache {
        print("From Cache")
      } else {
        print("From Server")
      }
    }
  }

  // Unsubscribe.
  listener.remove()
}

func listenToDocumentWithMetadataChanges(at docRef: DocumentReference) {
  let listener = docRef.addSnapshotListener(includeMetadataChanges: true) { document, error in
    if let document = document {
      if document.metadata.hasPendingWrites {
        print("Has pending writes")
      }
    }
  }

  // Unsubscribe.
  listener.remove()
}

func listenToDocuments(matching query: Query) {
  let listener = query.addSnapshotListener { snap, error in
    if let error = error {
      print("Uh oh! Listen canceled: \(error)")
      return
    }

    if let snap = snap {
      print("NEW SNAPSHOT (empty=\(snap.isEmpty) count=\(snap.count)")

      // TODO(mikelehen): Figure out how to make "for..in" syntax work
      // directly on documentSet.
      for document in snap.documents {
        // Note that document.data() is not nullable.
        let data: [String: Any] = document.data()
        print("Doc: ", data)
      }
    }
  }

  // Unsubscribe
  listener.remove()
}

func listenToQueryDiffs(onQuery query: Query) {
  let listener = query.addSnapshotListener { snap, error in
    if let snap = snap {
      for change in snap.documentChanges {
        switch change.type {
        case .added:
          print("New document: \(change.document.data())")
        case .modified:
          print("Modified document: \(change.document.data())")
        case .removed:
          print("Removed document: \(change.document.data())")
        }
      }
    }
  }

  // Unsubscribe
  listener.remove()
}

func listenToQueryDiffsWithMetadata(onQuery query: Query) {
  let listener = query.addSnapshotListener(includeMetadataChanges: true) { snap, error in
    if let snap = snap {
      for change in snap.documentChanges(includeMetadataChanges: true) {
        switch change.type {
        case .added:
          print("New document: \(change.document.data())")
        case .modified:
          print("Modified document: \(change.document.data())")
        case .removed:
          print("Removed document: \(change.document.data())")
        }
      }
    }
  }

  // Unsubscribe
  listener.remove()
}

func transactions() {
  let db = Firestore.firestore()

  let collectionRef = db.collection("cities")
  let accA = collectionRef.document("accountA")
  let accB = collectionRef.document("accountB")
  let amount = 20.0

  db.runTransaction({ (transaction, errorPointer) -> Any? in
    do {
      let balanceA = try transaction.getDocument(accA)["balance"] as! Double
      let balanceB = try transaction.getDocument(accB)["balance"] as! Double

      if balanceA < amount {
        errorPointer?.pointee = NSError(domain: "Foo", code: 123, userInfo: nil)
        return nil
      }
      transaction.updateData(["balance": balanceA - amount], forDocument: accA)
      transaction.updateData(["balance": balanceB + amount], forDocument: accB)
    } catch let error as NSError {
      print("Uh oh! \(error)")
    }
    return 0
  }) { result, error in
    // handle result.
  }
}

func types() {
  let _: CollectionReference
  let _: DocumentChange
  let _: DocumentReference
  let _: DocumentSnapshot
  let _: FieldPath
  let _: FieldValue
  let _: Firestore
  let _: FirestoreSettings
  let _: GeoPoint
  let _: Timestamp
  let _: ListenerRegistration
  let _: Query
  let _: QuerySnapshot
  let _: SnapshotMetadata
  let _: Transaction
  let _: WriteBatch
}