octonom
Version:
Object Document Mapper for any database
46 lines • 1.91 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = require("lodash");
const collection_1 = require("./collection");
// simple collection with an in-memory array
// note: we can't test Collection directly since it's abstract
class ArrayCollection extends collection_1.Collection {
constructor() {
super(...arguments);
this.array = [];
}
clear() {
this.array.splice(0, this.array.length);
}
insert(model, options = {}) {
return __awaiter(this, void 0, void 0, function* () {
if (options.validate !== false) {
yield model.validate();
}
const doc = lodash_1.find(this.array, { [this.modelIdField]: model[this.modelIdField] });
if (doc) {
throw new Error('duplicate key error');
}
this.array.push(this.toDb(model));
});
}
findById(id) {
return __awaiter(this, void 0, void 0, function* () {
const doc = lodash_1.find(this.array, { [this.modelIdField]: id });
if (!doc) {
return undefined;
}
return this.fromDb(doc);
});
}
}
exports.ArrayCollection = ArrayCollection;
//# sourceMappingURL=array-collection.js.map