tabel
Version:
A simple orm for PostgreSQL which works with simple javascript objects and arrays
98 lines (78 loc) • 2.02 kB
JavaScript
/**
* Already used methods:
* - setName
* - forModel
* - constrain
* - eagerLoad
* - load
*/
const {isString} = require('lodash');
const Scope = require('../Scope');
const Track = require('../Track');
class Relation {
constructor(ownerTable) {
this.ownerTable = ownerTable;
this.constraints = new Track();
this.activeModel = null;
this.relationName = null;
}
setName(relationName) {
this.relationName = relationName;
return this;
}
forModel(model) {
this.activeModel = model;
return this;
}
constrain(constraint, label='constraint') {
this.constraints.push(new Scope(constraint, label));
return this;
}
eagerLoad(...args) {
return this.constrain((t) => t.eagerLoad(...args), 'eagerLoad');
}
load(fromModels=[]) {
if (fromModels.length === 0) {
return Promise.resolve(fromModels);
}
return this.getRelated(fromModels).then((relatedModels) => {
return this.matchModels(this.initRelation(fromModels), relatedModels);
});
}
initRelation(fromModels=[]) {
throw new Error('not implemented');
}
getRelated(fromModels=[]) {
throw new Error('not implemented');
}
matchModels(fromModels=[], relatedModels=[]) {
throw new Error('not implemented');
}
jointLabel(label, {isLeftJoin=false}) {
return `${
isLeftJoin ? 'leftJoin' : 'join'
}.${this.constructor.name}.${this.relationName}${
isString(label) ? `.${label}` : ''
}`;
}
pivotJointLabel(label, {isLeftJoin=false}) {
return `${this.jointLabel(label, {isLeftJoin})}.pivot${
isString(label) ? `.${label}` : ''
}`;
}
throughJointLabel(label, {isLeftJoin=false}) {
return `${this.jointLabel(label, {isLeftJoin})}.through${
isString(label) ? `.${label}` : ''
}`;
}
join() {
throw new Error('not implemented');
}
joinPivot() {
throw new Error('not imeplemented');
}
joinThrough() {
throw new Error('not imeplemented');
}
}
module.exports = Relation;