Schema Files of our example

FileName: users.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var usersSchema= new Schema({
  userid:{type:String,index:true,unique:true,trim:true,required:true},
  password:{type:String,default:"1234567"},
  balancechips:{type:Number,default:0.0},
  state:{type:Number,default:2},
  emailid:{type:String,required:true,match: /.+\@.+\..+/,index:true}
},{collection:'users'});
 
usersSchema.statics.getId=function(_userid,cb){
   console.log("userid"+_userid);
   return UserModel.find({ userid: new RegExp(_userid, 'i') },{userid:1,_id:1}, cb);
};

usersSchema.methods.getUserId=function(_userid,cb){
  console.log("userid"+_userid);
   return UserModel.find({ userid: new RegExp(_userid, 'i') },{userid:1,_id:1}, cb);
};
UserModel = mongoose.model('Users',usersSchema);

module.exports={UserModel}; 

FileName: address.js

var mongoose = require('mongoose');
var users = require('./users');
var Schema = mongoose.Schema;

var addressSchema= new Schema({
  userid:{type:Schema.Types.ObjectId,ref:'users',required:true},
  addresstype:{type:String,default:"Residence"},
  city:{type:String},
  state:{type:String},
  country:{type:String},
  pincode:{type:Number,required:true}
},{collection:'address'});
 
AddressModel = mongoose.model('Address',addressSchema);

module.exports={AddressModel};

FileName: games.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var gamesSchema= new Schema({
  gameid:{type:String,index:true,unqiue:true},
  gamename:{type:String,trim:true},
  state:{type:Number,default:0},//0 Inactive 1 active
  minbet:{type:Number,default:1.0},
  maxbet:{type:Number,deafult:1000.0}
},{collection:'games'});
GamesModel = mongoose.model('Games',gamesSchema);
module.exports={GamesModel}; 

FileName: loginsession.js

var mongoose = require('mongoose');
var users = require('./users');
var Schema = mongoose.Schema;

var loginsessionSchema= new Schema({
  userid:{type:Schema.Types.ObjectId,ref:'Users',required:true},
  sessionid:{type:String,index:true,unqiue:true},
  ip:{type:String,trim:true},
  startworth:{type:Number,default:0},
  endworth:{type:Number,default:0},
  logintime:{type:Date,default:new Date()},
  logouttime:{type:Date,default:new Date()},
  totalbet:{type:Number,default:0.0},
  totalwin:{type:Number,default:0.0}
},{collection:'loginsession'});
LoginsessionModel = mongoose.model('LoginSession',loginsessionSchema);
module.exports={LoginsessionModel}; 

FileName: gameplaylogs.js

var mongoose = require('mongoose');
var users = require('./users');
var ls = require('./loginsession');
var games = require('./games');
var Schema = mongoose.Schema;

var gameplaylogsSchema= new Schema({
  logid:{type:String,index:true,trim:true,unique:true,required:true},
  userid:{type:Schema.Types.ObjectId,ref:'Users',required:true},
  sessionid:{type:Schema.Types.ObjectId,ref:'LoginSession',required:true},
  gameid:{type:Schema.Types.ObjectId,ref:'Games',required:true},
  playdetails:{type:String,trim:true},
  betamount:{type:Number,default:0},
  winamount:{type:Number,default:0},
  playtime:{type:Date,default:new Date()},
},{collection:'gameplaylogs'});
GameplaylogsModel = mongoose.model('GamePlayLogs',gameplaylogsSchema);
module.exports={GameplaylogsModel}; 

FileName: database.js

var mongoose = require('mongoose');
var con;
var conflag=false;
var init=function initdb(){
  mongoose.connect("mongodb://localhost/tutorialtous",callback);
}
var callback = function(err,data){
  if(err)
    console.log(err);
  else{
    console.log("connection success");
    conflag=true;
    con = mongoose.connection;
  } 
} 
module.exports={con,init,conflag};
Copyright © 2018-2020 TutorialToUs. All rights reserved.