UNPKG

2.31 kBJavaScriptView Raw
1/*!
2 * Module dependencies.
3 */
4var mongoose = require('mongoose')
5 , Schema = mongoose.Schema
6;
7
8
9/**
10 * Definition constructor
11 *
12 * @param {String} name
13 * @param {Object|mongoose.Schema|Function} schema
14 * @param {String} database
15 * @param {Array} deps dependencies
16 * @api public
17 */
18function Definition(name, schema, database, deps){
19
20 this.dependencies = deps || [];
21 this.database = database || mongoose;
22 this.name = name;
23 this.path = this.database+'.'+this.name;
24 this.loaded = false;
25
26 if( schema instanceof Schema ){
27 this.schema = schema;
28 }else if( typeof schema == 'function') {
29 this.schemaConstructor = schema;
30 }else {
31 this.schema = new Schema(schema);
32 }
33};
34
35
36
37/**
38 * List of dependencies for the Schema.
39 *
40 * @api public
41 * @property dependencies
42 */
43Definition.prototype.dependencies;
44
45
46/**
47 * Name of the database to create the Model on.
48 *
49 * @api public
50 * @property database
51 */
52Definition.prototype.database;
53
54
55/**
56 * Model name.
57 *
58 * @api public
59 * @property name
60 */
61Definition.prototype.name;
62
63
64/**
65 * Unique path to find Model definition.
66 *
67 * @api public
68 * @property path
69 */
70Definition.prototype.path;
71
72
73/**
74 * Flag to determine if the Model has already been created from the Schema.
75 *
76 * @api public
77 * @property loaded
78 */
79Definition.prototype.loaded;
80
81
82/**
83 * Reference to the Schema or callback used to create the Model.
84 *
85 * @api public
86 * @property schema
87 */
88Definition.prototype.schema;
89
90
91/**
92 * Checks if Schema defines any dependencies.
93 *
94 * @return {Boolean}
95 * @api public
96 * @method hasDependencies
97 * @memberOf Definition
98 */
99Definition.prototype.hasDependencies = function(){
100 return this.dependencies.length>0;
101};
102
103/**
104 * Executes the callback passed into the constructor as the schema param.
105 *
106 * When a `Function` is passed instead of an `Object` or `Schema`, and
107 * internal callback gets set to execute the callback which is expected
108 * to return a mongoose.Schema instance.
109 *
110 * @return {mongoose.Schema}
111 * @api public
112 * @method create
113 * @memberOf Definition
114 */
115Definition.prototype.create = function(modeler){
116 if( this.schemaConstructor){
117 this.schema = this.schemaConstructor( modeler);
118 return this.schema;
119 }
120};
121
122
123
124/*!
125 * Module exports.
126 */
127module.exports = Definition;
128