Created
May 29, 2020 03:40
-
-
Save Wonder2210/bb5d7feab96552d608959d2319b387b0 to your computer and use it in GitHub Desktop.
Models for PG-Graphql-TS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Model} from 'objection'; | |
import {Species,Maybe} from '../../__generated__/generated-types'; | |
import User from './User'; | |
class Pet extends Model{ | |
static tableName = "pets"; | |
id! : number; | |
name?: Maybe<string>; | |
specie?: Maybe<Species>; | |
created_at?:string; | |
owner_id!:number; | |
owner?:User; | |
static jsonSchema ={ | |
type:'object', | |
required:['name'], | |
properties:{ | |
id:{type:'integer'}, | |
name:{type:'string', min:1, max:255}, | |
specie:{type:'string',min:1, max:255}, | |
created_at:{type:'string',min:1, max:255} | |
} | |
}; | |
static relationMappings=()=>({ | |
owner:{ | |
relation:Model.BelongsToOneRelation, | |
modelClass:User, | |
join: { | |
from: 'pets.owner_id', | |
to: 'users.id', | |
} | |
} | |
}); | |
}; | |
export default Pet; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Model} from 'objection'; | |
import {Maybe} from '../../__generated__/generated-types'; | |
import Pet from './Pet'; | |
class User extends Model{ | |
static tableName = "users"; | |
id! : number; | |
full_name!: Maybe<string>; | |
country_code! : Maybe<string>; | |
created_at?:string; | |
pets?:Pet[]; | |
static jsonSchema = { | |
type:'object', | |
required:['full_name'], | |
properties:{ | |
id: { type:'integer'}, | |
full_name:{type :'string', min:1, max :255}, | |
country_code:{type :'string', min:1, max :255}, | |
created_at:{type :'string', min:1, max :255} | |
} | |
} | |
static relationMappings =()=>({ | |
pets: { | |
relation: Model.HasManyRelation, | |
modelClass: Pet, | |
join: { | |
from: 'users.id', | |
to: 'pets.owner_id' | |
} | |
} | |
}) | |
} | |
export default User; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment