UNPKG

@gecogvidanto/plugin-nedb

Version:

Nebd local database management plugin for ĞecoĞvidanto

117 lines 5.34 kB
"use strict"; /* * This file is part of @gecogvidanto/plugin-nedb. * Copyright (C) 2020 Stéphane Veyret * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. */ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); const bindata_1 = require("./bindata"); /** * A nedb collection (datastore in nedb vocabulary). */ class NedbDatastore { constructor(db, name, compactInterval, binaryStore) { this.db = db; this.name = name; this.compactInterval = compactInterval; this.binaryStore = binaryStore; this.nextCompact = compactInterval; } static checkCompact(_target, _propertyKey, descriptor) { const originalMethod = descriptor.value; descriptor.value = async function (...args) { const result = await originalMethod.apply(this, args); if (--this.nextCompact <= 0) { setImmediate(() => { this.db.db.persistence.compactDatafile(); }); this.nextCompact = this.compactInterval; } return result; }; return descriptor; } async insert(newDoc) { const binManager = new bindata_1.BinaryDataManager(this.binaryStore, this.name); const filtered = await binManager.inputFilter(newDoc); const result = (await this.db.insert(filtered)); if (binManager.transformed) { await binManager.save(result._id); } return binManager.outputFilter(result); } async count(query) { const binManager = new bindata_1.BinaryDataManager(this.binaryStore, this.name); const filtered = await binManager.inputFilter(query); return this.db.count(filtered); } async find(query, projection) { const binManager = new bindata_1.BinaryDataManager(this.binaryStore, this.name); const filtered = await binManager.inputFilter(query); const result = await this.db.find(filtered, projection); return binManager.outputFilterAll(result); } async findOne(query, projection) { const binManager = new bindata_1.BinaryDataManager(this.binaryStore, this.name); const filtered = await binManager.inputFilter(query); const result = await this.db.findOne(filtered, projection); return result ? binManager.outputFilter(result) : undefined; } async update(query, updateQuery, upsert = false) { const options = { upsert, multi: !upsert }; const binManager = new bindata_1.BinaryDataManager(this.binaryStore, this.name); const filteredUpdate = await binManager.inputFilter(updateQuery); if (binManager.transformed) { const modifs = await this.find(query, { _id: 1 }); await Promise.all(modifs.map(async (modif) => binManager.save(modif._id))); } const filteredQuery = await binManager.inputFilter(query); return this.db.update(filteredQuery, filteredUpdate, options); } async remove(query) { const binManager = new bindata_1.BinaryDataManager(this.binaryStore, this.name); const filtered = await binManager.inputFilter(query); return this.db.remove(filtered, { multi: true }); } } __decorate([ NedbDatastore.checkCompact, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], NedbDatastore.prototype, "insert", null); __decorate([ NedbDatastore.checkCompact, __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Object, Object]), __metadata("design:returntype", Promise) ], NedbDatastore.prototype, "update", null); __decorate([ NedbDatastore.checkCompact, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], NedbDatastore.prototype, "remove", null); exports.default = NedbDatastore; //# sourceMappingURL=NedbDatastore.js.map