var Location = app.models.location;
var EmployeeLocation = app.models.employeeLocation;
var Employee = app.models.employee;
EmployeeLocation.belongsTo(Location, {
foreignKey: 'id', // re-use the actual location id
properties: {
id: 'id',
address: 'address',
state: 'state',
city: 'city',
zip: 'zip'
}, // denormalize, transfer id
// For info on invertProperties see https://github.com/strongloop/loopback-datasource-juggler/pull/219
options: {
invertProperties: true
}
});
Employee.embedsMany(EmployeeLocation, {
as: 'locations',
scope: {
include: 'location'
}
});
{
"id": "54ee4c09d6f895de8d3ee978",
"employeeLocations": [{
"id": "54ee3efb9ed34efb826d0a72"
}, {
"id": "54ee4c3cd6f895de8d3ee97d"
}],
"locations": [{
"id": "54ee3efb9ed34efb826d0a72",
"location": { "id": "54ee3efb9ed34efb826d0a72",
"address": "12345 Front St",
"state": "MA",
"city": "Freetown",
"zip": "27513"
}
}, {
"id": "54ee4c3cd6f895de8d3ee97d",
"location": { "id": "54ee4c3cd6f895de8d3ee97d", "address": "123 Main Str",
"state": "TX",
"city": "Dallas",
"zip": "75857"
}
}]
}
{ "id": "54ee4c09d6f895de8d3ee978", "employeeLocations": [{ "id": "54ee3efb9ed34efb826d0a72" }, { "id": "54ee4c3cd6f895de8d3ee97d" }], "locations": [{ "id": "54ee3efb9ed34efb826d0a72", "address": "12345 Front St", "state": "MA", "city": "Freetown", "zip": "27513"
}, { "id": "54ee4c3cd6f895de8d3ee97d", "address": "123 Main Str", "state": "TX", "city": "Dallas", "zip": "75857" }]}