Stack Overflow Asked by iDaniel19 on November 2, 2020
I have the following Objection.js models:
Appointment:
'use strict'
const { Model } = require('objection')
class Appointment extends Model {
// Table name is the only required property.
static get tableName() {
return 'appointment'
}
static get idColumn() {
return 'appointmentId';
}
// This object defines the relations to other models.
static get relationMappings() {
// One way to prevent circular references
// is to require the model classes here.
const AppointmentType = require('./AppointmentType')
return {
appointmentType: {
relation: Model.BelongsToOneRelation,
// The related model. This can be either a Model subclass constructor or an
// absolute file path to a module that exports one.
modelClass: AppointmentType,
join: {
from: 'appointment.appointmentTypeId',
to: 'appointmentType.appointmentTypeId'
}
},
}
}
}
module.exports = Appointment
AppointmentType:
'use strict'
const { Model } = require('objection')
class AppointmentType extends Model {
// Table name is the only required property.
static get tableName() {
return 'appointmentType'
}
static get idColumn() {
return 'appointmentTypeId';
}
}
module.exports = AppointmentType
Using the following query:
await Appointment.query().withGraphJoined({appointmentType: true})
I get the following results:
{
"appointmentId": 1,
"duration": 12,
"appointmentTypeId": 2,
"appointmentType": {
"appointmentTypeId": 2,
"appointmentTypeName": "Type Name"
}
....
}
In most cases, the default return from objection is useful but in this one not so much. Would it be possible to return something like:
{
"appointmentId": 1,
"duration": 12,
"appointmentTypeName": "Type Name" // or "typeName": "Type Name"
...
}
I think this is not possible yet. I ll just parse the object again, or use it just like that. I’ll leave this here in case someone has found a nice way
you can select the columns you want and they'll return as one flat object
const appointments = await Appointment.query().select('appointmentId','duration', 'appointmentTypeName').leftJoinRelated('appointmentType');
but this is vulnerable to duplicates only if an appointment has many types. be careful
Answered by Abed Murrar on November 2, 2020
1 Asked on January 11, 2021 by amit-singh-rawat
1 Asked on January 11, 2021 by jay-sinha
1 Asked on January 11, 2021 by sara-daniel
2 Asked on January 10, 2021 by shubroto-shuvo
1 Asked on January 10, 2021 by red-apple
0 Asked on January 10, 2021 by unicornbutt
4 Asked on January 10, 2021 by toby-chamberlain
3 Asked on January 10, 2021
2 Asked on January 10, 2021 by nourza
1 Asked on January 10, 2021 by ritesh-naik
3 Asked on January 10, 2021 by essegn
1 Asked on January 10, 2021 by mister-sircode
2 Asked on January 10, 2021
0 Asked on January 10, 2021 by alperen-aksu
2 Asked on January 9, 2021 by user4584963
5 Asked on January 9, 2021 by meder-omuraliev
1 Asked on January 9, 2021 by shuaipeng-zhang
2 Asked on January 9, 2021 by zhengrong
Get help from others!
Recent Questions
Recent Answers
© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP