Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server Firestore from emulator #2098

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
fail-fast: false
matrix:
script-name: [lint, test, build, types]
script-name: [lint, test, build, types, seed]

steps:
- name: Checkout the repo
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
dist/
package-lock.json
data/seed
1 change: 1 addition & 0 deletions data/seed/auth_export/accounts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"kind":"identitytoolkit#DownloadAccountResponse","users":[]}
1 change: 1 addition & 0 deletions data/seed/auth_export/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"signIn":{"allowDuplicateEmails":false},"usageMode":"DEFAULT"}
16 changes: 16 additions & 0 deletions data/seed/firebase-export-metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"version": "9.23.0",
"firestore": {
"version": "1.13.1",
"path": "firestore_export",
"metadata_file": "firestore_export/firestore_export.overall_export_metadata"
},
"auth": {
"version": "9.23.0",
"path": "auth_export"
},
"storage": {
"version": "9.23.0",
"path": "storage_export"
}
}
Binary file not shown.
7 changes: 7 additions & 0 deletions data/seed/storage_export/buckets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"buckets": [
{
"id": "default-bucket"
}
]
}
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
"build": "npm run clean && NODE_ENV=production rollup --config rollup.config.js",
"clean": "rimraf dist",
"deploy": "npm run build && NODE_ENV=production firebase deploy",
"emulators:export": "firebase emulators:export ./data/seed",
"emulators:import": "FIRESTORE_EMULATOR_HOST=\"localhost:8080\" ts-node-script ./scripts/import",
"firestore:copy": "ts-node-script ./scripts/firestore-copy",
"firestore:init": "ts-node-script ./scripts/firestore-init",
"firestore:init": "ts-node-script ./scripts/import",
"fix": "concurrently npm:fix:*",
"fix:eslint": "eslint . --ext .ts --ext .js --fix",
"fix:prettier": "prettier . --write",
Expand All @@ -23,9 +25,10 @@
"lint:prettier": "prettier --check .",
"lint:stylelint": "stylelint \"src/components/**/*.ts\" \"src/styles/**/*.ts\"",
"postinstall": "npm run install:functions",
"seed": "firebase emulators:exec \"./scripts/validate-seed.sh\"",
"serve": "NODE_ENV=production concurrently --kill-others npm:start:*",
"start": "NODE_ENV=development concurrently --kill-others npm:start:*",
"start:emulators": "firebase emulators:start",
"start:emulators": "firebase emulators:start --import data/seed",
"start:functions": "npm --prefix functions start",
"start:rollup": "rollup --config rollup.config.js --watch",
"test": "jest",
Expand Down
11 changes: 9 additions & 2 deletions scripts/firebase-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@ import { cert, initializeApp, ServiceAccount } from 'firebase-admin/app';
import { getFirestore } from 'firebase-admin/firestore';
import serviceAccount from '../serviceAccount.json';

const credential = cert(serviceAccount as ServiceAccount);
initializeApp({ credential });
const { FIRESTORE_EMULATOR_HOST } = process.env;

if (FIRESTORE_EMULATOR_HOST) {
initializeApp({ projectId: 'demo-hoverboard' });
} else {
const credential = cert(serviceAccount as ServiceAccount);
initializeApp({ credential });
}

const firestore = getFirestore();

export { firestore };
31 changes: 0 additions & 31 deletions scripts/firestore-init/index.ts

This file was deleted.

37 changes: 37 additions & 0 deletions scripts/import.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Execute this script to import `default-firebase-data.json` data into Firestore.
*/

import { importBlog } from './firestore-init/blog';
import { importConfig } from './firestore-init/config';
import { importGallery } from './firestore-init/gallery';
import { importPartners } from './firestore-init/partners';
import { importPreviousSpeakers } from './firestore-init/previous-speakers';
import { importSchedule } from './firestore-init/schedule';
import { importSessions } from './firestore-init/sessions';
import { importSpeakers } from './firestore-init/speakers';
import { importTeam } from './firestore-init/team';
import { importTickets } from './firestore-init/tickets';
import { importVideos } from './firestore-init/videos';

console.log('Importing default data to Firestore');

importConfig()
.then(() => importBlog())
.then(() => importGallery())
.then(() => importPartners())
.then(() => importPreviousSpeakers())
.then(() => importSchedule())
.then(() => importSessions())
.then(() => importSpeakers())
.then(() => importTeam())
.then(() => importTickets())
.then(() => importVideos())
.then(() => {
console.log('Finished');
process.exit();
})
.catch((err: Error) => {
console.log(err);
process.exit();
});
3 changes: 3 additions & 0 deletions scripts/validate-seed.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
npm run emulators:import
npm run emulators:export -- --force
test -z "$(git diff --exit-code)"
22 changes: 18 additions & 4 deletions src/firebase.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
import { initializeApp } from 'firebase/app';
import { getFirestore, enableMultiTabIndexedDbPersistence } from 'firebase/firestore';
import { FirebaseApp, FirebaseOptions, initializeApp } from 'firebase/app';
import {
connectFirestoreEmulator,
enableMultiTabIndexedDbPersistence,
getFirestore,
} from 'firebase/firestore';
import { initializePerformance } from 'firebase/performance';
import { isLocalhost } from './utils/environment';

const response = await fetch('/__/firebase/init.json');
export const firebaseApp = initializeApp(await response.json());
export const db = getFirestore(firebaseApp);
const options: FirebaseOptions = await response.json();

const firebaseApp: FirebaseApp = initializeApp(options);
const db = getFirestore(firebaseApp);

if (isLocalhost()) {
console.log('Connecting to Firestore Emulator');
connectFirestoreEmulator(db, 'localhost', 8080);
}

initializePerformance(firebaseApp);
enableMultiTabIndexedDbPersistence(db);

export { db, firebaseApp };