@adonisjs/lucid
Version:
SQL ORM built on top of Active Record pattern
71 lines (70 loc) • 1.93 kB
JavaScript
;
/*
* @adonisjs/lucid
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseRelation = void 0;
/**
* Base relation to be extended by other factory relations
*/
class BaseRelation {
constructor(factory) {
Object.defineProperty(this, "factory", {
enumerable: true,
configurable: true,
writable: true,
value: factory
});
Object.defineProperty(this, "ctx", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "attributes", {
enumerable: true,
configurable: true,
writable: true,
value: {}
});
Object.defineProperty(this, "parent", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
}
/**
* Instantiates the relationship factory
*/
compile(relation, parent, callback) {
this.parent = parent;
const builder = this.factory().query(undefined, relation);
if (typeof callback === 'function') {
callback(builder);
}
builder.useCtx(this.ctx).mergeRecursive(this.attributes);
return builder;
}
/**
* Merge attributes with the relationship and its children
*/
merge(attributes) {
this.attributes = attributes;
return this;
}
/**
* Use custom ctx. This must always be called by the factory, otherwise
* `make` and `create` calls will fail.
*/
useCtx(ctx) {
this.ctx = ctx;
return this;
}
}
exports.BaseRelation = BaseRelation;