javascript - BookshelfJS: Is it possible to reformat data from `withPivot` -
i'm using bookshelf create simple restful api , return consistent camelcase format while using underscore_database_columns.
i using format , parse methods documentation exception of withpivot data.
all code simplified project please forgive syntax errors etc, may not exist in real code
i have 2 models, recipe , ingredient.
var recipe = basemodel.extend({ tablename: 'recipe', defaults: { name: null }, ingredients: function () { return .belongstomany('ingredient') .withpivot(['measurement', 'sort_order']); } })); var ingredient = basemodel.extend({ tablename: 'ingredients', defaults: { name: null }, recipes: function () { return .belongstomany('recipe'); } })); i querying following:
model .forge({ id: 1 }) .fetch({ withrelated: ['ingredients'] }) .then(function (model) { return res.json(model.tojson()); }) .catch(function (err) { return done(err); }); which returns:
{ "id": 1, "name": "yummy thing", "ingredients": [ { "id": 1, "name": "food stuff", "_pivot_ingredient_id": 1, "_pivot_recipe_id": 1, "_pivot_measurement": "loads", "_pivot_sort_order": 1 } ] } as can see _pivot data snake_case .withpivot(['measurement', 'sort_order']) pointing table column names rather attributes.
is there way reference , return pivot data camelcase?
i've overridden tojson works fine feels wrong given base models using camelcase default.
tojson: function () { var data = basemodel.prototype.tojson.apply(this, arguments); data.ingredients && (data.ingredients = data.ingredients.map(this.parse)); return data; } i'm open using through models couldn't work out how return through table data fetch. (i'm not set on the above json format long data there in form.)
Comments
Post a Comment