-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.coffee
119 lines (84 loc) · 2.58 KB
/
schema.coffee
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
_getKeysByValue = (obj, key, isArray) ->
isArray = isArray || false;
# console.log key
schemaKey = {}
if typeof obj is 'string'
schemaKey[key] = if isArray then '[string]' else 'string';
return schemaKey
if typeof obj is 'number'
schemaKey[key] = if isArray then '[number]' else 'number'
return schemaKey
if typeof obj is 'boolean'
schemaKey[key] = 'boolean'
return schemaKey
if obj instanceof Date
schemaKey[key] = 'date'
return schemaKey
if _.isArray obj
_getKeysByValue(obj[0], key, true)
else
for i of obj
if obj.hasOwnProperty(i)
_getKeysByValue(obj[i], key + (if isArray then ".$." else ".") + i)
@ZeroConfig = _.extend @ZeroConfig,
_getDocSample: (collection) ->
docs = collection.instance.find({}, {limit: 10}).fetch()
unless docs.length
# No documents. Cannot generate schema
return
# Get as many properties as possible
_superDoc = docs[0]
_.each docs, (doc) ->
_superDoc = _.extend _superDoc, doc
_superDoc
# Generates a JSON schema that can be stored in a collection
_generateProtoSchema: (collection) ->
console.log "Generating schema for #{collection.name}"
doc = @_getDocSample(collection)
console.log(doc);
_schemaObj = []
_.each doc, (v, k) ->
schemaKey = {}
# Can't Handle functions
if typeof v is 'function'
return
schemaKey = _getKeysByValue(v, k)
_schemaObj.push schemaKey
_schemaObj = _.flatten _schemaObj
return _schemaObj
_getSchemaKey: (value) ->
if typeof value is 'string'
type = String
if value instanceof Date
type = Date
type: type
_mapProtoSchema: (protoSchema) ->
console.log(protoSchema)
_schemaSeed = {}
for item in protoSchema
key = _.keys(item)[0]
propertyType = _.values(item)[0]
# TODO: Clean up code
if propertyType == 'string'
propertyType = String
if propertyType == 'number'
propertyType = Number
if propertyType == 'boolean'
propertyType = 'Boolean'
if propertyType instanceof Date
propertyType = Date
if propertyType == '[string]'
propertyType = [String]
if propertyType == '[number]'
propertyType = [Number]
_schemaSeed[key] =
type: propertyType
optional: true
# Remove _id
delete _schemaSeed._id
_schemaSeed
_generateSchema: (collection) ->
protoSchema = @_generateProtoSchema(collection)
schemaSeed = @_mapProtoSchema(protoSchema)
console.log(schemaSeed)
protoSchema