UNPKG

@mathrunet/masamune

Version:

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

95 lines 3.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FirestoreModelTimestampConverter = void 0; const firestore_model_field_value_converter_1 = require("../firestore_model_field_value_converter"); const utils_1 = require("../../utils"); const firestore_1 = require("firebase-admin/firestore"); /** * FirestoreConverter for [ModelTimestamp]. * * [ModelTimestamp]用のFirestoreConverter。 */ class FirestoreModelTimestampConverter extends firestore_model_field_value_converter_1.FirestoreModelFieldValueConverter { /** * FirestoreConverter for [ModelTimestamp]. * * [ModelTimestamp]用のFirestoreConverter。 */ constructor() { super(); this.type = "ModelTimestamp"; } convertFrom(key, value, original) { var _a, _b, _c, _d, _e, _f, _g, _h; if (typeof value === "number") { const targetKey = `#${key}`; const targetMap = (_a = original[targetKey]) !== null && _a !== void 0 ? _a : {}; const type = (_b = targetMap["@type"]) !== null && _b !== void 0 ? _b : ""; if (type == this.type) { return { [key]: value, }; } } else if (value instanceof firestore_1.Timestamp) { const targetKey = `#${key}`; const targetMap = (_c = original[targetKey]) !== null && _c !== void 0 ? _c : {}; const type = (_d = targetMap["@type"]) !== null && _d !== void 0 ? _d : ""; if (type == this.type) { return { [key]: value.toMillis(), }; } } else if (Array.isArray(value)) { const targetKey = `#${key}`; const targetList = (_e = original[targetKey]) !== null && _e !== void 0 ? _e : []; if (targetList != null && targetList.length > 0 && targetList.every((e) => e["@type"] === this.type)) { const res = []; for (const tmp of value) { if (typeof tmp === "number") { res.push(tmp); } else if (tmp instanceof firestore_1.Timestamp) { res.push(tmp.toMillis()); } } if (res.length > 0) { return { [key]: res, }; } } } else if ((0, utils_1.isDynamicMap)(value)) { const targetKey = `#${key}`; const targetMap = (_f = original[targetKey]) !== null && _f !== void 0 ? _f : {}; targetMap; if (targetMap != null) { const res = {}; for (const key in value) { const val = value[key]; const mapVal = (_g = targetMap[key]) !== null && _g !== void 0 ? _g : {}; const type = (_h = mapVal["@type"]) !== null && _h !== void 0 ? _h : ""; if (type != this.type) { continue; } if (typeof val === "number") { res[key] = val; } else if (val instanceof firestore_1.Timestamp) { res[key] = val.toMillis(); } } if (Object.keys(res).length > 0) { return { [key]: res, }; } } } return null; } } exports.FirestoreModelTimestampConverter = FirestoreModelTimestampConverter; //# sourceMappingURL=firestore_model_timestamp_converter.js.map