kakapo
Version:
Next generation mocking framework in Javascript
157 lines (156 loc) • 6.08 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var lodash_sample_1 = __importDefault(require("lodash.sample"));
var lodash_first_1 = __importDefault(require("lodash.first"));
var lodash_last_1 = __importDefault(require("lodash.last"));
var lodash_filter_1 = __importDefault(require("lodash.filter"));
var Database = (function () {
function Database() {
this.collectionStore = {};
}
Database.prototype.all = function (collectionName) {
var records = this.getCollection(collectionName).records;
return records;
};
Database.prototype.belongsTo = function (collectionName, conditions) {
var _this = this;
return function () {
if (conditions) {
return _this.findOne(collectionName, conditions);
}
else {
return lodash_sample_1.default(_this.all(collectionName));
}
};
};
Database.prototype.create = function (collectionName, size, factory) {
if (size === void 0) { size = 1; }
var dataFactory = factory || this.getCollection(collectionName).factory;
var records = [];
for (var index = 0; index < size; index++) {
var data = dataFactory();
var record = this.push(collectionName, data);
records.push(record);
}
return records;
};
Database.prototype.delete = function (collectionName, id) {
var collection = this.getCollection(collectionName);
var records = collection.records;
var record = records.reduce(function (result, record) { return record.id === id ? record : result; }, undefined);
if (record) {
var index = records.indexOf(record);
records.splice(index, 1);
return record;
}
else {
return null;
}
};
Database.prototype.exists = function (collectionName) {
return !!this.collectionStore[collectionName];
};
Database.prototype.find = function (collectionName, conditions) {
var records = this.getCollection(collectionName).records;
return lodash_filter_1.default(records, { data: conditions });
};
Database.prototype.findOne = function (collectionName, conditions) {
return lodash_first_1.default(this.find(collectionName, conditions));
};
Database.prototype.first = function (collectionName) {
var records = this.getCollection(collectionName).records;
return lodash_first_1.default(records);
};
Database.prototype.last = function (collectionName) {
var records = this.getCollection(collectionName).records;
return lodash_last_1.default(records);
};
Database.prototype.push = function (collectionName, data) {
var collection = this.getCollection(collectionName);
var uuid = collection.uuid, records = collection.records;
var record = {
id: uuid,
data: data
};
records.push(record);
collection.uuid++;
return record;
};
Database.prototype.register = function (collectionName, factory) {
this.collectionStore[collectionName] = {
uuid: 0,
records: [],
factory: factory
};
};
Database.prototype.reset = function () {
this.collectionStore = {};
};
Database.prototype.update = function (collectionName, id, data) {
var collection = this.getCollection(collectionName);
var records = collection.records;
var oldRecord = this.delete(collectionName, id);
if (oldRecord) {
var record = __assign({}, oldRecord, { data: __assign({}, oldRecord.data, data) });
records.push(record);
return record;
}
else {
throw new RecordNotFoundError(collectionName, id);
}
};
Database.prototype.getCollection = function (collectionName) {
var collection = this.collectionStore[collectionName];
if (collection) {
return collection;
}
else {
throw new CollectionNotFoundError(collectionName);
}
};
return Database;
}());
exports.Database = Database;
var CollectionNotFoundError = (function (_super) {
__extends(CollectionNotFoundError, _super);
function CollectionNotFoundError(collectionName) {
return _super.call(this, "Collection " + collectionName + " not found") || this;
}
return CollectionNotFoundError;
}(Error));
exports.CollectionNotFoundError = CollectionNotFoundError;
var RecordNotFoundError = (function (_super) {
__extends(RecordNotFoundError, _super);
function RecordNotFoundError(collectionName, id) {
return _super.call(this, "Record " + id + " not found in collection " + collectionName) || this;
}
return RecordNotFoundError;
}(Error));
exports.RecordNotFoundError = RecordNotFoundError;