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

Commit

Permalink
Firestore anytime soon? #507 (doc)
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyVerbruggen committed Nov 28, 2017
1 parent 1dc45f5 commit d5aece4
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

## Features
* [Realtime Database](docs/DATABASE.md)
* Firestore (work in progress)
* [Firestore](docs/FIRESTORE.md)
* [Authentication](docs/AUTHENTICATION.md)
* [Remote Config](docs/REMOTECONFIG.md)
* [Cloud Messaging](docs/MESSAGING.md)
Expand Down
18 changes: 9 additions & 9 deletions demo-ng/app/item/items.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,49 +42,49 @@ export class ItemsComponent implements OnInit {


// example from https://firebase.google.com/docs/firestore/query-data/get-data
const citiesRef = firebase.firestore().collection("cities");
const citiesCollection = firebase.firestore().collection("cities");

citiesRef.doc("SF").set({
citiesCollection.doc("SF").set({
name: "San Francisco",
state: "CA",
country: "USA",
capital: false,
population: 860000
});

citiesRef.doc("LA").set({
citiesCollection.doc("LA").set({
name: "Los Angeles",
state: "CA",
country: "USA",
capital: false,
population: 3900000
});

citiesRef.doc("SAC").set({
citiesCollection.doc("SAC").set({
name: "Sacramento",
state: "CA",
country: "USA",
capital: true,
population: 500000
});

citiesRef.doc("DC").set({
citiesCollection.doc("DC").set({
name: "Washington, D.C.",
state: "WA",
country: "USA",
capital: true,
population: 680000
});

citiesRef.doc("TOK").set({
citiesCollection.doc("TOK").set({
name: "Tokyo",
state: null,
country: "Japan",
capital: true,
population: 9000000
});

citiesRef.doc("BJ").set({
citiesCollection.doc("BJ").set({
name: "Beijing",
state: null,
country: "China",
Expand Down Expand Up @@ -144,7 +144,7 @@ export class ItemsComponent implements OnInit {
.get()
.then((querySnapshot: firestore.QuerySnapshot) => {
querySnapshot.forEach(doc => {
console.log(`Capital: ${doc.id} => ${JSON.stringify(doc.data())}`);
console.log(`Relatively small Californian city: ${doc.id} => ${JSON.stringify(doc.data())}`);
});
})
.catch(err => console.log("Where-get failed, error" + err));
Expand All @@ -160,7 +160,7 @@ export class ItemsComponent implements OnInit {
.get()
.then((querySnapshot: firestore.QuerySnapshot) => {
querySnapshot.forEach(doc => {
console.log(`${JSON.stringify(doc.data())}`);
console.log(`Large Californian city: ${doc.id} => ${JSON.stringify(doc.data())}`);
});
})
.catch(err => console.log("firestoreWhereOrderLimit failed, error" + err));
Expand Down
5 changes: 2 additions & 3 deletions docs/DATABASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ If you can spare 41 seconds, check (an older version of) this plugin's [demo app
[![YouTube demo, 41 sec](images/yt-thumb-database.png)](https://youtu.be/7zYU5e0Djkw "YouTube demo, 41 sec")

## Enabling the database features
Since this is the most likely feature you'll use with this plugin it has already been properly configured to interact with the database, so nothing to do here on top of the stuff in the main readme.
*Only* if you choose to use **Firestore** (instead of the default DB) these features won't be available.

## Functions

Expand Down Expand Up @@ -32,8 +32,7 @@ You can optionally pass `persist` to the [`init` function](../README.md#init) to
</details>

### setValue
Data is stored as JSON data at a specific path (which is appended to the URL you passed to `init`).
If you want to add data to a known path use this, otherwise use `push` (see below).
Data is stored as JSON data at a specific path. If you want to add data to a known path use this, otherwise use `push` (see below).

The plugin will take care of serializing JSON data to native data structures.

Expand Down
176 changes: 176 additions & 0 deletions docs/FIRESTORE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
<img src="https://raw.githubusercontent.com/EddyVerbruggen/nativescript-plugin-firebase/master/docs/images/features/firestore.png" height="85px" alt="Cloud Firestore"/>

## Enabling the database features
During plugin installation you'll be prompted to use either Firestore or the default DB.

In case you're upgrading and you have the `firebase.nativescript.json` file in your project root,
you can edit it and add: `"firestore": true`. Then do `rm -rf platforms/ios && rm -rf platforms/android && rm -rf node_modules && npm i`.

## Functions
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
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).

> The plugin will take care of serializing JSON data to and from native data structures.
### `init` / `initializeApp`
By default Firestore on iOS and Android persists data locally for offline usage (web doesn't by default, and the regular Firebase DB doesn't either on any platform).
If you don't like that awesome feature, you can pass `persist: false` to the [`init` function](../README.md#init).

> Note that `initializeApp` is simply an alias for `init` to make the plugin compatible with the web API.
```typescript
const firebase = require("nativescript-plugin-firebase/app");

firebase.initializeApp({
persist: false
});
```

### `collection`
A 'collection' is at the root of any Firestore interaction. Data is stored as 'documents' in a 'collection'.

```typescript
const citiesCollection = firebase.firestore().collection("cities");
```

### `collection.get()`
To get all documents inside a collection:

```typescript
const citiesCollection = firebase.firestore().collection("cities");

citiesCollection.get().then(querySnapshot => {
querySnapshot.forEach(doc => {
console.log(`${doc.id} => ${JSON.stringify(doc.data())}`);
});
});
```

### `collection.doc()`
A 'document' lives inside a 'collection' and contains the actual data:

```typescript
const sanFranciscoDocument = firebase.firestore().collection("cities").doc("SF");
```

### `collection.doc().get()`
To get the data inside a document:

```typescript
const sanFranciscoDocument = firebase.firestore().collection("cities").doc("SF");

sanFranciscoDocument.get().then(doc => {
if (doc.exists) {
console.log("Document data:", JSON.stringify(doc.data()));
} else {
console.log("No such document!");
}
});
```

### `collection.add()`
If you want to add a document with an auto-generated ID, use `add` on a *collection*:

```typescript
const citiesCollection = firebase.firestore().collection("cities");

citiesCollection.add({
name: "San Francisco",
state: "CA",
country: "USA",
capital: false,
population: 860000
}).then(documentRef => {
console.log("San Francisco added with auto-generated ID: " + documentRef.id);
});
```

### `collection.doc().set()`
If you want to specify an ID yourself, use `set` on a *document*:

```typescript
const citiesCollection = firebase.firestore().collection("cities");

citiesCollection.doc("SF").set({
name: "San Francisco",
state: "CA",
country: "USA",
capital: false,
population: 860000
});

citiesCollection.doc("LA").set({
name: "Los Angeles",
state: "CA",
country: "USA",
capital: false,
population: 3900000
});
```

### `collection.doc().update()`
Update any number of properties of a document:

```typescript
const sanFranciscoDocument = firebase.firestore().collection("cities").doc("SF");

sanFranciscoDocument.update({
population: 860001
}).then(() => {
console.log("SF population updated");
});
```

### `collection.doc().delete()`
Entirely remove a document from a collection:

```typescript
const sanFranciscoDocument = firebase.firestore().collection("cities").doc("SF");

sanFranciscoDocument.delete().then(() => {
console.log("SF was erased from the face of the earth!");
});
```

### `collection.where()`
Firestore supports advanced querying with the `where` function. Those `where` clauses can be chained to form logical 'AND' queries:

```typescript
const citiesCollection = firebase.firestore().collection("cities");

// "Gimme all cities in California with a population below 550000"
const query = citiesCollection
.where("state", "==", "CA")
.where("population", "<", 550000);

query
.get()
.then(querySnapshot => {
querySnapshot.forEach(doc => {
console.log(`Relatively small Californian city: ${doc.id} => ${JSON.stringify(doc.data())}`);
});
});
```

### Ordering and limiting results of `collection.where()`
Return data sorted (asc or desc), or limit to a certain number of results:

```typescript
const citiesCollection = firebase.firestore().collection("cities");

// "Gimme the two largest cities in California, the largest first please"
const query = citiesCollection
.where("state", "==", "CA")
.orderBy("population", "desc")
.limit(2);

query
.get()
.then(querySnapshot => {
querySnapshot.forEach(doc => {
console.log(`Large Californian city: ${doc.id} => ${JSON.stringify(doc.data())}`);
});
});
```

> Need something that's not supported yet? Please open an Issue or PR 😚

0 comments on commit d5aece4

Please sign in to comment.