UNPKG

@mathrunet/masamune

Version:

Manages packages for the server portion (NodeJS) of the Masamune framework.

166 lines 7.89 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } 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) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const functions = __importStar(require("firebase-functions/v2")); const algolia = __importStar(require("algoliasearch")); const default_firestore_model_field_value_converter_1 = require("../lib/model_field_value/default_firestore_model_field_value_converter"); /** * Synchronize data to Algolia. * * Algoliaにデータを同期します。 * * @param {string} process.env.ALGOLIA_APPID * Specify the Application ID for Algolia. * 1. open the Settings page at the bottom left of the Algolia dashboard screen and open "API Keys". * 2. Copy "Application ID". * Algolia用のApplication IDを指定します。 * 1. Algoliaのダッシュボードの画面左下のSettingsページを開き「API Keys」を開く。 * 2. 「Application ID」をコピーする。 * * @param {string} process.env.ALGOLIA_APIKEY * Specify the Application ID for Algolia. * 1. open the Settings page at the bottom left of the Algolia dashboard screen and open "API Keys". * 2. Open the "All API Key" screen and create a new API key from "New API Key". * - Be sure to add "AddObject" and "DeleteObject" to "ACL". * Algolia用のApplication IDを指定します。 * 1. Algoliaのダッシュボードの画面左下のSettingsページを開き「API Keys」を開く。 * 2. 「All API Key」の画面を開き「New API Key」から新しくAPIキーを作成する。 * - 「ACL」に「AddObject」「DeleteObject」を追加しておくこと。 */ module.exports = (regions, options, data) => { var _a, _b; return functions.firestore.onDocumentWritten({ document: `${options.path}/{docId}`, region: (_a = options.region) !== null && _a !== void 0 ? _a : regions[0], timeoutSeconds: options.timeoutSeconds, memory: options.memory, minInstances: options.minInstances, concurrency: options.concurrency, maxInstances: options.maxInstances, serviceAccount: (_b = options.serviceAccount) !== null && _b !== void 0 ? _b : undefined, }, (event) => __awaiter(void 0, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o; try { const afterExists = (_b = (_a = event.data) === null || _a === void 0 ? void 0 : _a.after.exists) !== null && _b !== void 0 ? _b : false; const beforeExists = (_d = (_c = event.data) === null || _c === void 0 ? void 0 : _c.before.exists) !== null && _d !== void 0 ? _d : false; const indexName = (_f = (_e = options.path) === null || _e === void 0 ? void 0 : _e.split("/").pop()) !== null && _f !== void 0 ? _f : ""; console.log(`Algolia: ${process.env.ALGOLIA_APPID}`); const client = algolia.algoliasearch((_g = process.env.ALGOLIA_APPID) !== null && _g !== void 0 ? _g : "", (_h = process.env.ALGOLIA_APIKEY) !== null && _h !== void 0 ? _h : ""); console.log(`Start: ${options.path} to ${indexName}`); // create if (!beforeExists && afterExists) { const data = (_j = event.data) === null || _j === void 0 ? void 0 : _j.after.data(); const key = (_k = event.data) === null || _k === void 0 ? void 0 : _k.after.id; console.log(`Create: ${key} to ${data}`); if (!key || !data) { return; } const converted = _convert(data); const update = Object.assign(Object.assign({}, converted), { "@uid": key, "objectID": key }); yield client.addOrUpdateObject({ indexName: indexName, objectID: key, body: update, }); // update } else if (beforeExists && afterExists) { const data = (_l = event.data) === null || _l === void 0 ? void 0 : _l.after.data(); const key = (_m = event.data) === null || _m === void 0 ? void 0 : _m.after.id; console.log(`Update: ${key} to ${data}`); if (!key || !data) { return; } const converted = _convert(data); const update = Object.assign(Object.assign({}, converted), { "@uid": key, "objectID": key }); yield client.addOrUpdateObject({ indexName: indexName, objectID: key, body: update, }); // delete } else if (beforeExists && !afterExists) { const key = (_o = event.data) === null || _o === void 0 ? void 0 : _o.after.id; console.log(`Delete: ${key}`); if (!key) { return; } yield client.deleteObject({ indexName: indexName, objectID: key, }); } } catch (err) { console.log(err); throw err; } })); }; function _convert(data) { const update = {}; var replaced = null; for (const key in data) { const val = data[key]; for (const converter of default_firestore_model_field_value_converter_1.defaultConverters) { replaced = converter.convertFrom(key, val, data); console.log(`Convert(${converter.type}): ${key} : ${val} to ${replaced}`); if (replaced !== null) { break; } } if (replaced !== null) { for (const k in replaced) { const v = replaced[k]; update[k] = v; } } else { update[key] = val; } } return update; } //# sourceMappingURL=algolia.js.map