import { BaseEntity , Changes , ConfigApi , EntityFilter , EntityGraphQLFilter , EntityManager , EntityOrmField , FilterOf , Flavor , getEm , GraphQLFilterOf , hasOne , Lens , Loaded , LoadHint , loadLens , ManyToOneReference , newChangesProxy , newRequiredRule , OptsOf , OrderBy , PartialOrNull , setField , setOpts , ValueFilter , ValueGraphQLFilter , } from "joist-orm" ; import { Context } from "src/context" ; import { Author , AuthorId , authorMeta , AuthorOrder , Book , bookMeta , newBook } from "./entities" ; export type BookId = Flavor < string , "Book" > ; export interface BookOpts { title : string ; author : Author ; } export interface BookIdsOpts { authorId ? : AuthorId | null ; } export interface BookFilter { id ? : ValueFilter < BookId , never > ; title ? : ValueFilter < string , never > ; createdAt ? : ValueFilter < Date , never > ; updatedAt ? : ValueFilter < Date , never > ; author ? : EntityFilter < Author , AuthorId , FilterOf < Author > , never > ; } export interface BookGraphQLFilter { id ? : ValueGraphQLFilter < BookId > ; title ? : ValueGraphQLFilter < string > ; createdAt ? : ValueGraphQLFilter < Date > ; updatedAt ? : ValueGraphQLFilter < Date > ; author ? : EntityGraphQLFilter < Author , AuthorId , GraphQLFilterOf < Author > > ; } export interface BookOrder { id ? : OrderBy ; title ? : OrderBy ; createdAt ? : OrderBy ; updatedAt ? : OrderBy ; author ? : AuthorOrder ; } export const bookConfig = new ConfigApi < Book , Context > ( ) ; bookConfig . addRule ( newRequiredRule ( "title" ) ) ; bookConfig . addRule ( newRequiredRule ( "createdAt" ) ) ; bookConfig . addRule ( newRequiredRule ( "updatedAt" ) ) ; bookConfig . addRule ( newRequiredRule ( "author" ) ) ; export abstract class BookCodegen extends BaseEntity { readonly __orm ! : EntityOrmField & { filterType : BookFilter ; gqlFilterType : BookGraphQLFilter ; orderType : BookOrder ; optsType : BookOpts ; optIdsType : BookIdsOpts ; factoryOptsType : Parameters < typeof newBook > [ 1 ] ; } ; readonly author : ManyToOneReference < Book , Author , never > = hasOne ( authorMeta , "author" , "books" ) ; constructor ( em , opts ) { super ( em , bookMeta , { } , opts ) ; setOpts ( this as any as Book , opts , { calledFromConstructor : true } ) ; } get id ( ) : BookId | undefined { return this . __orm . data [ "id" ] ; } get title ( ) : string { return this . __orm . data [ "title" ] ; } set title ( title ) { setField ( this , "title" , title ) ; } get createdAt ( ) : Date { return this . __orm . data [ "createdAt" ] ; } get updatedAt ( ) : Date { return this . __orm . data [ "updatedAt" ] ; } set ( opts ) { setOpts ( this as any as Book , opts ) ; } setPartial ( opts ) { setOpts ( this as any as Book , opts as OptsOf < Book > , { partial : true } ) ; } get changes ( ) : Changes < Book > { return newChangesProxy ( this as any as Book ) ; } async load < U , V > ( fn ) { return loadLens ( this as any as Book , fn ) ; } async populate < H extends LoadHint < Book > > ( hint ) { return getEm ( this ) . populate ( this as any as Book , hint ) ; } }