This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import firebase from "firebase/app"; | |
import "firebase/firestore"; | |
const randomNumber = Math.floor(Math.random() * 1000); | |
const docRef = firebase.firestore.doc("path/to/doc"); | |
db.runTransaction(async function(t) { | |
const doc = await t.get(docRef); | |
if (doc.exists && doc.data().number < randomNumber) { | |
console.log("setting new max"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Assumes that group members are stored in a subcollection under /groups/{groupId}/members/{userId} | |
const memberPath = '/familyMembers/{familyMemberId}/parents/{parentId}'; | |
// Trigger updates to our generated maps if group membership changes | |
exports.memberAdded = functions.firestore.document(memberPath).onCreate(memberAdded); | |
exports.memberDeleted = functions.firestore.document(memberPath).onDelete(memberDeleted); | |
async function getAllowedDocuments(parentId) { | |
// what goes here? | |
return ['foo', 'bar']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"aListOfRecords": { | |
// when querying our list, must get less than 50 at a time and be authenticated | |
".read": "auth != null && (query.limitToFirst <= 50 || query.limitToLast <= 50)", | |
"$aRecordId": { | |
// When fetching a single record, I must be logged in | |
".read": "auth != null", | |
// When writing a record, I must be logged in |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function callMeAnywhere(uid, foo) { | |
if( !uid ) throw new Error("Must authenticate"); | |
return foo + 'bar'; | |
} | |
functions.https.onCall((data, context) => { | |
const data = callMeAnywhere(context.auth.uid, data.foo); | |
return {data: data}; | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export type CompareFunction = (a: object, b: object) => number; | |
export type MergeQueryObserver = (docs: DocumentData[]) => void; | |
export type MergeErrorObserver = ( | |
e: Error | string | object, | |
queryPosition: number | |
) => void; | |
export class MergeQuery { | |
private observers: Set<{ fn: MergeQueryObserver; err: MergeErrorObserver }>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"firestore": { | |
"rules": "firestore.rules", | |
"indexes": "firestore.indexes.json" | |
}, | |
"emulators": { | |
"firestore": { | |
"port": 8080 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import db from './db'; | |
// Set this to the max ops per batch listed in the Firestore docs | |
// https://firebase.google.com/docs/firestore/manage-data/transactions | |
export const MAX_BATCH_OPS = 500; | |
// A batch processor that will accept any number of requests. | |
// It processes batches in the maximum number of events allowed. | |
// Does not guarantee atomicity across batches. | |
// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FirebaseApp primary = FirebaseApp.getInstance(); | |
FirebaseFirestore primaryDatabase = FirebaseFirestore.getInstance(); | |
// Reference the same setup/instance | |
FirebaseOptions options = primary.getOptions(); | |
/* | |
// Use multiple projects/environments. More here: https://firebase.google.com/docs/projects/multiprojects | |
FirebaseOptions options = new FirebaseOptions.Builder() | |
.setApplicationId("1:27992087142:android:ce3b6448250083d1") // Required for Analytics. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
JSONStream = require('JSONStream'); | |
es = require('event-stream'); | |
fileStream = storage.bucket('your-bucket').file('your-JSON-file').createReadStream(); | |
db = admin.firestore(); | |
return new Promise( (resolve, reject) => { | |
batchPromises = []; | |
batchSize = 0; | |
batch = db.batch(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// docs/{docId}/users is an array of user ids allowed to access the doc | |
match /docs/{docId} { | |
allow read if request.auth.uid in getData('docs/$(docId)').users; | |
} | |
/** | |
* Shortcut to simplify pathing | |
*/ | |
function getPath(childPath) { |
NewerOlder