-
Notifications
You must be signed in to change notification settings - Fork 43
/
SchemaHandler.js
93 lines (77 loc) · 3.28 KB
/
SchemaHandler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { dataType } from 'grakn-client';
let tx;
function SchemaHandler(graknTx) {
tx = graknTx;
}
function toGraknDatatype(dataTypeParam) {
switch (dataTypeParam) {
case 'string': return dataType.STRING;
case 'date': return dataType.DATE;
case 'boolean': return dataType.BOOLEAN;
case 'long': return dataType.LONG;
case 'double': return dataType.DOUBLE;
default: throw new Error(`Datatype not recognised. Received [${dataTypeParam}]`);
}
}
SchemaHandler.prototype.defineEntityType = async function define({ entityLabel, superType }) {
const type = await tx.putEntityType(entityLabel);
const directSuper = await tx.getSchemaConcept(superType);
await type.sup(directSuper);
};
SchemaHandler.prototype.defineRole = async function define({ roleLabel, superType }) {
const type = await tx.putRole(roleLabel);
const directSuper = await tx.getSchemaConcept(superType);
await type.sup(directSuper);
return type;
};
SchemaHandler.prototype.defineRelationType = async function define({ relationLabel, superType }) {
const type = await tx.putRelationType(relationLabel);
const directSuper = await tx.getSchemaConcept(superType);
await type.sup(directSuper);
return type;
};
SchemaHandler.prototype.defineAttributeType = async function define({ attributeLabel, superType, dataType }) {
const type = await tx.putAttributeType(attributeLabel, toGraknDatatype(dataType));
const directSuper = await tx.getSchemaConcept(superType);
await type.sup(directSuper);
};
SchemaHandler.prototype.defineRule = async function define({ ruleLabel, when, then }) {
return tx.putRule(ruleLabel, when, then);
};
SchemaHandler.prototype.deleteType = async function deleteType({ label }) {
const type = await tx.getSchemaConcept(label);
await type.delete();
return type.id;
};
SchemaHandler.prototype.addAttribute = async function addAttribute({ schemaLabel, attributeLabel }) {
const type = await tx.getSchemaConcept(schemaLabel);
const attribute = await tx.getSchemaConcept(attributeLabel);
return type.has(attribute);
};
SchemaHandler.prototype.deleteAttribute = async function deleteAttribute({ label, attributeLabel }) {
const type = await tx.getSchemaConcept(label);
const attribute = await tx.getSchemaConcept(attributeLabel);
return type.unhas(attribute);
};
SchemaHandler.prototype.addPlaysRole = async function addPlaysRole({ schemaLabel, roleLabel }) {
const type = await tx.getSchemaConcept(schemaLabel);
const role = await tx.getSchemaConcept(roleLabel);
return type.plays(role);
};
SchemaHandler.prototype.deletePlaysRole = async function deletePlaysRole({ label, roleLabel }) {
const type = await tx.getSchemaConcept(label);
const role = await tx.getSchemaConcept(roleLabel);
return type.unplay(role);
};
SchemaHandler.prototype.addRelatesRole = async function addRelatesRole({ schemaLabel, roleLabel }) {
const relationType = await tx.getSchemaConcept(schemaLabel);
const role = await tx.getSchemaConcept(roleLabel);
return relationType.relates(role);
};
SchemaHandler.prototype.deleteRelatesRole = async function deleteRelatesRole({ label, roleLabel }) {
const relationType = await tx.getSchemaConcept(label);
const role = await tx.getSchemaConcept(roleLabel);
await relationType.unrelate(role);
return role.delete();
};
export default SchemaHandler;