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

Commit

Permalink
Firebase Realtime DB and Firestore in the same app #553
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyVerbruggen committed Nov 28, 2017
1 parent a82d088 commit 1167bb4
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 20 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
[Firebase Android SDK Changelog](https://firebase.google.com/support/release-notes/android)


## 5.0.0 (work in progress)
## 5.0.1 (2017, November 28)

### New
- [#553](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/issues/553) Firebase Realtime DB and Firestore in the same app


## 5.0.0 (2017, November 28)

### New
- [#507](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/issues/507) Firestore anytime soon?
Expand Down
5 changes: 5 additions & 0 deletions demo-ng/app/item/items.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
<ScrollView>
<StackLayout class="page">
<Label text="After pressing those buttons, check the console log" textWrap="true"></Label>

<Label text="Authentication" class="h2"></Label>
<Button text="login anonymously" (tap)="loginAnonymously()" class="button button-user"></Button>

<Label text="Realtime DB" class="h2"></Label>
<Button text="Get companies" (tap)="doWebGetValueForCompanies()" class="button"></Button>

<Label text="Firestore" class="h2"></Label>
<Button text="Add" (tap)="firestoreAdd()" class="button"></Button>
<Button text="Set" (tap)="firestoreSet()" class="button"></Button>
Expand Down
11 changes: 11 additions & 0 deletions demo-ng/app/item/items.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Component, OnInit } from "@angular/core";
import { firestore } from "nativescript-plugin-firebase";
const firebase = require("nativescript-plugin-firebase/app");
const firebaseWebApi = require("nativescript-plugin-firebase/app");

@Component({
selector: "ns-items",
Expand Down Expand Up @@ -203,4 +204,14 @@ export class ItemsComponent implements OnInit {
})
.catch(err => console.log("Delete failed, error" + err));
}

public doWebGetValueForCompanies(): void {
const path = "/companies";
firebaseWebApi.database().ref(path)
.once("value")
.then(result => {
console.log(`${result.key} => ${JSON.stringify(result.val())}`);
})
.catch(error => console.log("doWebGetValueForCompanies error: " + error));
}
}
5 changes: 4 additions & 1 deletion docs/DATABASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ 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
*Only* if you choose to use **Firestore** (instead of the default DB) these features won't be available.
Before plugin version 5.0.0 this was enabled by default, but since 5.0.0 we're also supporting Firestore.

If your saved config file `firebase.nativescript.json` (in the root of your project) doesn't include `"realtimedb"` we'll assume `true` for backward compatibility.
You can disable it by editing that file and setting `"realtimedb": false`. Then run `rm -rf platforms && rm -rf node_modules && npm i`.

## Functions

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

## Enabling Firestore
During plugin installation you'll be prompted to use either Firestore or the default DB.
During plugin installation you'll be asked whether or not you use Firestore.

In case you're upgrading and you have the `firebase.nativescript.json` file in your project root, edit it and add: `"firestore": true`.
Then run `rm -rf platforms && rm -rf node_modules && npm i`.
Expand Down
18 changes: 13 additions & 5 deletions publish/scripts/installer.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ function askAndroidPromptResult(result) {
function promptQuestions() {
prompt.get([{
name: 'firestore',
description: 'Are you using Cloud Firestore instead of the regular Database engine (y/n)',
description: 'Are you using Cloud Firestore (y/n)',
default: 'n'
}, {
name: 'realtimedb',
description: 'Are you using Realtime DB (y/n)',
default: 'n'
}, {
name: 'remote_config',
Expand Down Expand Up @@ -222,8 +226,8 @@ function writePodFile(result) {
`pod 'Firebase', '~> 4.6.0'
pod 'Firebase/Auth'
# Uncomment if you want to enable the regular Database (instead of Cloud Firestore)
` + (!isSelected(result.firestore) ? `` : `#`) + `pod 'Firebase/Database'
# Uncomment if you want to enable Realtime DB
` + (!isPresent(result.realtimedb) || isSelected(result.realtimedb) ? `` : `#`) + `pod 'Firebase/Database'
# Uncomment if you want to enable Cloud Firestore
` + (isSelected(result.firestore) ? `` : `#`) + `pod 'Firebase/Firestore'
Expand Down Expand Up @@ -309,8 +313,8 @@ dependencies {
def googlePlayServicesVersion = project.hasProperty('googlePlayServicesVersion') ? project.googlePlayServicesVersion : firebaseVersion
compile "com.google.android.gms:play-services-base:$googlePlayServicesVersion"
// Uncomment if you want to use the regular Database (instead of 'Cloud Firestore')
` + (!isSelected(result.firestore) ? `` : `//`) + ` compile "com.google.firebase:firebase-database:$firebaseVersion"
// Uncomment if you want to use the regular Database
` + (!isPresent(result.realtimedb) || isSelected(result.realtimedb) ? `` : `//`) + ` compile "com.google.firebase:firebase-database:$firebaseVersion"
// Uncomment if you want to use 'Cloud Firestore'
` + (isSelected(result.firestore) ? `` : `//`) + ` compile "com.google.firebase:firebase-firestore:$firebaseVersion"
Expand Down Expand Up @@ -454,3 +458,7 @@ module.exports = function() {
function isSelected(value) {
return value === true || (typeof value === "string" && value.toLowerCase() === 'y');
}

function isPresent(value) {
return value !== undefined;
}
4 changes: 3 additions & 1 deletion src/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
tsconfig.json
references.d.ts
platforms/android/libraryproject/
platforms/ios/typings/
platforms/ios/typings/
platforms/android/typings/
platforms/web
6 changes: 4 additions & 2 deletions src/firebase.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ firebase.init = arg => {

arg = arg || {};

if (typeof(com.google.firebase.firestore) === "undefined") {
if (typeof(com.google.firebase.database) !== "undefined") {
firebase.ServerValue = {
TIMESTAMP: firebase.toJsObject(com.google.firebase.database.ServerValue.TIMESTAMP)
};
Expand All @@ -210,7 +210,9 @@ firebase.init = arg => {
fDatabase.getInstance().setPersistenceEnabled(true);
}
firebase.instance = fDatabase.getInstance().getReference();
} else {
}

if (typeof(com.google.firebase.firestore) !== "undefined") {
// Firestore has offline persistence enabled by default
if (!arg.persist) {
com.google.firebase.firestore.FirebaseFirestore.getInstance().setFirestoreSettings(
Expand Down
7 changes: 4 additions & 3 deletions src/firebase.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ firebase.init = arg => {
}

try {
// this is only available when the Database Pod is loaded (not with Firestore)
// this is only available when the Realtime DB Pod is loaded
if (typeof(FIRServerValue) !== "undefined") {
firebase.ServerValue = {
TIMESTAMP: FIRServerValue.timestamp()
Expand All @@ -636,9 +636,10 @@ firebase.init = arg => {
if (arg.persist) {
FIRDatabase.database().persistenceEnabled = true;
}

firebase.instance = FIRDatabase.database().reference();
} else if (typeof(FIRStorage) !== "undefined") {
}

if (typeof(FIRStorage) !== "undefined") {
// Firestore has offline persistence enabled by default
if (arg.persist === false) {
const fIRFirestoreSettings = FIRFirestoreSettings.new();
Expand Down
2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nativescript-plugin-firebase",
"version": "5.0.0",
"version": "5.0.1",
"description": "Fire. Base. Firebase!",
"main": "firebase",
"typings": "index.d.ts",
Expand Down
18 changes: 13 additions & 5 deletions src/scripts/postinstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -2927,7 +2927,11 @@ function askAndroidPromptResult(result) {
function promptQuestions() {
prompt.get([{
name: 'firestore',
description: 'Are you using Cloud Firestore instead of the regular Database engine (y/n)',
description: 'Are you using Cloud Firestore (y/n)',
default: 'n'
}, {
name: 'realtimedb',
description: 'Are you using Realtime DB (y/n)',
default: 'n'
}, {
name: 'remote_config',
Expand Down Expand Up @@ -3020,8 +3024,8 @@ function writePodFile(result) {
`pod 'Firebase', '~> 4.6.0'
pod 'Firebase/Auth'
# Uncomment if you want to enable the regular Database (instead of Cloud Firestore)
` + (!isSelected(result.firestore) ? `` : `#`) + `pod 'Firebase/Database'
# Uncomment if you want to enable Realtime DB
` + (!isPresent(result.realtimedb) || isSelected(result.realtimedb) ? `` : `#`) + `pod 'Firebase/Database'
# Uncomment if you want to enable Cloud Firestore
` + (isSelected(result.firestore) ? `` : `#`) + `pod 'Firebase/Firestore'
Expand Down Expand Up @@ -3107,8 +3111,8 @@ dependencies {
def googlePlayServicesVersion = project.hasProperty('googlePlayServicesVersion') ? project.googlePlayServicesVersion : firebaseVersion
compile "com.google.android.gms:play-services-base:$googlePlayServicesVersion"
// Uncomment if you want to use the regular Database (instead of 'Cloud Firestore')
` + (!isSelected(result.firestore) ? `` : `//`) + ` compile "com.google.firebase:firebase-database:$firebaseVersion"
// Uncomment if you want to use the regular Database
` + (!isPresent(result.realtimedb) || isSelected(result.realtimedb) ? `` : `//`) + ` compile "com.google.firebase:firebase-database:$firebaseVersion"
// Uncomment if you want to use 'Cloud Firestore'
` + (isSelected(result.firestore) ? `` : `//`) + ` compile "com.google.firebase:firebase-firestore:$firebaseVersion"
Expand Down Expand Up @@ -3253,6 +3257,10 @@ function isSelected(value) {
return value === true || (typeof value === "string" && value.toLowerCase() === 'y');
}

function isPresent(value) {
return value !== undefined;
}


/***/ })
/******/ ]);

0 comments on commit 1167bb4

Please sign in to comment.