-
Notifications
You must be signed in to change notification settings - Fork 50
/
model.js
163 lines (142 loc) · 3.39 KB
/
model.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { reduce, reduceRight, merge } from 'lodash';
import mongoose from 'mongoose';
const embeddedModels = {};
/**
* @method getField
* @param schemaPaths
* @return {Object} field
*/
function getField(schemaPath) {
const {
description,
hidden,
hooks,
ref,
index
} = schemaPath.options || {};
const name = schemaPath.path.split('.').pop();
const field = {
name,
description,
hidden,
hooks,
type: schemaPath.instance,
nonNull: !!index
};
if (schemaPath.enumValues && schemaPath.enumValues.length > 0) {
field.enumValues = schemaPath.enumValues;
}
// ObjectID ref
if (ref) {
field.reference = ref;
}
// Caster
if (schemaPath.caster) {
const {
instance,
options
} = schemaPath.caster;
const { ref } = options || {};
field.subtype = instance;
// ObjectID ref
if (ref) {
field.reference = ref;
}
}
return field;
}
/**
* Extracts tree chunk from path if it's a sub-document
* @method extractPath
* @param {Object} schemaPath
* @param {Object} model
* @return {Object} field
*/
function extractPath(schemaPath) {
const subNames = schemaPath.path.split('.');
return reduceRight(subNames, (fields, name, key) => {
const obj = {};
if (schemaPath instanceof mongoose.Schema.Types.DocumentArray) {
const subSchemaPaths = schemaPath.schema.paths;
const fields = extractPaths(subSchemaPaths, { name }); // eslint-disable-line no-use-before-define
obj[name] = {
name,
fields,
nonNull: false,
type: 'Array',
subtype: 'Object'
};
} else if (schemaPath instanceof mongoose.Schema.Types.Embedded) {
schemaPath.modelName = schemaPath.schema.options.graphqlTypeName || name;
// embedded model must be unique Instance
const embeddedModel = embeddedModels.hasOwnProperty(schemaPath.modelName)
? embeddedModels[schemaPath.modelName]
: getModel(schemaPath); // eslint-disable-line no-use-before-define
embeddedModels[schemaPath.modelName] = embeddedModel;
obj[name] = {
...getField(schemaPath),
embeddedModel
};
} else if (key === subNames.length - 1) {
obj[name] = getField(schemaPath);
} else {
obj[name] = {
name,
fields,
nonNull: false,
type: 'Object'
};
}
return obj;
}, {});
}
/**
* Merge sub-document tree chunks
* @method extractPaths
* @param {Object} schemaPaths
* @param {Object} model
* @return {Object) extractedSchemaPaths
*/
function extractPaths(schemaPaths, model) {
return reduce(schemaPaths, (fields, schemaPath) => (
merge(fields, extractPath(schemaPath, model))
), {});
}
/**
* Turn mongoose model to graffiti model
* @method getModel
* @param {Object} model Mongoose model
* @return {Object} graffiti model
*/
function getModel(model) {
const schemaPaths = model.schema.paths;
const name = model.modelName;
const fields = extractPaths(schemaPaths, { name });
return {
name,
fields,
model
};
}
/**
* @method getModels
* @param {Array} mongooseModels
* @return {Object} - graffiti models
*/
function getModels(mongooseModels) {
return mongooseModels
.map(getModel)
.reduce((models, model) => ({
...models,
[model.name]: model
}), {});
}
export default {
getModels
};
export {
extractPath,
extractPaths,
getModel,
getModels
};