forked from Breeze/breeze.js.labs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
breeze.metadata-helper.js
403 lines (365 loc) · 15.9 KB
/
breeze.metadata-helper.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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//#region Copyright, Version, and Description
/*
* Copyright 2014 IdeaBlade, Inc. All Rights Reserved.
* Use, reproduction, distribution, and modification of this code is subject to the terms and
* conditions of the IdeaBlade Breeze license, available at http://www.breezejs.com/license
*
* Author: Ward Bell
* Version: 1.0.5
* --------------------------------------------------------------------------------
* Adds metadataHelper extensions to Breeze
* Source:
* https://github.com/IdeaBlade/Breeze/blob/master/Breeze.Client/Scripts/Labs/breeze.metadata-helper.js
*
* Depends on Breeze which it patches
*
* You can use these helpers when creating metadata by hand
* to improve workflow and reduce data entry errors.
*
* The helpers reflect an opinion about developer workflow
* that may or may not work for you.
* Use these helpers "as is" or use for inspiration in creating your own.
*
* For example usage, see:
* https://github.com/IdeaBlade/Breeze/blob/master/Samples/DocCode/DocCode/tests/helpers/metadataOnClient.js
*
* For a discussion of how they work and why, see:
* http://www.breezejs.com/documentation/metadata-by-hand#addTypeToStore
*
*/
//#endregion
// ReSharper disable InconsistentNaming
(function (definition, window) {
if (window.breeze) {
definition(window.breeze);
} else if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {
// CommonJS or Node
var b = require('breeze');
definition(b);
} else if (typeof define === "function" && define["amd"] && !window.breeze) {
// Requirejs / AMD
define(['breeze'], definition);
} else {
throw new Error("Can't find breeze");
}
}(function (breeze) {
'use strict';
// MetadataHelper constructor
var helper = function (defaultNamespace, defaultAutoGeneratedKeyType) {
this.defaultNamespace = defaultNamespace || '';
this.defaultAutoGeneratedKeyType =
defaultAutoGeneratedKeyType ||
breeze.AutoGeneratedKeyType.None;
};
helper.prototype = {
constructor: helper,
addDataService: addDataService,
addTypeNameAsResource: addTypeNameAsResource,
addTypeToStore: addTypeToStore,
convertValidators: convertValidators,
findEntityKey: findEntityKey,
inferDefaultResourceName: inferDefaultResourceName,
inferValidators: inferValidators,
patch: patch,
pluralize: pluralize,
replaceDataPropertyAliases: replaceDataPropertyAliases,
replaceNavPropertyAliases: replaceNavPropertyAliases,
setDefaultAutoGeneratedKeyType: setDefaultAutoGeneratedKeyType,
setDefaultNamespace: setDefaultNamespace,
_hasOwnProperty: _hasOwnProperty,
_isArray: _isArray
};
breeze.config.MetadataHelper = helper;
var DT = breeze.DataType;
var Validator = breeze.Validator;
function addDataService(store, serviceName) {
store.addDataService(
new breeze.DataService({ serviceName: serviceName })
);
}
// Create the type from the definition hash and add the type to the store
// fixes some defaults, infers certain validators,
// add adds the type's "shortname" as a resource name
function addTypeToStore(store, typeDef) {
this.patch(typeDef);
var type = typeDef.isComplexType ?
new breeze.ComplexType(typeDef) :
new breeze.EntityType(typeDef);
store.addEntityType(type);
this.inferValidators(type);
this.addTypeNameAsResource(store, type);
return type;
}
// Often helpful to have the type's 'shortName' available as a resource name
// as when composing a query to be executed locally against the cache.
// This function adds the type's 'shortName' as one of the resource names for the type.
// Theoretically two types in different models could have the same 'shortName'
// and thus we would associate the same resource name with the two different types.
// While unlikely, breeze should offer a way to remove a resource name for a type.
function addTypeNameAsResource(store, type) {
if (!type.isComplexType) {
store.setEntityTypeForResourceName(type.shortName, type);
}
}
// While Breeze requires that the validators collection be defined with Validator instances
// we support alternative expression of validators in JSON form (as if coming from the server)
// Validator:
// phone: { maxLength: 24, validators: [ Validator.phone() ] },
// JSON:
// phone: { maxLength: 24, validators: [ {name: 'phone'} ] },
// This fn converts JSON to a Validator instance
function convertValidators(typeName, propName, propDef) {
var validators = propDef.validators;
if (!_isArray(validators)) {
//throw "{0}.{1}.validators must be an array".format(typeName, propName);
// coerce to array instead of throwing
propDef.validators = validators = [validators];
}
validators.forEach(function (val, ix) {
if (val instanceof Validator) return;
try {
validators[ix] = Validator.fromJSON(val);
} catch (ex) {
throw "{0}.{1}.validators[{2}] = '{3}' can't be converted to a known Validator."
.format(typeName, propName, ix, JSON.stringify(val));
}
});
}
function findEntityKey(typeDef) {
var dps = typeDef.dataProperties;
var typenameId = typeDef.shortName.toLowerCase() + 'id';
for (var key in dps) {
var prop = dps[key];
if (prop.isPartOfKey) { // found a key part; stop analysis
return key;
}
// if type were Person, would look for 'id' or 'personid'
if (prop.isPartOfKey == null) {
// isPartOfKey is null or undefined; is it a candidate?
var keyLc = key.toLowerCase();
if (keyLc === 'id' || keyLc === typenameId) {
// infer this property is the key; stop further analysis
prop.isPartOfKey = true;
return key;
}
}
}
return null;
}
function inferDefaultResourceName(typeDef) {
if (typeDef.defaultResourceName === undefined) {
typeDef.defaultResourceName = this.pluralize(typeDef.shortName);
}
}
function inferValidators(entityType) {
entityType.dataProperties.forEach(function (prop) {
if (!prop.isNullable) { // is required.
addValidator(prop, Validator.required());
};
addValidator(prop, getDataTypeValidator(prop));
if (prop.maxLength != null && prop.dataType === DT.String) {
addValidator(prop, Validator.maxLength({ maxLength: prop.maxLength }));
}
});
return entityType;
function addValidator(prop, validator) {
if (!validator) { return; } // no validator arg
var valName = validator.name;
var validators = prop.validators;
var found = validators.filter(function (val) { return val.name == valName; });
if (!found.length) { // this validator has not already been specified
validators.push(validator);
}
}
function getDataTypeValidator(prop) {
var dataType = prop.dataType;
var validatorCtor = !dataType || dataType === DT.String ? null : dataType.validatorCtor;
return validatorCtor ? validatorCtor() : null;
}
}
function normalizeNavProp(key, prop) {
switch (typeof (prop)) {
case 'string':
return { entityTypeName: prop };
case 'object':
return prop;
default:
// nav prop name (key) is same as EntityName (PascalCased)
var ename = key.substr(0, 1).toUpperCase() + key.substr(1);
return { entityTypeName: ename };
}
}
// Patch some defaults in the type definition object
// Todo: consider moving some of these patches into breeze itself
function patch(typeDef) {
var key, prop;
if (typeDef.name) { // 'name' -> 'shortName' property
renameAttrib(typeDef, 'name', 'shortName');
}
var typeName = typeDef.shortName;
// if no namespace specified, assign the helper defaultNamespace
var namespace = typeDef.namespace = typeDef.namespace || this.defaultNamespace;
if (!typeDef.isComplexType) {
this.inferDefaultResourceName(typeDef);
this.findEntityKey(typeDef);
// if entityType lacks an autoGeneratedKeyType, use the helper defaultAutoGeneratedKeyType
typeDef.autoGeneratedKeyType = typeDef.autoGeneratedKeyType || this.defaultAutoGeneratedKeyType;
}
var dps = typeDef.dataProperties;
for (key in dps) {
prop = dps[key];
this.replaceDataPropertyAliases(prop);
if (prop.complexTypeName && prop.complexTypeName.indexOf(":#") === -1) {
// if complexTypeName is unqualified, suffix with the entity's own namespace
prop.complexTypeName += ':#' + namespace;
}
// key always required (not nullable) unless explictly nullable
if (prop.isPartOfKey) { prop.isNullable = prop.isNullable === true; }
if (prop.validators) { this.convertValidators(typeName, key, prop); }
};
var navs = typeDef.navigationProperties;
for (key in navs) {
prop = navs[key] = normalizeNavProp(key, navs[key]);
this.replaceNavPropertyAliases(prop);
var propTypeName = prop.entityTypeName;
// append the namespace to entityTypeName if missing
var nsStart = propTypeName.indexOf(":#");
if (nsStart === -1) {
// name is unqualified; append the namespace
prop.entityTypeName += ':#' + namespace;
} else {
propTypeName = propTypeName.slice(0, nsStart);
}
// Infer that it's a child nav if no FKs, no invFKs, and not a collection
if (prop.foreignKeyNames === undefined && prop.isScalar !== false &&
prop.invForeignKeyNames === undefined) {
// Look for candidate FK property among the data properties as
// (1) propertyname + id OR (2) unqualified typename + 'id'
var candidate1 = key.toLowerCase() + 'id';
var candidate2 = propTypeName.toLowerCase() + 'id';
var fk = Object.keys(dps).filter(
function (k) {
k = k.toLowerCase();
return k === candidate1 || k === candidate2;
})[0];
if (fk) { prop.foreignKeyNames = [fk]; }
}
if (prop.associationName === undefined) {
var isParent = prop.isScalar === false ||
prop.invForeignKeyNames ||
prop.foreignKeyNames === undefined;
// association name is 'ChildType_ParentType'
prop.associationName =
(isParent ? propTypeName : typeName) + '_' +
(isParent ? typeName : propTypeName);
}
// coerce FK names to array
var keyNames = prop.foreignKeyNames;
if (keyNames && !_isArray(keyNames)) {
prop.foreignKeyNames = [keyNames];
}
keyNames = prop.invForeignKeyNames;
if (keyNames && !_isArray(keyNames)) {
prop.invForeignKeyNames = [keyNames];
}
};
}
function pluralize(word) {
// Lame English pluralizer; plenty better on the web
var len = word.length;
switch (word[len - 1]) {
case 's': // class -> classes
return word + 'es';
case 'x': // box -> boxes
return word + 'es';
case 'y': // fly -> flies
return word.substr(0, len - 1) + 'ies';
default: // cat -> cats
return word + 's';
}
}
function renameAttrib(obj, oldName, newName) {
if (obj[newName] !== undefined) {
throw "renameAttrib error; new name, '" + newName + "' is already defined for the object.";
}
obj[newName] = obj[oldName];
delete obj[oldName];
}
/*
* Support common aliases in DataProperty attributes to reduce tedium
* type -> dataType
* null -> isNullable
* max -> maxLength
* default -> defaultValue
*/
function replaceDataPropertyAliases(prop) {
for (var key in prop) {
if (_hasOwnProperty(prop, key)) {
var keyLc = key.toLowerCase();
if (keyLc === 'type') {
renameAttrib(prop, key, 'dataType');
}
if (keyLc === 'max' && (prop.dataType === undefined || prop.dataType === DT.String)) {
renameAttrib(prop, key, 'maxLength');
} else if (keyLc.indexOf('null') > -1 && key !== 'isNullable' && typeof (prop[key]) === 'boolean') {
renameAttrib(prop, key, 'isNullable');
} else if (keyLc.indexOf('key') > -1 && key !== 'isPartOfKey' && typeof (prop[key]) === 'boolean') {
renameAttrib(prop, key, 'isPartOfKey');
} else if (keyLc === 'default') {
renameAttrib(prop, key, 'defaultValue');
}
// Mongo subdocuments could be collections of complex types
else if (keyLc === 'isone' || keyLc === 'hasone') {
renameAttrib(prop, key, 'isScalar');
} else if (keyLc === 'ismany' || keyLc === 'hasmany') {
prop[key] = !prop[key];
renameAttrib(prop, key, 'isScalar');
}
}
}
}
/*
* Support common aliases in Navigation Property attributes to reduce tedium
* type -> entityTypeName
* FK|FKs -> foreignKeyNames
* invFK|invFKs -> invForeignKeyNames
* assoc -> associationName
* isOne | hasOne -> isScalar
* isMany | hasMany -> isScalar with boolean flipped
*/
function replaceNavPropertyAliases(prop) {
for (var key in prop) {
if (_hasOwnProperty(prop, key)) {
var keyLc = key.toLowerCase();
if (keyLc === 'type') {
renameAttrib(prop, key, 'entityTypeName');
} else if (keyLc === 'fk' || keyLc === 'fks') {
renameAttrib(prop, key, 'foreignKeyNames');
} else if (keyLc === 'isone' || keyLc === 'hasone') {
renameAttrib(prop, key, 'isScalar');
} else if (keyLc === 'ismany' || keyLc === 'hasmany') {
prop[key] = !prop[key];
renameAttrib(prop, key, 'isScalar');
} else if (keyLc === 'invfk' || keyLc === 'invfks') {
renameAttrib(prop, key, 'invForeignKeyNames');
} else if (keyLc.indexOf('assoc') > -1 && key !== 'associationName') {
renameAttrib(prop, key, 'associationName');
}
}
}
}
function setDefaultAutoGeneratedKeyType(autoGeneratedKeyType) {
this.defaultAutoGeneratedKeyType =
autoGeneratedKeyType ||
breeze.AutoGeneratedKeyType.None;
}
function setDefaultNamespace(namespace) {
this.defaultNamespace = namespace || '';
}
function _hasOwnProperty(obj, key) {
return Object.prototype.hasOwnProperty.call(obj, key);
}
function _isArray(obj) {
return Object.prototype.toString.call(obj) === '[object Array]';
}
}, this));