UNPKG

simpleddp-node

Version:

The aim of this library is to simplify the process of working with meteor server over DDP protocol using external JS environments

112 lines (111 loc) 3.61 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ddpCollection = void 0; const ejson_1 = __importDefault(require("ejson")); const fullCopy_js_1 = require("../helpers/fullCopy.js"); const ddpOnChange_js_1 = require("./ddpOnChange.js"); const ddpReactiveCollection_1 = require("./ddpReactiveCollection"); /** * DDP collection class. */ class ddpCollection { _filter = false; _name; _server; ddpConnection; constructor(name, server) { this._name = name; this._server = server; } /** * Allows to specify specific documents inside the collection for reactive data and fetching. * Important: if you change filter function it won't change for the already created reactive objects. */ filter(f = false) { this._filter = f; return this; } /** * Imports data inside the collection and emits all relevant events. * Both string and JS object types are supported. */ importData(data) { const c = typeof data === 'string' ? ejson_1.default.parse(data) : data; if (c[this._name]) { c[this._name].forEach((doc, i, arr) => { // @ts-ignore if (!this._filter || (this._filter && typeof this._filter === 'function' && this._filter(doc, i, arr))) { this.ddpConnection.emit('added', { msg: 'added', _id: doc._id, collection: this._name, fields: doc.fields }); } }); } } /** * Exports data from the collection. */ exportData(format) { const collectionCopy = { [this._name]: this.fetch() }; if (format == 'raw') { return collectionCopy; } return ejson_1.default.stringify(collectionCopy); } /** * Returns collection data based on filter and on passed settings. Supports skip, limit and sort. * Order is 'filter' then 'sort' then 'skip' then 'limit'. * sort is a standard js array sort function. */ fetch(settings) { let skip; let limit; let sort; if (settings) { skip = settings.skip; limit = settings.limit; sort = settings.sort; } const c = this._server.collections[this._name]; let collectionCopy = c ? (0, fullCopy_js_1.fullCopy)(c) : []; if (this._filter) collectionCopy = collectionCopy.filter(this._filter); if (sort) collectionCopy.sort(sort); if (typeof skip === 'number') collectionCopy.splice(0, skip); if (typeof limit === 'number' || limit == Infinity) collectionCopy.splice(limit); return collectionCopy; } /** * Returns reactive collection object. * @see ddpReactiveCollection */ reactive(settings) { return new ddpReactiveCollection_1.ddpReactiveCollection(this, settings, this._filter); } /** * Returns change observer. * @see ddpOnChange */ onChange(f, filter) { const obj = { collection: this._name, f, filter }; if (this._filter) obj.filter = this._filter; if (filter) obj.filter = filter; return (0, ddpOnChange_js_1.ddpOnChange)(obj, this._server); } } exports.ddpCollection = ddpCollection;