1+ import { Schema , model , Model } from 'mongoose' ;
2+
3+ // An interface that describes attributes that a user should have
4+ interface UserAttrs {
5+ tg_id :number ;
6+ first_name ?:string ;
7+ last_name ?:string ;
8+ username ?:string ;
9+ is_bot :boolean ;
10+ is_active ?:boolean ;
11+ bot_name ?:string ;
12+ }
13+
14+ // An interface that describes what attributes a user model should have
15+ interface UserModel extends Model < UserDoc > {
16+ build ( attrs :UserAttrs ) :UserDoc
17+ }
18+
19+ // An interface that descibes single user properties
20+ interface UserDoc extends Document {
21+ tg_id :number ;
22+ first_name ?:string ;
23+ last_name ?:string ;
24+ username ?:string ;
25+ is_bot :boolean ;
26+ is_active ?:boolean ;
27+ last_action ?:string ;
28+ created_at ?:Date ;
29+ bot_name ?:string ;
30+ }
31+
32+ // Creating user schema
33+ const userSchema = new Schema ( {
34+ tg_id :{ type :Number } ,
35+ is_bot :{ type :Boolean } ,
36+ first_name :{ type :String } ,
37+ last_name :{ type :String } ,
38+ username :{ type :String } ,
39+ bot_name :{ type :String } ,
40+ is_active :{ type :Boolean , default :false } ,
41+ last_action :{ type :String } ,
42+ created_at :{ type :Date , default :Date . now }
43+
44+ } )
45+ // Statics
46+ userSchema . static ( 'build' , ( attrs :UserAttrs ) => { return new User ( attrs ) } )
47+
48+ // Creating user model
49+ const User = model < UserDoc & UserModel > ( 'User' , userSchema )
50+
51+ export { User , UserAttrs , UserDoc }