shaman-website-compiler
Version:
Compile raw HTML, CSS and Javascript into the smallest possible, SEO friendly website.
63 lines • 2.89 kB
JavaScript
;
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 });
exports.MongoAdapter = void 0;
require("reflect-metadata");
var mongodb_1 = require("mongodb");
var inversify_1 = require("inversify");
var MongoAdapter = /** @class */ (function () {
function MongoAdapter(config) {
var _this = this;
this.config = config;
/* istanbul ignore next */
this.openConnection = function () {
var client = new mongodb_1.MongoClient(_this.config.mongoUri, _this.config.options);
return client.connect().then(function (_) { return client.db(); }).then(function (db) {
_this.client = db;
});
};
this.run = function (query) {
return _this.open().then(function (_) { return _this.client.collection(query.path); })
.then(function (collection) { return collection.find(query.args).toArray(); })
.then(function (result) {
if (query.sort)
result = _this.sortEntities(query, result);
if (query.limit)
result = _this.limitEntities(query, result);
return result;
});
};
this.open = function () {
if (_this.client)
return Promise.resolve();
return _this.openConnection();
};
this.sortEntities = function (query, entities) {
var key = query.sort.key;
var result = entities.sort(function (a, b) { return a[key] < b[key] ? -1 : 1; });
if (query.sort.descending)
result = result.reverse();
return result;
};
this.limitEntities = function (query, entities) {
if (entities.length <= query.limit)
return entities;
return entities.slice(0, query.limit);
};
}
MongoAdapter = __decorate([
(0, inversify_1.injectable)(),
__metadata("design:paramtypes", [Object])
], MongoAdapter);
return MongoAdapter;
}());
exports.MongoAdapter = MongoAdapter;
//# sourceMappingURL=mongo.adapter.js.map