import mongoose = require ( 'mongoose' ) ; import CourseBook from './model/CourseBook' ; var CourseBookSchema = new mongoose . Schema ( { year : { type : Number , required : true } , semester : { type : Number , required : true } , updated_at : Date } ) ; let mongooseModel = mongoose . model ( 'CourseBook' , CourseBookSchema , 'coursebooks' ) ; export async function findAll ( ) { let docs = await mongooseModel . find ( { } , '-_id year semester updated_at' ) . sort ( [ [ "year" , - 1 ] , [ "semester" , - 1 ] ] ) . exec ( ) ; return docs . map ( fromMongoose ) ; } export async function findRecent ( ) { let doc = await mongooseModel . findOne ( { } , '-_id year semester updated_at' ) . sort ( [ [ "year" , - 1 ] , [ "semester" , - 1 ] ] ) . exec ( ) ; return fromMongoose ( doc ) ; } export async function findByYearAndSemester ( year , semester ) { let doc = await mongooseModel . findOne ( { year : year , semester : semester } ) . exec ( ) ; return fromMongoose ( doc ) ; } export async function insert ( courseBook ) { await new mongooseModel ( courseBook ) . save ( ) ; } export async function update ( courseBook ) { await mongooseModel . updateOne ( { year : courseBook . year , semester : courseBook . semester } , courseBook ) . exec ( ) ; } function fromMongoose ( mongooseDoc ) { if ( mongooseDoc === null ) return null ; return { year : mongooseDoc . year , semester : mongooseDoc . semester , updated_at : mongooseDoc . updated_at } ; }