-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
82 lines (75 loc) · 2.75 KB
/
test.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
var util = require('util');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = mongoose.Schema.Types.ObjectId;
var Mixed = mongoose.Schema.Types.Mixed;
var Utils = require('./lib');
var sub = {
// simple types
nested: { file: {name:String, image: {type:String}}},
str: String, strType: {type: String, enum: ['on', 'off']},
num: Number, numType: {type: Number, some: true, some1: {prop: 'test'}, private: false},
bool: Boolean, boolType: {type: Boolean, some: true},
arr: [], arrType: {type:[]},
//buf: Buffer, bufType: {type: Buffer},
date: Date, dateType: {type: Date, some: true},
objId: ObjectId, objIdType: { type: ObjectId},
mix: Mixed, mixType: { type: Mixed },
obj: {}, objType: { type: {}}
};
var subSchema = new Schema({
// simple types
str: String, strType: {type: String, enum: ['on', 'off']},
num: Number, numType: {type: Number, some: true, some1: {prop: 'test'}, private: false},
bool: Boolean, boolType: {type: Boolean, some: true},
arr: [], arrType: {type:[]},
//buf: Buffer, bufType: {type: Buffer},
date: Date, dateType: {type: Date, some: true},
objId: ObjectId, objIdType: { type: ObjectId},
mix: Mixed, mixType: { type: Mixed },
obj: {}, objType: { type: {}},
type: {type: String}
});
var schema = new Schema({
//exceptional
type: {type: String, enum: ['on', 'off']},
// nested type with mixed types
nested: { file: {name:String, image: {type:String}}},
// simple types
str: String, strType: {type: String, enum: ['on', 'off']},
num: Number, numType: {type: String, some: true},
bool: Boolean, boolType: {type: Boolean, some: true},
arr: [], arrType: {type:[]},
simpleArr: [String], simpleArrType: {type:[String]},
buf: Buffer, bufType: {type: Buffer},
date: Date, dateType: {type: Date, some: true},
objId: ObjectId, objIdType: { type: ObjectId, ref: 'Users'},
// mixed types
mix: Mixed, mixType: { type: Mixed },
obj: {}, objType: { type: {}},
sub: sub, subType: {type: sub, private: true, some1: {prop: true}},
//mixed type
subArr: [sub], subArrType: { type: [sub] },
// sub schema type
subSch: [subSchema], subSchType: {type: [subSchema], test: 'foo'}
//schema can not be a type
//subSchema: subSchema, subSchemaType: {type: subSchema}
});
schema.plugin(function (schema) {
var utils = new Utils(schema);
var normalized = utils.schema;
//console.warn(util.inspect(normalized, {depth: null}));
var flatten = utils.flatten();
console.log(util.inspect(flatten, {depth: null}));
utils.flatEach(flatten, function(val, key){
//console.log(key);
});
var r = utils.flatReduce(flatten, function(result, val, key){
if (val.some){
result[key] = val;
}
return result;
});
//console.log(r);
});
mongoose.model('schema', schema, 'schema');