term-notify-server/notify-term/schema.js

83 lines
1.4 KiB
JavaScript
Raw Normal View History

2016-02-11 16:16:42 +00:00
var Dragonfly = global.Dragonfly;
var cl = global.botanLoader;
var util = require( "util" );
var events = require( "events" );
var mongoose = require( "mongoose" );
var Schema = mongoose.Schema;
var options = cl.load( "notifyterm.config.db" );
var throwEverything = function( err ) {
if( err ) {
throw err;
}
};
var db = mongoose.connection;
db.on( "error", throwEverything );
mongoose.connect( options.host, options.auth );
/* Schema Heads */
var R_Tokens = { type: Schema.Types.ObjectId, ref: "Tokens" };
/* End Schema Heads */
var M_Tokens = new Schema({
name: { type: String, unique: true }
, token: { type: String }
2016-06-29 04:08:23 +00:00
, expired: { type: Boolean, default: false }
2016-02-11 19:12:38 +00:00
, date_created: {
type: Date
, default: Date.now
}
2016-02-11 16:16:42 +00:00
});
var DB = function ()
{
events.EventEmitter.call( this );
var Models = [
{ name: "Tokens" , schema: M_Tokens , hasKey: true }
];
var l = Models.length;
var _widx = 0;
var _widxl = 0;
var _self = this;
var ready = function()
{
_self.ready = true;
_self.emit( "ready", _self );
};
var waitIndex = function( err )
{
_widx ++;
throwEverything( err );
if( _widx == _widxl ) ready();
};
this.ready = false;
for( var i = 0; i < l; i ++ )
{
var mod = Models[i];
var b = mongoose.model( mod.name, mod.schema );
if( mod.hasKey )
{
_widxl ++;
b.on( "index", waitIndex );
}
this[ mod.name ] = b;
}
if( !_widxl ) ready();
};
util.inherits( DB, events.EventEmitter );
module.exports = new DB();