UNPKG

tanam

Version:

Pluggable CMS for Firebase

86 lines 3.87 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const admin = require("firebase-admin"); const siteCollection = () => admin.firestore().collection('tanam').doc(process.env.GCLOUD_PROJECT); const siteRef = () => admin.database().ref('tanam').child(process.env.GCLOUD_PROJECT); const normalizeUrl = (url) => `/${url}`.replace(/\/+/g, '/'); function getDocumentById(docId) { return __awaiter(this, void 0, void 0, function* () { console.log(`[document.service.getDocumentById] ID: ${docId}`); const querySnap = yield siteCollection() .collection('documents') .where('status', '==', 'published') .where('id', '==', docId) .limit(1) .get(); console.log(`[document.service.getDocumentById] Number of query results: ${querySnap.docs.length}`); return querySnap.empty ? null : querySnap.docs[0].data(); }); } exports.getDocumentById = getDocumentById; function getDocumentByUrl(url) { return __awaiter(this, void 0, void 0, function* () { console.log(`[document.service.getDocumentByUrl] URL: ${url}`); const querySnap = yield siteCollection() .collection('documents') .where('status', '==', 'published') .where('url', '==', normalizeUrl(url)) .limit(1) .get(); console.log(`[document.service.getDocumentByUrl] Number of query results: ${querySnap.docs.length}`); const results = []; for (const doc of querySnap.docs) { results.push(doc.data()); } return results; }); } exports.getDocumentByUrl = getDocumentByUrl; function getDocument404() { return __awaiter(this, void 0, void 0, function* () { console.log(`[document.service.getDocument404]`); const querySnap = yield siteCollection() .collection('documents') .where('status', '==', 'published') .where('url', '==', '/404-page') .limit(1) .get(); const results = []; for (const doc of querySnap.docs) { results.push(doc.data()); } return results; }); } exports.getDocument404 = getDocument404; /** * This method builds up a dependency graph by registering document references * * A document reference is when one document is embedding another document through * partial templates or by using lookup directives to use data from another document * inside of the current web template. * * Once any of those documents are changed, the graph needs to be traversed until * all rippling changes have been re-rendered. * * @param docId The ID of the document that is referring to other documents * @param references One or more document IDs that are being referred to in a document */ function addDependency(docId, references) { return __awaiter(this, void 0, void 0, function* () { console.log(`[addDependency] ${JSON.stringify({ docId, references })}`); return siteCollection().collection('documents').doc(docId).update({ dependencies: admin.firestore.FieldValue.arrayUnion(...references), }); }); } exports.addDependency = addDependency; //# sourceMappingURL=document.service.js.map