New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: How to make query with relation between pets and users #26
Comments
After many hours I could find a way to get a relational query running. First I figured out that in Bookshelf always a hasMany is declared for the opposite direction of belongsTo. What did I do in particular: 1 new resource Ownercreating a new resource owner should give me the assignemnt of user to the pet npm run console make:resource Owner 2 OwnerController.tsdelete everything what is CRUD, hence we are only querying ` // Get middlewares @controller('/owners', authenticate.use)
} OwnerRepository.tsdelete everything related to CRUD ` export class OwnerRepository {
} OwnerService.tsdelete everything related to CRUD ` export class OwnerService {
} User.tsadding the following: `
` Owner.tsdelete all things what relates to table, add the fetchById method ` export class Owner extends Bookshelf.Model {
} Pets.tscreate new file Pets.ts with following: ` export class Pets extends Bookshelf.Collection {
} resulthttp://localhost:3000/api/owners can be called
http://localhost:3000/api/owners/3 can be called
conclusion
|
After more hours I could resolve the relational query. What did I do in particular: 1 new resource Ownercreating a new resource owner should give me the assignemnt of user to the pet npm run console make:resource Owner 2 OwnerController.tsdelete everything what is CRUD, hence we are only querying ` // Get middlewares @controller('/owners', authenticate.use)
} OwnerRepository.tsdeleted everything which relates to CRUD ` export class OwnerRepository {
} OwnerService.tsdeleted everything which relates to CRUD ` export class OwnerService {
} User.tsadding the following: `
` Pet.tsadd the custom functions `
` Owner.tsdeleted this file Pets.tscreate new file Pets.ts which creates a collection from the model ` export class Pets extends Bookshelf.Collection { resulthttp://localhost:3000/api/owners can be called
http://localhost:3000/api/owners/3 can be called
conclusion
|
Hi, Looks oaky for me. You could also just add the pets as a collection to the user queries. |
Thank you for the nice repository.
I managed to have the pets created and they are also exposed to the api after creating a middleware for it.
My question is probably very simple, but I am struggling with the code.
How can I now manage to make a query which pets belong to a user? And what is the best way to accomplish it (creating a new controller "userpets" or simply adding a fetchPetsByUserId() into the User.ts)?
How and where can I use the method user() which was inserted in the Pet.ts?
I would appreciate if you could add this as an example.
With best regards
foxthefox
The text was updated successfully, but these errors were encountered: