-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildEntitySchema.js
75 lines (72 loc) · 2.94 KB
/
buildEntitySchema.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
import SimpleSchema from 'simpl-schema'
//noinspection JSUnusedGlobalSymbols
/**
* Build the simple schema based on the field metadata (an array of fields)
* - by default properties will be included as a string property
* - unless the required flag is true, the property will be marked optional
* - for a date property a regex will be included to ensure it is passed in the correct format to SF
* - for a lookup property an associated property named [SF relationship property name] will be created,
* with a sub schema of Name + Id. For example AccountId => Account = { Name, Id }, Record_Owner__c => Record_Owner__r = {Name, Id}
* @param fieldMetadata object - metadata obtained with getFieldMetadata
* @param allowMissingRequiredFields [bool] if true, fields will all be marked as optional
* @return SimpleSchema object
*/
export default function buildEntitySchema(fieldMetadata, allowMissingRequiredFields = true) {
const schema = fieldMetadata.reduce((current, field) => {
let fieldDef = {type: getTypeForField(field.type), optional: (field.required !== 'true' && field.required !== true)}
if (field.label)
fieldDef.label = field.label
switch (field.type) {
case 'Lookup':
// add field for the lookup entity
current[field.fullName.replace(/Id$/, '').replace(/__c$/, '__r')] = {
type: lookupSchema,
optional: allowMissingRequiredFields || (field.required !== 'true' && field.required !== true),
label: field.label || field.fullName
}
break;
case 'Date':
fieldDef.regEx = /\d{4}-\d{2}-\d{2}/
break
case 'Summary':
case 'Formula':
// do not include those in the schema, so that they are cleaned out on update
// note that they will also not be automatically refreshed until the entity itself is reloaded from SF
// ideally we should have those automatically calculated using the SF definitions!
// But that may be a bit much right now.
fieldDef = null
break
case 'Currency':
case 'Number':
if (field.precision === "0") { //noinspection JSUnresolvedVariable
fieldDef.type = SimpleSchema.Integer
}
break
}
if (fieldDef && allowMissingRequiredFields)
// otherwise we just leave it undefined - which is the same as making it required
fieldDef.optional = true
if (fieldDef)
current[field.fullName] = fieldDef
return current
}, {})
return new SimpleSchema(schema).extend({
Id: {type: String, optional: true}
})
}
function getTypeForField(type) {
switch (type) {
case 'Checkbox':
return Boolean
case 'Currency':
case 'Number':
return Number
default:
return String
}
}
// for entity lookup we only include the name.
// Note that this will cause the other fields to be cleaned up when the schema is validated!
const lookupSchema = new SimpleSchema({
Name: {type: String}, Id: {type: String}
})