Creating meta object in API response - loopbackjs

How do I make my API response in the following format using loopbackJS.
{
"meta": {
"limit": 20,
"next": "/api/Questions/?limit=20&offset=20",
"offset": 0,
"previous": null,
"total_count": 215
},
"objects": [...]
}

You can rewrite the response, I'm using an afterRemote remote hook to handle it.
Here's an example of common/models/car.js to get you started.
module.exports = function(Car) {
// before the static .find methods
Car.afterRemote('**', function(ctx, car, next) {
console.log(ctx.methodString, 'was invoked remotely');
if(ctx.result) {
var result = ctx.result;
ctx.result = {
objects: result,
meta: {
limit: 20,
next: ctx.req.baseUrl,
offset: 0,
previous: '',
}
};
if(Array.isArray(ctx.result.objects)) {
ctx.result.meta.count = ctx.result.objects.length;
}
}
next();
});
};
With that code you get an output like this:
{"objects":[
{"model":"Nissan","make":"Leaf","year":2013,"id":1},
{"model":"Telsa","make":"S","year":2014,"id":2},
{"model":"Chevrolet","make":"Volt","year":2014,"id":3},
{"model":"Toyota","make":"Prius C","year":2015,"id":4}
],
"meta":{
"limit":20,
"next":"/api/cars",
"offset":0,
"previous":"",
"count":4
}}

Related

JSON Validate check based on response from arrayElement

I wanted to check from the response format if status=AVAILABLE then arrayElement should return with roomTypeId else roomTypeId should not return for other status.
[
{
"status": "SOLDOUT",
"propertyId": "dc00e77f",
"Fee": 0,
"isComp": false
},
{
"roomTypeId": "c5730b9e",
"status": "AVAILABLE",
"propertyId": "dc00e77f",
"price": {
"baseAveragePrice": 104.71,
"discountedAveragePrice": 86.33
},
"Fee": 37,
"isComp": false
},
]
[
{
"status": "SOLDOUT",
"propertyId": "773000cc-468a-4d86-a38f-7ae78ecfa6aa",
"resortFee": 0,
"isComp": false
},
{
"roomTypeId": "c5730b9e-78d1-4c1c-a429-06ae279e6d4d",
"status": "AVAILABLE",
"propertyId": "dc00e77f-d6bb-4dd7-a8ea-dc33ee9675ad",
"price": {
"baseAveragePrice": 104.71,
"discountedAveragePrice": 86.33
},
"resortFee": 37,
"isComp": false
},
]
I tried to check this from below;
pm.test("Verify if Status is SOLDOUT, roomTypeId and price information is not returned ", () => {
var jsonData = pm.response.json();
jsonData.forEach(function(arrayElement) {
if (arrayElement.status == "SOLDOUT")
{
pm.expect(arrayElement).to.not.include("roomTypeId");
}
else if (arrayElement.status == "AVAILABLE")
{
pm.expect(arrayElement).to.include("roomTypeId");
}
});
});
You need to check if the property exists or not.
With the have.property syntax you can do that.
You can read the Postman API reference docs and also Postman uses a fork of chai internally, so ChaiJS docs should also help you.
Updated script:
pm.test("Verify if Status is SOLDOUT, roomTypeId and price information is not returned ", () => {
var jsonData = pm.response.json();
jsonData.forEach(function(arrayElement) {
if (arrayElement.status === "SOLDOUT") {
pm.expect(arrayElement).to.not.have.property("roomTypeId");
} else if (arrayElement.status === "AVAILABLE") {
pm.expect(arrayElement).to.have.property("roomTypeId");
}
});
});

How to create a loopback <model>.json file from unstructured data

I can create a loopback model from an example json instance as shown here https://docs.strongloop.com/display/public/LB/Creating+models+from+unstructured+data. But from there is there an API to create the .json file in common/models?
#BoLaMN from Looback Gitter said this:
try app.registry.modelBuilder.introspect(json); that should give you a properties object. Then just add name, base to it and fs.writeSync JSON.stringify(obj).
I haven't tried yet, but it makes sense.
I've been using this:
var User = db.buildModelFromInstance('User', user, {idInjection: true});
var UserModel = (loopback.getModel(User));
var UserModelJSON = {}
UserModelJSON.name = 'User';
UserModelJSON.base = 'PersistedModel';
UserModelJSON.properties = UserModel.definition.rawProperties;
console.log(JSON.stringify(UserModelJSON));
fs.writeFile('User.json', 'UserModelJSON', function(err) {
if (err) throw err;
});
In your loopback /server directory at the root of your project, if you define the datasources.json file like so
{
"db": {
...
},
"restResourceName": {
"name": "restResourceName",
"baseURL": "http://restResourceUrl/",
"crud": true,
"connector": "rest"
}
}
And you define a boot script, like so
const fs = require('fs')
const path = require('path')
const writeInstance = (server, modelName, instance) => {
let Model = server.dataSources.restResourceName.buildModelFromInstance(modelName, instance)
let modelJson = Model.definition.toJSON()
delete modelJson.settings.base
fs.writeFile(path.join(__dirname, '..', 'models', modelName + '.json'),
JSON.stringify(modelJson, null, 2), (/*...*/) => {
/*...*/
})
}
module.exports = function (server) {
writeInstance(server, "Atom", {
atomicWeight: 6
})
}
You will generate a model file Atom.json
{
"name": "Atom",
"properties": {
"atomicWeight": {
"type": "Number"
},
"id": {
"type": "Number",
"id": 1,
"generated": true,
"updateOnly": true
}
},
"settings": {
"strict": false,
"forceId": "auto",
"replaceOnPUT": true
}
}
If you want to see the models in the loopback api explorer (swagger GUI), you must add the model to the model-config.json with a public: true property
{
"_meta": {
"sources": [
...
],
"mixins": [
...
]
},
"Atom": {
"public": true,
"dataSource": "restResourceName"
}
}

