-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (54 loc) · 2.19 KB
/
index.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
const orbitjsToFortuneModel = (orbitModel, orbitModelName = '') => {
let fortuneModel = {};
Object.keys(orbitModel.attributes).map(attributeName => {
const field = orbitModel.attributes[attributeName];
switch (field.type) {
case 'boolean':
fortuneModel[attributeName] = Boolean;
break;
case 'date':
case 'date-time':
fortuneModel[attributeName] = Date;
break;
case 'number':
fortuneModel[attributeName] = Number;
break;
case 'string':
fortuneModel[attributeName] = String;
break;
default:
throw new Error(`${orbitModelName}.attributes[${attributeName}]: Unrecognized attribute type "${field.type}"`);
}
});
Object.keys(orbitModel.relationships).map(relationshipName => {
const relation = orbitModel.relationships[relationshipName];
switch (relation.type) {
case 'hasOne':
if (relation.inverse) {
fortuneModel[relationshipName] = [relation.model, relation.inverse];
} else {
fortuneModel[relationshipName] = relation.model;
}
break;
case 'hasMany':
if (relation.inverse) {
fortuneModel[relationshipName] = [Array(relation.model), relation.inverse];
} else {
fortuneModel[relationshipName] = Array(relation.model);
}
break;
default:
throw new Error(`${orbitModelName}.relationships[${relationshipName}]: Unrecognized relationship type "${relation.type}"`);
}
});
return fortuneModel;
};
const orbitjsToFortuneSchema = (orbitSchema) => Object.keys(orbitSchema)
.reduce((fortuneSchema, orbitModelName) => {
fortuneSchema[orbitModelName] = orbitjsToFortuneModel(orbitSchema[orbitModelName], orbitModelName);
return fortuneSchema;
}, {});
module.exports = {
orbitjsToFortuneModel,
orbitjsToFortuneSchema,
};