-
Notifications
You must be signed in to change notification settings - Fork 196
Expand file tree
/
Copy pathserializer-utils.js
More file actions
325 lines (272 loc) · 8.89 KB
/
serializer-utils.js
File metadata and controls
325 lines (272 loc) · 8.89 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
'use strict';
var isPlainObject = require('lodash/isPlainObject');
var isFunction = require('lodash/isFunction');
var _find = require('lodash/find');
var _merge = require('lodash/merge');
var _identity = require('lodash/identity');
var _transform = require('lodash/transform');
var _mapValues = require('lodash/mapValues');
var _mapKeys = require('lodash/mapKeys');
var _pick = require('lodash/pick');
var _pickBy = require('lodash/pickBy');
var _keys = require('lodash/keys');
var _each = require('lodash/each');
var _isNil = require('lodash/isNil');
var Inflector = require('./inflector');
module.exports = function (collectionName, record, payload, opts) {
function isComplexType(obj) {
return Array.isArray(obj) || isPlainObject(obj);
}
function keyForAttribute(attribute) {
if (isPlainObject(attribute)) {
return _transform(attribute, function (result, value, key) {
if (isComplexType(value)) {
result[keyForAttribute(key)] = keyForAttribute(value);
} else {
result[keyForAttribute(key)] = value;
}
});
} else if (Array.isArray(attribute)) {
return attribute.map(function (attr) {
if (isComplexType(attr)) {
return keyForAttribute(attr);
} else {
return attr;
}
});
} else {
if (isFunction(opts.keyForAttribute)) {
return opts.keyForAttribute(attribute);
} else {
return Inflector.caserize(attribute, opts);
}
}
}
function getId() {
return opts.id || 'id';
}
function getRef(current, item, opts) {
if (isFunction(opts.ref)) {
return opts.ref(current, item);
} else if (opts.ref === true) {
if (Array.isArray(item)) {
return item.map(function (val) {
return String(val);
});
} else if (item) {
return String(item);
}
} else if (item && item[opts.ref]){
return String(item[opts.ref]);
}
}
function getType(str, attrVal, attrOpts) {
var type;
attrVal = attrVal || {};
attrOpts = attrOpts || {};
if (isFunction(attrOpts.typeForAttribute)) {
type = attrOpts.typeForAttribute(str, attrVal);
}
if (type === undefined && isFunction(opts.typeForAttribute)) {
type = opts.typeForAttribute(str, attrVal);
}
// If the pluralize option is on, typeForAttribute returned undefined or wasn't used
if ((opts.pluralizeType === undefined || opts.pluralizeType) && type === undefined) {
type = Inflector.pluralize(str);
}
if (type === undefined) {
type = str;
}
return type;
}
function getLinks(current, links, dest) {
return _mapValues(links, function (value) {
if (isFunction(value)) {
return value(record, current, dest);
} else {
return value;
}
});
}
function getMeta(current, meta) {
if (isFunction(meta)) {
return meta(record);
} else {
return _mapValues(meta, function (value) {
if (isFunction(value)) {
return value(record, current);
} else {
return value;
}
});
}
}
function pick(obj, attributes) {
return _mapKeys(_pick(obj, attributes), function (value, key) {
return keyForAttribute(key);
});
}
function isCompoundDocumentIncluded(included, item) {
return _find(payload.included, { id: item.id, type: item.type });
}
function pushToIncluded(dest, include) {
var included = isCompoundDocumentIncluded(dest, include);
if (included) {
// Merge relationships
included.relationships = _merge(included.relationships,
_pickBy(include.relationships, _identity));
// Merge attributes
included.attributes = _merge(included.attributes,
_pickBy(include.attributes, _identity));
} else {
if (!dest.included) { dest.included = []; }
dest.included.push(include);
}
}
this.serialize = function (dest, current, attribute, opts) {
var that = this;
var data = null;
if (opts && opts.ref) {
if (!dest.relationships) { dest.relationships = {}; }
if (Array.isArray(current[attribute])) {
data = current[attribute].map(function (item) {
return that.serializeRef(item, current, attribute, opts);
});
} else {
data = that.serializeRef(current[attribute], current, attribute,
opts);
}
dest.relationships[keyForAttribute(attribute)] = {};
if (!opts.ignoreRelationshipData) {
dest.relationships[keyForAttribute(attribute)].data = data;
}
if (opts.relationshipLinks) {
var links = getLinks(current[attribute], opts.relationshipLinks, dest);
if (links.related) {
dest.relationships[keyForAttribute(attribute)].links = links;
}
}
if (opts.relationshipMeta) {
dest.relationships[keyForAttribute(attribute)].meta =
getMeta(current[attribute], opts.relationshipMeta);
}
} else {
if (Array.isArray(current[attribute])) {
if (current[attribute].length && isPlainObject(current[attribute][0])) {
data = current[attribute].map(function (item) {
return that.serializeNested(item, current, attribute, opts);
});
} else {
data = current[attribute];
}
dest.attributes[keyForAttribute(attribute)] = data;
} else if (isPlainObject(current[attribute])) {
data = that.serializeNested(current[attribute], current, attribute, opts);
dest.attributes[keyForAttribute(attribute)] = data;
} else {
dest.attributes[keyForAttribute(attribute)] = current[attribute];
}
}
};
this.serializeRef = function (dest, current, attribute, opts) {
var that = this;
var id = getRef(current, dest, opts);
var type = getType(attribute, dest, opts);
var relationships = [];
var includedAttrs = [];
if (opts.attributes) {
if (dest) {
opts.attributes.forEach(function (attr) {
if (opts[attr] && !dest[attr] && opts[attr].nullIfMissing) {
dest[attr] = null;
}
});
}
relationships = opts.attributes.filter(function (attr) {
return opts[attr];
});
includedAttrs = opts.attributes.filter(function (attr) {
return !opts[attr];
});
}
var included = { type: type, id: id };
if (includedAttrs) { included.attributes = pick(dest, includedAttrs); }
relationships.forEach(function (relationship) {
if (dest && (isComplexType(dest[relationship]) || dest[relationship] === null)) {
that.serialize(included, dest, relationship, opts[relationship]);
}
});
if (includedAttrs.length &&
(opts.included === undefined || opts.included)) {
if (opts.includedLinks) {
included.links = getLinks(dest, opts.includedLinks);
}
if (typeof id !== 'undefined') { pushToIncluded(payload, included); }
}
var json = typeof id !== 'undefined' ? { type: type, id: id } : null;
if (dest && dest.meta) {
json.meta = dest.meta
}
return json
};
this.serializeNested = function (dest, current, attribute, opts) {
var that = this;
var embeds = [];
var attributes = [];
var ret = {};
if (opts && opts.attributes) {
embeds = opts.attributes.filter(function (attr) {
return opts[attr];
});
attributes = opts.attributes.filter(function (attr) {
return !opts[attr];
});
if (attributes) { ret.attributes = pick(dest, attributes); }
embeds.forEach(function (embed) {
if (isComplexType(dest[embed])) {
that.serialize(ret, dest, embed, opts[embed]);
}
});
} else {
ret.attributes = dest;
}
return ret.attributes;
};
this.perform = function () {
var that = this;
if( record === null ){
return null;
}
// If option is present, transform record
if (opts && opts.transform) {
record = opts.transform(record);
}
// Top-level data.
var data = { type: getType(collectionName, record) };
if (!_isNil(record[getId()])) { data.id = String(record[getId()]); }
// Data links.
if (opts.dataLinks) {
data.links = getLinks(record, opts.dataLinks);
}
// Data meta
if (opts.dataMeta) {
data.meta = getMeta(record, opts.dataMeta);
}
_each(opts.attributes, function (attribute) {
var splittedAttributes = attribute.split(':');
if (opts[attribute] && !record[attribute] && opts[attribute].nullIfMissing) {
record[attribute] = null;
}
if (splittedAttributes[0] in record) {
if (!data.attributes) { data.attributes = {}; }
var attributeMap = attribute;
if (splittedAttributes.length > 1) {
attribute = splittedAttributes[0];
attributeMap = splittedAttributes[1];
}
that.serialize(data, record, attribute, opts[attributeMap]);
}
});
return data;
};
};