import { ID , ObjectionModelToPojo } from '@api/shared/shared.types' ; import { Model , ModelOptions , Pojo } from 'objection' ; import { BaseModel } from '@api/db/base.model' ; import { CategoryModel } from '@api/categories/category.model' ; import { UserModel } from '@api/users/user.model' ; import { MediaModel } from '@api/media/media.model' ; import { ReactableModel } from '@api/reactions/reaction.model' ; export class PostModel extends BaseModel { title : string ; categoryId : ID ; category ? : CategoryModel ; userId : ID ; user ? : UserModel ; media ? : MediaModel ; reactable ? : ReactableModel ; static tableName = 'Post' ; static get jsonSchema ( ) { return { type : 'object' , required : [ 'title' , 'categoryId' , 'userId' ] , properties : { title : { type : 'string' , minLength : 5 , maxLength : 200 } , } , } ; } $parseJson ( json , opt ) { const parsed = super . $parseJson ( json , opt ) as ObjectionModelToPojo < PostModel > ; if ( typeof parsed . title === 'string' ) { parsed . title = parsed . title . trim ( ) ; } return parsed ; } static relationMappings = ( ) => ( { category : { relation : Model . BelongsToOneRelation , modelClass : CategoryModel , join : { from : 'Post.categoryId' , to : 'Category.id' , } , } , user : { relation : Model . BelongsToOneRelation , modelClass : UserModel , join : { from : 'Post.userId' , to : 'User.id' , } , } , media : { relation : Model . HasOneRelation , modelClass : MediaModel , join : { from : 'Post.id' , to : 'Media.postId' , } , } , reactable : { relation : Model . HasOneRelation , modelClass : ReactableModel , join : { from : 'Post.id' , to : 'Reactable.postId' , } , } , } ) ; }