/
schema.js
41 lines (32 loc) · 1.03 KB
/
schema.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
var Schema = require('./BaseSchema')
var schema = module.exports = function(schemaDescription) {
var doc, schemaObject
if (arguments.length === 2) {
doc = schemaDescription
schemaDescription = arguments[1]
}
if (this instanceof schema) {
// When called with new, create a schema object and then return the schema function
var constructor = Schema.extend(schemaDescription)
schemaObject = new constructor()
if (doc) schemaObject.doc = doc
return schemaObject.wrap()
} else {
// When called as simple function, forward everything to fromJS
// and then resolve schema.self to the resulting schema object
schemaObject = Schema.fromJS(schemaDescription)
schema.self.resolve(schemaObject)
if (doc) schemaObject.doc = doc
return schemaObject.wrap()
}
}
schema.Schema = Schema
schema.toJSON = function(sch) {
return Schema.fromJS(sch).toJSON()
}
schema.fromJS = function(sch) {
return Schema.fromJS(sch).wrap()
}
schema.fromJSON = function(sch) {
return Schema.fromJSON(sch).wrap()
}