UNPKG

@mathrunet/masamune

Version:

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

347 lines 12.4 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 }); exports.where = where; exports.hasMatch = hasMatch; exports.get = get; exports.cursor = cursor; const admin = __importStar(require("firebase-admin")); /** * Create a filter for loading Firestore collections. * * Firestoreのコレクションをロードする際のフィルターを作成します。 * * @param query * Specifies a reference to a Firestore collection. * * Firestoreのコレクションのリファレンスを指定します。 * * @param wheres * Specifies the filter to be applied to the collection. * * コレクションに適用するフィルターを指定します。 * * @returns * Returns a Firestore query with the specified filter. * * 指定されたフィルターを持つFirestoreのクエリを返します。 */ function where({ query, wheres, }) { if (!wheres) { return query; } for (let w of wheres) { const type = w["type"]; const key = w["key"]; const value = w["value"]; if (type === undefined || key === undefined || type === null || key === null) { continue; } switch (type) { case "equalTo": query = query.where(key, "==", value); break; case "notEqualTo": query = query.where(key, "!=", value); break; case "lessThan": query = query.where(key, "<", value); break; case "greaterThan": query = query.where(key, ">", value); break; case "lessThanOrEqualTo": query = query.where(key, "<=", value); break; case "greaterThanOrEqualTo": query = query.where(key, ">=", value); break; case "arrayContains": query = query.where(key, "array-contains", value); break; case "arrayContainsAny": query = query.where(key, "array-contains-any", value); break; case "whereIn": query = query.where(key, "in", value); break; case "whereNotIn": query = query.where(key, "not-in", value); break; case "isNull": query = query.where(key, "==", null); break; case "isNotNull": query = query.where(key, "!=", null); break; default: break; } } return query; } /** * Judges whether all the conditions in [conditons] match the document data in [data]. * * If a reference is included in [data], the document is retrieved recursively and the condition is determined. * * [data]のドキュメントデータに対して、[conditions]の条件が全て一致するかどうかを判定します。 * * [data]の中にリファレンスが含まれている場合、再帰的にドキュメントを取得し条件を判定します。 * * @param data * Target document data. * * 対象となるドキュメントデータ。 * * @param conditions * Conditions to be matched. * * 一致させる条件。 * * @returns * Returns true if all conditions match, false otherwise. * * 全ての条件が一致する場合はtrue、それ以外はfalseを返します。 */ function hasMatch(_a) { return __awaiter(this, arguments, void 0, function* ({ data, conditions, }) { if (!conditions) { return true; } for (let c of conditions) { const type = c["type"]; const key = c["key"]; const value = c["value"]; if (key === undefined || key === null) { continue; } const source = data[key]; if (source instanceof admin.firestore.DocumentReference) { const doc = yield source.get(); const data = doc.data(); console.log(`Reference data ${JSON.stringify(data)}`); if (Array.isArray(value)) { const res = yield hasMatch({ data, conditions: value }); if (!res) { return false; } continue; } else if (_isObject(value)) { const res = yield hasMatch({ data, conditions: [value] }); if (!res) { return false; } continue; } } if (type === undefined || type === null) { continue; } switch (type) { case "equalTo": if (value === undefined || value === null) { continue; } if (source !== value) { return false; } break; case "notEqualTo": if (value === undefined || value === null) { continue; } if (source === value) { return false; } break; case "lessThan": if (value === undefined || value === null || typeof source !== "number" || typeof value !== "number") { continue; } if (source >= value) { return false; } break; case "greaterThan": if (value === undefined || value === null || typeof source !== "number" || typeof value !== "number") { continue; } if (source <= value) { return false; } break; case "lessThanOrEqualTo": if (value === undefined || value === null || typeof source !== "number" || typeof value !== "number") { continue; } if (source > value) { return false; } break; case "greaterThanOrEqualTo": if (value === undefined || value === null || typeof source !== "number" || typeof value !== "number") { continue; } if (source < value) { return false; } break; case "arrayContains": if (value === undefined || value === null || !Array.isArray(source)) { continue; } if (!source.includes(value)) { return false; } break; case "arrayContainsAny": if (value === undefined || value === null || !Array.isArray(source) || !Array.isArray(value)) { continue; } if (!source.some(v => value.includes(v))) { return false; } break; case "whereIn": if (value === undefined || value === null || !Array.isArray(value)) { continue; } if (!value.includes(source)) { return false; } break; case "whereNotIn": if (value === undefined || value === null || !Array.isArray(value)) { continue; } if (value.includes(source)) { return false; } break; case "isNull": if (source !== undefined && source !== null) { return false; } break; case "isNotNull": if (!(source !== undefined && source !== null)) { return false; } break; default: break; } } return true; }); } /** * Get the value of the specified field from the document data. * * ドキュメントデータから指定されたフィールドの値を取得します。 * * @param data * Target document data. * * 対象となるドキュメントデータ。 * * @param field * Specifies the field to be retrieved. * * 取得するフィールドを指定します。 * * @returns * Returns the value of the specified field. * * 指定されたフィールドの値を返します。 */ function get(_a) { return __awaiter(this, arguments, void 0, function* ({ data, field, }) { if (typeof field === "string") { return data[field]; } const key = field["key"]; const source = data[key]; if (source instanceof admin.firestore.DocumentReference) { const doc = yield source.get(); const data = doc.data(); return get({ data, field: field["value"] }); } return source; }); } /** * Get [limit] documents from [cursor]. * * [cursor]から[limit]個のドキュメントを取得します。 * * @param query * Specifies a reference to a Firestore collection. * * Firestoreのコレクションのリファレンスを指定します。 * * @param limit * Specifies the number of documents to be retrieved. * * 取得するドキュメントの数を指定します。 * * @param cursor * Specifies the document to start retrieving from. * * 取得を開始するドキュメントを指定します。 * * @returns */ function cursor({ query, limit, cursor, }) { if (!cursor) { return query.limit(limit); } return query.startAfter(cursor).limit(limit); } function _isObject(obj) { return typeof obj === "object" && obj !== null; } //# sourceMappingURL=firestore.js.map