neo4-js
Version:
Neo4j graphdb object graph mapper for javascript
80 lines (79 loc) • 3.8 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("./utils");
const HasMany = require("./HasManyRelation");
const HasOne = require("./HasOneRelation");
class Relation {
constructor(src, property) {
this.src = src;
this.lazy = property;
if (utils_1.lazy(property.relation)) {
this.init();
}
}
/**
* Initialise all lazy stuff
*/
init() {
const property = utils_1.lazy(this.lazy);
if (!property)
return;
const relation = utils_1.lazy(property.relation);
if (!relation)
return;
const from = utils_1.lazy(relation.from);
const to = utils_1.lazy(relation.to);
if (!from || !to)
return;
this.relationType = {
many: property.many,
out: property.out !== null ? property.out : from === this.src,
any: property.any,
};
this.dest = utils_1.lazy(from === this.src ? relation.to : relation.from);
if (!this.dest)
return;
this.propertyName = property.propertyName;
this.label = relation.via;
delete this.lazy;
}
addFunctionsToInstance(instance, charGenerator) {
if (this.lazy)
this.init();
if (this.relationType.many) {
return this.addHasManyToInstance(instance, charGenerator);
}
return this.addHasOneToInstance(instance, charGenerator);
}
addHasManyToInstance(instance, charGenerator) {
// @ts-ignore
instance[this.propertyName] = {
get: (props, relationProps = {}) => HasMany.get.bind(this)(instance, this.label, this.relationType, props, relationProps, charGenerator, this.propertyName),
update: (options, whereProps, relationProps = {}, whereRelationProps = {}) => {
const optionsInUse = options.props ||
options.whereProps ||
options.relationProps ||
options.whereRelationProps;
return HasMany.update.bind(this, instance, this.label, this.relationType)(optionsInUse ? options.props : options, options.whereProps || whereProps, options.relationProps || relationProps, options.whereRelationProps || whereRelationProps);
},
remove: (props, relationProps = {}) => HasMany.remove.bind(this, instance, this.label, this.relationType)(props, relationProps),
create: (props, relationProps = {}) => HasMany.create.bind(this, instance, this.label, this.relationType)(props, relationProps),
add: (instances, relationProps = {}) => HasMany.add.bind(this, instance, this.label, this.relationType)(instances, relationProps),
count: (props, relationProps = {}) => HasMany.count.bind(this, instance, this.label, this.relationType)(props, relationProps),
};
return instance;
}
addHasOneToInstance(instance, charGenerator) {
// @ts-ignore
instance[this.propertyName] = {
get: () => HasOne.get.bind(this)(instance, this.label, this.relationType, charGenerator, this.propertyName),
update: (props, label) => HasOne.update.bind(this, instance, this.label, this.relationType)(props),
create: (props, label) => HasOne.create.bind(this, instance, this.label, this.relationType)(props),
add: (destInstance, label) => HasOne.add.bind(this, instance, this.label, this.relationType)(destInstance),
remove: (label) => HasOne.remove.bind(this, instance, this.label, this.relationType)(),
hasOne: (label) => HasOne.hasOne.bind(this, instance, this.label, this.relationType)(),
};
return instance;
}
}
exports.Relation = Relation;