@giancarlosgza/blog-firebase
v0.4.6
Published
Firebase backend for the blog packages: Firestore and Storage rules, indexes and the scheduled-publish Cloud Function
Readme
@giancarlosgza/blog-firebase
The Firebase side of the blog packages: Firestore and Storage security rules, Firestore indexes and the scheduled-publish Cloud Function. Install it in the repo that deploys the Firebase project (usually the admin), so rules, indexes and functions upgrade with the layers.
The data contract is described in docs/schema.md.
Rules and indexes
npm install -D @giancarlosgza/blog-firebasePoint firebase.json at the package files:
{
"firestore": {
"rules": "node_modules/@giancarlosgza/blog-firebase/firestore.rules",
"indexes": "node_modules/@giancarlosgza/blog-firebase/firestore.indexes.json"
},
"storage": {
"rules": "node_modules/@giancarlosgza/blog-firebase/storage.rules"
}
}Staff access comes from admins/{uid}.role (admin or editor) in Firestore; the Storage rules read
it too. To customize the rules, copy the files into the repo instead.
That Firestore read from the Storage rules is a cross-service call, which Firebase only allows once the
Storage service agent may read Firestore. Run the first Storage rules deploy interactively and accept the
prompt (--non-interactive skips it), or click Fix issue on the Storage rules banner in the console.
Without it, every upload is denied, staff included.
Scheduled publishing
Publishes posts whose status is scheduled once publishAt has passed, with the same steps as the
admin's publish route (sanitized body into postContents, status, history event). It runs hourly.
In the functions codebase, add this package as a regular dependency next to firebase-admin and
firebase-functions (Cloud Build skips devDependencies):
// functions/index.js
const { createPublishScheduledPosts } = require('@giancarlosgza/blog-firebase')
exports.publishScheduledPosts = createPublishScheduledPosts({ region: 'us-central1' })Any onSchedule option (schedule, region, timeZone, memory...) can be passed. The Admin SDK is
initialized on the first run when the codebase has not done it. publishDuePosts(db, now) and
publishScheduledPost(db, slug, now) are exported for custom triggers and tests.
Development
pnpm test:rules (from the repo root) runs the rules tests on the Firestore and Storage emulators.
It needs the Firebase CLI and Java.
