1- import { ObjectID } from "mongodb" ;
2- import * as mongoose from "mongoose" ;
3- import "./connection" ;
1+ import mongoose from "mongoose" ;
2+ import "./connection.js" ;
43const { assign, keys } = Object ;
54
65const reposValidator = ( repos : string [ ] ) => {
@@ -58,10 +57,10 @@ const userSchemaObj: mongoose.SchemaDefinition = {
5857 validate : { validator : Number . isInteger } ,
5958 } ,
6059} ;
61- const UserSchema = new mongoose . Schema ( userSchemaObj , {
60+ const UserSchema = new mongoose . Schema < RawUser > ( userSchemaObj , {
6261 bufferCommands : false ,
6362} ) ;
64- export const UserModel = mongoose . model ( "User" , UserSchema ) ;
63+ export const UserModel = mongoose . model < RawUser > ( "User" , UserSchema ) ;
6564
6665export class CuteRepo {
6766 filter : number ;
@@ -89,24 +88,26 @@ export class UpdateAllReposParams {
8988}
9089
9190export class RawUser {
92- _id ?: ObjectID ;
93- accessToken ? : string ;
94- alerted ? : string [ ] [ ] ;
91+ _id : mongoose . Types . ObjectId ;
92+ accessToken : string ;
93+ alerted : string [ ] [ ] ;
9594 checkAt ?: number ;
96- email ? : string ;
95+ email : string ;
9796 frequency ?: string ;
9897 githubId ?: number ;
99- majors ? : string [ ] ;
100- minors ? : string [ ] ;
101- mutedRepos ? : string [ ] ;
98+ majors : string [ ] ;
99+ minors : string [ ] ;
100+ mutedRepos : string [ ] ;
102101 passwordEncrypted ?: string ;
103- patches ? : string [ ] ;
104- repos ? : string [ ] ;
102+ patches : string [ ] ;
103+ repos : string [ ] ;
105104 watching ?: boolean ;
106105 watchingStars ?: number ;
107106 [ key : string ] : any ;
108107}
109108
109+ export type RawUserUpdate = Partial < RawUser > ;
110+
110111export class User extends CuteUser {
111112 constructor ( params : CuteUser ) {
112113 super ( ) ;
@@ -189,7 +190,7 @@ export class User extends CuteUser {
189190 static async load ( conditions : any ) {
190191 const { id, ...rest } = conditions ;
191192 if ( id ) {
192- conditions = { _id : new ObjectID ( id ) , ...rest } ;
193+ conditions = { _id : new mongoose . Types . ObjectId ( id ) , ...rest } ;
193194 }
194195 const raw = await UserModel . findOne ( conditions ) ;
195196 return raw ? new User ( toCute ( raw ) ) : null ;
@@ -261,7 +262,7 @@ export class User extends CuteUser {
261262 params : UpdateAllReposParams
262263 ) {
263264 const { muted, filter } = params ;
264- const command : RawUser = { } ;
265+ const command : RawUserUpdate = { } ;
265266 const repoNames = repos . map ( ( r ) => r . repo ) ;
266267 if ( typeof muted !== "undefined" ) {
267268 command . mutedRepos = muted ? repoNames : [ ] ;
@@ -281,9 +282,9 @@ export class User extends CuteUser {
281282}
282283
283284function toRaw ( { id, repos, ...rest } : CuteUser ) : RawUser {
284- const result : RawUser = rest ;
285+ const result : RawUserUpdate = rest ;
285286 if ( id ) {
286- result . _id = new ObjectID ( id ) ;
287+ result . _id = new mongoose . Types . ObjectId ( id ) ;
287288 }
288289 if ( repos ) {
289290 assign ( result , {
@@ -296,7 +297,7 @@ function toRaw({ id, repos, ...rest }: CuteUser): RawUser {
296297 }
297298 return Object . keys ( result )
298299 . filter ( ( k ) => k === "_id" || ! ! userSchemaObj [ k ] )
299- . reduce ( ( r , k ) => ( { ...r , [ k ] : result [ k ] } ) , { } ) ;
300+ . reduce ( ( r , k ) => ( { ...r , [ k ] : result [ k ] } ) , { } as RawUser ) ;
300301}
301302
302303function toCute ( raw : RawUser ) : CuteUser {
0 commit comments