Skip to content

Instantly share code, notes, and snippets.

@danahartweg
danahartweg / test-firestore-on-ci.sh
Last active September 30, 2022 04:10
Running the firestore emulator in a CI environment without IPv6
#!/bin/sh
EMULATOR="cloud-firestore-emulator"
EMULATOR_TARGET=$(find ~/.cache/firebase/emulators/ -type f -name "$EMULATOR*.jar" | sort -r | head -n1)
if [ -z "$EMULATOR_TARGET" ]; then
echo "Could not find the firestore emulator. Ending test run."
exit 1
fi
@danahartweg
danahartweg / firebase.json
Created October 15, 2019 22:08
Firestore configuration - Unit testing Cloud Firestore
{
"emulators": {
"firestore": {
"host": "127.0.0.1"
},
"functions": {
"host": "127.0.0.1"
}
},
"firestore": {
@danahartweg
danahartweg / firestore.rules
Last active October 19, 2019 20:54
Firestore rules - Unit testing Cloud Firestore
service cloud.firestore {
match /databases/{database}/documents {
// GENERIC //
function hasRoleForResource(targetResource, targetRoles) {
return targetResource.data.role in targetRoles;
}
// USERS //
@danahartweg
danahartweg / admin.ts
Created October 15, 2019 22:28
Function admin helper - Unit testing Cloud Firestore
import * as admin from 'firebase-admin';
let cachedAdmin: admin.app.App;
export function init(overrideApp?: admin.app.App) {
if (cachedAdmin) {
return;
}
cachedAdmin = overrideApp || admin.initializeApp();
@danahartweg
danahartweg / update-membership-on-creation.ts
Created October 18, 2019 23:16
Homestead creation membership function - Unit testing Cloud Firestore
import { firestore } from 'firebase-functions';
import { FieldValue, getFirestore } from '../admin';
/**
* When a new homestead is created we want to automatically set the owner access document for that homestead.
* Additionally, we want to set the reverse relationship on the user document.
*/
export const updateMembershipOnHomesteadCreation = firestore
.document('homesteads/{homesteadId}')
@danahartweg
danahartweg / constants.ts
Last active October 19, 2019 00:07
Test helper constants - Unit testing Cloud Firestore
import * as uuid from 'uuid/v4';
type Collections =
| 'catchAlls'
| 'homesteads'
| 'users';
export enum COLLECTIONS {
CATCH_ALL = 'catchAlls',
HOMESTEADS = 'homesteads',
@danahartweg
danahartweg / firestore-helpers.ts
Created October 19, 2019 00:23
Firestore test harness helpers - Unit testing Cloud Firestore
import * as firebase from '@firebase/testing';
export type Firestore = firebase.firestore.Firestore;
export type DocumentReference = firebase.firestore.DocumentReference;
let testIncrement = 0;
let useRealProjectId = false;
const projectIdBase = `firestore-emulator-${Date.now()}`;
function adjustTestIncrement() {
@danahartweg
danahartweg / list_plant_varieties.dart
Created December 22, 2019 03:54
Listing plant varieties - Efficiently managing large Cloud Firestore lists
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter/material.dart';
import 'package:grow_smart/blocs/homestead/bloc.dart';
import 'package:grow_smart/services/service_locator.dart';
class ListPlantVarieties extends StatelessWidget {
@override
build(context) {
@danahartweg
danahartweg / create-indices.ts
Created December 22, 2019 04:00
Creating homestead indices - Efficiently managing large Cloud Firestore lists
import { firestore } from 'firebase-functions';
import { getFirestore } from '../admin';
import { IndexMeta } from '../types';
/**
* When a new homestead is created we want to create
* the needed indices so they can be immediately accessed.
*/
export const createIndicesForHomestead = firestore
@danahartweg
danahartweg / update-index-meta.ts
Created December 22, 2019 04:17
Updating index meta information - Efficiently managing large Cloud Firestore lists
import { firestore } from 'firebase-functions';
// @ts-ignore there is no declaration file for this module
import * as sizeOf from 'firestore-size';
import { getFirestore } from '../admin';
import { IndexMeta } from '../types';
// 1 MiB * 1024 kB * 1024 B * margin of error
const MAX_DOCUMENT_SIZE = 1 * 1024 * 1024 * 0.95;