hexo 的json database之find
查找当前写的源文章
1 | Box.prototype._getExistingFiles = function(){ |
我们看到this.Cache.find
这句代码是从缓存中查找,那么Cache是什么呢?
1 |
|
是从models文件夹下注册的model,然后把model放入db中。
1 | Database.prototype.model = function(name, schema){ |
我们看下Cache
的Model:1
2
3
4
5
6
7
8
9module.exports = function(ctx){
var Cache = new Schema({
_id: {type: String, required: true},
shasum: {type: String},
modified: {type: Number, default: Date.now}
});
return Cache;
};
声明了一个Schema,Scheme构造函数中会把当前schema参数加入path中,以便于查询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
82if (schema){
this.add(schema);
}
Schema.prototype.add = function(schema, prefix_){
var prefix = prefix_ || '';
var keys = Object.keys(schema);
var len = keys.length;
var key, value;
if (!len) return;
for (var i = 0; i < len; i++){
key = keys[i];
value = schema[key];
this.path(prefix + key, value);
}
};
Schema.prototype.path = function(name, obj){
if (obj == null){
return this.paths[name];
}
var type;
var nested = false;
if (obj instanceof SchemaType){
type = obj;
} else {
switch (typeof obj){
case 'function':
type = getSchemaType(name, {type: obj});
break;
//比如Cache的model就是{type: String, required: true}
case 'object':
if (obj.type){
type = getSchemaType(name, obj);
} else if (isArray(obj)){
type = new Types.Array(name, {
child: obj.length ? getSchemaType(name, obj[0]) : new SchemaType(name)
});
} else {
type = new Types.Object();
nested = Object.keys(obj).length > 0;
}
break;
default:
throw new TypeError('Invalid value for schema path `' + name + '`');
}
}
this.paths[name] = type;
this._updateStack(name, type);
if (nested) this.add(obj, name + '.');
};
function getSchemaType(name, options){
var Type = options.type || options;
var typeName = Type.name;
//String就是内置的类型
if (builtinTypes[typeName]){
return new Types[typeName](name, options);
} else {
return new Type(name, options);
}
}
var builtinTypes = {
String: true,
Number: true,
Boolean: true,
Array: true,
Object: true,
Date: true
};
最重要的就是各种Types:
1 | exports.Mixed = require('../schematype'); |
比如String:
1 | function SchemaTypeString(name, options){ |
这边的match方法就是在schema类的_parseQuery中有可能用到的,下回再讲。