Angular NgRessource with paginated results from django rest framework

My api basically returns something like this:
GET /api/projects/
{
"count": 26,
"next": "http://127.0.0.1:8000/api/projects/?page=2",
"previous": null,
"results": [
{
"id": 21,
"name": "Project A",
...
},
{
"id": 19,
"name": "Project B",
...
},
...
]
}
Using NgResource, I am able to query the api and get the data like this:
var PROJECT = $resource('/api/projects/:id/', {id:'#id'},{
query : {
method : 'GET',
isArray : false
}
});
factory.project_list = function(callback) {
PROJECT.query({},function(project_list){
factory.project_list = project_list.results;
callback();
});
};
My different projects are now available in factory.project_list. The issue here is that each item in factory.project_list are not ngResource items. So I can't call methods such as .$save(), .$update()...
I saw a transformResponse() function but I'm not able to get it working easily...
Do you have any idea what could be the best approach here ?
This is what worked for me:
app.config(['$resourceProvider', function($resourceProvider) {
$resourceProvider.defaults.stripTrailingSlashes = false;
}]);
services.factory('Project', ['$resource',
function($resource) {
return $resource('api/project/:id/', {}, {
query: {
method: 'GET',
url: 'api/projects/',
isArray: true,
transformResponse: function(data, headers) {
return angular.fromJson(data).results;
},
},
});
}
]);

Ember get hasMany

I have Model
EmberApp.Card = DS.Model.extend({
balance: DS.attr('number'),
operations: DS.hasMany('operation', { async: true })
});
Than I make
self.store.find('card', 'me')
response
{
"card": {
"id": "53620486168e3e581cb5851a",
"balance": 20
}
}
getting card Model but without operations and setting it to controller prop "currentCard"
Then I want to find operations throw url /cards/me/operations
response
{"operation": [{ "id": 1, "type": 1 }] }
How can I do it from this.controllerFor('*').get('currentCard')... ?
You actually need to return your operations as a list of ID's in your card response like this:
{
"card": {
"id": "53620486168e3e581cb5851a",
"balance": 20,
"operations": [1, 2, 3]
}
}
This will enable you to do this in another route:
this.modelFor('card').get('operations');
or this in your cards controller:
this.get('content.operations');
This will execute the following call to your API:
/api/operations?ids[]=1&ids[]=2&ids[]=3
First of all you should add links to response from cards/me
EmberApp.ApplicationSerializer = DS.RESTSerializer.extend({
normalizePayload: function(type, payload) {
if (type.toString() === 'EmberApp.Card') {
payload.links = { 'operations': '/cards/me/operations' };
return { card: payload };
} else if (type.toString() === 'EmberApp.Operation') {
return { operations: payload };
}
}
});
Then in route
EmberApp.CardOperationsRoute = Ember.Route.extend({
model: function() {
return this.controllerFor('sessions.new')
.get('currentCard')
.get('operations');
}
});

Ember.js and API pagination

I'm using Ember.js (v1.2.0) with an API which returns paginated JSON data like this:
{
"count": 5,
"next": "http://127.0.0.1:8000/some/resource/?page=2",
"previous": null,
"results": [
{
"id": 37,
"title": "Some title",
"description": "Some description",
},
{
"id": 35,
"title": "Sdflskdf",
"description": "sdfkdsjf",
},
{
"id": 34,
"title": "Some other title",
"description": "Dsdlfksdf",
},
]
}
I'm not using ember-data, so I'm using a plain ember object as my model and loading the data like this:
App.SomeResource = Ember.Object.extend({});
App.SomeResource.reopenClass({
find: function () {
return $.getJSON('/some/resource/').then(function (response) {
return response.results.map(function (data) {
return App.SomeResource.create(data);
});
});
},
});
The find method on my model class returns a promise which resolves to an array of objects. While creates SomeResource objects, all the pagination data is lost.
Is there a way to store count, next and previous page urls somewhere when the promise resolves?
I am assigning them to global object but you should do better.
App.SomeResource = Ember.Object.extend({});
App.SomeResource.reopenClass({
find: function () {
return $.getJSON('/some/resource/').then(function (response) {
return RSVP.all(response.results.map(function (data) {
return App.SomeResource.create(data);
})).then(function(createdResources) {
window.count = response.count;
window.next = response.next;
window.previous = response.previous;
return createdResources;
});
});
}
});
Rather than storing this metadata on the global window object, I came up with this:
App.SomeResource.reopenClass({
find: function () {
var url = '/some/resource/';
return Ember.$.getJSON(url).then(function (response) {
response.results = response.results.map(function (resource) {
return App.SomeResource.create(resource);
});
return response;
});
},
});
SomeResource.find() just instantiates Ember objects from the results array and then returns the response with the rest of the data untouched. The route then receives the response and sets up the pagination data and the model in the setupController function:
App.SomeResourceRoute = Ember.Route.extend({
model: function () {
return App.SomeResource.find();
},
setupController: function(controller, model) {
controller.setProperties({count: model.count,
pageCount: model.page_count,
currentPage: model.current_page,
pageRange: model.page_range,
previous: model.previous,
next: model.next,
model: model.results,});
},
});
It works, but maybe there is a better way.