neo4-js
Version:
Neo4j graphdb object graph mapper for javascript
137 lines (136 loc) • 5.73 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 index_1 = require("./index");
const Model_1 = require("./Model");
function getRelationString(label, relationType, variable = "") {
return `${!relationType.out && !relationType.any ? "<" : ""}-[${variable}:${label}]-${!relationType.out && !relationType.any ? "" : ">"}`;
}
function get(instance, label, relationType, charGenerator, propertyName) {
if (charGenerator) {
// Return this to build a query with include
const variable = charGenerator.next();
const relationVariable = charGenerator.next();
const relationString = getRelationString(label, relationType, relationVariable);
return {
model: this.dest,
fakeInstance: Model_1.createFakeModelInstance(this.dest, charGenerator),
match: `${relationString}(${variable}:${this.dest.label})`,
where: [],
flatProps: [],
result: [variable],
variable,
relationVariable,
propertyName,
hasOne: true,
};
}
const relationString = getRelationString(label, relationType);
return index_1.default
.run(`
MATCH (a:${this.src.label} {guid:{_srcGuid}})${relationString}(b:${this.dest.label})
RETURN b
`, { _srcGuid: instance.props.guid })
.then(result => {
if (result.length === 0) {
return null;
}
if (result.length > 1) {
throw new Error("hasOne has more than one relations");
}
return Model_1.createModelInstance(this.dest, result[0].b);
});
}
exports.get = get;
function create(instance, label, relationType, props) {
return __awaiter(this, void 0, void 0, function* () {
const destInstance = yield this.dest.create(props);
const relationString = getRelationString(label, relationType);
const result = yield index_1.default.run(`
MATCH (a:${this.src.label} {guid:{srcGuid}})${relationString}(b:${this.dest.label})
DETACH DELETE b
`, { srcGuid: instance.props.guid });
yield index_1.default.run(`
MATCH
(a:${this.src.label} {guid:{srcGuid}}),
(b:${this.dest.label} {guid:{destGuid}})
MERGE (a)${relationString}(b)
`, { srcGuid: instance.props.guid, destGuid: destInstance.props.guid });
return Promise.resolve(destInstance);
});
}
exports.create = create;
function remove(instance, label, relationType) {
return __awaiter(this, void 0, void 0, function* () {
const relationString = getRelationString(label, relationType, "c");
const result = yield index_1.default.run(`
MATCH (a:${this.src.label} {guid:{srcGuid}})${relationString}(b:${this.dest.label})
DELETE c
`, { srcGuid: instance.props.guid });
return Promise.resolve(result._stats);
});
}
exports.remove = remove;
function add(instance, label, relationType, destInstance) {
return __awaiter(this, void 0, void 0, function* () {
const relationString = getRelationString(label, relationType);
const result = yield index_1.default.run(`
MATCH
(a:${this.src.label} {guid:{srcGuid}}),
(b:${this.dest.label} {guid:{destGuid}})
MERGE (a)${relationString}(b)
`, { srcGuid: instance.props.guid, destGuid: destInstance.props.guid });
if (result._stats.relationshipsCreated === 1) {
return Promise.resolve(true);
}
return Promise.resolve(false);
});
}
exports.add = add;
function hasOne(instance, label, relationType) {
return __awaiter(this, void 0, void 0, function* () {
const relationString = getRelationString(label, relationType);
const result = yield index_1.default.run(`
MATCH
(a:${this.src.label} {guid:{srcGuid}})${relationString}(b:${this.dest.label})
RETURN b
`, { srcGuid: instance.props.guid });
if (result.length === 1) {
return Promise.resolve(true);
}
else if (result.length > 1) {
return Promise.reject(new Error("HasOne relation has more than one relations"));
}
return Promise.resolve(false);
});
}
exports.hasOne = hasOne;
function update(instance, label, relationType, newProps) {
return __awaiter(this, void 0, void 0, function* () {
const relationString = getRelationString(label, relationType);
let props = yield index_1.default.run(`
MATCH (a:${this.src.label} {guid:{srcGuid}})${relationString}(b:${this.dest.label})
RETURN b
`, { srcGuid: instance.props.guid });
if (props.length === 0) {
return Promise.resolve(true);
}
else if (props.length > 1) {
return Promise.reject(new Error("HasOne relation has more than one relations"));
}
props = props[0].b;
const destInstance = yield this.dest.update({ guid: props.guid }, newProps);
if (destInstance.length === 1) {
return Promise.resolve(destInstance[0]);
}
return Promise.reject(new Error("Problem with the update"));
});
}
exports.update = update;
;