@decaf-ts/db-decorators
Version:
Agnostic database decorators and repository
67 lines (66 loc) • 1.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Context = void 0;
const DataCache_1 = require("./DataCache.cjs");
const errors_1 = require("./errors.cjs");
class Context extends DataCache_1.DataCache {
constructor(operation, model, parent) {
super();
this.operation = operation;
this.model = model;
this.parent = parent;
}
get timestamp() {
return new Date();
}
async get(key) {
try {
return super.get(key);
}
catch (e) {
if (this.parent)
return this.parent.get(key);
throw e;
}
}
async pop(key) {
if (key in this.cache)
return super.pop(key);
if (!this.parent)
throw new errors_1.NotFoundError(`Key ${key} not in dataStore`);
return this.parent.pop(key);
}
child(operation, model) {
return this.constructor(operation, model, this);
}
static async from(operation, model,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
...args) {
return new Context(operation, model);
}
static async args(operation, model, args, contextual) {
const last = args.pop();
async function getContext() {
if (contextual)
return contextual.context(operation, model, ...args);
return new Context(operation, model);
}
let c;
if (last) {
if (last instanceof Context) {
c = last;
args.push(last);
}
else {
c = await getContext();
args.push(last, c);
}
}
else {
c = await getContext();
args.push(c);
}
return { context: c, args: args };
}
}
exports.Context = Context;