@mathrunet/masamune
Version:
Manages packages for the server portion (NodeJS) of the Masamune framework.
386 lines • 11 kB
JavaScript
"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;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.CollectionModel = exports.DocumentChangeModel = exports.QueryDocumentModel = exports.DocumentModel = void 0;
const firestore = __importStar(require("firebase-admin/firestore"));
const default_model_field_value_converter_1 = require("../model_field_value/default_model_field_value_converter");
const model_field_value_1 = require("../model_field_value/model_field_value");
const firestore_1 = require("@google-cloud/firestore");
/**
* Document model.
*
* ドキュメントモデル。
*
* @param {firestore.DocumentSnapshot<AppModelType, DbModelType>} source
* Source document snapshot.
*
* ソースドキュメントスナップショット。
*
* @param {AppModelType | undefined} data
* Data.
*
* データ。
*
* @returns {DocumentModel<AppModelType, DbModelType>}
* Document model.
*
* ドキュメントモデル。
*/
class DocumentModel {
constructor(source, data) {
this._source = source;
this._data = data;
this.exists = source.exists;
this.ref = source.ref;
this.id = source.id;
this.createTime = source.createTime;
this.updateTime = source.updateTime;
this.readTime = source.readTime;
}
data() {
return this._data;
}
get(fieldPath) {
return this._source.get(fieldPath);
}
isEqual(other) {
return this._source.isEqual(other);
}
_data;
_source;
exists;
ref;
id;
createTime;
updateTime;
readTime;
}
exports.DocumentModel = DocumentModel;
/**
* Query document model.
*
* クエリドキュメントモデル。
*
* @param {firestore.QueryDocumentSnapshot<AppModelType, DbModelType>} source
* Source query document snapshot.
*
* ソースクエリドキュメントスナップショット。
*
* @param {AppModelType | undefined} data
* Data.
*
* データ。
*
* @returns {QueryDocumentModel<AppModelType, DbModelType>}
* Query document model.
*
* クエリドキュメントモデル。
*/
class QueryDocumentModel extends DocumentModel {
constructor(source, data) {
super(source, data);
this.createTime = source.createTime;
this.updateTime = source.updateTime;
}
data() {
return this._data ?? {};
}
get(fieldPath) {
return this._source.get(fieldPath);
}
isEqual(other) {
return this._source.isEqual(other);
}
createTime;
updateTime;
}
exports.QueryDocumentModel = QueryDocumentModel;
/**
* Document change model.
*
* ドキュメント変更モデル。
*
* @param {firestore.DocumentChange<AppModelType, DbModelType>} source
* Source document change.
*
* ソースドキュメント変更。
*
* @param {QueryDocumentModel<AppModelType, DbModelType>} doc
* Document model.
*
* ドキュメントモデル。
*
* @returns {DocumentChangeModel<AppModelType, DbModelType>}
* Document change model.
*
* ドキュメント変更モデル。
*/
class DocumentChangeModel {
constructor(source, doc) {
this._source = source;
this.type = source.type;
this.doc = doc;
this.oldIndex = source.oldIndex;
this.newIndex = source.newIndex;
}
isEqual(other) {
return this._source.isEqual(other);
}
_source;
type;
doc;
oldIndex;
newIndex;
}
exports.DocumentChangeModel = DocumentChangeModel;
/**
* Collection model.
*
* コレクションモデル。
*
* @param {firestore.QuerySnapshot<AppModelType, DbModelType>} source
* Source query snapshot.
*
* ソースクエリスナップショット。
*
* @param {Array<QueryDocumentModel<AppModelType, DbModelType>>} data
* Data.
*
* データ。
*
* @param {DocumentChangeModel<AppModelType, DbModelType>[]} changes
* Changes.
*
* 変更内容。
*
* @returns {CollectionModel<AppModelType, DbModelType>}
* Collection model.
*
* コレクションモデル。
*/
class CollectionModel {
constructor(source, data, changes) {
this._source = source;
this.docs = data;
this.query = source.query;
this.size = source.size;
this.empty = source.empty;
this.readTime = source.readTime;
this._changes = changes;
}
_changes;
_source;
docChanges() {
return this._changes;
}
forEach(callback, thisArg) {
for (const doc of this.docs) {
callback(doc);
}
}
isEqual(other) {
return this._source.isEqual(other);
}
query;
docs;
size;
empty;
readTime;
}
exports.CollectionModel = CollectionModel;
/**
* Load the document.
*
* ドキュメントを読み込みます。
*/
firestore.DocumentReference.prototype.load = async function () {
const result = await this.get();
const data = result.data() ?? {};
const firestoreInstance = result.ref.firestore;
const converted = default_model_field_value_converter_1.ModelFieldValueConverterUtils.convertFrom({
data: default_model_field_value_converter_1.FirestoreModelFieldValueConverterUtils.convertFrom({
data: data,
firestoreInstance: firestoreInstance,
}),
});
return new DocumentModel(result, converted);
};
/**
* Save the document.
*
* ドキュメントを保存します。
*
* @param {firestore.PartialWithFieldValue<T>} data
* Data to save.
*
* 保存するデータ。
*
* @param {firestore.SetOptions} options
* Options for saving.
*
* 保存オプション。
*/
firestore.DocumentReference.prototype.save = async function (data) {
const update = data ?? {};
const firestoreInstance = this.firestore;
const converted = default_model_field_value_converter_1.FirestoreModelFieldValueConverterUtils.convertTo({
data: default_model_field_value_converter_1.ModelFieldValueConverterUtils.convertTo({
data: update,
}),
firestoreInstance: firestoreInstance,
});
return await this.set({
...converted,
"@uid": this.id,
"@time": new Date(),
});
};
/**
* Save the document.
*
* ドキュメントを保存します。
*
* @param {firestore.PartialWithFieldValue<T>} data
* Data to save.
*
* 保存するデータ。
*
* @param {firestore.SetOptions} options
* Options for saving.
*
* 保存オプション。
*/
firestore.DocumentReference.prototype.save = async function (data, options) {
const update = data ?? {};
const firestoreInstance = this.firestore;
const converted = default_model_field_value_converter_1.FirestoreModelFieldValueConverterUtils.convertTo({
data: default_model_field_value_converter_1.ModelFieldValueConverterUtils.convertTo({
data: update,
}),
firestoreInstance: firestoreInstance,
});
return await this.set({
...converted,
"@uid": this.id,
"@time": new Date(),
}, options);
};
/**
* Load the collection.
*
* コレクションを読み込みます。
*/
firestore.Query.prototype.load = async function () {
const result = await this.get();
const docs = [];
const changes = [];
for (const doc of result.docs) {
const data = doc.data() ?? {};
const firestoreInstance = doc.ref.firestore;
const converted = default_model_field_value_converter_1.ModelFieldValueConverterUtils.convertFrom({
data: default_model_field_value_converter_1.FirestoreModelFieldValueConverterUtils.convertFrom({
data: data,
firestoreInstance: firestoreInstance,
}),
});
docs.push(new QueryDocumentModel(doc, converted));
}
for (const doc of result.docChanges()) {
const data = doc.doc.data() ?? {};
const firestoreInstance = doc.doc.ref.firestore;
const converted = default_model_field_value_converter_1.ModelFieldValueConverterUtils.convertFrom({
data: default_model_field_value_converter_1.FirestoreModelFieldValueConverterUtils.convertFrom({
data: data,
firestoreInstance: firestoreInstance,
}),
});
changes.push(new DocumentChangeModel(doc, new QueryDocumentModel(doc.doc, converted)));
}
return new CollectionModel(result, docs, changes);
};
/**
* Convert to ModelRefBase.
*
* ModelRefBaseに変換します。
*
* @returns {ModelRefBase}
* ModelRefBase.
*
* ModelRefBase。
*/
firestore.DocumentReference.prototype.toModelRefBase = function () {
const path = this.path.replace(/\/+$/, "");
return new model_field_value_1.ModelRefBase(path, this.firestore.doc(path));
};
/**
* Convert to ModelTimestamp.
*
* ModelTimestampに変換します。
*
* @returns {ModelTimestamp}
* ModelTimestamp.
*
* ModelTimestamp。
*/
firestore.Timestamp.prototype.toModelTimestamp = function () {
return new model_field_value_1.ModelTimestamp(this.toDate());
};
/**
* Convert to ModelGeoValue.
*
* ModelGeoValueに変換します。
*
* @returns {ModelGeoValue}
* ModelGeoValue.
*
* ModelGeoValue。
*/
firestore.GeoPoint.prototype.toModelGeoValue = function () {
return new model_field_value_1.ModelGeoValue(this.latitude, this.longitude);
};
/**
* Convert to ModelVectorValue.
*
* ModelVectorValueに変換します。
*
* @returns {ModelVectorValue}
* ModelVectorValue.
*
* ModelVectorValue。
*/
firestore_1.VectorValue.prototype.toModelVectorValue = function () {
return new model_field_value_1.ModelVectorValue(this.toArray());
};
//# sourceMappingURL=firestore.extension.js.map